Skip to content

Commit

Permalink
only sending mqtt when value changed
Browse files Browse the repository at this point in the history
improved logs
  • Loading branch information
drohhyn committed Jan 21, 2024
1 parent d500088 commit ac0b625
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ source bin/activate
pip3 install pytesseract paho-mqtt vncdotool
python3 bwt_perla_smartmeter.py
```
create your own `pera.cfg` from the `pera.cfg-EXAMPLE` in the same folder.
create your own `perla.cfg` from the `perla.cfg-EXAMPLE` in the same folder.
26 changes: 16 additions & 10 deletions bwt_perla_smartmeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ def bwt_login():
vncclient.mouseDown(1)
vncclient.mouseUp(1)

def send_capture(var_name, var_name_old, regex_exp, x_pos, y_pos, x_size, y_size):
vncclient.captureRegion(var_name+'.png',x_pos,y_pos,x_size,y_size)
output_file=pytesseract.image_to_string(Image.open(var_name+'.png'),lang = 'eng',config = '-c page_separator=""')
def send_capture(var_name, regex_exp, x_pos, y_pos, x_size, y_size):
vncclient.captureRegion(var_name+'.png', x_pos, y_pos, x_size, y_size)
output_file=pytesseract.image_to_string(Image.open(var_name+'.png'), lang = 'eng', config = '-c page_separator=""')
#print(output_file)
output_regex=re.search('(.*)'+regex_exp,output_file)
old_value = globals()[var_name+'_old']
#print(var_name+" output_regex:",output_regex)
if output_regex:
output_regex=output_regex.group(1)
Expand All @@ -64,13 +65,17 @@ def send_capture(var_name, var_name_old, regex_exp, x_pos, y_pos, x_size, y_size
print('OCR '+var_name+' failed')
print('Trying to re-login')
bwt_login()
if output_regex!=var_name_old:
#print("MQTT: Publish throughput: ",throughput)

#print("bwt value ", var_name)
#print("old output ",old_value)
#print("new output ",output_regex)
if output_regex!=old_value and output_regex is not None:
try:
mqttclient.publish(mqtt_topic + var_name, payload=output_regex, qos=1, retain=False)
print("+++ MQTT: Publish "+var_name+": ", output_regex)
except:
print("MQTT: Publish throughput failed!")
var_name_old=output_regex
print("--- MQTT: Publish "+var_name+" failed!")
globals()[var_name+'_old']=output_regex

bwt_login()

Expand All @@ -84,9 +89,10 @@ def send_capture(var_name, var_name_old, regex_exp, x_pos, y_pos, x_size, y_size
NaCl_old=-1
while True:
# Capture regions
send_capture("throughput", throughput_old, "[Il1\|]*./[bh]", 50, 70, 90, 25)
send_capture("volume", volume_old, "[Il1\|]", 60, 150, 80, 25)
send_capture("NaCl", NaCl_old, "%", 198, 108, 45, 25)
send_capture("throughput", "[Il1\|]*./[bh]", 60, 70, 75, 25)
send_capture("volume", "[Il1\|]", 60, 148, 65, 25)
send_capture("NaCl", "%", 198, 108, 45, 25)
#print("### next sequence ###\n")

# Keep VNC connection alive
vncclient.mouseMove(400,0)
Expand Down

0 comments on commit ac0b625

Please sign in to comment.