load('api_config.js');
load('api_events.js');
load('api_gpio.js');
load('api_mqtt.js');
load('api_net.js');
load('api_sys.js');
load('api_timer.js');

let led = Cfg.get('pins.led');
let button = Cfg.get('pins.button');
//let topic = '/devices/' + Cfg.get('device.id') + '/state';
//let topic = '/devices/' + Cfg.get('device.id') + '/state';
let outTopic = '/devices/' + Cfg.get('device.id') + '/events';
let inTopic = '/devices/' + Cfg.get('device.id') + '/config';

let counter = 10;
let hudi_val = 30;
let is_wifi_connect = false;
let is_mqtt_connect = false;
//print('LED GPIO:', led, 'button GPIO:', button);

let getInfo = function() {
  return JSON.stringify({
    total_ram: Sys.total_ram(),
    free_ram: Sys.free_ram()
  });
};

// Blink built-in LED every second
GPIO.set_mode(led, GPIO.MODE_OUTPUT);
Timer.set(1000 /* 1 sec */, Timer.REPEAT, function() {
  if(is_wifi_connect){
  	//print('WiFi connected');
    GPIO.write(led,0);
  }else{
    let value = GPIO.toggle(led);
    //print('LED toggle');
  }
    //print(value ? 'Tick' : 'Tock', 'uptime:', Sys.uptime(), getInfo());

}, null);

let getInfo = function() {
  return JSON.stringify({
    total_ram: Sys.total_ram() / 1024,
    free_ram: Sys.free_ram() / 1024,
    temp: 21,
    hum: 65
  });
};

Timer.set(6000 /* 1 sec */, Timer.REPEAT, function() {

    if(!is_wifi_connect)
      return;
    if(!is_mqtt_connect)
      return;
    let message = '{\"id\":\"';
    message = message+Cfg.get('device.id');
    message = message+'\",\"hum":\"';

    hudi_val ++;
    if(hudi_val >100)
      hudi_val = 10;
    if(hudi_val < 100){
      message = message+'0'+JSON.stringify(hudi_val);
    }else{
    
      message = message+JSON.stringify(hudi_val);
    }      
    message = message + '\","temp\":\"';
    counter++;
    if(counter >100)
      counter = 10;
    if(counter < 100){
      message = message+'0'+JSON.stringify(counter);
    }else{
    
      message = message+JSON.stringify(counter);
    }
    message = message + '\"}';
  

  let ok = MQTT.pub(outTopic, message, 1);
    print('Published:', ok ? 'yes' : 'no', 'topic:', outTopic, 'message:', message);    
 
 
 
 
}, null);
  /*
    let ok = MQTT.pub(topic, message, 1);
  print('Published:', ok ? 'yes' : 'no', 'topic:', topic, 'message:', message);    
}, null);
*/

// Publish to MQTT topic on a button press. Button is wired to GPIO pin 0
GPIO.set_button_handler(button, GPIO.PULL_UP, GPIO.INT_EDGE_NEG, 200, function() {
  let message = getInfo();
  //let ok = MQTT.pub(topic, message, 1);
  //print('Published:', ok, topic, '->', message);
  
}, null);

MQTT.sub(inTopic, function(conn, topic, msg) {
  print('Received Topic:', topic, 'message:', msg);
}, null);



MQTT.setEventHandler(function(conn, ev) {
  if (ev === MQTT.EV_CONNACK) {
    print('MQTT CONNECTED');
    is_mqtt_connect = true;
  }else if(ev === MQTT.EV_CLOSE){
    print("MQTT CLOSED");
    is_mqtt_connect = false;
  }
}, null);
 

// Monitor network connectivity.
Event.addGroupHandler(Net.EVENT_GRP, function(ev, evdata, arg) {
  let evs = '???';
  if (ev === Net.STATUS_DISCONNECTED) {
    evs = 'DISCONNECTED';
    is_wifi_connect = false;
  } else if (ev === Net.STATUS_CONNECTING) {
    evs = 'CONNECTING';
  } else if (ev === Net.STATUS_CONNECTED) {
    evs = 'CONNECTED';
    is_wifi_connect = true;    
  } else if (ev === Net.STATUS_GOT_IP) {
    evs = 'GOT_IP';
  }
  print('== Net event:', ev, evs);
}, null);
