Help to define a correct array

#1

Hi. I need help to organise an array in a function node.

  • When datas are valid (flag = A) ... running fine
  • When datas are invalid datas (flag = else) ... wrong split. The additional array wifiAccessPoints can't be cut like it should be, as: ssid, macAddress, signalStrength

Below, only 4 wifiAccessPoints, but it can be really more...

[{"id":"a3659948.e3a2d8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"808e6dcf.2024e","type":"inject","z":"a3659948.e3a2d8","name":"Valid IWAP01","topic":"","payload":"IWAP01190215A2232.9806N11404.9355E000.1061830323.8706000908000102,460,0,9520,3671#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":116,"y":114,"wires":[["46808aec.699a04"]]},{"id":"b3d6f3f0.126ee","type":"inject","z":"a3659948.e3a2d8","name":"Invalid IWAP01","topic":"","payload":"IWAP01190215V0000.0000N00000.0000E000.1061830323.8706000908000102,460,0,9520,3671,Home|74-DE-2B-44-88-8C|97&Home1|74-DE-2B-44-88-8C|97&Home2|74-DE-2B-44-88-8C|97&Home3|74-DE-2B-44-88-8C|97#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":126.5,"y":196.00000190734863,"wires":[["46808aec.699a04"]]},{"id":"46808aec.699a04","type":"function","z":"a3659948.e3a2d8","name":"AP01 split flag A/V TOTAL","func":"// flag = A\n//IWAP01080524A2232.9806N11404.9355E000.1061830323.8706000908000102,460,0,9520,3671#\n// flag = else\n//IWAP01080524V0000.0000N00000.0000E000.1061830323.8706000908000102,460,0,9520,3671,Home|74-DE-2B-44-88-8C|97&Home1|74-DE-2B-44-88-8C|97&Home2|74-DE-2B-44-88-8C|97&Home3|74-DE-2B-44-88-8C|97# \nlet answer = {}\nlet elements = msg.payload.replace('#','').split(\",\")                                                           // this gives an array containing the bits\n// elements[0] is the long string starting IWAP01\nanswer.command = elements[0].substr(0,6)\nlet dateStr = elements[0].substr(6,6)                                                                           // eg \"190215\" 15th Feb 2019\nanswer.date = new Date(Number(dateStr.substr(0,2))+2000, Number(dateStr.substr(2,2))-1, dateStr.substr(4,2))    // 080524 - date\nanswer.valid = (elements[0].substr(12,1) === \"A\")                                                               // true or false - A or V\nif (answer.valid) {\n    let degreesN = Number(elements[0].substr(13,2))\n    let minutesN = Number(elements[0].substr(15,7))\n    let ns = elements[0].substr(22,1)                                                                           // N or S\n    let degreesE = Number(elements[0].substr(23,3))\n    let minutesE = Number(elements[0].substr(26,7))\n    let ew = elements[0].substr(33,1)                                                                           // E or W\n    node.warn(`${degreesN}  ${minutesN}  ${ns}  ${degreesE}  ${minutesE} ${ew}`)\n    answer.latitude = degreesN + minutesN/60                                                                    // 2232.9806N\n    if (ns === \"S\") answer.latitude = -answer.latitude\n    answer.longitude = degreesE + minutesE/60\n    if (ew === \"W\") answer.longitude = -answer.longitude                                                        // 11404.9355E\n} else {\n    answer.latitude = 0\n    answer.longitude = 0\n}\n\nanswer.speedMoving = elements[0].substr(34,5)                                                                   // 000.1\nanswer.angleDirection = elements[0].substr(45,6)                                                                // 323.87\nanswer.GSMsignal = elements[0].substr(51,3)                                                                     // 060\nanswer.satellites = elements[0].substr(54,3)                                                                    // 009\nanswer.batteryLevel = elements[0].substr(57,3)                                                                  // 080\n// LBS datas\nanswer.mobileCountryCode = elements[1]                                                                          // 460\nanswer.mobileNetworkCode = elements[2]                                                                          // 0\nanswer.locationAreaCode = elements[3]                                                                           // 9520       \nanswer.cellId = elements[4]                                                                                     // 3671\n// WIFI bases only with flag = else\n//\n// answer.wifiAccessPoints = elements[5]\n//\nanswer.wifiAccessPoints = []\nif (answer.valid === \"else\") {\n    i = elements[4]+1                 // move on to wifi bases\n    let wifiAccessPointsStrings = elements[5].split(\"&\")\n    let j;\n    for (j=0; j<wifiAccessPointsStrings.length; j++) {\n        let baseElements = wifiAccessPointsStrings[j].split(\"|\")\n        answer.wifiAccessPoints.push({ssid: baseElements[0], macAddress: baseElements[1], signalStrength: baseElements[2]})  // Home|74-DE-2B-44-88-8C|97&Home1|74-DE-2B-44-88-8C|97&Home2|74-DE-2B-44-88-8C|97&Home3|74-DE-2B-44-88-8C|97\n    }\n}\n\nmsg.payload = answer\nreturn msg\n\n\n\n","outputs":1,"noerr":0,"x":382.50000762939453,"y":144.00000190734863,"wires":[["8e6852f6.ee3a4"]]},{"id":"8e6852f6.ee3a4","type":"debug","z":"a3659948.e3a2d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":629.5000076293945,"y":144.00000381469727,"wires":[]}]

Someone who can help? Thanks...

#2

Solved! Thanks to all...

#3

it would be good if you could post your solution so that if someone else searches and find this thread it could be useful to them

#4

Right ...
I just did a mistake writing my array and defining my variable "i".

The right solution is :

[{"id":"9b0ef6dd.ce4ec8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"4e5ac99.5724138","type":"inject","z":"9b0ef6dd.ce4ec8","name":"Valid IWAP01","topic":"","payload":"IWAP01190215A2232.9806N11404.9355E000.1061830323.8706000908000102,460,0,9520,3671#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":102,"y":130,"wires":[["15f96759.7be979"]]},{"id":"bd546645.c19378","type":"inject","z":"9b0ef6dd.ce4ec8","name":"Invalid IWAP01","topic":"","payload":"IWAP01190215V0000.0000N00000.0000E000.1061830323.8706000908000102,460,0,9520,3671,Home|74-DE-2B-44-88-8C|97&Home1|74-DE-2B-44-88-8C|97&Home2|74-DE-2B-44-88-8C|97&Home3|74-DE-2B-44-88-8C|97#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":112.5,"y":212.00000190734863,"wires":[["15f96759.7be979"]]},{"id":"15f96759.7be979","type":"function","z":"9b0ef6dd.ce4ec8","name":"AP01 split flag A/V TOTAL","func":"// flag = A\n//IWAP01080524A2232.9806N11404.9355E000.1061830323.8706000908000102,460,0,9520,3671#\n// flag = else\n//IWAP01080524V0000.0000N00000.0000E000.1061830323.8706000908000102,460,0,9520,3671,Home|74-DE-2B-44-88-8C|97&Home1|74-DE-2B-44-88-8C|97&Home2|74-DE-2B-44-88-8C|97&Home3|74-DE-2B-44-88-8C|97# \nlet answer = {}\nlet elements = msg.payload.replace('#','').split(\",\")                                                           // this gives an array containing the bits\n// elements[0] is the long string starting IWAP01\nanswer.command = elements[0].substr(0,6)\nlet dateStr = elements[0].substr(6,6)                                                                           // eg \"190215\" 15th Feb 2019\nanswer.date = new Date(Number(dateStr.substr(0,2))+2000, Number(dateStr.substr(2,2))-1, dateStr.substr(4,2))    // 080524 - date\nanswer.valid = (elements[0].substr(12,1) === \"A\")                                                               // true or false - A or V\nif (answer.valid) {\n    let degreesN = Number(elements[0].substr(13,2))\n    let minutesN = Number(elements[0].substr(15,7))\n    let ns = elements[0].substr(22,1)                                                                           // N or S\n    let degreesE = Number(elements[0].substr(23,3))\n    let minutesE = Number(elements[0].substr(26,7))\n    let ew = elements[0].substr(33,1)                                                                           // E or W\n    node.warn(`${degreesN}  ${minutesN}  ${ns}  ${degreesE}  ${minutesE} ${ew}`)\n    answer.latitude = degreesN + minutesN/60                                                                    // 2232.9806N\n    if (ns === \"S\") answer.latitude = -answer.latitude\n    answer.longitude = degreesE + minutesE/60\n    if (ew === \"W\") answer.longitude = -answer.longitude                                                        // 11404.9355E\n} else {\n    answer.latitude = 0\n    answer.longitude = 0\n}\n\nanswer.speedMoving = elements[0].substr(34,5)                                                                   // 000.1\nanswer.angleDirection = elements[0].substr(45,6)                                                                // 323.87\nanswer.signalStrength = elements[0].substr(51,3)                                                                // 060\nanswer.satellites = elements[0].substr(54,3)                                                                    // 009\nanswer.batteryLevel = elements[0].substr(57,3)                                                                  // 080\n// LBS datas\nanswer.mobileCountryCode = elements[1]                                                                          // 460\nanswer.mobileNetworkCode = elements[2]                                                                          // 0\nanswer.locationAreaCode = elements[3]                                                                           // 9520       \nanswer.cellId = elements[4]                                                                                     // 3671\n// WIFI bases only with flag = else\n// answer.wifiAccessPoints = elements[5]\nlet i;\nanswer.wifiAccessPoints = []\n    if (!answer.valid) {\n        //i = elements[4] + 1       // ns: i never used          // move on to wifi bases\n        let wifiAccessPointsStrings = elements[5].split(\"&\")\n        let j;\n        for (j = 0; j < wifiAccessPointsStrings.length; j++) {\n            let baseElements = wifiAccessPointsStrings[j].split(\"|\")\n            answer.wifiAccessPoints.push({ ssid: baseElements[0], macAddress: baseElements[1], signalStrength: baseElements[2] })  // Home|74-DE-2B-44-88-8C|97&Home1|74-DE-2B-44-88-8C|97&Home2|74-DE-2B-44-88-8C|97&Home3|74-DE-2B-44-88-8C|97\n    }\n}\nmsg.payload = answer\nreturn msg\n\n\n\n","outputs":1,"noerr":0,"x":380.50000762939453,"y":162.00000190734863,"wires":[["fe2c68ad.044f58"]]},{"id":"fe2c68ad.044f58","type":"debug","z":"9b0ef6dd.ce4ec8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":604.5000076293945,"y":162.0000057220459,"wires":[]}]
1 Like