add mqtt publishing

This commit is contained in:
Asaril 2020-03-12 09:37:16 +01:00
parent 220c5585bb
commit 1da22413c1
2 changed files with 43 additions and 9 deletions

View file

@ -19,6 +19,8 @@ http://bridge1.soehnle.de/devicedataservice/dataservice?data=25e4e40901a2c100000
- A5000000000000000100000000000000000000000000000056e5abd9
### measurement (0x24)
0 2 14 30 38 42 46 50 60
0 1 7 15 19 21 23 25 30
24 e4e40901a2c1 db020200d49c0138 132c560e 233c 0000 0000 0000000000 39f052f7
24 e4e40901a2c1 db020200d49c0138 09c492cb 233c 1303 15f5 0000000000 5848fded
MT bridge_id wght watr?

View file

@ -1,14 +1,17 @@
import http.server
import socketserver
import urllib.parse
import urllib.request
import paho.mqtt.publish as mq
import time
import json
import struct
PORT=18001
PORT = 18001
DATA_SRV_PATH = "/devicedataservice/dataservice"
#?data=25e4e40901a2c100000000000000000000413a41040000000000000000001d9222e6
BROKER = ('homeproxy', 1883)
TOPIC_PREFIX = 'scalefix/raw'
RESPONSES = {
@ -21,9 +24,34 @@ RESPONSES = {
}
_last_msg = ''
def do_publish(data):
global _last_msg
if data != _last_msg:
now = int(time.time())
bridge = data[2:14]
unk_id = data[14:39]
unk_val = data[30:38]
weight = int(data[38:42],16)
bia = (
int(data[42:50],16),
int(data[42:46],16),
int(data[46:50],16)
)
msgs = [
(TOPIC_PREFIX+'/weight', json.dumps({'timestamp':now, 'weight':weight, 'bia':[bia[1],bia[2]]})),
(TOPIC_PREFIX+'/all', json.dumps({'timestamp':now, 'bridge':bridge, 'unknown_id':unk_id,
'unkown_value':unk_val, 'weight':weight, 'bia':[bia[1],bia[2]], 'bia_long':bia[0], 'raw':data}))
]
mq.multiple(msgs, hostname=BROKER[0], port=BROKER[1])
_last_msg = data
class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
#print ('do_GET:%s'%self.path)
parts = urllib.parse.urlsplit(self.path)
if parts.path == DATA_SRV_PATH:
@ -37,8 +65,7 @@ class Handler(http.server.SimpleHTTPRequestHandler):
data_str = params['data'][0]
bin_data = bytes.fromhex(data_str)
msg_type = bin_data[0]
msg_type=int(data_str[0:2],16)
if msg_type not in RESPONSES:
print ('got unknown message: ' + data_str)
@ -46,10 +73,15 @@ class Handler(http.server.SimpleHTTPRequestHandler):
response = RESPONSES[msg_type]
#print('handle message:%d'%msg_type)
if msg_type == 0x24:
do_publish(data_str)
self.send_response(http.server.HTTPStatus.OK)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", len(response))
self.end_headers()
if response:
self.wfile.write(response.encode('utf-8'))
with socketserver.TCPServer(("",PORT), Handler) as httpd: