Berry 'tasmota.is_network_up()' (#23532)

This commit is contained in:
s-hadinger 2025-06-10 20:56:50 +02:00 committed by GitHub
parent 4002344227
commit 84059199d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 2006 additions and 1973 deletions

View File

@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
- Berry `compile` and `tasmota.compile` option to compile in local context (#23457)
- Support for AP33772S USB PD Sink Controller as used in CentyLab RotoPD
- Berry mqtt publish rule processing
- Berry `tasmota.is_network_up()`
### Breaking Changed

View File

@ -184,6 +184,7 @@ class be_class_tasmota (scope: global, name: Tasmota) {
exec_cmd, closure(class_Tasmota_exec_cmd_closure)
gc, closure(class_Tasmota_gc_closure)
event, closure(class_Tasmota_event_closure)
is_network_up, closure(class_Tasmota_is_network_up_closure)
when_network_up, closure(class_Tasmota_when_network_up_closure)
run_network_up, closure(class_Tasmota_run_network_up_closure)
add_driver, closure(class_Tasmota_add_driver_closure)

View File

@ -724,12 +724,16 @@ class Tasmota
end
end
# returns `true` if the network stack is connected
def is_network_up()
return tasmota.wifi()['up'] || tasmota.eth()['up']
end
# add a closure to the list to be called when network is connected
# or call immediately if network is already up
def when_network_up(cl)
self.check_not_method(cl)
var is_connected = tasmota.wifi()['up'] || tasmota.eth()['up']
if is_connected
if self.is_network_up()
cl() # call closure
else
if (self._wnu == nil)
@ -743,8 +747,7 @@ class Tasmota
# run all pending closures when network is up
def run_network_up()
if (self._wnu == nil) return end
var is_connected = tasmota.wifi()['up'] || tasmota.eth()['up']
if is_connected
if self.is_network_up()
# run all closures in a safe loop
while (size(self._wnu) > 0)
var cl = self._wnu[0]
@ -760,13 +763,12 @@ class Tasmota
end
def event(event_type, cmd, idx, payload, raw)
import introspect
if event_type=='every_50ms'
if (event_type == 'every_50ms')
if (self._wnu) self.run_network_up() end # capture when network becomes connected
self.run_timers()
end #- first run deferred events -#
if event_type=='every_250ms'
if (event_type == 'every_250ms')
self.run_cron()
end
@ -777,11 +779,12 @@ class Tasmota
keep_going = true
end
if event_type=='cmd' return self.exec_cmd(cmd, idx, payload)
elif event_type=='tele' return self.exec_tele(payload)
elif event_type=='rule' return self.exec_rules(payload, bool(idx))
elif event_type=='gc' return self.gc()
if (event_type == 'cmd') return self.exec_cmd(cmd, idx, payload)
elif (event_type == 'tele') return self.exec_tele(payload)
elif (event_type == 'rule') return self.exec_rules(payload, bool(idx))
elif (event_type == 'gc') return self.gc()
elif self._drivers
import introspect
var i = 0
while i < size(self._drivers)
var d = self._drivers[i]
@ -811,6 +814,16 @@ class Tasmota
return done
end
######################################################################
# add_driver
#
# Add an instance to the dispatchin of Berry events
#
# Args:
# - `d`: instance (or driver)
# The events will be dispatched to this instance whenever
# it has a method with the same name of the instance
######################################################################
def add_driver(d)
if type(d) != 'instance'
raise "value_error", "instance required"