-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.py
178 lines (150 loc) · 4.88 KB
/
conn.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
import ibmiotf.device
from tornado import gen
import conf
from models import Backend, LogEntry
import utils
from logger import gen_log
from commons.schemas import COMMON_LOG_SCHEMA
from commons import obd
from commons import utils as time_utils
DEFAULT_CONFIGURATIONS = {
'password': '',
'apn': '',
'report_mode': conf.TCP_LONG_CONN,
'main_server_ip': '',
'main_server_port': '',
'backup_server_ip': '',
'backup_server_port': '',
'sms_gateway': '',
'heartbeat_interval': 15,
'sack_enable': False,
'protocol_format': conf.ASCII_FORMAT,
'send_interval': 30,
'fixed_report_mode': conf.FIXED_TIMING_REPORT_MODE
}
options = {
"org": "qrqu70",
"type": "vehicle",
"id": "864251020002569",
"auth-method": "token",
"auth-token": "a-qrqu70-lgtfdke36o"
}
def extract_codes(dtc_number, raw_codes):
DTC_CODE_CONVERSION = {
'0': 'P0',
'1': 'P1',
'2': 'P2',
'3': 'P3',
'4': 'C0',
'5': 'C1',
'6': 'C2',
'7': 'C3',
'8': 'B0',
'9': 'B1',
'A': 'B2',
'B': 'B3',
'C': 'U0',
'D': 'U1',
'E': 'U2',
'F': 'U3'
}
if not dtc_number:
return ''
dtcs, j = [], 0
for i in range(0, dtc_number):
raw_code = raw_codes[j:j + 4]
dtc_code = DTC_CODE_CONVERSION[raw_code[0]] + raw_code[1:]
dtcs.append(dtc_code)
j += 4
return obd.DTC_CODE_SPLITTER.join(dtcs)
class QueclinkConnection(object):
r"""High level terminal connection."""
default_mil_interval = 60 * 5
def __init__(self, session, **kwargs):
self._session = session
self.__imei = None
self.serial_number = '0000'
self.config = DEFAULT_CONFIGURATIONS.copy()
self.config.update(kwargs)
@property
def backend(self):
return Backend.instance()
@property
def is_auth(self):
return self._session.is_open()
@property
def io_loop(self):
return self._session.io_loop
@property
def session_key(self):
return self._session.session_key
@gen.coroutine
def on_open(self, imei):
r"""Callback that signifies that connection is open"""
self.__imei = imei
msg = {'imei': imei,
'conn_status': 1,
'ts': str(time_utils.now())}
@gen.coroutine
def on_report(self, original_msg, response, sack, from_buffer=False):
log = dict(response.log.__dict__)
log_entry = LogEntry()
log_entry.imei = log.get('unique_id', self.session_key)
try:
log_entry.gps_utc_time = time_utils.dt2ts(time_utils.to_dt(
log.get('gps_utc_time')))
except (ValueError, TypeError):
return
if response.header in (conf.FIXED_REPORT, conf.OBD_REPORT):
log_entry.gps_accuracy = log.get('gps_accuracy', None)
log_entry.speed = log.get('speed', None)
log_entry.altitude = log.get('altitude', None)
log_entry.longitude = log.get('longitude', None)
log_entry.latitude = log.get('latitude', None)
# mapped_log['rpm'] = log.get('rpm', None)
else:
gen_log.warning("Common Protocol hasn't conform to report %s",
response.header)
raise gen.Return(None)
session = self.backend.get_session()
json_log = log_entry.json()
try:
"""Everything I need to do here"""
session.add(log_entry)
session.commit()
json_log = log_entry.json()
except Exception as e:
session.rollback()
finally:
session.close()
my_data = {'d': {
"lat": str(log.get('latitude', None)),
"long": str(log.get('longitude', None))
}}
try:
dev = ibmiotf.device.Client(options)
dev.connect()
dev.publishEvent("gps", "json", my_data)
dev.disconnect()
except Exception as ex:
gen_log.info('failed to publish %s', ex)
gen_log.info('MESSAGE PUBLISHED %s', json_log)
raise gen.Return(None)
@gen.coroutine
def on_ack(self, original_msg, msg, sack):
gen_log.info("PROCESSED ACK: %s[ack-%s]", msg, sack)
if self.config['sack_enable'] or (msg.type == conf.ACK and
msg.header == conf.HEARTBEAT_ACK):
self._session.send_message(sack)
raise gen.Return(None)
def verify_conn(self):
return self._session.make_rto({'sub_cmd': conf.RTO_VER})
@gen.coroutine
def configure(self):
r"""Executes commands for device configuration.
Returns unique id if configuration has been successful."""
pass
def on_close(self):
msg = {'imei': self.__imei,
'conn_status': 0,
'ts': str(time_utils.now())}