Enable/Disable wlan with scheduler

Very strange that there is no single reply.

After reading this post about Functions in CMD Scripts I adapted my scripts and it is now working perfectly as I wanted it.

I wanted to have the following feature: the existing wlan MyWLAN should be enabled at the following times:
mon, tue, wed, thu 06:00-08:00 16:30-23:00
fri 06:00-08:00 13:00-23:59
sat 08:00-23:59
sun 08:00-23:00

I wanted to realize this with schedulers. So I created the following schedulers:
EnableMyWLAN-06-00
EnableMyWLAN-08-00
EnableMyWLAN-13-00
EnableMyWLAN-16-30
DisableMyWLAN-08-00
DisableMyWLAN-23-00
DisableMyWLAN-23-59

Each scheduler passes the name of the WLAN, the days when the WLAN should be enabled/disabled and the caps-man interfaces to enable/disable to a global function, e.g. the scheduler-entry for EnableMyWLAN-06-00 looks like:

/system script run "Func-EnableWlan"
:global EnableWlan
:local runFunc [:parse (":local input \"MyWLAN,{mon,tue,wed,thu,fri},{cap01-2.4-MyWLAN,cap01-5-MyWLAN}\"; " . $EnableWlan)]
$runFunc

The scheduler uses the following 3 scripts, which must be created manually:

Func-EnableWlan

# EnableWlan: 	Checks if today is in given array of days where wlan should be enabled
# 				and enables given caps-man interface(s) if so
# Input array:
#	0 = string name of wlan (will just be logged) to enable
#	1 = string comma-concatenated days of week when wlan should be enabled, e.g. "mon,tue,wed,thu,fri"
#	2 = string comma-concatenated names of caps-man interfaces to enable
# Output:
#	nothing
:global EnableWlan ":set input [:toarray \$input]
	:if ([:len \$input] = 3) do={
		:local wlanName [:tostr [:pick \$input 0]]
		:local dayStr [:tostr [:pick \$input 1]]
		:local capStr [:tostr [:pick \$input 2]]

		/system script run \"Func-GetDayOfWeek\"
		:global GetDayOfWeek

		:global dayOfWeek \"\"
		:local runFunc [:parse (\":global dayOfWeek;\" . \
			\$GetDayOfWeek . \
			\":set dayOfWeek \\\$output\")
		]
		\$runFunc
		
		:log info \"checking if wlan \$wlanName must be enabled on \$[:pick [/system clock get time] 0 5]/\$dayOfWeek\"
		:local days [:toarray \$dayStr]
		:local match [:find \$days \$dayOfWeek]
		:if ([:type \$match]!=\"nil\") do={
			:log info \"wlan \$wlanName must be enabled\"
			:foreach cap in=[:toarray \$capStr] do={
				:log info \"now enabling interface \$cap\"
				/caps-man interface enable \$cap;
			};
		} else={ :log info \"no need to enable wlan \$wlanName now\" }
	};"

Func-DisableWlan

# DisableWlan: 	Checks if today is in given array of days where wlan should be disabled
# 				and disables given caps-man interface(s) if so
# Input array:
#	0 = string name of wlan (will just be logged) to disable
#	1 = string comma-concatenated days of week when wlan should be disabled, e.g. "mon,tue,wed,thu,fri"
#	2 = string comma-concatenated names of caps-man interfaces to disable
# Output:
#	nothing
:global DisableWlan ":set input [:toarray \$input]
	:if ([:len \$input] = 3) do={
		:local wlanName [:tostr [:pick \$input 0]]
		:local dayStr [:tostr [:pick \$input 1]]
		:local capStr [:tostr [:pick \$input 2]]

		/system script run \"Func-GetDayOfWeek\"
		:global GetDayOfWeek

		:global dayOfWeek \"\"
		:local runFunc [:parse (\":global dayOfWeek;\" . \
			\$GetDayOfWeek . \
			\":set dayOfWeek \\\$output\")
		]
		\$runFunc
		
		:log info \"checking if wlan \$wlanName must be disabled on \$[:pick [/system clock get time] 0 5]/\$dayOfWeek\"
		:local days [:toarray \$dayStr]
		:local match [:find \$days \$dayOfWeek]
		:if ([:type \$match]!=\"nil\") do={
			:log info \"wlan \$wlanName must be disabled\"
			:foreach cap in=[:toarray \$capStr] do={
				:log info \"now disabling interface \$cap\"
				/caps-man interface disable \$cap;
			};
		} else={ :log info \"no need to disable wlan \$wlanName now\" }
	};"

Func-GetDayOfWeek

# GetDayOfWeek: Estimates day of week of today
# Input:
#	nothing
# Output: 
#	string day of week ("sun", "mon", "tue", "wed", "thu", "fri", "sat")
# original DayOfWeek-script by melboyscout (http://forum.mikrotik.com/t/calculate-day-of-week-from-date/53946/1)
:global GetDayOfWeek ":local output \"\"
	:local date [/system clock get date]

	:local months [:toarray \"jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec\"]
	:local daytbl [:toarray \"sun,mon,tue,wed,thu,fri,sat\"]

	:local month [:pick \$date 0 3]
	:local day [:pick \$date 4 6]
	:local dayc [:pick \$date 5 6]
	:local year [:pick \$date 7 11]

	:if ([:pick \$date 4 5] = 0) do={ :set day (\$dayc)}

	:local sum 0
	:local aaa 0
	:local yyy 0
	:local mmm 0
	:local nmonth 1

	:for mindex from=0 to=[:len \$months] do={
		:if ([:pick \$months \$mindex] = \$month) do={:set nmonth (\$mindex + 1) }
	}

	:set aaa ((14 - \$nmonth) / 12)
	:set yyy (\$year - \$aaa)
	:set mmm (\$nmonth + 12 * \$aaa - 2)
	:set sum (7000 + \$day + \$yyy + (\$yyy / 4) - (\$yyy / 100) + (\$yyy / 400) + ((31 * \$mmm) / 12))
	:set sum (\$sum - ((\$sum / 7) * 7))
	:set output [:pick \$daytbl \$sum];"

Maybe someone else is interested. For me it is working quite well. The script code is reduced to a mimimum to avoid duplicate/redundand code. Func-EnableWlan/Func-DisableWlan could be merged to a single function but I didn’t like all those ifs to decide between enable/disable.

Best regards,
cyb