/*20171208
input message should contain:
msg.timestamp ie 1514764800000 is an epoch milliseconds timestamp representing 2018.01.01. at 00:00:00.000
(if msg.timestamp does not exist then "now" will be used)
either single value-based payload:
msg.payload ie 72.1 or any string or number representing the value for the topic
msg.topic ie diningroom/temperature
or multi-variable object-based payload containing property : value pairs (use . as delimiter)
msg.payload ie {basement$temperature : 72.1, diningroom$temperature: 73.2}
msg.topic ie wisconsin/summerhome becomes the prefix for each item, creating final items such as wisconsin/summerhome/basement/temperature
*/
var wwQueue = flow.get('wwQueue') ||{"data":[]};
//var dt = new Date(); // defaults to now //var dtstring = dt.toISOString();
//use timestamp if it exists otherwise default to now
if ((typeof msg.timestamp !== 'undefined')){dtstring = new Date(msg.timestamp).toISOString();}
else {dtstring = new Date().toISOString();}
i = 0;
while (
(typeof wwQueue.data !== 'undefined') &&
(typeof wwQueue.data[i] !== 'undefined') &&
(typeof wwQueue.data[i].dateTime !== 'undefined') &&
(new Date(wwQueue.data[i].dateTime).getTime() > new Date(dtstring).getTime()) //while we find newer dates keep looking
) {i++;}
var obj = {};
if (
(typeof wwQueue.data !== 'undefined')&&
(typeof wwQueue.data[i] !== 'undefined')&&
(typeof wwQueue.data[i].dateTime !== 'undefined')&&
(new Date(wwQueue.data[i].dateTime).getTime() == new Date(dtstring).getTime()) //if exact date found then we need to handle existing
) {
obj = wwQueue.data.splice(i,1)[0];
} else {
obj.dateTime = dtstring;
}
if (typeof msg.payload !== "object"){
var tag = msg.topic.replace(/\//g,"."); //globally replace / with .
obj[tag]=msg.payload;//add new tag and value to this dateTime record
} else {
var prefix = msg.topic.replace(/\//g,".") + ".";
var tag = "";
for(var propertyName in msg.payload) {
tag = prefix + propertyName;//propertyName.replace(/\$/g,".") was going to use $ delimiter but dot "." works
obj[tag] = msg.payload[propertyName];
// node.error(JSON.stringify(obj,null, 4));
}
}
wwQueue.data.splice(i,0,obj); //add new, full dateTime record back to all record data in the right spot
flow.set('wwQueue',wwQueue);
msg.topic = "tapia/home";
msg.payload = wwQueue;
return msg;
/*
{
"data": [
{
"dateTime": "2017-03-20T12:21:12.199Z",
"Reactor3.Level": 1.51860072870498,
"Reactor3.Temp": 27.1360543141452,
"Reactor3.InletValve": 0,
"Reactor3.Step": 7,
"Line3.Units": 1518,
"Line3.Product": "BRN"
}
]
}
*/
** AND ** then the message is passed through a secure Bearer token node where it stores my private authorization info
// configure http (headers,url,method)
msg.headers = {
// "Authorization" : msg.authorization,
"Content-type" : "application/json"
};
msg.method = "POST";
msg.headers.Authorization = msg.Authorization; //copy Authorization to be under headers
delete msg.Authorization; // and remove from old location
return msg;
/*
The following works (replace token with real one):
msg.headers = {
"Authorization" : "Bearer aThisIsMyFakeTokenYourWillBeLonger-AjsTIEvRKKQ",
"Content-type" : "application/json"
};
msg.method = "POST";
*/