#include <MQTTClient.h>
#include <ArduinoJson.h>
#include <WiFi101.h>

#define LED_RED_PIN 11
#define LED_YELLOW_PIN 13
#define RED_DELAY 5000
#define YELLOW_DELAY 10000


const char* _SSID     = "[Wi-Fi SSID]";
const char* _PASSWORD = "[Wi-Fi Password]";  

// ARTIK Cloud MQTT params
char mqttCloudServer[]     = "api.artik.cloud";
int  mqttCloudPort         = 1883;
char mqttCloudClientName[] = "ARTIK-Arduino";
char mqttCloudUsername[]   = "[device-id]"; 
char mqttCloudPassword[]   = "[device-token]"; 
char mqttCloudActionsIn[]    = "/v1.1/actions/[device-id]"; 

WiFiClient ipCloudStack;
MQTTClient mqttCloudClient;

char buf[128];

int savedRedValue, savedYellowValue;

unsigned long savedRedTime, savedYellowTime;

void setup() {

  Serial.begin(57600);  

  pinMode(LED_RED_PIN, OUTPUT);
  pinMode(LED_YELLOW_PIN, OUTPUT);

  savedRedValue = savedYellowValue = 0;
  
  // Wifi Setting
  WiFi.begin(_SSID, _PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  
  mqttCloudClient.begin(mqttCloudServer, mqttCloudPort, ipCloudStack);

  Serial.println("start ARTIK Cloud connect"); Serial.println();
  
  while (!mqttCloudClient.connect(mqttCloudClientName, mqttCloudUsername, mqttCloudPassword)) {
    Serial.print("*");
    delay(500);    
  }

  mqttCloudClient.subscribe(mqttCloudActionsIn);

}

void messageReceived(String topic, String payload, char * bytes, unsigned int length) {


  parseBuffer(payload);
}

void parseBuffer(String payload) {
  StaticJsonBuffer<200> jsonBuffer;
  String json = payload;
  JsonObject& root = jsonBuffer.parseObject(json);
  const char* nameparam = root["actions"][0]["name"];
  const int actionLEDRed = root["actions"][0]["parameters"]["led_red"];
  const int actionLEDYellow = root["actions"][0]["parameters"]["led_yellow"];

  Serial.print("name="); Serial.println(nameparam);
  Serial.print("led_red="); Serial.println(actionLEDRed);
  Serial.print("led_yellow="); Serial.println(actionLEDYellow);
  Serial.println();


  if (actionLEDRed == 1) {
    if (savedRedValue != actionLEDRed) {
      digitalWrite(LED_RED_PIN, HIGH);
      savedRedValue = actionLEDRed;
    } 

    savedRedTime = millis();      
        
  } else {
    if (savedRedValue != actionLEDRed) {
      if (millis() - savedRedTime > RED_DELAY) {
        digitalWrite(LED_RED_PIN, LOW);
        savedRedValue = actionLEDRed;      
      }
    }
  }


  if (actionLEDYellow == 1) {
    if (savedYellowValue != actionLEDYellow) {
      digitalWrite(LED_YELLOW_PIN, HIGH);
      savedYellowValue = actionLEDYellow;
    } 

    savedYellowTime = millis();      
        
  } else {
    if (savedYellowValue != actionLEDYellow) {
      if (millis() - savedYellowTime > YELLOW_DELAY) {
        digitalWrite(LED_YELLOW_PIN, LOW);
        savedYellowValue = actionLEDYellow;      
      }
    }
  }

  
}

void loop() {

  mqttCloudClient.loop();
  delay(500);
    
}
