-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconncarmain.py
243 lines (179 loc) · 4.82 KB
/
conncarmain.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
"""
#
# Author: L. Saetta
# created: 27 december 2017
# last update: 21/01/2018
#
# published under MIT license (see LICENSE file)
"""
# pylint: disable=invalid-name
import configparser
import datetime
import json
import os
import requests
import sys
import time
# only if using Googe Voice
sys.path.append('/home/pi/AIY-voice-kit-python/src')
import aiy.voicehat
from Device import Device
from OBDII import OBDII
from OBDIISimulator import OBDIISimulator
# control Python Version
vers = sys.version_info.major
# Configuration
# to format datetime
STFORMAT1 = "%d-%m-%Y %H:%M:%S"
STFORMAT2 = "%d-%m-%Y"
# used for START_TRIP msg
STFORMAT3 = "%d-%m-%Y %H"
# send a msg every 5 sec.
sleepTime = 5
# the name of MQTT topic where msgs are sent
TOPIC_NAME = 'cardata'
TOPIC_ALERTS_NAME = "caralerts"
# 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']
# should be read from config file
HOST = "ubuntucontainers-dashboariotnew-zekci6cs.srv.ravcloud.com"
PORT = 8883
# file name for local logging
FNAME = OBD2HOME + "/msgs" + datetime.datetime.now().strftime(STFORMAT2) + ".log"
#
# get Position
#
def get_position():
URL_POSITION = 'http://localhost:1880/position'
lat = 0
lon = 0
alt = 0
try:
response = requests.get(URL_POSITION)
jData = json.loads(response.content.decode("utf-8"))
lat = jData['lat']
lon = jData['lon']
alt = jData['alt']
except:
print('Error in GET position')
print('*** Error info: ', sys.exc_info()[0], sys.exc_info()[1])
# if error returns zeros
return lat, lon, alt
#
# createJSONMsg()
# create the msg in JSON format starting from OBDII readings
#
def createJSONMsg():
msg = {}
# in the main code it is defined if Simulator or not !
msg = obdii.getMessage()
msg['CARID'] = carID
# added 21/01/2018 (Sensor Fusion)
# get Position and add to msg
lat, lon, alt = get_position()
msg['LAT'] = lat
msg['LON'] = lon
msg['ALT'] = alt
# format in JSON
msgJson = json.dumps(msg)
return msgJson
#
#
# createEventMsg
#
def createEventMsg(event_type):
msg = {}
# in the main code it is defined if Simulator or not !
msg['EVENT_TYPE'] = event_type
msg['CARID'] = carID
msg['DAYHOUR'] = datetime.datetime.now().strftime(STFORMAT3)
# format in JSON
msgJson = json.dumps(msg)
return msgJson
#
# **** Main ****
#
#
# Test if simulation mode (no acquiring data from OBDII)
#
# runMode = SIMUL|ACQUIRE|NULL (null means ACQUIRE)
parLenght = len(sys.argv)
if parLenght >= 2:
runMode = sys.argv[1]
else:
runMode = "ACQUIRE"
print("")
print("***")
print("*** OBD2 Data Acquisition Program started ***")
print("***")
print("*** RUN MODE: ", runMode)
print('*** Python Version ', vers)
# MQTT connectivity is encapsulated in the Device class
# see Device.py
clientID = carID
# clientID is passed to make possible different clientID
# MQTT doesn't allow different clients with same ID (second is disconnected)
gateway = Device(clientID)
# try connecting in loop
# to handle case in which initially no network connection
while gateway.isConnected() != True:
try:
gateway.connect(HOST, PORT, "YES")
except:
print('*** Error in MQTT connection !')
print('\n')
print('*** Error info: ', sys.exc_info()[0], sys.exc_info()[1])
time.sleep(sleepTime)
# to signal MQTT OK blink
led = aiy.voicehat.get_led()
led.set_state(aiy.voicehat.LED.BLINK)
#
# connectivity to OBDII interface
# here it decides if running a simulator or real client
#
if runMode == "ACQUIRE":
obdii = OBDII()
else:
# SIMUL
obdii = OBDIISimulator()
#
# main loop
#
if msgLogging == "YES":
pFile = open(FNAME, "w")
# wait for MQTT connection OK
# (at this point should be connected)
gateway.wait_for_conn_ok()
# in this point I must insert the SEND for START_TRIP msg
try:
msgJson = createEventMsg("START_TRIP")
gateway.publish(TOPIC_ALERTS_NAME, msgJson)
except:
print('Error in sending START_TRIP...')
nMsgs = 0
while True:
nMsgs = nMsgs + 1
print('*** Sending msg n. ', nMsgs)
# create the msg to send from data
msgJson = createJSONMsg()
try:
time.sleep(0.5)
led.set_state(aiy.voicehat.LED.ON)
gateway.publish(TOPIC_NAME, msgJson)
time.sleep(0.5)
led.set_state(aiy.voicehat.LED.OFF)
except:
print('Error in sending...')
print('*** Error info: ', sys.exc_info()[0], sys.exc_info()[1])
if msgLogging == "YES":
# log msg on file...
pFile.write(msgJson)
pFile.write('\n')
time.sleep(sleepTime)
if msgLogging == "YES":
pFile.close()