Skip to content

Commit 44377c9

Browse files
committed
bump to 0.4.0_20240503
optimize MQTT -- close #5
1 parent 23c5390 commit 44377c9

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

esp32/main.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
bat_events_no = 0 # used to count numbers of battery events
4040
pwr_events_no = 0 # used to count numbers of power events
4141
sys_events_no = 0 # used to count numbers of system events
42-
sw_ver = "PytesSerial_Esp v0.4.0_20240501"
42+
sw_ver = "PytesSerial_Esp v0.4.0_20240503"
4343
version = sw_ver
4444

4545
def serial_write(req, size):
@@ -279,11 +279,13 @@ def json_serialize():
279279
global errors_no
280280
global errors
281281
global json_data
282+
global json_data_old
282283
global bat_events_no
283284
global pwr_events_no
284285
global sys_events_no
285286
global bats
286287
try:
288+
json_data_old = json_data
287289
json_data={'relay_local_time':TimeStamp,
288290
'powers' : powers,
289291
'voltage': sys_voltage,
@@ -487,11 +489,11 @@ def mqtt_discovery():
487489
state_topic = "homeassistant/sensor/" + dev_name + "/" + msg["uniq_id"] + "/config"
488490
msg ["name"] = names[n]+"_"+str(power)
489491
msg ["stat_t"] = "pytes_serial/" + dev_name + "/" + str(power-1) + "/cells/" + ids[n]
490-
if dev_cla[n] != "None":
492+
if dev_cla[n] != None:
491493
msg ["dev_cla"] = dev_cla[n]
492-
if stat_cla[n] != "None":
494+
if stat_cla[n] != None:
493495
msg ["stat_cla"] = stat_cla[n]
494-
if unit_of_meas[n] != "None":
496+
if unit_of_meas[n] != None:
495497
msg ["unit_of_meas"] = unit_of_meas[n]
496498

497499
msg ["val_tpl"] = "{{ value_json.value }}"
@@ -534,7 +536,11 @@ def mqtt_publish():
534536
# We will publish these later
535537
if key in ["devices", "cells_data"]:
536538
continue
537-
539+
540+
# If the value was published before, skip it
541+
if json_data_old and value == json_data_old[key]:
542+
continue
543+
538544
state_topic = "pytes_serial/" + dev_name + "/" + key
539545
if isinstance(value, dict) or isinstance(value, list):
540546
message = json.dumps(value)
@@ -553,6 +559,15 @@ def mqtt_publish():
553559
# Do not publish these
554560
if key in ["power"]:
555561
continue
562+
563+
# If the value was published before, skip it
564+
if (
565+
json_data_old and
566+
len(json_data["devices"]) == powers and
567+
len(json_data_old["devices"]) == powers and
568+
value == json_data_old["devices"][device["power"] - 1][key]
569+
):
570+
continue
556571

557572
state_topic = "pytes_serial/" + dev_name + "/" + device_idx + "/" + key
558573
if isinstance(value, dict) or isinstance(value, list):
@@ -574,6 +589,15 @@ def mqtt_publish():
574589
if key in ["power", "cells"]:
575590
continue
576591

592+
# If the value was published before, skip it
593+
if (
594+
json_data_old and
595+
len(json_data["cells_data"]) == powers and
596+
len(json_data_old["cells_data"]) == powers and
597+
value == json_data_old["cells_data"][device["power"] - 1][key]
598+
):
599+
continue
600+
577601
state_topic = "pytes_serial/" + dev_name + "/" + device_idx + "/cells/" + key
578602
if isinstance(value, dict) or isinstance(value, list):
579603
message = json.dumps(value)
@@ -593,6 +617,17 @@ def mqtt_publish():
593617
if key in ["power", "cell"]:
594618
continue
595619

620+
# If the value was published before, skip it
621+
if(
622+
json_data_old and
623+
len(json_data["cells_data"]) == powers and
624+
len(json_data_old["cells_data"]) == powers and
625+
len(json_data["cells_data"][device["power"] - 1]["cells"]) == cells and
626+
len(json_data_old["cells_data"][device["power"] - 1]["cells"]) == cells and
627+
value == json_data_old["cells_data"][device["power"] - 1]["cells"][cell["cell"] - 1][key]
628+
):
629+
continue
630+
596631
state_topic = "pytes_serial/" + dev_name + "/" + device_idx + "/cells/" + cell_idx + "/" + key
597632
if isinstance(value, dict) or isinstance(value, list):
598633
message = json.dumps(value)
@@ -835,6 +870,8 @@ def reboot_machine():
835870
led.off()
836871
print('...program initialisation completed starting main loop')
837872

873+
json_data = {}
874+
838875
# starting main loop
839876
while True:
840877
time.sleep(0.2)

0 commit comments

Comments
 (0)