-
Notifications
You must be signed in to change notification settings - Fork 2
/
vedirect.py
105 lines (92 loc) · 3.75 KB
/
vedirect.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
#!/usr/bin/python3
###############################################
# Copyright by IT Stall (www.itstall.de) 2018 #
# Author: Dennis Eisold #
# Created: 11.07.2018 #
###############################################
import sys
from serial import Serial
import paho.mqtt.client as mqtt
import json
import getopt
def main(argv):
global device
try:
opts, args = getopt.getopt(argv, "hd:", ["device="])
except getopt.GetoptError:
print('vedirect.py --device <device>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print('vedirect.py --device <device>')
sys.exit()
elif opt in ("-d", "--device"):
device = arg
if __name__ == "__main__":
main(sys.argv[1:])
if(device == 'None'):
print("We need a device to listen on")
print('vedirect.py --device <device>')
exit()
try:
with open('/opt/opencamper/config.json') as f:
config = json.load(f)
except KeyError:
print("No config file found")
exit()
try:
if(config[device]['protocol']):
with open('/opt/opencamper/lib/protocols/'+config[device]['protocol']+'.json') as f:
deviceprotocol = json.load(f)
except KeyError:
print("No protocol in config specified")
exit()
try:
port = Serial(config[device]['port'], config[device]['speed'], rtscts=True, dsrdtr=True)
except ValueError:
print("Could not connect to Port: " + config[device]['port'])
mqtt_server = config[device]['mqtt_setting']
if(mqtt_server != 0):
client = mqtt.Client()
client.loop_start()
client.connect(config[mqtt_server]['host'], config[mqtt_server]['port'], config[mqtt_server]['timeout'])
if config[mqtt_server]['username'] is not 0 and config[mqtt_server]['password'] is not 0:
client.username_pw_set(config[mqtt_server]['username'], config[mqtt_server]['password'])
data = {}
if(config[device]["mqtt_counter"] < 1):
counter = 0
try:
while True:
line = port.readline().strip()
if line:
try:
key, value = [x.strip() for x in line.split()[:2]]
# If Value is a Checksum, we can ignore it
if(key.decode("utf-8") != 'Checksum'):
# If Key is in protocol specified, we use that one, and looking for calculation and units
# If not, we use the received one
try:
newkey = deviceprotocol['Key'][key.decode("utf-8")]['name']
except KeyError:
newkey = key.decode("utf-8")
# Is a specification for the key values in the protocol?
if(newkey in deviceprotocol):
try:
data[newkey] = deviceprotocol[newkey][str(value.decode("utf-8"))]
except KeyError:
data[newkey] = value.decode("utf-8")
else:
data[newkey] = value.decode("utf-8")
try:
calc = deviceprotocol['Key'][key.decode("utf-8")]['calc']
data[newkey] = float(value.decode("utf-8")) / float(calc)
except KeyError:
calc = 0
else:
checksum = value.decode("utf-8")
if(mqtt_server != 0):
client.publish(config[device]['mqtt_topic'], json.dumps(data), int(config[device]['mqtt_qos']))
print(json.dumps(data));
except ValueError: error = "Malformed Line: {}".format(line)
except KeyboardInterrupt: pass
port.close()