From 969c3eb57fcebfdcef13de54218d7f08e7e8e8e8 Mon Sep 17 00:00:00 2001 From: Romain Bazile Date: Tue, 12 Jan 2021 14:58:36 +0100 Subject: [PATCH] UI: fix location parsing --- flows/main.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flows/main.json b/flows/main.json index 5357df6..120e253 100644 --- a/flows/main.json +++ b/flows/main.json @@ -7430,7 +7430,7 @@ "type": "function", "z": "b771c342.49603", "name": "Validate Form", - "func": "function ConvertDDMMToDD(input) {\n // Input Format 36°57.4439' N, 110°4.2100' W\n // From https://stackoverflow.com/questions/1140189/converting-latitude-and-longitude-to-decimal-values\n var parts = input.split(/[^\\d\\w]+/)\n var dd = Number(parts[0]) + (Number(parts[1]) + Number(parts[2])/10000)/60\n return dd.toFixed(6) + parts[3]\n}\n\nfunction ValidateCoordinates(input, lat){\n // Input Format 36.574439°N, 110.42100°W\n // Or 36°57.4439'N, 110°4.2100'W\n if (input.match(\"'\")){\n input = ConvertDDMMToDD(input)\n }\n var direction = input.match(/[NSEW]/)\n var position = input.match(/[\\+\\-\\d\\.]+/)\n var error = {}\n \n if (direction === null){\n error.topic = \"Error with the \"\n error.payload = \"You need to explicitely enter N/S/E/W\"\n return [null, error]\n }\n \n // Test that position is only made of digits!\n if(/^[\\+\\-]/.test(position)){\n error.topic = \"Error with the \"\n error.payload = \"Use of +/- sign is inconsistent with N/S/E/W letter! Please only use N/S/E/W!\"\n return [null, error]\n }\n \n var dd = Number(position)\n if (lat){\n // Check latitude\n if (direction == \"S\" || direction == \"N\") {\n if (dd>90.0){\n error.topic = \"Error with the \"\n error.payload = \"Latitude is more than 90°\"\n return [null, error]\n }\n }\n if (direction == \"W\" || direction == \"E\") {\n error.topic = \"Error with the \"\n error.payload = \"This is not a Latitude!\"\n return [null, error]\n }\n }\n else{\n // Check longitude\n if (direction == \"W\" || direction == \"E\") {\n if (dd>180.0){\n error.topic = \"Error with the \"\n error.payload = \"Longitude is more than 180°\"\n return [null, error]\n }\n }\n if (direction == \"N\" || direction == \"S\") {\n error.topic = \"Error with the \"\n error.payload = \"This is not a Longitude!\"\n return [null, error]\n }\n }\n \n if (direction == \"S\" || direction == \"W\") {\n dd = dd * -1\n } // Don't do anything for N or E\n return [dd.toFixed(4), null]\n \n}\n\nfunction ValidateDate(input){\n // Input Format 2020-12-25\n var error = {};\n \n if (! /20\\d{2}-[0-1]\\d-[0-3]\\d/.test(input)){\n error.topic = \"Error with the date\";\n error.payload = \"The date should respect the ISO format YYYY-MM-DD\";\n return [null, error];\n }\n else {\n var date = input.match(/\\d+/g);\n if (!((2000 < date[0]) && (date[0] < 2100))){\n error.topic = \"Error with the date\"\n error.payload = \"The year should be between 2000 and 2100\"\n return [null, error]\n }\n else if (!((0 < date[1]) && (date[1] <= 12))){\n error.topic = \"Error with the date\"\n error.payload = \"The month should be between 01 and 12\"\n return [null, error]\n }\n else if (!((0 < date[2]) && (date[2] <= 31))){\n error.topic = \"Error with the date\"\n error.payload = \"The day should be between 01 and 31\"\n return [null, error]\n }\n }\n return [input.replace(/-/g, ''), null]\n}\n\nfunction ValidateTime(input){\n // Input Format 12:00 or 12:00:00\n var error = {}\n \n if (! /[0-2]?\\d:[0-5]\\d(:[0-5]\\d)?/.test(input)){\n error.topic = \"Error with the time\"\n error.payload = \"The date should respect the ISO format HH:MM(:SS) (:SS is optional)\"\n return [null, error]\n }\n else {\n var time = input.match(/\\d+/g)\n \n if (!((0 <= time[0]) && (time[0] < 24))){\n error.topic = \"Error with the time\"\n error.payload = \"The hours should be 0 and 23.\"\n return [null, error]\n }\n else if (!((0 <= time[1]) && (time[1] < 60))){\n error.topic = \"Error with the time\"\n error.payload = \"The minutes should be between 0 and 59\"\n return [null, error]\n }\n \n if (time.length > 2){\n if (!((0 <= time[2]) && (time[2] < 60))){\n error.topic = \"Error with the time\"\n error.payload = \"The seconds should be between 01 and 31\"\n return [null, error]\n }\n }\n else {\n input = input + \":00\"\n }\n }\n return [input.replace(/:/g, ''), null]\n}\n\n\nvar ret\nvar payload_for_form = {payload:{}}\n\nif (msg.topic == \"net_retrieval_location\"){\n ret = ValidateCoordinates(msg.payload.object_lat_end, true);\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" Latitude\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_lat_end\", ret[0])\n ret = ValidateCoordinates(msg.payload.object_lon_end, false)\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" Longitude\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_lon_end\", ret[0]);\n \n ret = ValidateDate(msg.payload.object_date_end)\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" of the sample retrieval\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_date_end\", ret[0])\n \n ret = ValidateTime(msg.payload.object_time_end)\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" of the sample retrieval\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_time_end\", ret[0])\n}\nelse{\n ret = ValidateCoordinates(msg.payload.object_lat, true);\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" Latitude\";\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_lat\", ret[0]);\n payload_for_form.payload[\"object_lat_end\"] = msg.payload.object_lat;\n \n ret = ValidateCoordinates(msg.payload.object_lon, false);\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" Longitude\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_lon\", ret[0])\n payload_for_form.payload[\"object_lon_end\"] = msg.payload.object_lon;\n\n ret = ValidateDate(msg.payload.object_date)\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" of the sample\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_date\", ret[0])\n payload_for_form.payload[\"object_date_end\"] = msg.payload.object_date;\n \n ret = ValidateTime(msg.payload.object_time)\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" of the sample\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_time\", ret[0])\n payload_for_form.payload[\"object_time_end\"] = msg.payload.object_time;\n}\nreturn [{topic: \"Coordinates valid!\", payload: \"All good!\"}, payload_for_form]\n", + "func": "function ConvertDDMMToDD(input) {\n // Input Format 36°57.4439' N, 110°4.2100' W\n // From https://stackoverflow.com/questions/1140189/converting-latitude-and-longitude-to-decimal-values\n if (!input.match(/\\d\\d°\\d\\d\\.*\\d*\\'\\s*[NSEW]/)){\n \treturn \"parsing error\"\n }\n var parts = input.split(/[^\\d.\\w]+/)\n if (parts.length != 3){\n \treturn \"parsing error\"\n }\n var dd = Number(parts[0]) + Number(parts[1])/60\n return dd.toFixed(6) + parts[2]\n}\n\nfunction ValidateCoordinates(input, lat){\n // Input Format 36.574439°N, 110.42100°W\n // Or 36°57.4439'N, 110°4.2100'W\n if (input.match(\"'\")){\n input = ConvertDDMMToDD(input)\n }\n var direction = input.match(/[NSEW]/)\n var position = input.match(/[\\+\\-\\d\\.]+/)\n var error = {}\n\n if (input.startsWith(\"parsing error\")){\n error.topic = \"Error with the \"\n error.payload = \"You need to respect the format example, 36.574439°N or 36°57.4439'N\"\n return [null, error]\n }\n \n if (direction === null){\n error.topic = \"Error with the \"\n error.payload = \"You need to explicitely enter N/S/E/W\"\n return [null, error]\n }\n \n // Test that position is only made of digits!\n if(/^[\\+\\-]/.test(position)){\n error.topic = \"Error with the \"\n error.payload = \"Use of +/- sign is inconsistent with N/S/E/W letter! Please only use N/S/E/W!\"\n return [null, error]\n }\n \n var dd = Number(position)\n if (lat){\n // Check latitude\n if (direction == \"S\" || direction == \"N\") {\n if (dd>90.0){\n error.topic = \"Error with the \"\n error.payload = \"Latitude is more than 90°\"\n return [null, error]\n }\n }\n if (direction == \"W\" || direction == \"E\") {\n error.topic = \"Error with the \"\n error.payload = \"This is not a Latitude!\"\n return [null, error]\n }\n }\n else{\n // Check longitude\n if (direction == \"W\" || direction == \"E\") {\n if (dd>180.0){\n error.topic = \"Error with the \"\n error.payload = \"Longitude is more than 180°\"\n return [null, error]\n }\n }\n if (direction == \"N\" || direction == \"S\") {\n error.topic = \"Error with the \"\n error.payload = \"This is not a Longitude!\"\n return [null, error]\n }\n }\n \n if (direction == \"S\" || direction == \"W\") {\n dd = dd * -1\n } // Don't do anything for N or E\n return [dd.toFixed(4), null]\n \n}\n\nfunction ValidateDate(input){\n // Input Format 2020-12-25\n var error = {};\n \n if (! /20\\d{2}-[0-1]\\d-[0-3]\\d/.test(input)){\n error.topic = \"Error with the date\";\n error.payload = \"The date should respect the ISO format YYYY-MM-DD\";\n return [null, error];\n }\n else {\n var date = input.match(/\\d+/g);\n if (!((2000 < date[0]) && (date[0] < 2100))){\n error.topic = \"Error with the date\"\n error.payload = \"The year should be between 2000 and 2100\"\n return [null, error]\n }\n else if (!((0 < date[1]) && (date[1] <= 12))){\n error.topic = \"Error with the date\"\n error.payload = \"The month should be between 01 and 12\"\n return [null, error]\n }\n else if (!((0 < date[2]) && (date[2] <= 31))){\n error.topic = \"Error with the date\"\n error.payload = \"The day should be between 01 and 31\"\n return [null, error]\n }\n }\n return [input.replace(/-/g, ''), null]\n}\n\nfunction ValidateTime(input){\n // Input Format 12:00 or 12:00:00\n var error = {}\n \n if (! /[0-2]?\\d:[0-5]\\d(:[0-5]\\d)?/.test(input)){\n error.topic = \"Error with the time\"\n error.payload = \"The date should respect the ISO format HH:MM(:SS) (:SS is optional)\"\n return [null, error]\n }\n else {\n var time = input.match(/\\d+/g)\n \n if (!((0 <= time[0]) && (time[0] < 24))){\n error.topic = \"Error with the time\"\n error.payload = \"The hours should be 0 and 23.\"\n return [null, error]\n }\n else if (!((0 <= time[1]) && (time[1] < 60))){\n error.topic = \"Error with the time\"\n error.payload = \"The minutes should be between 0 and 59\"\n return [null, error]\n }\n \n if (time.length > 2){\n if (!((0 <= time[2]) && (time[2] < 60))){\n error.topic = \"Error with the time\"\n error.payload = \"The seconds should be between 01 and 31\"\n return [null, error]\n }\n }\n else {\n input = input + \":00\"\n }\n }\n return [input.replace(/:/g, ''), null]\n}\n\n\nvar ret\nvar payload_for_form = {payload:{}}\n\nif (msg.topic == \"net_retrieval_location\"){\n ret = ValidateCoordinates(msg.payload.object_lat_end, true);\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" Latitude\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_lat_end\", ret[0])\n ret = ValidateCoordinates(msg.payload.object_lon_end, false)\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" Longitude\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_lon_end\", ret[0]);\n \n ret = ValidateDate(msg.payload.object_date_end)\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" of the sample retrieval\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_date_end\", ret[0])\n \n ret = ValidateTime(msg.payload.object_time_end)\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" of the sample retrieval\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_time_end\", ret[0])\n}\nelse{\n ret = ValidateCoordinates(msg.payload.object_lat, true);\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" Latitude\";\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_lat\", ret[0]);\n payload_for_form.payload[\"object_lat_end\"] = msg.payload.object_lat;\n \n ret = ValidateCoordinates(msg.payload.object_lon, false);\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" Longitude\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_lon\", ret[0])\n payload_for_form.payload[\"object_lon_end\"] = msg.payload.object_lon;\n\n ret = ValidateDate(msg.payload.object_date)\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" of the sample\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_date\", ret[0])\n payload_for_form.payload[\"object_date_end\"] = msg.payload.object_date;\n \n ret = ValidateTime(msg.payload.object_time)\n if (ret[1] !== null){\n msg.topic = ret[1].topic + \" of the sample\"\n msg.payload = ret[1].payload\n return [msg, null]\n }\n global.set(\"object_time\", ret[0])\n payload_for_form.payload[\"object_time_end\"] = msg.payload.object_time;\n}\nreturn [{topic: \"Coordinates valid!\", payload: \"All good!\"}, payload_for_form]\n", "outputs": 2, "noerr": 0, "initialize": "",