Test curl data in format to be sent from cell modem now going into DB.

This commit is contained in:
jkaplon 2015-10-18 17:18:03 -04:00
parent 31a7cc6517
commit 5077b1436d

View File

@ -103,31 +103,59 @@ app.post('/', function(req, res){
var source = req.body.source;
console.log(req.body);
var innerDataJSON = JSON.parse(req.body.data);
var status = JSON.stringify(innerDataJSON.status, null, 4).slice(1,-1);
var coreid = JSON.stringify(req.body.coreid, null, 4).slice(1,-1);
var pubAt = JSON.stringify(req.body.published_at, null, 4).slice(1,-1);
var stmt = db.prepare("INSERT INTO Alerts (OrigJSON, coreid, published_at, status) VALUES (?, ?, ?, ?)");
stmt.run(
JSON.stringify(req.body, null, 4),
coreid,
pubAt,
status
);
stmt.finalize();
// If it's stripped down JSON sent by cell modem, there won't be a req.body.data key w/full-nested JSON as value!!!
// ???how to fix???
// If post body includes coreid and published_at, assume it's from a particle devcie and parse accordingly.
if (req.body.coreid !== undefined && req.body.published_at !== undefined) {
var innerDataJSON = JSON.parse(req.body.data);
var status = JSON.stringify(innerDataJSON.status, null, 4).slice(1,-1);
var coreid = JSON.stringify(req.body.coreid, null, 4).slice(1,-1);
var pubAt = JSON.stringify(req.body.published_at, null, 4).slice(1,-1);
var stmt = db.prepare("INSERT INTO Alerts (OrigJSON, coreid, published_at, status) VALUES (?, ?, ?, ?)");
stmt.run(
JSON.stringify(req.body, null, 4),
coreid,
pubAt,
status
);
stmt.finalize();
} else { // parse for minimal data from cell modem.
var deviceid = JSON.stringify(req.body.data, null, 4).slice(1,4);
var statusCode = JSON.stringify(req.body.data, null, 4).slice(5,7);
var statusFromCode;
switch (statusCode) {
case "01": statusFromCode = "Open"; break;
case "02": statusFromCode = "Closed"; break;
case "03": statusFromCode = "GoodMorning"; break;
case "04": statusFromCode = "GoodEvening";
default: statusFromCode = "UnknownStatus"
};
var received_at = new Date(Date.now());
received_at = received_at.toISOString();
console.log(statusFromCode + ' ' + received_at);
var stmtCell = db.prepare("INSERT INTO Alerts (OrigJSON, coreid, published_at, status) VALUES (?, ?, ?, ?)");
stmtCell.run(
JSON.stringify(req.body, null, 4),
deviceid,
received_at,
statusFromCode
);
stmtCell.finalize();
}
});
setInterval(function() {
// Check every hour to see if GoodMorning or GoodEvening has gone missing.
var deadManQry = "select published_at from Alerts where datetime(published_at) > datetime('now', '-14.5 hours') order by datetime(published_at) limit 1";
//db.get(deadManQry, function(err, row){
//if (err !== null) { console.log(err); }
db.get(deadManQry, function(err, row){
if (err !== null) { console.log(err); }
//else if (typeof row == undefined) {
//mailOptions.text = 'An alert message was received: \n\n';
else { // TEST, send the email as long as there's no error.
mailOptions.text = "TEST TEST TEST...It's been too long since the last data transmission from device. \n\n";
// Don't include any other details for now, will need to change DB query to get details on last message received.
//mailOptions.text = mailOptions.text + 'Status message, ' + status + '\n';
//mailOptions.text = mailOptions.text + 'Published at, ' + courtsopenUtils.getLocDateFromUTC(pubAt) + ' ' + courtsopenUtils.getLocTimeFromUTC(pubAt) + '\n';
//mailOptions.text = mailOptions.text + 'From device, ' + courtsopenUtils.getCoreNameFromCoreId(db, coreid) + '\n';
//mailOptions.text = mailOptions.text + 'From device, ' + row.coreName + '\n';
//transporter.sendMail(mailOptions, function(error, info){
//if(error){
@ -136,7 +164,8 @@ setInterval(function() {
//console.log('Message sent: ' + info.response);
//}
//});
//}
}
});
}, 60000);
app.listen(3000, function() {