Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
2 lines (1 sloc) 11.7 KB
[{"id":"2645a2d3.c6dbfe","type":"function","z":"df8e8d4a.5d829","name":"gearbox","func":"// do you want a \"retained\" message on each topic?\nvar retained = true;\nvar selected = context.get('selected') || null;\n\nif (!selected)\n{\n node.status({fill:\"red\",shape:\"ring\",text:\"'neutral'\"});\n}\n\nif (msg.topic == \"select\")\n{\n selected = msg.payload;\n context.set('selected', selected);\n node.status({fill:\"green\",shape:\"dot\",text:selected});\n if (retained)\n {\n var retains = context.get('retains') || [];\n if (retains[selected]) {\n return {topic: selected, payload: retains[selected]};\n }\n }\n}\nelse \n{\n if (retained)\n {\n var retains = context.get('retains') || [];\n retains[msg.topic] = msg.payload;\n context.set('retains', retains);\n }\n if (msg.topic == selected) \n {\n return msg;\n }\n}\n// otherwise send nothing\n","outputs":1,"noerr":0,"x":1127,"y":121,"wires":[["c27c9b9a.c642b"]]},{"id":"43cd17d6.09ed58","type":"trigger","z":"df8e8d4a.5d829","op1":"neutral","op2":"cheerlights","op1type":"str","op2type":"str","duration":"5","extend":true,"units":"min","reset":"","name":"Cheerlights if not doing Bus Times","x":685,"y":200,"wires":[["ce1100a4.6f6508"]]},{"id":"d2c35de3.68d1f8","type":"inject","z":"df8e8d4a.5d829","name":"Weekdays 7-8am","topic":"","payload":"1","payloadType":"str","repeat":"","crontab":"*/1 7 * * 1,2,3,4,5","once":false,"x":95,"y":260,"wires":[["c4088749.cf2a48"]]},{"id":"f968d11b.b782f8","type":"inject","z":"df8e8d4a.5d829","name":"every minute","topic":"run","payload":"1","payloadType":"str","repeat":"60","crontab":"","once":false,"x":85,"y":220,"wires":[["1dbb321f.f7aa2e"]]},{"id":"c27c9b9a.c642b","type":"function","z":"df8e8d4a.5d829","name":"mux","func":"\nreturn msg;","outputs":1,"noerr":0,"x":1615,"y":220,"wires":[["bed6dce7.4780e","61f39217.9ecb64"]]},{"id":"bed6dce7.4780e","type":"debug","z":"df8e8d4a.5d829","name":"Debug","active":true,"console":"false","complete":"payload","x":1735,"y":260,"wires":[]},{"id":"28eb4ee0.72836a","type":"inject","z":"df8e8d4a.5d829","name":"Flash Red Four Times","topic":"","payload":"#ff00004","payloadType":"str","repeat":"","crontab":"","once":false,"x":1365,"y":20,"wires":[["c27c9b9a.c642b"]]},{"id":"7b8dd82d.47dc58","type":"function","z":"df8e8d4a.5d829","name":"add 0","func":"msg.payload += \"0\";\nreturn msg;","outputs":1,"noerr":0,"x":775,"y":120,"wires":[["6596c5e1.e2a374"]]},{"id":"c4088749.cf2a48","type":"http request","z":"df8e8d4a.5d829","name":"TfL API","method":"GET","ret":"txt","url":"http://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?LineName=AAA,BB&StopCode1=CCCCC&DirectionID=1&ReturnList=EstimatedTime","tls":"","x":325,"y":260,"wires":[["43cd17d6.09ed58","a4fbbcee.9ec7f","df381000.b4683"]]},{"id":"a4fbbcee.9ec7f","type":"function","z":"df8e8d4a.5d829","name":"Time Till Stop","func":"// Time Till Stop\n//\n// This function parses the API message returned by TFL\n// to extract the ETA of buses at the desired stop.\n// It then subtracts the current time to calculate the\n// time left until each bus arrives at the stop.\n\n// It returns an array of messages containing the\n// times-till-stop in minutes for the incoming buses.\n\n// Initialise output message array.\nvar timeMsgs = [];\n\n// Split the API message on \"]\" character\n// to get an array of bus data.\nvar buses = msg.payload.split(\"]\");\n\n\nvar current_time = Date.now();\n//node.warn(\"current_time: \"+current_time);\n\n\n// Discard the first element as it contains metadata\nbuses.shift();\n// Discard last item as it's an empty string,\n// which is a side-effect of splitting on ']'.\nbuses.pop();\n\n// Iterate over all buses in the array\nfor (var i in buses) {\n\n\t// In javascript, for/in loops return index, not value\n\t// so we need to index into original array to get bus info.\n\tbus = buses[i];\n\t\n // The per-bus data fields are comma-separated,\n // so split them on \",\".\n fields = bus.split(\",\");\n \n // The second field (indexes start at 0) is the\n // ETA of the bus in milliseconds since Unix time\n // epoch (1st Jan 1970.)\n var eta = fields[1];\n \n // Calculate time-till-stop by subtracting the\n // current time from the ETA and dividing by 1000\n // to turn it into seconds instead of milliseconds.\n \n var tts = (eta - current_time) / 1000;\n // then turn it into integer minutes (rounded down) to err on the safe side\n tts = Math.floor(tts/60); \n \n\t// Add time-till-stop to output message array.\n timeMsgs.push(tts);\n}\n\n// output the array of minutes-till-stop\nmsg.payload = timeMsgs;\nreturn msg;","outputs":"1","noerr":0,"x":625,"y":260,"wires":[["fef60ae7.a1c6e8","143f6f17.fc8f71"]]},{"id":"a762ed4.6ea961","type":"change","z":"df8e8d4a.5d829","name":"black","rules":[{"t":"set","p":"payload","pt":"msg","to":"#0000000","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1415,"y":380,"wires":[["c27c9b9a.c642b"]]},{"id":"fef60ae7.a1c6e8","type":"debug","z":"df8e8d4a.5d829","name":"","active":false,"console":"false","complete":"false","x":935,"y":280,"wires":[]},{"id":"df381000.b4683","type":"debug","z":"df8e8d4a.5d829","name":"","active":true,"console":"false","complete":"false","x":615,"y":300,"wires":[]},{"id":"143f6f17.fc8f71","type":"function","z":"df8e8d4a.5d829","name":"thresholds","func":"// count how many buses are 3-6 minutes away, and \n// how many are 3-30 minutes away\n// returns the two counts so we can decide what to display\n\n// \"greens\" are the buses that are 3-6 minutes away\nvar greens = 0;\n// \"reds\" are the buses that are within 30 mins\n// (actually, bus will be red if this count is 0)\nvar reds = 0;\n\nfor (var index in msg.payload)\n{\n var tts = msg.payload[index];\n if (tts >=3 && tts <= 6) // 3-6 mins away\n {\n greens ++;\n //node.warn(tts+\" -> green\");\n }\n if (tts >=3 && tts <= 30) // 3-30 mins away\n {\n reds ++;\n //node.warn(tts+\" -> red\");\n }\n}\n\n// return the number of greens and number of reds we counted\nmsg.payload = {'greens': greens, 'reds': reds};\nreturn msg;","outputs":"1","noerr":0,"x":935,"y":360,"wires":[["ac9fc361.dee97","1e871ada.269335"]]},{"id":"3665b84b.2696e8","type":"change","z":"df8e8d4a.5d829","name":"green","rules":[{"t":"set","p":"payload","pt":"msg","to":"#005500*","tot":"str"}],"x":1255,"y":360,"wires":[["c27c9b9a.c642b"]]},{"id":"ac9fc361.dee97","type":"switch","z":"df8e8d4a.5d829","name":"greens?","property":"payload.greens","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","outputs":2,"x":1125,"y":380,"wires":[["3665b84b.2696e8"],["3be072be.b551b6"]]},{"id":"3be072be.b551b6","type":"switch","z":"df8e8d4a.5d829","name":"reds?","property":"payload.reds","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","outputs":2,"x":1255,"y":400,"wires":[["a762ed4.6ea961"],["22397431.3be9dc"]]},{"id":"22397431.3be9dc","type":"change","z":"df8e8d4a.5d829","name":"red","rules":[{"t":"set","p":"payload","pt":"msg","to":"#FF0000*","tot":"str"}],"x":1415,"y":420,"wires":[["c27c9b9a.c642b"]]},{"id":"1e871ada.269335","type":"debug","z":"df8e8d4a.5d829","name":"","active":false,"console":"false","complete":"payload","x":1135,"y":280,"wires":[]},{"id":"54ea474a.145968","type":"debug","z":"df8e8d4a.5d829","name":"","active":false,"console":"false","complete":"payload","x":1135,"y":200,"wires":[]},{"id":"6596c5e1.e2a374","type":"change","z":"df8e8d4a.5d829","name":"cheerlights","rules":[{"t":"set","p":"topic","pt":"msg","to":"cheerlights","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":935,"y":120,"wires":[["2645a2d3.c6dbfe"]]},{"id":"ce1100a4.6f6508","type":"change","z":"df8e8d4a.5d829","name":"select","rules":[{"t":"set","p":"topic","pt":"msg","to":"select","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":915,"y":200,"wires":[["54ea474a.145968","2645a2d3.c6dbfe"]]},{"id":"d3b0cac5.57aaa","type":"inject","z":"df8e8d4a.5d829","name":"","topic":"select","payload":"cheerlights","payloadType":"str","repeat":"","crontab":"","once":true,"x":945,"y":60,"wires":[["2645a2d3.c6dbfe"]]},{"id":"820f92a8.7a1aa","type":"inject","z":"df8e8d4a.5d829","name":"test \"no buses\"","topic":"","payload":"[]","payloadType":"json","repeat":"","crontab":"","once":false,"x":645,"y":360,"wires":[["143f6f17.fc8f71"]]},{"id":"455f93b1.8273d4","type":"inject","z":"df8e8d4a.5d829","name":"Test: Start","topic":"select","payload":"run","payloadType":"str","repeat":"","crontab":"","once":false,"x":65,"y":140,"wires":[["1dbb321f.f7aa2e"]]},{"id":"e929673.42c1e18","type":"inject","z":"df8e8d4a.5d829","name":"Test: Stop","topic":"select","payload":"stop","payloadType":"str","repeat":"","crontab":"","once":true,"x":65,"y":180,"wires":[["1dbb321f.f7aa2e"]]},{"id":"d99bc57f.bb7ce8","type":"inject","z":"df8e8d4a.5d829","name":"Go Blue","topic":"","payload":"#0000ff0","payloadType":"str","repeat":"","crontab":"","once":false,"x":1325,"y":100,"wires":[["c27c9b9a.c642b"]]},{"id":"fd5e6874.ad4578","type":"inject","z":"df8e8d4a.5d829","name":"Go Green","topic":"","payload":"#00ff000","payloadType":"str","repeat":"","crontab":"","once":false,"x":1325,"y":60,"wires":[["c27c9b9a.c642b"]]},{"id":"61f39217.9ecb64","type":"mqtt out","z":"df8e8d4a.5d829","name":"","topic":"Lights","qos":"","retain":"","broker":"9c5092a3.873418","x":1735,"y":180,"wires":[]},{"id":"a1f29890.0e1fc8","type":"http request","z":"df8e8d4a.5d829","name":"Cheerlights API","method":"GET","ret":"txt","url":"http://api.thingspeak.com/channels/1417/field/2/last.txt","tls":"","x":625,"y":120,"wires":[["7b8dd82d.47dc58"]]},{"id":"d099be6.afe58c","type":"inject","z":"df8e8d4a.5d829","name":"Don't need to touch","topic":"","payload":"","payloadType":"date","repeat":"30","crontab":"","once":true,"x":405,"y":80,"wires":[["a1f29890.0e1fc8"]]},{"id":"1bcad187.7d9d26","type":"comment","z":"df8e8d4a.5d829","name":"Edit the TFL API node! Double click here for info.","info":"The URL needs to be in this format\nhttp://countdown.api.tfl.gov.uk/interfaces/ura/instant_V1?LineName=AAA,BB&StopCode1=CCCCC&DirectionID=1&ReturnList=EstimatedTime\n\nChange:\nLinename is the bus number you require.\nYou can add more than one, separated by commas\nReplace AA and BB with the ones required.\n\nStopCode1 is the bus-stop number, this can be found on the bus-stop.\nReplace CCCCC with the bus-stop number required.\n\nDirectionID\ninbound = 1 or outbound = 2","x":305,"y":320,"wires":[]},{"id":"1dbb321f.f7aa2e","type":"function","z":"df8e8d4a.5d829","name":"gearbox","func":"// do you want a \"retained\" message on each topic?\nvar retained = true;\nvar selected = context.get('selected') || null;\n\nif (!selected)\n{\n node.status({fill:\"red\",shape:\"ring\",text:\"'neutral'\"});\n}\n\nif (msg.topic == \"select\")\n{\n selected = msg.payload;\n context.set('selected', selected);\n node.status({fill:\"green\",shape:\"dot\",text:selected});\n if (retained)\n {\n var retains = context.get('retains') || [];\n if (retains[selected]) {\n return {topic: selected, payload: retains[selected]};\n }\n }\n}\nelse \n{\n if (retained)\n {\n var retains = context.get('retains') || [];\n retains[msg.topic] = msg.payload;\n context.set('retains', retains);\n }\n if (msg.topic == selected) \n {\n return msg;\n }\n}\n// otherwise send nothing\n","outputs":1,"noerr":0,"x":263,"y":169,"wires":[["c4088749.cf2a48"]]},{"id":"9c5092a3.873418","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]