@sigi234 anbei wie es nun bei mir läuft.
hab den extractData von MegrCurry mit dem Addon von rurh70 genommen. Hat zwar kein Debug aber funktioniert damit:
/*
returns:
1\. travelDurationTraffic in min
2\. travelDuration in min
3\. traveDelay in min
4\. trafficJam boolean
5\. trafficCongestion string
6\. travelDistance in km
7\. msg object (for debugging and return to the loop)
*/
var travelDurationTraffic = Math.ceil(msg.payload.resourceSets[0].resources[0].travelDurationTraffic / 60);
var travelDuration = Math.ceil(msg.payload.resourceSets[0].resources[0].travelDuration / 60);
var msgTraffic = [
{
topic: msg.topic_prefix + ".name",
payload: msg.name
},
{
topic: msg.topic_prefix + ".travelDurationTraffic",
payload: travelDurationTraffic
},
{
topic: msg.topic_prefix + ".travelDuration",
payload: travelDuration
},
{
topic: msg.topic_prefix + ".travelDelay",
payload: travelDurationTraffic - travelDuration
},
{
topic: msg.topic_prefix + ".trafficJam",
payload: (travelDurationTraffic - travelDuration >= msg.threshold)
},
{
topic: msg.topic_prefix + ".trafficCongestion",
payload: msg.payload.resourceSets[0].resources[0].trafficCongestion
},
{
topic: msg.topic_prefix + ".travelDistance",
payload: msg.payload.resourceSets[0].resources[0].travelDistance
},
{
topic: msg.topic_prefix + ".travelDescription",
payload: msg.payload.resourceSets[0].resources[0].routeLegs[0].description
}
];
return [msgTraffic,msg];
Und hier nochmal der komplette Flow:
[
{
"id": "f38be5c5.594708",
"type": "tab",
"label": "BingMaps Traffic",
"disabled": false,
"info": ""
},
{
"id": "ec0cc8e4.d21bf8",
"type": "http request",
"z": "f38be5c5.594708",
"name": "askBing",
"method": "GET",
"ret": "obj",
"url": "",
"x": 460,
"y": 400,
"wires": [
[
"65099a04.fa68e4",
"bf572708.026bb8",
"671cf5a3.93243c"
]
]
},
{
"id": "457c032a.c977cc",
"type": "inject",
"z": "f38be5c5.594708",
"name": "Trigger",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "600",
"crontab": "",
"once": false,
"x": 181.88883209228516,
"y": 144.00007438659668,
"wires": [
[
"f8b6cb8e.148ff8"
]
]
},
{
"id": "f56d10d3.1a8c6",
"type": "function",
"z": "f38be5c5.594708",
"name": "defineRoutes",
"func": "/*\nDefine routes:\nfrom: starting address, encode spaces with %20\nto: destination address, encode spaces with %20\nthreshold: in minutes, if duration with traffic is larger than threshold, set trafficJam to true\nname: name :-)\n*/\n\nmsg.routes = [\n {\n way: \"wayPoint.1='xxxxxxxxx'&\" +\n \"viaWayPoint.2='xxxxxxxxxx'&\" +\n \"viaWayPoint.3='xxxxxxxxxxx'&\" +\n \"wayPoint.4='xxxxxxxxxxxxx'\",\n threshold: 5,\n name: 'Home2Office'\n },\n {\n way: \"wayPoint.1='xxxxxxxx'&\" +\n \"viaWayPoint.2='xxxxxxxxxx'&\" +\n \"viaWayPoint.3='xxxxxxxxxxx'&\" +\n \"wayPoint.4='xxxxxxxxxx'\",\n threshold: 5,\n name: 'Office2Home'\n }\n];\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 518.8889770507812,
"y": 144.0000762939453,
"wires": [
[
"ed4644fd.7f8808"
]
]
},
{
"id": "47798bfa.dc51e4",
"type": "function",
"z": "f38be5c5.594708",
"name": "prepareIteration",
"func": "// msg.method = \"GET\";\n\nmsg.url = encodeURI(\"http://dev.virtualearth.net/REST/v1/Routes?\" + msg.payload.way + \"&optimize=timeWithTraffic&key=\" + msg.config.bing_key);\n\nmsg.topic_prefix = msg.config.topic_prefix + msg.payload.name;\n\nmsg.name = msg.payload.name; \n\nmsg.threshold = msg.payload.threshold;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 298.22222900390625,
"y": 400.5555419921875,
"wires": [
[
"ec0cc8e4.d21bf8",
"4bb3a641.55bbd8"
]
]
},
{
"id": "f8b6cb8e.148ff8",
"type": "function",
"z": "f38be5c5.594708",
"name": "setConfig",
"func": "msg.config = {\n bing_key: \"xxxxxxxxxxx\",\n topic_prefix: \"node-red.0.traffic.\"\n};\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 344.11106872558594,
"y": 144.5556182861328,
"wires": [
[
"f56d10d3.1a8c6",
"bbfabcd1.22792"
]
]
},
{
"id": "ed4644fd.7f8808",
"type": "function",
"z": "f38be5c5.594708",
"name": "loopRoutes",
"func": "context.routes = context.routes || msg.routes;\n\n// iterate through all the routes\nif (context.routes.length > 0) {\n msg.payload = context.routes.shift();\n return [null, msg];\n}\n\n// Reset routes-context, otherwise we immediately stop in the next run\ncontext.routes = false;\n// we are done with the loop\nreturn [msg, null];",
"outputs": "2",
"noerr": 0,
"x": 208.6666259765625,
"y": 338.22222900390625,
"wires": [
[],
[
"47798bfa.dc51e4"
]
]
},
{
"id": "bbfabcd1.22792",
"type": "debug",
"z": "f38be5c5.594708",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"complete": "true",
"x": 728.6666259765625,
"y": 264.22222900390625,
"wires": []
},
{
"id": "51df14db.22edcc",
"type": "ioBroker out",
"z": "f38be5c5.594708",
"name": "toIoBroker",
"topic": "",
"ack": "true",
"autoCreate": "true",
"x": 663.6666259765625,
"y": 599.2222290039062,
"wires": []
},
{
"id": "ae4bbdde.5e5c6",
"type": "catch",
"z": "f38be5c5.594708",
"name": "",
"scope": null,
"x": 740,
"y": 140,
"wires": [
[
"bbfabcd1.22792"
]
]
},
{
"id": "c6a64342.37eab",
"type": "comment",
"z": "f38be5c5.594708",
"name": "Define your config and routes here. ",
"info": "Define your config and routes here.",
"x": 422.6666259765625,
"y": 107.2222900390625,
"wires": []
},
{
"id": "4bb3a641.55bbd8",
"type": "debug",
"z": "f38be5c5.594708",
"name": "WEB",
"active": false,
"tosidebar": true,
"console": false,
"complete": "url",
"x": 711,
"y": 312.00006103515625,
"wires": []
},
{
"id": "65099a04.fa68e4",
"type": "debug",
"z": "f38be5c5.594708",
"name": "Answer",
"active": false,
"tosidebar": true,
"console": false,
"complete": "true",
"x": 721,
"y": 366.00006103515625,
"wires": []
},
{
"id": "dc7542ef.84429",
"type": "delay",
"z": "f38be5c5.594708",
"name": "Pause",
"pauseType": "delay",
"timeout": "3",
"timeoutUnits": "seconds",
"rate": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"x": 370,
"y": 540,
"wires": [
[
"ed4644fd.7f8808"
]
]
},
{
"id": "bf572708.026bb8",
"type": "switch",
"z": "f38be5c5.594708",
"name": "Error?",
"property": "payload.statusCode",
"rules": [
{
"t": "neq",
"v": "200"
},
{
"t": "else"
}
],
"checkall": "true",
"outputs": 2,
"x": 181,
"y": 570.0000610351562,
"wires": [
[
"dc7542ef.84429"
],
[
"59391f0e.ac165"
]
]
},
{
"id": "671cf5a3.93243c",
"type": "debug",
"z": "f38be5c5.594708",
"name": "StatusCode",
"active": false,
"tosidebar": true,
"console": false,
"complete": "payload.statusCode",
"x": 700,
"y": 410.00006103515625,
"wires": []
},
{
"id": "4ca548a6.1616f8",
"type": "catch",
"z": "f38be5c5.594708",
"name": "",
"scope": null,
"x": 160,
"y": 784.0000610351562,
"wires": [
[
"59391f0e.ac165"
]
]
},
{
"id": "e2c4beb7.0393",
"type": "comment",
"z": "f38be5c5.594708",
"name": "defineRoutes Backup",
"info": "/*\nDefine routes:\nfrom: starting address, encode spaces with %20\nto: destination address, encode spaces with %20\nthreshold: in minutes, if duration with traffic is larger than threshold, set trafficJam to true\nname: name :-)\n*/\n\nmsg.routes = [\n {\n way: \".1='xxxxxxxx'&\" +\n \".2='xxxxxxx'\",\n threshold: 5,\n name: 'Home2Office'\n },\n {\n way: \".1='Horsterfeld 1,31542 Bad Nenndorf'&\" +\n \"via.2='Mindener Straße 4, 30890 Nordgoltern'&\" +\n \"via.3='Landwehrkreisel / Frankfurter Allee, 30459 Hannover'&\" +\n \".4='Laatzener Str. 1,30539 Hannover'\",\n threshold: 5,\n name: 'Home2OfficeLandstr'\n },\n {\n way: \".1='Horsterfeld 1,31542 Bad Nenndorf'&\" +\n \"via.2='Autobahnkreuz Hannover-Buchholz, 30659 Hannover'&\" +\n \".3='Laatzener Str. 1,30539 Hannover'\",\n threshold: 5,\n name: 'Home2OfficeA2'\n },\n {\n way: \".1='Laatzener Str. 1,30539 Hannover'&\" +\n \"via.2='Landwehrkreisel / Frankfurter Allee, 30459 Hannover'&\" +\n \"via.3='Mindener Straße 4, 30890 Nordgoltern'&\" +\n \".4='Horsterfeld 1,31542 Bad Nenndorf'\",\n threshold: 5,\n name: 'Office2HomeLandstr'\n },\n {\n way: \".1='Laatzener Str. 1,30539 Hannover'&\" +\n \".2='Horsterfeld 1,31542 Bad Nenndorf'\",\n threshold: 5,\n name: 'Office2Home'\n },\n {\n way: \".1='Laatzener Str. 1,30539 Hannover'&\" +\n \"via.2='A37, 30655 Gross Buchholz'&\" +\n \".3='Horsterfeld 1,31542 Bad Nenndorf'\",\n threshold: 5,\n name: 'Office2HomeA2'\n }\n];\n\nreturn msg;",
"x": 460,
"y": 60,
"wires": []
},
{
"id": "59391f0e.ac165",
"type": "function",
"z": "f38be5c5.594708",
"name": "extractData",
"func": "/*\n\nreturns:\n\n1\\. travelDurationTraffic in min\n\n2\\. travelDuration in min\n\n3\\. traveDelay in min\n\n4\\. trafficJam boolean\n\n5\\. trafficCongestion string\n\n6\\. travelDistance in km\n\n7\\. msg object (for debugging and return to the loop)\n\n*/\n\nvar travelDurationTraffic = Math.ceil(msg.payload.resourceSets[0].resources[0].travelDurationTraffic / 60);\n\nvar travelDuration = Math.ceil(msg.payload.resourceSets[0].resources[0].travelDuration / 60);\n\n \n\nvar msgTraffic = [\n\n {\n\n topic: msg.topic_prefix + \".name\",\n\n payload: msg.name\n\n },\n\n {\n\n topic: msg.topic_prefix + \".travelDurationTraffic\",\n\n payload: travelDurationTraffic\n\n },\n\n { \n\n topic: msg.topic_prefix + \".travelDuration\",\n\n payload: travelDuration\n\n },\n\n { \n\n topic: msg.topic_prefix + \".travelDelay\",\n\n payload: travelDurationTraffic - travelDuration\n\n },\n\n { \n\n topic: msg.topic_prefix + \".trafficJam\",\n\n payload: (travelDurationTraffic - travelDuration >= msg.threshold)\n\n },\n\n { \n\n topic: msg.topic_prefix + \".trafficCongestion\",\n\n payload: msg.payload.resourceSets[0].resources[0].trafficCongestion\n\n },\n\n { \n\n topic: msg.topic_prefix + \".travelDistance\",\n\n payload: msg.payload.resourceSets[0].resources[0].travelDistance\n\n },\n \n { \n\n topic: msg.topic_prefix + \".travelDescription\",\n\n payload: msg.payload.resourceSets[0].resources[0].routeLegs[0].description\n\n }\n\n];\n\nreturn [msgTraffic,msg];\n\n ",
"outputs": 2,
"noerr": 0,
"x": 330,
"y": 680,
"wires": [
[
"51df14db.22edcc"
],
[
"dc7542ef.84429"
]
]
}
]