Matter improve read sensors (#20302)

* Matter improve read_sensors scheduling

* Imprtove logging

* add scheduler call

* remove unwanted print

* fix
This commit is contained in:
s-hadinger 2023-12-24 09:31:53 +01:00 committed by GitHub
parent a6a8214ea7
commit 6c0028de4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 5164 additions and 5005 deletions

View File

@ -32,6 +32,17 @@ class Matter_Plugin_Sensor : Matter_Plugin_Device
var tasmota_sensor_matcher # Actual matcher object
var shadow_value # Last known value
#############################################################
# Constructor
#
# device: contains the root device object so the plugin can "call home"
# endpoint: (int) the endpoint number (16 bits)
# arguments: (map) the map for all complementary arguments that are plugin specific
def init(device, endpoint, config)
super(self).init(device, endpoint, config)
device.add_read_sensors_schedule(self.UPDATE_TIME)
end
#############################################################
# parse_configuration
#

View File

@ -72,6 +72,11 @@ class Matter_Device
var root_salt
var root_w0
var root_L
# cron equivalent to call `read_sensors()` regularly and dispatch to all entpoints
var probe_sensor_time # number of milliseconds to wait between each `read_sensors()` or `nil` if none active
var probe_sensor_timestamp # timestamp for `read_sensors()` probe (in millis())
# if timestamp is `0`, this should be scheduled in priority
#############################################################
def init()
@ -132,10 +137,6 @@ class Matter_Device
# autoconfigure other plugins if needed
self.autoconf_device()
# for now read sensors every 30 seconds
# TODO still needed?
tasmota.add_cron("*/30 * * * * *", def () self._trigger_read_sensors() end, "matter_sensors_30s")
self._start_udp(self.UDP_PORT)
self.start_mdns_announce_hostnames()
@ -317,6 +318,8 @@ class Matter_Device
# dispatch every 250ms to all plugins
def every_250ms()
self.message_handler.every_250ms()
# call read_sensors if needed
self.read_sensors_scheduler()
# call all plugins, use a manual loop to avoid creating a new object
var idx = 0
while idx < size(self.plugins)
@ -325,12 +328,36 @@ class Matter_Device
end
end
#############################################################
# add a scheduler for `read_sensors` and update schedule time
# if it's more often than previously
def add_read_sensors_schedule(update_time)
if (self.probe_sensor_time == nil) || (self.probe_sensor_time > update_time)
self.probe_sensor_time = update_time
self.probe_sensor_timestamp = matter.jitter(update_time)
end
end
#############################################################
# check if we need to call `read_sensors()`
def read_sensors_scheduler()
if (self.probe_sensor_time == nil) return end # nothing to schedule
if (self.probe_sensor_timestamp == 0) || (tasmota.time_reached(self.probe_sensor_timestamp))
self._trigger_read_sensors()
# set new next timestamp
self.probe_sensor_timestamp = tasmota.millis(self.probe_sensor_time)
end
end
#############################################################
# trigger a read_sensors and dispatch to plugins
# Internally used by cron
def _trigger_read_sensors()
import json
var rs_json = tasmota.read_sensors()
if tasmota.loglevel(3)
tasmota.log("MTR: read_sensors: "+str(rs_json), 3)
end
if rs_json == nil return end
var rs = json.load(rs_json)
if rs != nil

View File

@ -6,6 +6,230 @@
extern const bclass be_class_Matter_Plugin_Sensor;
/********************************************************************
** Solidified function: append_state_json
********************************************************************/
be_local_closure(Matter_Plugin_Sensor_append_state_json, /* name */
be_nested_proto(
6, /* nstack */
1, /* argc */
2, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
1, /* has constants */
( &(const bvalue[ 4]) { /* constants */
/* K0 */ be_nested_str_weak(shadow_value),
/* K1 */ be_nested_str_weak(null),
/* K2 */ be_nested_str_weak(_X2C_X22_X25s_X22_X3A_X25s),
/* K3 */ be_nested_str_weak(JSON_NAME),
}),
be_str_weak(append_state_json),
&be_const_str_solidified,
( &(const binstruction[13]) { /* code */
0x88040100, // 0000 GETMBR R1 R0 K0
0x4C080000, // 0001 LDNIL R2
0x20040202, // 0002 NE R1 R1 R2
0x78060001, // 0003 JMPF R1 #0006
0x88040100, // 0004 GETMBR R1 R0 K0
0x70020000, // 0005 JMP #0007
0x58040001, // 0006 LDCONST R1 K1
0x60080018, // 0007 GETGBL R2 G24
0x580C0002, // 0008 LDCONST R3 K2
0x88100103, // 0009 GETMBR R4 R0 K3
0x5C140200, // 000A MOVE R5 R1
0x7C080600, // 000B CALL R2 3
0x80040400, // 000C RET 1 R2
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: init
********************************************************************/
be_local_closure(Matter_Plugin_Sensor_init, /* name */
be_nested_proto(
9, /* nstack */
4, /* argc */
2, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
1, /* has constants */
( &(const bvalue[ 3]) { /* constants */
/* K0 */ be_nested_str_weak(init),
/* K1 */ be_nested_str_weak(add_read_sensors_schedule),
/* K2 */ be_nested_str_weak(UPDATE_TIME),
}),
be_str_weak(init),
&be_const_str_solidified,
( &(const binstruction[12]) { /* code */
0x60100003, // 0000 GETGBL R4 G3
0x5C140000, // 0001 MOVE R5 R0
0x7C100200, // 0002 CALL R4 1
0x8C100900, // 0003 GETMET R4 R4 K0
0x5C180200, // 0004 MOVE R6 R1
0x5C1C0400, // 0005 MOVE R7 R2
0x5C200600, // 0006 MOVE R8 R3
0x7C100800, // 0007 CALL R4 4
0x8C100301, // 0008 GETMET R4 R1 K1
0x88180102, // 0009 GETMBR R6 R0 K2
0x7C100400, // 000A CALL R4 2
0x80000000, // 000B RET 0
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: value_changed
********************************************************************/
be_local_closure(Matter_Plugin_Sensor_value_changed, /* name */
be_nested_proto(
1, /* nstack */
1, /* argc */
2, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
0, /* has constants */
NULL, /* no const */
be_str_weak(value_changed),
&be_const_str_solidified,
( &(const binstruction[ 1]) { /* code */
0x80000000, // 0000 RET 0
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: parse_configuration
********************************************************************/
be_local_closure(Matter_Plugin_Sensor_parse_configuration, /* name */
be_nested_proto(
5, /* nstack */
2, /* argc */
2, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
1, /* has constants */
( &(const bvalue[ 7]) { /* constants */
/* K0 */ be_nested_str_weak(tasmota_sensor_filter),
/* K1 */ be_nested_str_weak(find),
/* K2 */ be_nested_str_weak(ARG),
/* K3 */ be_nested_str_weak(tasmota_sensor_matcher),
/* K4 */ be_nested_str_weak(tasmota),
/* K5 */ be_nested_str_weak(Rule_Matcher),
/* K6 */ be_nested_str_weak(parse),
}),
be_str_weak(parse_configuration),
&be_const_str_solidified,
( &(const binstruction[13]) { /* code */
0x8C080301, // 0000 GETMET R2 R1 K1
0x88100102, // 0001 GETMBR R4 R0 K2
0x7C080400, // 0002 CALL R2 2
0x90020002, // 0003 SETMBR R0 K0 R2
0x88080100, // 0004 GETMBR R2 R0 K0
0x780A0005, // 0005 JMPF R2 #000C
0xB80A0800, // 0006 GETNGBL R2 K4
0x88080505, // 0007 GETMBR R2 R2 K5
0x8C080506, // 0008 GETMET R2 R2 K6
0x88100100, // 0009 GETMBR R4 R0 K0
0x7C080400, // 000A CALL R2 2
0x90020602, // 000B SETMBR R0 K3 R2
0x80000000, // 000C RET 0
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: parse_sensors
********************************************************************/
be_local_closure(Matter_Plugin_Sensor_parse_sensors, /* name */
be_nested_proto(
8, /* nstack */
2, /* argc */
2, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
1, /* has constants */
( &(const bvalue[ 6]) { /* constants */
/* K0 */ be_nested_str_weak(VIRTUAL),
/* K1 */ be_nested_str_weak(tasmota_sensor_matcher),
/* K2 */ be_nested_str_weak(pre_value),
/* K3 */ be_nested_str_weak(match),
/* K4 */ be_nested_str_weak(shadow_value),
/* K5 */ be_nested_str_weak(value_changed),
}),
be_str_weak(parse_sensors),
&be_const_str_solidified,
( &(const binstruction[22]) { /* code */
0x88080100, // 0000 GETMBR R2 R0 K0
0x740A0012, // 0001 JMPT R2 #0015
0x88080101, // 0002 GETMBR R2 R0 K1
0x780A0010, // 0003 JMPF R2 #0015
0x8C080102, // 0004 GETMET R2 R0 K2
0x6010000A, // 0005 GETGBL R4 G10
0x88140101, // 0006 GETMBR R5 R0 K1
0x8C140B03, // 0007 GETMET R5 R5 K3
0x5C1C0200, // 0008 MOVE R7 R1
0x7C140400, // 0009 CALL R5 2
0x7C100200, // 000A CALL R4 1
0x7C080400, // 000B CALL R2 2
0x4C0C0000, // 000C LDNIL R3
0x200C0403, // 000D NE R3 R2 R3
0x780E0005, // 000E JMPF R3 #0015
0x880C0104, // 000F GETMBR R3 R0 K4
0x200C0403, // 0010 NE R3 R2 R3
0x780E0002, // 0011 JMPF R3 #0015
0x8C0C0105, // 0012 GETMET R3 R0 K5
0x7C0C0200, // 0013 CALL R3 1
0x90020802, // 0014 SETMBR R0 K4 R2
0x80000000, // 0015 RET 0
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: pre_value
********************************************************************/
be_local_closure(Matter_Plugin_Sensor_pre_value, /* name */
be_nested_proto(
2, /* nstack */
2, /* argc */
2, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
0, /* has constants */
NULL, /* no const */
be_str_weak(pre_value),
&be_const_str_solidified,
( &(const binstruction[ 1]) { /* code */
0x80040200, // 0000 RET 1 R1
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: update_virtual
********************************************************************/
@ -64,191 +288,6 @@ be_local_closure(Matter_Plugin_Sensor_update_virtual, /* name */
/*******************************************************************/
/********************************************************************
** Solidified function: parse_sensors
********************************************************************/
be_local_closure(Matter_Plugin_Sensor_parse_sensors, /* name */
be_nested_proto(
8, /* nstack */
2, /* argc */
2, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
1, /* has constants */
( &(const bvalue[ 6]) { /* constants */
/* K0 */ be_nested_str_weak(VIRTUAL),
/* K1 */ be_nested_str_weak(tasmota_sensor_matcher),
/* K2 */ be_nested_str_weak(pre_value),
/* K3 */ be_nested_str_weak(match),
/* K4 */ be_nested_str_weak(shadow_value),
/* K5 */ be_nested_str_weak(value_changed),
}),
be_str_weak(parse_sensors),
&be_const_str_solidified,
( &(const binstruction[22]) { /* code */
0x88080100, // 0000 GETMBR R2 R0 K0
0x740A0012, // 0001 JMPT R2 #0015
0x88080101, // 0002 GETMBR R2 R0 K1
0x780A0010, // 0003 JMPF R2 #0015
0x8C080102, // 0004 GETMET R2 R0 K2
0x6010000A, // 0005 GETGBL R4 G10
0x88140101, // 0006 GETMBR R5 R0 K1
0x8C140B03, // 0007 GETMET R5 R5 K3
0x5C1C0200, // 0008 MOVE R7 R1
0x7C140400, // 0009 CALL R5 2
0x7C100200, // 000A CALL R4 1
0x7C080400, // 000B CALL R2 2
0x4C0C0000, // 000C LDNIL R3
0x200C0403, // 000D NE R3 R2 R3
0x780E0005, // 000E JMPF R3 #0015
0x880C0104, // 000F GETMBR R3 R0 K4
0x200C0403, // 0010 NE R3 R2 R3
0x780E0002, // 0011 JMPF R3 #0015
0x8C0C0105, // 0012 GETMET R3 R0 K5
0x7C0C0200, // 0013 CALL R3 1
0x90020802, // 0014 SETMBR R0 K4 R2
0x80000000, // 0015 RET 0
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: parse_configuration
********************************************************************/
be_local_closure(Matter_Plugin_Sensor_parse_configuration, /* name */
be_nested_proto(
5, /* nstack */
2, /* argc */
2, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
1, /* has constants */
( &(const bvalue[ 7]) { /* constants */
/* K0 */ be_nested_str_weak(tasmota_sensor_filter),
/* K1 */ be_nested_str_weak(find),
/* K2 */ be_nested_str_weak(ARG),
/* K3 */ be_nested_str_weak(tasmota_sensor_matcher),
/* K4 */ be_nested_str_weak(tasmota),
/* K5 */ be_nested_str_weak(Rule_Matcher),
/* K6 */ be_nested_str_weak(parse),
}),
be_str_weak(parse_configuration),
&be_const_str_solidified,
( &(const binstruction[13]) { /* code */
0x8C080301, // 0000 GETMET R2 R1 K1
0x88100102, // 0001 GETMBR R4 R0 K2
0x7C080400, // 0002 CALL R2 2
0x90020002, // 0003 SETMBR R0 K0 R2
0x88080100, // 0004 GETMBR R2 R0 K0
0x780A0005, // 0005 JMPF R2 #000C
0xB80A0800, // 0006 GETNGBL R2 K4
0x88080505, // 0007 GETMBR R2 R2 K5
0x8C080506, // 0008 GETMET R2 R2 K6
0x88100100, // 0009 GETMBR R4 R0 K0
0x7C080400, // 000A CALL R2 2
0x90020602, // 000B SETMBR R0 K3 R2
0x80000000, // 000C RET 0
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: pre_value
********************************************************************/
be_local_closure(Matter_Plugin_Sensor_pre_value, /* name */
be_nested_proto(
2, /* nstack */
2, /* argc */
2, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
0, /* has constants */
NULL, /* no const */
be_str_weak(pre_value),
&be_const_str_solidified,
( &(const binstruction[ 1]) { /* code */
0x80040200, // 0000 RET 1 R1
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: append_state_json
********************************************************************/
be_local_closure(Matter_Plugin_Sensor_append_state_json, /* name */
be_nested_proto(
6, /* nstack */
1, /* argc */
2, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
1, /* has constants */
( &(const bvalue[ 4]) { /* constants */
/* K0 */ be_nested_str_weak(shadow_value),
/* K1 */ be_nested_str_weak(null),
/* K2 */ be_nested_str_weak(_X2C_X22_X25s_X22_X3A_X25s),
/* K3 */ be_nested_str_weak(JSON_NAME),
}),
be_str_weak(append_state_json),
&be_const_str_solidified,
( &(const binstruction[13]) { /* code */
0x88040100, // 0000 GETMBR R1 R0 K0
0x4C080000, // 0001 LDNIL R2
0x20040202, // 0002 NE R1 R1 R2
0x78060001, // 0003 JMPF R1 #0006
0x88040100, // 0004 GETMBR R1 R0 K0
0x70020000, // 0005 JMP #0007
0x58040001, // 0006 LDCONST R1 K1
0x60080018, // 0007 GETGBL R2 G24
0x580C0002, // 0008 LDCONST R3 K2
0x88100103, // 0009 GETMBR R4 R0 K3
0x5C140200, // 000A MOVE R5 R1
0x7C080600, // 000B CALL R2 3
0x80040400, // 000C RET 1 R2
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: value_changed
********************************************************************/
be_local_closure(Matter_Plugin_Sensor_value_changed, /* name */
be_nested_proto(
1, /* nstack */
1, /* argc */
2, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
0, /* has constants */
NULL, /* no const */
be_str_weak(value_changed),
&be_const_str_solidified,
( &(const binstruction[ 1]) { /* code */
0x80000000, // 0000 RET 0
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified class: Matter_Plugin_Sensor
********************************************************************/
@ -256,21 +295,22 @@ extern const bclass be_class_Matter_Plugin_Device;
be_local_class(Matter_Plugin_Sensor,
3,
&be_class_Matter_Plugin_Device,
be_nested_map(13,
be_nested_map(14,
( (struct bmapnode*) &(const bmapnode[]) {
{ be_const_key_weak(ARG, 1), be_nested_str_weak(filter) },
{ be_const_key_weak(value_changed, -1), be_const_closure(Matter_Plugin_Sensor_value_changed_closure) },
{ be_const_key_weak(UPDATE_TIME, 3), be_const_int(5000) },
{ be_const_key_weak(ARG, -1), be_nested_str_weak(filter) },
{ be_const_key_weak(append_state_json, 0), be_const_closure(Matter_Plugin_Sensor_append_state_json_closure) },
{ be_const_key_weak(update_virtual, -1), be_const_closure(Matter_Plugin_Sensor_update_virtual_closure) },
{ be_const_key_weak(ARG_HINT, 8), be_nested_str_weak(Filter_X20pattern) },
{ be_const_key_weak(tasmota_sensor_matcher, -1), be_const_var(1) },
{ be_const_key_weak(parse_sensors, -1), be_const_closure(Matter_Plugin_Sensor_parse_sensors_closure) },
{ be_const_key_weak(JSON_NAME, -1), be_nested_str_weak() },
{ be_const_key_weak(parse_configuration, -1), be_const_closure(Matter_Plugin_Sensor_parse_configuration_closure) },
{ be_const_key_weak(pre_value, 12), be_const_closure(Matter_Plugin_Sensor_pre_value_closure) },
{ be_const_key_weak(append_state_json, -1), be_const_closure(Matter_Plugin_Sensor_append_state_json_closure) },
{ be_const_key_weak(shadow_value, 9), be_const_var(2) },
{ be_const_key_weak(UPDATE_TIME, -1), be_const_int(5000) },
{ be_const_key_weak(tasmota_sensor_filter, -1), be_const_var(0) },
{ be_const_key_weak(init, 13), be_const_closure(Matter_Plugin_Sensor_init_closure) },
{ be_const_key_weak(tasmota_sensor_matcher, -1), be_const_var(1) },
{ be_const_key_weak(ARG_HINT, 12), be_nested_str_weak(Filter_X20pattern) },
{ be_const_key_weak(parse_sensors, 10), be_const_closure(Matter_Plugin_Sensor_parse_sensors_closure) },
{ be_const_key_weak(JSON_NAME, -1), be_nested_str_weak() },
{ be_const_key_weak(pre_value, -1), be_const_closure(Matter_Plugin_Sensor_pre_value_closure) },
{ be_const_key_weak(shadow_value, 7), be_const_var(2) },
{ be_const_key_weak(parse_configuration, -1), be_const_closure(Matter_Plugin_Sensor_parse_configuration_closure) },
{ be_const_key_weak(value_changed, -1), be_const_closure(Matter_Plugin_Sensor_value_changed_closure) },
})),
be_str_weak(Matter_Plugin_Sensor)
);