Last active
February 11, 2016 16:19
-
-
Save dexterlabora/b2c685e86e0ea1377e65 to your computer and use it in GitHub Desktop.
Train schedule and automation with Node-RED
This file contains hidden or 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
[{"id":"b1d8d4eb.4e2728","type":"mqtt-broker","z":"e60e9052.19f17","broker":"aws.internetoflego.com","port":"1883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"15","cleansession":true,"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":""},{"id":"ba1ad5cc.45e528","type":"pubnub-keys","pub_key":"","sub_key":""},{"id":"4419e8c7.bbe618","type":"http request","z":"e60e9052.19f17","name":"API Call","method":"GET","ret":"txt","url":"","x":534.5,"y":105,"wires":[["9cb083cd.634f8","2ddf34e0.d220cc"]]},{"id":"4bf1e239.b40e1c","type":"inject","z":"e60e9052.19f17","name":"","topic":"","payload":"910GLONFLDS","payloadType":"string","repeat":"1","crontab":"","once":true,"x":106.5,"y":109,"wires":[["110734c1.eef8cb"]]},{"id":"9a4a5cb0.65b5a","type":"function","z":"e60e9052.19f17","name":"Request URL to TFL","func":"msg.stationid = msg.payload;\nmsg.url = \"https://api.tfl.gov.uk/StopPoint/\"+msg.stationid+\"/arrivals\";\nreturn msg;","outputs":1,"noerr":0,"x":366.5,"y":106,"wires":[["4419e8c7.bbe618"]]},{"id":"9cb083cd.634f8","type":"debug","z":"e60e9052.19f17","name":"TFL API response","active":true,"console":"false","complete":"payload","x":719.5,"y":107,"wires":[]},{"id":"9e12935e.61ed7","type":"debug","z":"e60e9052.19f17","name":"TFL Next Trains","active":true,"console":"false","complete":"payload","x":662.5,"y":223,"wires":[]},{"id":"2d79be7.fd28642","type":"pubnub out","z":"e60e9052.19f17","keys":"ba1ad5cc.45e528","channel":"trainschedule","x":204.5,"y":341,"wires":[]},{"id":"2ddf34e0.d220cc","type":"json","z":"e60e9052.19f17","name":"","x":689.5,"y":141,"wires":[["2e361136.d1c9ee"]]},{"id":"b09a07d2.4f65f8","type":"http in","z":"e60e9052.19f17","name":"","url":"/trainschedule","method":"get","swaggerDoc":"","x":98.5,"y":65,"wires":[["aca72f6.f5358d"]]},{"id":"aca72f6.f5358d","type":"function","z":"e60e9052.19f17","name":"request","func":"msg.payload = \"910GLONFLDS\";\nreturn msg;","outputs":1,"noerr":0,"x":257.5,"y":64,"wires":[["9a4a5cb0.65b5a"]]},{"id":"f628b4f.f09d748","type":"http response","z":"e60e9052.19f17","name":"","x":633.5,"y":259,"wires":[]},{"id":"6b14d440.94eb2c","type":"comment","z":"e60e9052.19f17","name":"TFL Schedule","info":"","x":79.5,"y":29,"wires":[]},{"id":"7ed35cd5.812ca4","type":"comment","z":"e60e9052.19f17","name":"Track Switch","info":"","x":77.5,"y":294,"wires":[]},{"id":"4e1f5b18.b1e0a4","type":"comment","z":"e60e9052.19f17","name":"Sort Outbound Trains","info":"","x":98.5,"y":189,"wires":[]},{"id":"2e361136.d1c9ee","type":"function","z":"e60e9052.19f17","name":"Outbound Schedule new schema","func":"var tfl = msg.payload;\nvar trains = {};\n\n// check for valid data first\nif (msg.statusCode === 200) {\n var expected;\n var nextTrains = [];\n \n if(tfl[0] !== undefined){\n var station = tfl[0].stationName;\n\n // sort schedule\n tfl.sort(function(a,b){\n \treturn (a.timeToStation - b.timeToStation);\n \t});\n \n // iterate through all schedule records and save required data\n for (var i = 0; i < tfl.length; i++) {\n if(tfl[i].direction == 'outbound'){\n \tnextTrains.push({\n \t 'expected': (tfl[i].timeToStation)/60,\n \t 'destination': tfl[i].destinationName\n \t});\n } \n }\n }else{\n // no schedule received\n station = \"\";\n nextTrains.push({\n 'expected': 0,\n \t'destination': \"Out of Service\"\n })\n }\n trains.station = station;\n trains.schedule = nextTrains;\n}\n\n// display and send schedule data\nconsole.log(\"outbound schedule new schema > \");\nconsole.log(util.inspect(trains, false, null));\nmsg.payload = trains;\nreturn msg;","outputs":1,"noerr":0,"x":318.5,"y":229,"wires":[["9e12935e.61ed7","2d79be7.fd28642","e9321505.16cde8","f628b4f.f09d748"]]},{"id":"b4b2a289.4b4d6","type":"mqtt out","z":"e60e9052.19f17","name":"","topic":"","qos":"","retain":"","broker":"b1d8d4eb.4e2728","x":595.5,"y":500,"wires":[]},{"id":"e9321505.16cde8","type":"function","z":"e60e9052.19f17","name":"Format to MQTT display string","func":"var station = msg.payload.station;\nvar schedule = msg.payload.schedule;\nvar destination;\nvar split;\nmsg.topic = \"/trainschedule/\" + msg.stationid;\n\nif (schedule[0].destination == \"Out of Service\"){\n destination = schedule[0].destination;\n msg.payload = destination;\n return msg;\n}else{\n // Split the string and only use the first two words of each value\n split = station.split(\" \");\n station = split[0] +\" \"+ split[1] + \"\\n\\n\"; \n msg.payload = station;\n for(var x = 0; x < schedule.length; x++){\n split = schedule[x].destination.split(\" \");\n destination = split[0] +\" \"+ split[1];\n msg.payload += pad(schedule[x].expected) + \"m\" + \" \" + destination + \"\\n\";\n }\n return msg;\n}\n\n\nfunction pad(n) {\n return (n < 10) ? (\"0\" + n) : n;\n}","outputs":1,"noerr":0,"x":290.5,"y":473,"wires":[["b4b2a289.4b4d6","e1254324.1edac"]]},{"id":"56e7e2e7.a9181c","type":"comment","z":"e60e9052.19f17","name":"Schedule Display","info":"","x":83,"y":414,"wires":[]},{"id":"110734c1.eef8cb","type":"delay","z":"e60e9052.19f17","name":"","pauseType":"rate","timeout":"30","timeoutUnits":"seconds","rate":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":228,"y":148,"wires":[["9a4a5cb0.65b5a"]]},{"id":"e1254324.1edac","type":"debug","z":"e60e9052.19f17","name":"MQTT Format","active":false,"console":"false","complete":"true","x":587,"y":415,"wires":[]},{"id":"12aefeac.ed5101","type":"comment","z":"e60e9052.19f17","name":"MQTT topic /trainschedule/stationid","info":"stationid is inherited from inject","x":628,"y":461,"wires":[]}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment