+#define DEBUG_LEVEL 5
+
+#include <SPI.h>
+#include <Configuration.h>
+#include <Debug.h>
+#include <ParsedStream.h>
+#include <SpiUart.h>
+#include <WiFly.h>
+#include <WiFlyClient.h>
+#include <WiFlyDevice.h>
+#include <WiFlyServer.h>
+#include <_Spi.h>
+
 const long _startThreshold = 30;
 const long _continueThreshold = 5;
 class Reporter {

  }
}

class SerialReporter: public Reporter {
public:
  SerialReporter(byte number);

}


class LEDReporter: public Reporter {
public:
  LEDReporter(byte pin);
  virtual void ReportStop(long pulses);
  virtual void ReportContinue(long pulses);
  virtual void ReportStart(long pulses);
  virtual void ReportIgnore(long pulses);
private:
  byte _pin;
};

LEDReporter::LEDReporter(byte pin) { 
  _pin = pin;
  pinMode(_pin, OUTPUT);
}

void LEDReporter::ReportStop(long pulses){
  digitalWrite(_pin, LOW);
}
void LEDReporter::ReportContinue(long pulses) {
}
void LEDReporter::ReportStart(long pulses){
  digitalWrite(_pin, HIGH);
}
void LEDReporter::ReportIgnore(long pulses){
}

class WiFlyHttp  {
  public:
    WiFlyHttp(const char* host, int port, byte pin);
    void MakeRequest(String url);
  private:
    WiFlyClient* _client;
    byte _pin;
};
WiFlyHttp::WiFlyHttp(const char* host, int port, byte pin) { 
  _client = new WiFlyClient(host, port);
  _pin = pin;
  pinMode(_pin, OUTPUT);
  digitalWrite(_pin, HIGH);
}

void WiFlyHttp::MakeRequest(String url){
  digitalWrite(_pin, LOW);
  Serial.println(url);
  if (_client->connect()) {
    _client->println("GET " + url + " HTTP/1.0");
    _client->println();
    while(_client->available() || _client->connected()) {
      Serial.print(_client->read());
    }
  }
  digitalWrite(_pin, HIGH);
}

class WiFlyReporter: public Reporter {
public:
  WiFlyReporter(WiFlyHttp* http, String tapId);
  virtual void ReportStop(long pulses);
  virtual void ReportContinue(long pulses);
  virtual void ReportStart(long pulses);
  virtual void ReportIgnore(long pulses);
  void Heartbeat();
private:
  void MakeRequest(String url);
  String _tapId;
  WiFlyHttp* _http;
};

WiFlyReporter::WiFlyReporter(WiFlyHttp* http, String tapId) { 
  _http = http;
  _tapId = tapId;
}

void WiFlyReporter::MakeRequest(String url){
  _http->MakeRequest(url);
}
void WiFlyReporter::ReportStop(long pulses){
  MakeRequest("/api/Tap/" + _tapId + "/StopPour?volume=" + (pulses / 1000));
}
void WiFlyReporter::ReportContinue(long pulses) {
  MakeRequest("/api/Tap/" + _tapId + "/Pouring?volume=" + (pulses / 1000));
}
void WiFlyReporter::ReportStart(long pulses){
  MakeRequest("/api/Tap/" + _tapId + "/StartPour");
}
void WiFlyReporter::ReportIgnore(long pulses){
}
void WiFlyReporter::Heartbeat(){
  MakeRequest("/api/Status/heartbeat");
}


Tap* tap1;
Tap* tap2;
WiFlyHttp* http;

// prep - initialize serial (TX on pin 1) and wire up the interrupts for pulses from the taps (pins 2 and 3)
void setup() {
  Serial.begin(9600);
  while(!Serial);
  
  Serial.println("Starting");
  WiFly.begin();
  Serial.println("Joining");
  if (!WiFly.join("Rightpoint-Guest", "RP@29NWacker")) {
     // Handle the failure
     Serial.println("Failed to connect");
  }
  else{
     Serial.println("Connected");
  }
  
  WiFly.configure(WIFLY_BAUD, 38400);

  Serial.println(WiFly.ip());

  http = new WiFlyHttp("pourcast.labs.rightpoint.com", 80, 9);
  tap1 = new Tap(new MultiReporter(new SerialReporter(1), new LEDReporter(10), new WiFlyReporter(http, "535c61a951aa0405287989ec")));
  attachInterrupt(0, tap1Pulse, RISING);
  tap2 = new Tap(new MultiReporter(new SerialReporter(2), new LEDReporter(11), new WiFlyReporter(http, "537d28db51aa04289027cde5")));
  attachInterrupt(1, tap2Pulse, RISING);
}

