2023-03-07 10:57:42 +01:00
|
|
|
#!rsc by RouterOS
|
|
|
|
# RouterOS script: mode-button
|
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:31:30 +01:00
|
|
|
# requires device-mode, scheduler
|
2023-11-15 21:31:18 +01:00
|
|
|
#
|
2023-03-07 10:57:42 +01:00
|
|
|
# act on multiple mode and reset button presses
|
2025-01-24 20:46:11 +01:00
|
|
|
# https://rsc.eworm.de/doc/mode-button.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 ];
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:global ModeButton;
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-08 12:45:38 +01:00
|
|
|
:global LogPrint;
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:set ($ModeButton->"count") ($ModeButton->"count" + 1);
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:local Scheduler [ /system/scheduler/find where name="_ModeButtonScheduler" ];
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:if ([ :len $Scheduler ] = 0) do={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint info $ScriptName ("Creating scheduler _ModeButtonScheduler, counting presses...");
|
2025-05-06 09:44:23 +02:00
|
|
|
:global ModeButtonScheduler do={ :onerror Err {
|
2024-12-08 22:22:18 +01:00
|
|
|
:local FuncName $0;
|
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:global ModeButton;
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-08 12:45:38 +01:00
|
|
|
:global LogPrint;
|
2024-03-04 13:48:01 +01:00
|
|
|
:global ModeButtonScheduler;
|
|
|
|
:global ValidateSyntax;
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:local LEDInvert do={
|
|
|
|
:global ModeButtonLED;
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:global IfThenElse;
|
|
|
|
|
2024-11-15 23:07:26 +01:00
|
|
|
:local LED [ /system/leds/find where leds=$ModeButtonLED \
|
|
|
|
!disabled type~"^(on|off)\$" interface=[] ];
|
2024-03-04 13:48:01 +01:00
|
|
|
:if ([ :len $LED ] = 0) do={
|
|
|
|
:return false;
|
|
|
|
}
|
|
|
|
/system/leds/set type=[ $IfThenElse ([ get $LED type ] = "on") "off" "on" ] $LED;
|
2023-03-07 10:57:42 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:local Count ($ModeButton->"count");
|
|
|
|
:local Code ($ModeButton->[ :tostr $Count ]);
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:set ($ModeButton->"count") 0;
|
|
|
|
:set ModeButtonScheduler;
|
|
|
|
/system/scheduler/remove [ find where name="_ModeButtonScheduler" ];
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:if ([ :len $Code ] > 0) do={
|
|
|
|
:if ([ $ValidateSyntax $Code ] = true) do={
|
2024-12-08 22:22:18 +01:00
|
|
|
$LogPrint info $FuncName ("Acting on " . $Count . " mode-button presses: " . $Code);
|
2023-03-07 10:57:42 +01:00
|
|
|
|
2024-03-04 13:48:01 +01:00
|
|
|
:for I from=1 to=$Count do={
|
|
|
|
$LEDInvert;
|
|
|
|
:if ([ /system/routerboard/settings/get silent-boot ] = false) do={
|
|
|
|
:beep length=200ms;
|
|
|
|
}
|
|
|
|
:delay 200ms;
|
|
|
|
$LEDInvert;
|
|
|
|
:delay 200ms;
|
2023-03-07 10:57:42 +01:00
|
|
|
}
|
|
|
|
|
2025-05-08 09:45:59 +02:00
|
|
|
:onerror Err {
|
2024-11-15 23:02:43 +01:00
|
|
|
[ :parse $Code ];
|
2025-05-08 09:45:59 +02:00
|
|
|
} do={
|
2024-12-08 22:22:18 +01:00
|
|
|
$LogPrint warning $FuncName \
|
2025-05-08 09:45:59 +02:00
|
|
|
("The code for " . $Count . " mode-button presses failed with runtime error: " . $Err);
|
2024-11-15 23:02:43 +01:00
|
|
|
}
|
2024-03-04 13:48:01 +01:00
|
|
|
} else={
|
2024-12-08 22:22:18 +01:00
|
|
|
$LogPrint warning $FuncName \
|
|
|
|
("The code for " . $Count . " mode-button presses failed syntax validation!");
|
2024-03-04 13:48:01 +01:00
|
|
|
}
|
2023-03-07 10:57:42 +01:00
|
|
|
} else={
|
2024-12-08 22:22:18 +01:00
|
|
|
$LogPrint info $FuncName ("No action defined for " . $Count . " mode-button presses.");
|
2023-03-07 10:57:42 +01:00
|
|
|
}
|
2025-05-06 09:44:23 +02:00
|
|
|
} do={
|
|
|
|
:global ExitError; $ExitError false $0 $Err;
|
2024-12-09 09:03:41 +01:00
|
|
|
} }
|
2024-03-04 13:48:01 +01:00
|
|
|
/system/scheduler/add name="_ModeButtonScheduler" \
|
|
|
|
on-event=":global ModeButtonScheduler; \$ModeButtonScheduler;" interval=3s;
|
|
|
|
} else={
|
2024-03-08 12:45:38 +01:00
|
|
|
$LogPrint debug $ScriptName ("Updating scheduler _ModeButtonScheduler...");
|
2024-03-04 13:48:01 +01:00
|
|
|
/system/scheduler/set $Scheduler start-time=[ /system/clock/get time ];
|
2023-03-07 10:57:42 +01:00
|
|
|
}
|
2025-05-06 09:44:23 +02:00
|
|
|
} do={
|
|
|
|
:global ExitError; $ExitError $ExitOK [ :jobname ] $Err;
|
2024-12-06 10:31:52 +01:00
|
|
|
}
|