forked from floyduww/MeaterBlockMQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meater_reader_buf.py
executable file
·271 lines (208 loc) · 8.19 KB
/
meater_reader_buf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/python3
from socket import *
import select
import paho.mqtt.client as mqtt
import math
from string import Template
import time
import ast
from configparser import ConfigParser
import sys,os
import protobuf.meater_block_pb2
sys.path.append(os.getcwd())
UDP_IP = "255.255.255.255"
# This is to build the device broadcast string.
thisDevice = protobuf.meater_block_pb2.MeaterLink()
thisDevice.device.part1 = 21578
thisDevice.device.part2 = 12
thisDevice.device.device_type = 3
thisDevice.device.inc = 1
thisDevice.device.device_mac = 42
config = ConfigParser()
MQTT_HOSTNAME = os.getenv('MQTT_HOSTNAME')
MQTT_PORT = os.getenv('MQTT_PORT')
MQTT_USEAUTH = os.getenv('MQTT_USEAUTH')
MQTT_USERNAME = os.getenv('MQTT_USERNAME')
MQTT_PASSWORD = os.getenv('MQTT_PASSWORD')
MQTT_TIMEOUT = os.getenv('MQTT_TIMEOUT')
MQTT_ZERO_OUT = os.getenv('MQTT_ZERO_OUT')
BLOCK_TIMEOUT = os.getenv('BLOCK_TIMEOUT')
BLOCK_UDP_PORT = os.getenv('BLOCK__UDP_PORT')
SCALE = os.getenv('SCALE')
MEAT_TABLE_FILE = os.getenv('MEAT_TABLE_FILE')
config.read('config.ini')
if (not MQTT_HOSTNAME):
MQTT_HOSTNAME = config.get('mqtt', 'MQTT_HOSTNAME', fallback="")
if (not MQTT_PORT):
MQTT_PORT = config.getint('mqtt', 'MQTT_PORT',fallback=1883)
if (not MQTT_TIMEOUT):
MQTT_TIMEOUT = config.getint('mqtt', 'MQTT_TIMEOUT',fallback=60)
if (not MQTT_USEAUTH):
MQTT_USEAUTH = config.getboolean('mqtt', 'MQTT_USEAUTH', fallback=False)
if (not MQTT_USERNAME):
MQTT_USERNAME = config.get('mqtt', 'MQTT_USERNAME', fallback="")
if (not MQTT_PASSWORD):
MQTT_PASSWORD = config.get('mqtt', 'MQTT_PASSWORD', fallback="")
if (not MQTT_ZERO_OUT):
MQTT_ZERO_OUT = config.getboolean('mqtt', 'MQTT_ZERO_OUT', fallback=False)
if (not BLOCK_TIMEOUT):
BLOCK_TIMEOUT = config.getint('block', 'BLOCK_TIMEOUT',fallback=60)
if (not BLOCK_UDP_PORT):
BLOCK_UDP_PORT = config.getint('block', 'BLOCK_UDP_PORT',fallback=7878)
if (not SCALE):
SCALE = config.get('block', 'SCALE', fallback='F')
if (not MEAT_TABLE_FILE):
MEAT_TABLE_FILE = config.get('block', 'MEAT_TABLE_FILE', fallback="meat_table.txt")
def probe_data(probe):
probeArr = {}
probeArr["batt"] = probe.battery
probeArr["sig"] = probe.ble_signal
probeArr["cooking"] = "0" + str(probe.cook_data.cook_stage)
probeArr["m_temp"] = math.floor(toScale(probe.current_temps.m_temp_raw))
probeArr["a_temp"] = math.floor(toScale(probe.current_temps.a_temp_raw))
probeArr["version"] = probe.fw_version
probeArr["targ_temp"] = 0
probeArr["cook_name"] = ""
probeArr["meat_type"] = ""
if(probe.cook_data.cook_stage != 0):
probeArr["targ_temp"] = math.floor(toScale(probe.cook_data.targ_temp_raw * 2))
probeArr["meat_type"] = probe.cook_data.meat_type_int
if (len(probe.cook_data.cook_name) > 0):
probeArr["cook_name"] = probe.cook_data.cook_name
else:
if probeArr["meat_type"] in dictionary.keys():
probeArr["cook_name"] = dictionary[probeArr["meat_type"]]
else:
probeArr["cook_name"] = probeArr["meat_type"]
return probeArr
def toCelsius(value):
return (float(value))/32.0
def toFahrenheit(value):
return ((toCelsius(value)*9)/5)+32.0
def toScale(value):
global SCALE
if SCALE == 'F':
return toFahrenheit(value)
else:
return toCelsius(value)
def on_publish(client, userdata, mid):
return True
def on_disconnect(mqttc, userdata, rc):
if rc != 0:
print("Unexpected disconnection.\nError Code: " + str(rc) + "\nReconnecting...")
mqttc.reconnect()
else:
print("Disconnected successfully")
def on_connect(mqttc, userdata, rc, something):
print("Connected to " + MQTT_HOSTNAME + " successfully")
def sendBlockOn():
mqttc.publish(topicBlockStatus, "on", qos=0, retain=True)
return 1
def sendBlockOff(blockStatus):
if blockStatus:
mqttc.publish(topicBlockStatus, "off", qos=0, retain=True)
if(MQTT_ZERO_OUT):
id = 1
mqttc.publish(topicMeat.substitute(id=id), "0", qos=0, retain=True)
mqttc.publish(topicAmbient.substitute(id=id), "0", qos=0, retain=True)
print("Meater Link Off")
return 0
def processPacket(packet):
global blockStatus
global lastReceive
global thisDevice
print("-----------------")
print('len(packet)='+str(len(packet)))
print('len(packet[0])='+str(len(packet[0])))
print('len(packet[1])='+str(len(packet[1])))
print(packet[1])
meater_link = protobuf.meater_block_pb2.MeaterLink()
print(packet[0].hex())
meater_link.ParseFromString(packet[0])
print("---meater_link---")
print(meater_link)
print(meater_link.SerializeToString().hex())
if (meater_link.HasField('linkData')):
print("I have data")
else:
# this is for the handshake when using a meater or meater+
# some_int needs a more descriptive variable name
if (meater_link.HasField('queryData')):
for some_int in meater_link.queryData.some_int:
if (some_int not in thisDevice.queryData.some_int ):
thisDevice.queryData.some_int.append(some_int)
print("Move along")
return False
blockStatus = sendBlockOn()
lastReceive = time.time()
probes = {}
block_power = meater_link.linkData.part3.blockInfo.power
mqttc.publish(topicBlockPower, str(block_power), qos=0, retain=True)
probe_num = 0
for probe in meater_link.linkData.part3.probe:
probe_num = probe_num + 1
if (probe.connected):
probes[probe_num] = probe_data(probe)
for id in probes:
probe = probes[id]
mqttc.publish(topicCook.substitute(id=id), probe["cooking"], qos=0, retain=True)
mqttc.publish(topicBattery.substitute(id=id), probe["batt"], qos=0, retain=True)
mqttc.publish(topicCookName.substitute(id=id), probe["cook_name"], qos=0, retain=True)
mqttc.publish(topicMeatType.substitute(id=id), probe["meat_type"], qos=0, retain=True)
mqttc.publish(topicTargetTemp.substitute(id=id), probe["targ_temp"], qos=0, retain=True)
mqttc.publish(topicMeat.substitute(id=id), probe["m_temp"], qos=0, retain=True)
mqttc.publish(topicAmbient.substitute(id=id), probe["a_temp"], qos=0, retain=True)
# mqtt setup and connect
mqttc = mqtt.Client()
mqttc.on_publish = on_publish
mqttc.on_disconnect = on_disconnect
mqttc.on_connect = on_connect
if (MQTT_USEAUTH):
mqttc.username_pw_set(username=MQTT_USERNAME,password=MQTT_PASSWORD)
mqttc.connect(MQTT_HOSTNAME, MQTT_PORT, 300)
# udp socket to listen to
s_client = socket(AF_INET, SOCK_DGRAM)
s_client.setblocking(0)
s_client.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
s_client.bind(('', BLOCK_UDP_PORT))
# send initial broadcast
msg_as_byte = bytearray.fromhex(thisDevice.SerializeToString().hex())
s_client.sendto(msg_as_byte, (UDP_IP,BLOCK_UDP_PORT))
# mqtt topics
topicCook = Template("meater/probe/$id/cook")
topicBattery = Template("meater/probe/$id/battery")
topicMeatType = Template("meater/probe/$id/meatType")
topicTargetTemp = Template("meater/probe/$id/targetTemp")
topicMeat = Template("meater/probe/$id/meat")
topicAmbient = Template("meater/probe/$id/ambient")
topicCookName = Template("meater/probe/$id/cookName")
topicBlockStatus = "meater/block/status"
topicBlockPower = "meater/block/power"
mqttc.loop_start()
inputs = [s_client]
outputs = []
socket_timeout = 1
lastReceive = time.time()
lastSend = time.time()
blockStatus = 1
file = open(MEAT_TABLE_FILE, "r")
contents = file.read()
dictionary = ast.literal_eval(contents)
file.close()
while(1):
readable, writable, exceptional = select.select(
inputs, outputs, inputs, socket_timeout)
for s in readable:
if s is s_client:
data = s.recvfrom(1024)
processPacket(data)
for s in writable:
print("write")
for s in exceptional:
print("exceptional")
if time.time() - lastSend > 5:
msg_as_byte = bytearray.fromhex(thisDevice.SerializeToString().hex())
s_client.sendto(msg_as_byte, (UDP_IP,BLOCK_UDP_PORT))
lastSend = time.time()
if time.time() - lastReceive > BLOCK_TIMEOUT:
blockStatus = sendBlockOff(blockStatus)