Skip to content

Instantly share code, notes, and snippets.

@lbearl
Created February 14, 2020 04:57
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 lbearl/34dee5c7d971d44c8591cc8c43b73cb9 to your computer and use it in GitHub Desktop.
Save lbearl/34dee5c7d971d44c8591cc8c43b73cb9 to your computer and use it in GitHub Desktop.
Azure IoT Function code for DuoDesire
module.exports = function (context, IoTHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array: ${IoTHubMessages}`);
const date = new Date();
// This is PST. Probably a better way of doing this, but /shrug
const dateString = new Date(date.getTime() - (480 * 60000 ))
.toISOString()
.split("T")[0];
context.bindings.tableBinding = [];
IoTHubMessages.forEach(message => {
var host = context.bindingData.systemPropertiesArray[0]["iothub-connection-device-id"];
var enqTime = context.bindingData.systemPropertiesArray[0]["iothub-enqueuedtime"];
const blob = JSON.parse(message);
context.bindings.tableBinding.push({
PartitionKey: dateString,
RowKey: enqTime,
Device: host,
Action: blob.message
})
context.log(`Processed message: ${message}`);
});
context.done();
};
module.exports = async function (context, myQueueItem) {
context.log('JavaScript queue trigger function processed work item', myQueueItem);
context.bindings.message = {}
context.bindings.message = {
// Customize this to be the message you want Twilio to send.
body : "DuoDesire Msg: Tonight its dually desired",
to : myQueueItem
};
context.done();
};
module.exports = async function (context, myTimer) {
var timeStamp = new Date().toISOString();
var date = new Date();
// This is PST. Probably a better way of doing this, but /shrug
var dateString = new Date(date.getTime() - (480 * 60000 ))
.toISOString()
.split("T")[0];
if (myTimer.isPastDue)
{
context.log('JavaScript is running late!');
}
var today = [];
for(var i in context.bindings.inputTable){
var it = context.bindings.inputTable[i];
if(it.PartitionKey == dateString)
{
context.log(`Looking at device: ${it.Device}`)
today.push(it)
}
}
today = Array.from(new Set(today))
for(var i in context.bindings.inputTable){
var it = context.bindings.inputTable[i];
if(it.PartitionKey == dateString)
{
if(it.Action != 'Single click'){
today = today.filter(x=> x.Device != it.Device)
}
if(it.Action == 'Finish'){
// today is already finished, so reset the array.
today = []
}
}
}
if(today.length == 2){
context.log('Duo Desire activate!')
context.bindings.outputTable = []
context.bindings.outputTable.push({
PartitionKey: dateString,
RowKey: "FINISH",
Action: "Finish"
})
context.bindings.outputQueueItem = ["+<PHONE1>", "+<PHONE2>"]
}
context.log('JavaScript timer trigger function ran!', timeStamp);
context.done();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment