Skip to content

Instantly share code, notes, and snippets.

@noopkat
Created March 28, 2017 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noopkat/b87bdbec6e9dc9973e6036280a55e974 to your computer and use it in GitHub Desktop.
Save noopkat/b87bdbec6e9dc9973e6036280a55e974 to your computer and use it in GitHub Desktop.
Azure IOT Hub + Table Storage function App
'use strict';
const https = require("https");
const uuid = require("uuid/v4");
const IotClient = require("azure-iothub").Client;
const IotMessage = require("azure-iot-common").Message;
// main function
module.exports = function (context, eventHubMessage) {
// log this for debugging
context.log("JavaScript event hub trigger function processed work item:", JSON.stringify(eventHubMessage));
// put the initial table storage metadata together
const tableMetadata = {
"PartitionKey": "sensorReading",
"RowKey": uuid()
};
// get precipitation data to commit to table row
getWeatherData(function(error, precipData) {
// if we got weather data, proceed
if (!error) {
const deviceOrigin = eventHubMessage.IoTHub.ConnectionDeviceId;
sendPrecipDataToDevice(precipData, deviceOrigin, function(error) {
// we can just log this, as we can safely ignore any failed attempts at cloud to device messaging
if (error) context.log(error);
// merge table storage metadata, sensor data, and precipitation data
const outData = Object.assign(tableMetadata, eventHubMessage, precipData);
// finally return all data for output into table storage
context.done(null, outData);
});
} else { // if we couldn't get the weather data, we should at least store the sensor data
// merge table storage metadata and sensor data
const outData = Object.assign(tableMetadata, eventHubMessage);
// return all data for output into table storage
context.done(null, outData);
}
});
};
// weather api function
const getWeatherData = function(callback) {
const options = {
host: "api.darksky.net",
path: `/forecast/${process.env.DARK_SKY_KEY}/40.679225,-74.012153`,
method: "GET",
headers: {
"Content-Type": "application/json"
}
};
// set up the weather api request
const req = https.request(options, function (res) {
let responseString = "";
// on each data event, append the data to the entire response string
res.on("data", function (data) {
responseString += data;
});
// on response end, parse the response string, pull out the precipitation data and return it to the callback
res.on("end", function () {
const weatherInfo = JSON.parse(responseString);
const precipData = {
precipIntensity: weatherInfo.currently.precipIntensity,
precipProbability: weatherInfo.currently.precipProbability
};
// return data to callback when response ends
return callback(null, precipData);
});
});
// if there's an error in making the API request
req.on("error", function(error) {
return callback(error);
});
// end the request to send it out immediately without a body
req.end();
}
// iot hub function to dispatch weather data back to device
const sendPrecipDataToDevice = function(data, deviceid, callback) {
const iotClient = IotClient.fromConnectionString(process.env.IOT_CONN_STRING);
// open a client to iot hub, and send precipitation data back to the device
iotClient.open(function (error) {
if (error) return callback(error);
const dataString = JSON.stringify(data);
const message = new IotMessage(dataString);
iotClient.send(deviceid, message, callback);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment