2023-03-07 10:57:42 +01:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: gps-track
|
2025-01-02 00:04:06 +01:00
|
|
|
# Copyright (c) 2018-2025 Christian Hesse <mail@eworm.de>
|
2025-01-24 20:46:11 +01:00
|
|
|
# https://rsc.eworm.de/COPYING.md
|
2023-03-07 10:57:42 +01:00
|
|
|
#
|
2025-02-07 17:39:48 +01:00
|
|
|
# requires RouterOS, version=7.15
|
2025-01-29 10:21:40 +01:00
|
|
|
# requires device-mode, fetch
|
2023-11-15 21:31:18 +01:00
|
|
|
#
|
2023-03-07 10:57:42 +01:00
|
|
|
# track gps data by sending json data to http server
|
2025-01-24 20:46:11 +01:00
|
|
|
# https://rsc.eworm.de/doc/gps-track.md
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-12-06 10:31:52 +01:00
|
|
|
:local ExitOK false;
|
2025-05-06 09:44:23 +02:00
|
|
|
:onerror Err {
|
2025-05-06 14:08:37 +02:00
|
|
|
:global GlobalConfigReady; :global GlobalFunctionsReady;
|
|
|
|
:retry { :if ($GlobalConfigReady != true || $GlobalFunctionsReady != true) \
|
|
|
|
do={ :error ("Global config and/or functions not ready."); }; } delay=500ms max=50;
|
2024-03-06 15:28:55 +01:00
|
|
|
:local ScriptName [ :jobname ];
|
2024-03-04 13:48:01 +01:00
|
|
|
|
|
|
|
:global GpsTrackUrl;
|
|
|
|
:global Identity;
|
|
|
|
|
2024-04-12 11:14:01 +02:00
|
|
|
:global FetchUserAgentStr;
|
2024-03-08 12:45:38 +01:00
|
|
|
:global LogPrint;
|
2024-03-04 13:48:01 +01:00
|
|
|
:global ScriptLock;
|
|
|
|
:global WaitFullyConnected;
|
|
|
|
|
2024-03-05 16:12:36 +01:00
|
|
|
:if ([ $ScriptLock $ScriptName ] = false) do={
|
2024-12-06 10:31:52 +01:00
|
|
|
:set ExitOK true;
|
2024-03-06 15:28:55 +01:00
|
|
|
:error false;
|
2024-03-05 16:12:36 +01:00
|
|
|
}
|
2024-03-04 13:48:01 +01:00
|
|
|
$WaitFullyConnected;
|
|
|
|
|
|
|
|
:local CoordinateFormat [ /system/gps/get coordinate-format ];
|
|
|
|
:local Gps [ /system/gps/monitor once as-value ];
|
|
|
|
|
|
|
|
:if ($Gps->"valid" = true) do={
|
2025-05-08 09:41:57 +02:00
|
|
|
:onerror Err {
|
2024-04-12 11:14:01 +02:00
|
|
|
/tool/fetch check-certificate=yes-without-crl output=none http-method=post \
|
|
|
|
http-header-field=({ [ $FetchUserAgentStr $ScriptName ]; "Content-Type: application/json" }) \
|
2024-04-12 11:10:34 +02:00
|
|
|
http-data=[ :serialize to=json { "identity"=$Identity; \
|
2024-04-12 11:14:01 +02:00
|
|
|
"lat"=($Gps->"latitude"); "lon"=($Gps->"longitude") } ] $GpsTrackUrl as-value;
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint debug $ScriptName ("Sending GPS data in " . $CoordinateFormat . " format: " . \
|
2024-03-04 13:48:01 +01:00
|
|
|
"lat: " . ($Gps->"latitude") . " " . \
|
2024-03-08 12:45:38 +01:00
|
|
|
"lon: " . ($Gps->"longitude"));
|
2025-05-08 09:41:57 +02:00
|
|
|
} do={
|
|
|
|
$LogPrint warning $ScriptName ("Failed sending GPS data: " . $Err);
|
2024-03-04 13:48:01 +01:00
|
|
|
}
|
|
|
|
} else={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint debug $ScriptName ("GPS data not valid.");
|
2023-06-13 09:28:32 +02:00
|
|
|
}
|
2025-05-06 09:44:23 +02:00
|
|
|
} do={
|
|
|
|
:global ExitError; $ExitError $ExitOK [ :jobname ] $Err;
|
2024-12-06 10:31:52 +01:00
|
|
|
}
|