Skip to content

Instantly share code, notes, and snippets.

@zobalogh
Created December 1, 2014 11:57
Show Gist options
  • Save zobalogh/08d7dfda4622999363c8 to your computer and use it in GitHub Desktop.
Save zobalogh/08d7dfda4622999363c8 to your computer and use it in GitHub Desktop.
bus_or_underground_flow
[{"id":"4e726fa6.b18d9","type":"tfl-bus","lat":"51.507268","lon":"-0.16573","radius":"600","stopCode1":"71003","lineID":"274","stopName":"Hyde Park Street towards Lancaster Gate or Notting Hill","lineName":"274 to Lancaster Gate","name":"Bus to Acton Green","acceptedtcs":true,"x":317,"y":170,"z":"a163e101.5e9c2","wires":[["94523edd.6badc"]]},{"id":"daa088a3.255f78","type":"tfl underground","line":"Piccadilly","name":"Check Piccadilly line status","acceptedtcs":true,"x":234,"y":261,"z":"a163e101.5e9c2","wires":[["9b69fbc5.649608"]]},{"id":"b65db9d6.49a248","type":"inject","name":"","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":111,"y":170,"z":"a163e101.5e9c2","wires":[["4e726fa6.b18d9","daa088a3.255f78","244841bf.dbb7be"]]},{"id":"244841bf.dbb7be","type":"openweathermap","name":"London weather","lon":"","lat":"","city":"London","country":"England","x":236,"y":66,"z":"a163e101.5e9c2","wires":[["a4eb163f.5b14e8"]]},{"id":"9b69fbc5.649608","type":"function","name":"Are there any problems on the Piccadilly line to Action Town?","func":"var stations = [\"Hyde Park Corner\", \"Knightsbridge\", \"South Kensington\", \"Earl's Court\", \"Hammersmith\", \"Turnham Green\", \"Acton Town\"];\nvar lineok = true;\nvar minordelays = false;\n\nif (msg.payload.goodservice) {\n // minor delays are reported as good service but sometimes have a info about delay\n\tif (msg.payload.description === \"Minor Delays\") {\n\t minordelays = true;\n\t if (msg.payload.branchdisruptions.length > 0) {\n\t for (var l = 0; l < msg.payload.branchdisruptions.length; l++) {\n\t \n\t\t \tvar disruption = msg.payload.branchdisruptions[l];\n\t\t \tvar stationFrom = disruption.StationFrom[0].$.Name;\n\t \t\tvar index = -1;\n\t \t\tfor (var j = 0; j < stations.length; j++) {\n\t \t\t\tif(stations[j] === stationFrom) {\n\t \t \t\tindex = j;\n\t \t\t\t}\n\t \t\t}\n\t \t\tif ((index !== -1) && (index !== (stations.length - 1))) {\n\t \t\t\tvar stationTo = disruption.StationTo[0].$.Name;\n\t \t\t\tfor (var k = index; k < stations.length; j++) {\n\t \t \t\t\tif(stations[k] === stationTo) {\n\t \t\t\tlineok = false;\n\t \t\t\t}\n\t \t\t\t}\n\t \t\t}\n\t\t\t}\n\t\t}\t\t\t\n\t}\n} else {\n\tlineok = false;\n}\nmsg.payload = {};\nmsg.topic = {};\nmsg.topic.picadilly = {};\nmsg.topic.picadilly.ok = false;\n\nif (lineok) {\n\tmsg.topic.picadilly.ok = true;\n} \t\n\nreturn msg;","outputs":1,"x":600,"y":261,"z":"a163e101.5e9c2","wires":[["a1040f49.5efbf"]]},{"id":"a1040f49.5efbf","type":"function","name":"Make decision","func":"\n\nif (!context.travelData) {\n\tcontext.travelData = {};\n}\n\nif(msg.topic.weatherOK) {\n\tcontext.travelData.weatherOK = msg.topic.weatherOK;\n}\n\nif(msg.topic.picadilly && msg.topic.picadilly.ok) {\n\tcontext.travelData.piccadillyOK = msg.topic.picadilly.ok;\n}\n\nif(msg.topic.busOK) {\n\tcontext.travelData.busOK = msg.topic.busOK;\n}\n\nmsg = {};\n\n// done set up\n\nif(context.travelData.piccadillyOK && context.travelData.busOK && context.travelData.weatherOK) {\n\tif(context.travelData.weatherOK === false) {\n\t\tif(context.travelData.piccadillyOK === true) {\n\t\t\tmsg.payload = \"You should use the underground as the weather's bad.\";\n\t\t} else {\n\t\t\tmsg.payload = \"The weather is bad and the underground is broken. Hire a taxi.\";\n\t\t}\n\t} else { // weather's good\n\t\tif(context.travelData.busOK === true) {\n\t\t\tmsg.payload = \"The weather is good, the bus is running, take the bus please and enjoy the scenery.\";\n\t\t} else if(context.travelData.busOK === false) {\n\t\t\tif(context.travelData.piccadillyOK === true) {\n\t\t\t\tmsg.payload = \"You should use the underground as the buses are not operational.\";\n\t\t\t} else {\n\t\t\t\tmsg.payload = \"The buses and the underground both have trouble. Take a taxi.\";\n\t\t\t}\n\t\t}\n\t}\n\treturn msg;\n} else {\n\tmsg.payload = context.travelData;\n\treturn msg;\n}","outputs":1,"x":936,"y":78,"z":"a163e101.5e9c2","wires":[["fd5dc55e.02a238"]]},{"id":"94523edd.6badc","type":"function","name":"Is my bus arriving within 10 mins?","func":"msg.topic = {};\nmsg.topic.busOK = true;\nvar estimatedBusArrival = msg.payload.EstimatedTime;\n\nif(estimatedBusArrival) {\n\tvar tenMinutesMillis = 600000;\n\tvar currentTimeMillis = new Date().getTime();\n\tvar timeDifference = (estimatedBusArrival.getTime()) - currentTimeMillis; // assuming that the bus arrival is in the future\n\tif(timeDifference <= tenMinutesMillis) {\n\t\tmsg.topic.busOK = true;\n\t} else {\n\t\tmsg.topic.busOK = false;\n\t}\n} else {\n\tmsg.topic.busOK = false;\n}\n\nreturn msg;","outputs":1,"x":602,"y":170,"z":"a163e101.5e9c2","wires":[["a1040f49.5efbf"]]},{"id":"fd5dc55e.02a238","type":"debug","name":"","active":true,"console":"false","complete":"payload","x":1164,"y":123,"z":"a163e101.5e9c2","wires":[]},{"id":"a4eb163f.5b14e8","type":"switch","name":"Is the weather Good?","property":"payload.weather","rules":[{"t":"regex","v":"Rain|Snow|Thunderstorm"},{"t":"else"}],"checkall":"false","outputs":2,"x":464,"y":66,"z":"a163e101.5e9c2","wires":[["2cb49436.d34b6c"],["ffa21523.005de8"]]},{"id":"ffa21523.005de8","type":"function","name":"Mark as good","func":"msg.topic = {};\nmsg.topic.weatherOK = true;\nreturn msg;","outputs":1,"x":681,"y":104,"z":"a163e101.5e9c2","wires":[["a1040f49.5efbf"]]},{"id":"2cb49436.d34b6c","type":"function","name":"Mark as bad","func":"msg.topic = {};\nmsg.topic.weatherOK = false;\nreturn msg;","outputs":1,"x":681,"y":23,"z":"a163e101.5e9c2","wires":[["a1040f49.5efbf"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment