Created
November 23, 2017 23:09
-
-
Save jamesbulpin/f11045900f08aec20b7aa057ba6dedb0 to your computer and use it in GitHub Desktop.
Azure Function to handle device-to-cloud messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
function jrbHomeAutomation(context, event) { | |
context.log("jrbHomeAutomation event " + JSON.stringify(event)); | |
if ((event.topic) && (event.message)) { | |
switch (event.topic) { | |
case "Alert": | |
request({ | |
url: "<logic app trigger URL>", | |
method: "POST", | |
json: true, | |
body: event | |
}, function (error, response, body){ | |
context.log(response); | |
}); | |
break; | |
} | |
} | |
} | |
module.exports = function (context, events) { | |
//context.log(`JavaScript eventhub trigger function called for message array ${events}`); | |
//context.log("context", JSON.stringify(context)); | |
// https://github.com/Azure/azure-webjobs-sdk-script/wiki/Trigger-Binding-Data | |
for (i = 0; i < events.length; i++) | |
{ | |
context.log("Processing message", JSON.stringify(events[i])); | |
// EventData properties can be accessed via binding data, | |
// including custom properties, system properties, etc. | |
var bindingData = context.bindingData, | |
eventProperties = bindingData.propertiesArray[i], | |
systemProperties = bindingData.systemPropertiesArray[i]; | |
switch (systemProperties["iothub-connection-device-id"]) { | |
case "MyMQTTBroker": | |
jrbHomeAutomation(context, events[i]); | |
break; | |
} | |
} | |
context.done(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment