text: /*
  * IoT Hub Raspberry Pi NodeJS - Microsoft Sample Code - Copyright (c) 2017 - Licensed MIT
  */
  'use strict';
  
  
  const fs = require('fs');
  const path = require('path');
  
  const Client = require('azure-iot-device').Client;
  const ConnectionString = require('azure-iot-device').ConnectionString;
  const Message = require('azure-iot-device').Message;
  const Protocol = require('azure-iot-device-mqtt').Mqtt;
  
  // Edison packages
  const five = require("johnny-five");
  const Edison = require("edison-io");
  const board = new five.Board({
    io: new Edison()
  });
  
  const MessageProcessor = require('./messageProcessor.js');
  
  var sendingMessage = false;
  var messageId = 0;
  var client, config, messageProcessor;
  
  function sendMessage() {
    if (!sendingMessage) { return; }
    messageId++;
    messageProcessor.getMessage(messageId, (content, temperatureAlert) => {
      var message = new Message(content);
      message.properties.add('temperatureAlert', temperatureAlert ? 'true' : 'false');
      console.log('Sending message: ' + content);
      client.sendEvent(message, (err) => {
        if (err) {
          console.error('Failed to send message to Azure IoT Hub');
        } else {
          blinkLED();
          console.log('Message sent to Azure IoT Hub');
        }
        setTimeout(sendMessage, config.interval);
      });
    });
  }
  
  function onStart(request, response) {
    console.log('Try to invoke method start(' + (request.payload || '') + ')');
    sendingMessage = true;
  
    response.send(200, 'Successully start sending message to cloud', function (err) {
      if (err) {
        console.error('[IoT hub Client] Failed sending a method response:\n' + err.message);
      }
    });
  }
  
  function onStop(request, response) {
    console.log('Try to invoke method stop(' + (request.payload || '') + ')')
    sendingMessage = false;
  
    response.send(200, 'Successully stop sending message to cloud', function (err) {
      if (err) {
        console.error('[IoT hub Client] Failed sending a method response:\n' + err.message);
      }
    });
  }
  
  function receiveMessageCallback(msg) {
    blinkLED();
    var message = msg.getData().toString('utf-8');
    client.complete(msg, () => {
      console.log('Receive message: ' + message);
    });
  }
  
  function blinkLED() {
    config.led.blink();
  }
  
  function initClient(connectionStringParam, credentialPath) {
    var connectionString = ConnectionString.parse(connectionStringParam);
    var deviceId = connectionString.DeviceId;
  
    // fromConnectionString must specify a transport constructor, coming from any transport package.
    client = Client.fromConnectionString(connectionStringParam, Protocol);
  
    // Configure the client to use X509 authentication if required by the connection string.
    if (connectionString.x509) {
      // Read X.509 certificate and private key.
      // These files should be in the current folder and use the following naming convention:
      // [device name]-cert.pem and [device name]-key.pem, example: myraspberrypi-cert.pem
      var connectionOptions = {
        cert: fs.readFileSync(path.join(credentialPath, deviceId + '-cert.pem')).toString(),
        key: fs.readFileSync(path.join(credentialPath, deviceId + '-key.pem')).toString()
      };
  
      client.setOptions(connectionOptions);
  
      console.log('[Device] Using X.509 client certificate authentication');
    }
    return client;
  }
  
  (function (connectionString) {
    // read in configuration in config.json
    try {
      config = require('./config.json');
    } catch (err) {
      console.error('Failed to load config.json: ' + err.message);
      return;
    }
  
    // set up led and sensor
    board.on('ready', () => {
      config.led = new five.Led(config.LEDPin);
      messageProcessor = new MessageProcessor(config);
      sendingMessage = true;
    });
  
  
    // create a client
    // read out the connectionString from process environment
    connectionString = connectionString || process.env['AzureIoTHubDeviceConnectionString'];
    client = initClient(connectionString, config);
  
    client.open((err) => {
      if (err) {
        console.error('[IoT hub Client] Connect error: ' + err.message);
        return;
      }
  
      // set C2D and device method callback
      client.onDeviceMethod('start', onStart);
      client.onDeviceMethod('stop', onStop);
      client.on('message', receiveMessageCallback);
      setInterval(() => {
        client.getTwin((err, twin) => {
          if (err) {
            console.error("get twin message error");
            return;
          }
          config.interval = twin.properties.desired.interval || config.interval;
        });
      }, config.interval);
      sendMessage();
    });
  })(process.argv[2]);
  