-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcarcommand-processor.py
162 lines (124 loc) · 4 KB
/
carcommand-processor.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
"""
#
# Author: L. Saetta
# created: 01 january 2018
# last update: 01/01/2018
#
# published under MIT license (see LICENSE file)
#
# This module implement a command processor
# published under MIT license (see LICENSE file)
#
"""
# pylint: disable=invalid-name
import configparser
import datetime
import json
import os
import sys
import time
from Device import Device
# Configuration
# to format datetime
STFORMAT1 = "%d-%m-%Y %H:%M:%S"
STFORMAT2 = "%d-%m-%Y"
# read configuration from gateway.ini file
# read OBD2_HOME env variable
OBD2HOME = os.getenv('OBD2_HOME')
config = configparser.ConfigParser()
config.read(OBD2HOME + '/gateway.ini')
msgLogging = config['DEFAULT']['msgLogging']
carID = config['DEFAULT']['carID']
fNameCommands = OBD2HOME + "/" + config['DEFAULT']['COMMAND_LOG']
TOPIC_COMMAND = 'carcommands/' + carID
# time between connections attempt
sleepTime = 2
def receive_msgs(mqttc, obj, msg):
# example of valid msg for TEST
# {"CARID":"0001", "DTIME":"01-01-2018 18:15:26","COMM_TYPE":"PRINT","PARAMS":[]}
validMsg = {"CARID":"0001", "SENDER":"TEST",
"DTIME":"01-01-2018 18:15:26","COMM_TYPE":"PRINT","PARAMS":[1,"pippo"]}
print('\n')
print('Received command to process: ')
try:
# first need to check carID
msgJson = json.loads(msg.payload.decode("utf-8"))
if msgJson['CARID'] == carID:
# OK to process msg
process_msgs(msg)
else:
# ignore msg, do nothing
pass
except:
print('\n')
print('*** Error in parsing command: ')
print('*** Error info: ', sys.exc_info()[0], sys.exc_info()[1])
print('*** Command received: ', msg.payload)
def process_msgs(msg):
# here we define the only commands supported
# insert here other commands...
options = {"PRINT": doPrint,
"BLINK": doBlink,
"OTHERS": doOthers
}
try:
msgJson = json.loads(msg.payload.decode("utf-8"))
print('CARID: ', msgJson['CARID'])
print('DTIME: ', msgJson['DTIME'])
print('SENDER: ', msgJson['SENDER'])
print('COMMAND: ', msgJson['COMM_TYPE'])
print('PARAMS: ', msgJson['PARAMS'])
# logging
pFileCommands.write(str(msg.payload) + "\n")
pFileCommands.flush()
# here we call the function handling the single command
# it serches the name of the func in the dictionary options !!!
options[msgJson['COMM_TYPE']](msgJson['COMM_TYPE'], msgJson['PARAMS'])
except:
print('\n')
print('*** Error in parsing command: ')
print('*** Error info: ', sys.exc_info()[0], sys.exc_info()[1])
print('*** Command received: ', msg.payload)
#
# These are the functions dedicated to single commands
# TODO: put the code to implement commands
#
def doPrint(commType, parms):
print('*** Executing Print command ...')
def doBlink(commType, parms):
print('*** Executing Blink command ...')
def doOthers(commType, parms):
print('*** Executing Others command ...')
#
# **** Main ****
#
print("")
print("***")
print("*** OBD2 Command Processor started ***")
print("***")
print('*** Reading commands from topic: ', TOPIC_COMMAND)
# MQTT connectivity is encapsulated in the Device class
# see Device.py
# assign a different clientID (+SUB)
clientID = carID + "SUB"
gateway = Device(clientID)
# try connecting in loop
# to handle case in which initially no network connection
while gateway.isConnected() != True:
try:
gateway.connect()
except:
print('*** Error in MQTT connection !')
time.sleep(sleepTime)
# wait for MQTT connection OK
# (at this point should be connected)
gateway.wait_for_conn_ok()
# Subscribes to the topic dedicated to the Car
gateway.subscribe(TOPIC_COMMAND)
# redefine here the fcallback to call when msg received
gateway.set_on_message(receive_msgs)
# file for logging commands
pFileCommands = open(fNameCommands, "w")
while True:
# waiting for commands
time.sleep(sleepTime)