UNPKG

node-red-contrib-barco-sources-bing

Version:

utility to get map information using bing

184 lines (169 loc) 6.42 kB
"use strict" module.exports = function(RED) { const request = require("request"); let _mongoUtils = require("./mongoUtils.js"); let _mqttUtils = require("./mqttUtils.js"); let _config = null; let _interval = null; let _node = null; let mqtturl = `mqtt://${process.env.MQTT_BROKER}:${process.env.MQTT_PORT}`; //let mqtturl = 'mqtt://10.51.49.122:10101'; // bing mappings const bing_severities = { "1": "LOW", "2": "LOW", "3": "MED", "4": "HIGH" }; const bing_types = { "1": "Accident", "2": "Congestion", "3": "DisabledVehicle", "4": "MassTransit", "5": "Miscellaneous", "6": "OtherNews", "7": "PlannedEvent", "8": "RoadHazard", "9": "Construction", "10": "Alert", "11": "Weather" }; function getBingData(){ console.log("Inside getBingData"); request.get( `http://dev.virtualearth.net/REST/v1/Traffic/Incidents/${_config.mapsArea}?severity=1,2,3,4&type=1,2,3,4,5,6,7,8,9,10,11&key=${_config.mapsKey}`, (err,res,body)=>{ if(err||res.statusCode!==200){ //error handling console.log("Error in getting Bing response"+err); } else { //save to mongo console.log("Got response from Bing"); let data = JSON.parse(body); sendToWorldMap(data); parseMessage(data, function(message){ _mongoUtils.updateDocument(message, function(err){ if(err){_node.status({fill:"red",shape:"dot",text:RED._("Error in saving update")});} else { _mqttUtils.publishEvent(_config.basePath, function(err){ if(err){_node.status({fill:"red",shape:"dot",text:RED._("Error in sending update")});} else { _node.status({fill:"green",shape:"dot",text:RED._("Bing.status.connected")}); } }); _node.status({fill:"green",shape:"dot",text:RED._("Bing.status.connected")}); } }); }); } }); } function sendToWorldMap(data){ /*let incidents = data.resourceSets[0].resources; incidents.forEach(e=>{ _node.send({payload:{ "name" : e.description, "command": {"zoom":3}, "lat": e.point.coordinates[0], "lon": e.point.coordinates[1], "photoUrl" : "http://www.universalcargo.com/wp-content/uploads/Shipping_Alert.png" }} ); });*/ let message = {payload:{ "mqtt": { "path":'/com/barco/analytics/map/bing', "url" : mqtturl }, "rest" : _config.basePath+'/incidents/bing' }}; //console.log("Sending message to worldmap: "+JSON.stringify(message)); _node.send(message); } function parseMessage(data, callback){ let returnArray = []; let incidents = data.resourceSets[0].resources; incidents.forEach(e=>{ let ob = { "type": e.type, "severity": e.severity, "description": { "short": e.description, "long": e.description }, "start": getISODateFormat(e.start, function(result){return result}), "end": getISODateFormat(e.end, function(result){return result}), "coordinates": { "lat": e.point.coordinates[0], "lon": e.point.coordinates[1] }, "lastModified": getISODateFormat(e.lastModified, function(result){return result}), "iconURL": 'http://content.mqcdn.com/mqtraffic/congestion_mod.png', "source": e }; returnArray.push(ob); }); let message = { "payload": { "bing": { "source": data, "incidents": returnArray } } }; callback(message); } function getISODateFormat(date, callback){ let oldDate = String(date); let newDate = oldDate.split("(")[1].split(")")[0]; //console.log("Transformed Date is :"+ newDate); return callback(new Date(Number(newDate)).toISOString()); } function BingSource(config) { RED.nodes.createNode(this,config); _config = config; _node = this; console.log("BasePath is: "+_config.basePath) if(_config.disable === true){ console.log("Stopping interval") stopInterval(); } else { console.log("Starting interval") setTimeout(function(){getBingData()}, 10000); startInterval(); } RED.httpNode.get('/incidents/bing', function (req, res) { _mongoUtils.findDocuments(function(err, msg){ if(err) res.send(err); else{ //console.log("####got response from mongodb: "+JSON.stringify(msg)); msg = JSON.parse(JSON.stringify(msg)); msg = msg.bing; msg.incidents.forEach(e=>{ e.type = bing_types[String(e.type)]; e.severity = bing_severities[String(e.severity)]; }); res.send(msg); } }); }.bind(this)); this.on("close", function (removed, done) { if (removed) { _mqttUtils.closeConnection(); } let node = this; done(); }); } function stopInterval() { _interval && clearInterval(_interval); _interval = null; } function startInterval() { _interval = setInterval(()=>{ getBingData(); }, 5*60*1000); } RED.nodes.registerType("bing",BingSource); };