/**
 * Not sure if I want to use unique IoT devices for each sensor
 * (and manage their locations via the IoT portal) or if I treat
 * the Pi as the device and the sensors are just part of the payload.
 *
 * Currently, this treats the Pi as the device and the sensors as
 * part of the payload.
 * How I map the sensors to human readable locations is TBD.
 */

/**
 * delayinms is the delay, in milliseconds, to wait between publishes
 * of data. Here are some common values:
 * Hour = 3600000
 * Half-hour = 1800000
 * Minute = 60000
 * Second = 6000
 */
var delayinms = 1800000; // every half hour
var suspend = false;

if (!!!process.env.AUTH_TOKEN) {
  console.error("Error, environment variable AUTH_TOKEN not set")
  process.exit()
}

var fs = require('fs');
var moment = require('moment');
var sensor = require('ds18x20');
var iotf = require('ibmiotf');
var config = {
               "org" : "oengiu",
               "type" : "raspi3monitor",
               "id" : "mct1001",
               "auth-method" : "token",
               "auth-token" : process.env.AUTH_TOKEN
             };
var deviceClient = new iotf.IotfDevice(config);
deviceClient.log.setLevel('debug');
deviceClient.connect();

var devices = sensor.list();

deviceClient.on('connect', function () {
  console.log("connected");
  doTask();
  setInterval(doTask, delayinms); 
});
 
deviceClient.on('reconnect', function () {
  console.log("reconnected");
});

deviceClient.on("command", function (commandName, format, payload, topic) {
  console.log('got command: ' + commandName);
  if (commandName === "suspend") {
    suspend = true;
  } else {
    suspend = false;
  }
});

deviceClient.on("error", function (err) {
  console.log("Error : " + err);
});

/**
 * Published json structure:
 * {
 *   "ts": "YYYY-MM-DDTHH:mm:ss.SSSZZ",
 *   "sensors": [
 *     { "id": "28-*", "temp": 0.0 }
 *   ]
 * }
 */
function doTask() {
    if (!suspend)  {
      var monval = { sensors: [] };

      for (var device in devices) {
        var data = {};
        data.id = device.id();

        var ctemp = sensor.get( device );
        var ftemp = ((ctemp * 9) / 5) + 32;
        data.temp = Math.round(ftemp * 10, 1) / 10;

        monval.sensors.add(data);
      }

      monval.ts = moment().format("YYYY-MM-DDTHH:mm:ss.SSSZZ");

      var payload = JSON.stringify(monval);
      console.log("publishing payload:" + payload);
      deviceClient.publish('itemsvc', 'json', payload);
    }
}

@@ -541,17 +541,17 @@
 econd = 
-6
+1
 000%0A */%0A
@@ -567,19 +567,16 @@
 inms = 1
-800
 000; // 
@@ -611,16 +611,60 @@
 = false;
+ // global suspend flag, set via IoT command
 %0A%0Aif (!!
@@ -780,32 +780,8 @@
 %0A%7D%0A%0A
-var fs = require('fs');%0A
 var 
@@ -1216,38 +1216,8 @@
 );%0A%0A
-var devices = sensor.list();%0A%0A
 devi
@@ -1283,22 +1283,33 @@
 ed%22);%0A  
-doTask
+publishSensorData
 ();%0A  se
@@ -1318,22 +1318,33 @@
 nterval(
-doTask
+publishSensorData
 , delayi
@@ -1830,53 +1830,40 @@
 s%22: 
-%5B%0A *     %7B %22id%22: %2228-*%22, %22temp%22: 0.0 %7D%0A *   %5D
+%7B '28-00000574c791': 22.9, ... %7D
 %0A * 
@@ -1881,21 +1881,30 @@
 ion 
-doTask
+publishSensorData
 () %7B%0A
-  
   if
@@ -1926,313 +1926,83 @@
     
-  var monval = %7B sensors: %5B%5D %7D;%0A%0A      for (var device in devices) %7B%0A        var data = %7B%7D;%0A        data.id = device.id();%0A%0A        var ctemp = sensor.get( device );%0A        var ftemp = ((ctemp * 9) / 5) + 32;%0A        data.temp = Math.round(ftemp * 10, 1) / 10;%0A%0A       
+sensor.getAll(function (err, tempObj) %7B%0A      var
  monval
-.
+ = %7B 
 sensors
-.add(data);%0A      %7D%0A
+: %5B%5D %7D;
 %0A   
@@ -2061,16 +2061,48 @@
 SSSZZ%22);
+%0A      monval.sensors = tempObj;
 %0A%0A      
@@ -2177,16 +2177,17 @@
 payload:
+ 
 %22 + payl
@@ -2250,14 +2250,20 @@
 oad);%0A  
+  %7D);%0A
   %7D%0A%7D%0A

simplify setting data, just use tempObj