gis::ks108设备接入解码
ks108 Gps定位模块接入服务中心,解码类:
1
#
-- coding:utf-8 --
2 # 解码器定义
3 # 2011.3.10 scott sw2us.com
4 # from aobject import *
5 import os,os.path,sys,time,datetime,copy
6 # import codec
7
8 # MediaCodecType = MediaDataType
9 class MediaDataType:
10 GPS = 1 << 0
11 AUDIO = 1 << 1
12 VIDEO = 1 << 2
13 IMAGE = 1 << 3
14 TEXT = 1 << 4
15 IODATA = 1 << 5
16 RAWBLOB = 1 << 6
17 COMMAND = 1 << 7 # 通用命令
18 ALARM = 1 << 8 # 报警信息
19 UNDEFINED = 0xff
20
21
22 AOCTRL_CMD_SHAKE_ACK = 1 # 应答握手信号信息
23 AOCTRL_CMD_REG_ACK = 2 # 终端注册响应消息
24 AOCTRL_CMD_SAMPLING_TIMESET = 3 # 等时连续回传设置
25 AOCTRL_CMD_ALARM_ACK = 4 # 应答报警消息
26 AOCTRL_CMD_NAMED = 5 # 一次点名消息
27 AOCTRL_CMD_SPEEDSET = 6 # 设置车速上下限
28 AOCTRL_CMD_POWER_ONOFF = 7 # 电路控制信号
29 AOCTRL_CMD_OIL_ONOFF = 8 # 油路控制信号
30 AOCTRL_CMD_REBOOT = 9 # 控制设备重启消息
31 AOCTRL_CMD_ACC_ON_TIME = 10 # 设置ACC开发送数据间隔
32 AOCTRL_CMD_ACC_OFF_TIME = 11 # 设置ACC关发送数据间隔
33 AOCTRL_CMD_BARRIER_SET = 12 # 设置电子围栏消息
34 AOCTRL_CMD_GETLOCATION = 13 # 应答获取终端所在位置消息
35 AOCTRL_CMD_LISTEN_START = 14 # 监听命令
36 AOCTRL_CMD_COMMADDR_SET = 15 # 设置终端IP地址和端口
37 AOCTRL_CMD_APN_SET = 16 # 设置APN消息
38 AOCTRL_CMD_GET_VERSION = 17 # 读取终端版本消息
39 AOCTRL_CMD_CLEAR_ALARMS = 18 # 取消所有报警消息
40 AOCTRL_CMD_CLEAR_MILES = 19 # 里程清零消息
41 AOCTRL_CMD_INIT_MILES = 20 # 里程初始化消息
42 AOCTRL_CMD_UPDATING = 21 # 启动升级消息
43
44 AOCTRL_CMD_SHACK_REQ = 31 # 握手信号消息
45 AOCTRL_CMD_REG_REQ = 32 # 终端注册信息
46 AOCTRL_CMD_SAMPLING_TIMESET_ACK = 32 # 应答等时连续回传设置
47 AOCTRL_CMD_ALARM_REQ = 33 # 警报消息
48 AOCTRL_CMD_NAMED_ACK = 34 # 应答点名信息
49 AOCTRL_CMD_SIMPLING_GPSDATA = 35 # 等时连续回传消息
50 AOCTRL_CMD_SIMPLING_END = 36 # 连续回传结束消息
51 AOCTRL_CMD_SPEEDSET_ACK = 37 # 应答设置车速上下限
52 AOCTRL_CMD_POWERCTRL_ACK = 38 # 应答电路控制
53 AOCTRL_CMD_OILCTRL_ACK = 39 # 应答油路控制
54 AOCTRL_CMD_REBOOT_ACK = 40 # 应答设备重启消息
55 AOCTRL_CMD_ACCON_TIMESET_ACK = 41 # 应答设置ACC开发送数据间隔
56 AOCTRL_CMD_ACCOFF_TIMESET_ACK = 42 # 应答设置ACC关发送数据间隔
57 AOCTRL_CMD_BARRIER_SET_ACK = 43 # 应答设置电子围栏消息
58 AOCTRL_CMD_GETLOCATION_ACK = 44 # 获取终端所在位置消息
59 AOCTRL_CMD_LISTEN_ACK = 45 # 应答监听命令
60 AOCTRL_CMD_COMMADDR_SET_ACK = 46 # 应答设置终端IP地址和端口
61 AOCTRL_CMD_APN_SET_ACK = 47 # 应答设置APN消息
62 AOCTRL_CMD_GETVERSION_ACK = 48 # 应答读取终端版本消息
63 AOCTRL_CMD_CLEAR_ALARMS_ACK = 49 # 应答取消所有报警消息
64 AOCTRL_CMD_CLEAR_MILES_ACK = 50 # 应答里程清零消息
65 AOCTRL_CMD_UPDATING_ACK = 61 # 应答启动升级消息
66 AOCTRL_CMD_INIT_MILES_ACK = 62 # 应答初始化里程消息
67
68 AOCTRL_CMD_LIST = {
69 AOCTRL_CMD_SHAKE_ACK:u ' 应答握手信号信息 ' ,
70 AOCTRL_CMD_REG_ACK:u ' 终端注册响应消息 ' ,
71 AOCTRL_CMD_SAMPLING_TIMESET:u ' 等时连续回传设置 ' ,
72 AOCTRL_CMD_ALARM_ACK:u ' 应答报警消息 ' ,
73 AOCTRL_CMD_NAMED:u ' 一次点名消息 ' ,
74 AOCTRL_CMD_SPEEDSET:u ' 设置车速上下限 ' ,
75
76 AOCTRL_CMD_POWER_ONOFF:u ' 电路控制信号 ' ,
77 AOCTRL_CMD_OIL_ONOFF:u ' 油路控制信号 ' ,
78 AOCTRL_CMD_REBOOT:u ' 控制设备重启消息 ' ,
79 AOCTRL_CMD_ACC_ON_TIME :u ' 设置ACC开发送数据间隔 ' ,
80 AOCTRL_CMD_ACC_OFF_TIME :u ' 设置ACC关发送数据间隔 ' ,
81 AOCTRL_CMD_BARRIER_SET :u ' 设置电子围栏消息 ' ,
82 AOCTRL_CMD_GETLOCATION :u ' 应答获取终端所在位置消息 ' ,
83 AOCTRL_CMD_LISTEN_START :u ' 监听命令 ' ,
84 AOCTRL_CMD_COMMADDR_SET :u ' 设置终端IP地址和端口 ' ,
85 AOCTRL_CMD_APN_SET :u ' 设置APN消息 ' ,
86 AOCTRL_CMD_GET_VERSION :u ' 读取终端版本消息 ' ,
87 AOCTRL_CMD_CLEAR_ALARMS :u ' 取消所有报警消息 ' ,
88 AOCTRL_CMD_CLEAR_MILES :u ' 里程清零消息 ' ,
89 AOCTRL_CMD_INIT_MILES :u ' 里程初始化消息 ' ,
90 AOCTRL_CMD_UPDATING :u ' 启动升级消息 ' ,
91
92 AOCTRL_CMD_SHACK_REQ :u ' 握手信号消息 ' ,
93 AOCTRL_CMD_REG_REQ :u ' 终端注册信息 ' ,
94 AOCTRL_CMD_SAMPLING_TIMESET_ACK :u ' 应答等时连续回传设置 ' ,
95 AOCTRL_CMD_ALARM_REQ :u ' 警报消息 ' ,
96 AOCTRL_CMD_NAMED_ACK :u ' 应答点名信息 ' ,
97 AOCTRL_CMD_SIMPLING_GPSDATA :u ' 等时连续回传消息 ' ,
98 AOCTRL_CMD_SIMPLING_END :u ' 连续回传结束消息 ' ,
99 AOCTRL_CMD_SPEEDSET_ACK :u ' 应答设置车速上下限 ' ,
100 AOCTRL_CMD_POWERCTRL_ACK :u ' 应答电路控制 ' ,
101 AOCTRL_CMD_OILCTRL_ACK :u ' 应答油路控制 ' ,
102 AOCTRL_CMD_REBOOT_ACK :u ' 应答设备重启消息 ' ,
103 AOCTRL_CMD_ACCON_TIMESET_ACK :u ' 应答设置ACC开发送数据间隔 ' ,
104 AOCTRL_CMD_ACCOFF_TIMESET_ACK:u ' 应答设置ACC关发送数据间隔 ' ,
105 AOCTRL_CMD_BARRIER_SET_ACK :u ' 应答设置电子围栏消息 ' ,
106 AOCTRL_CMD_GETLOCATION_ACK :u ' 获取终端所在位置消息 ' ,
107 AOCTRL_CMD_LISTEN_ACK :u ' 应答监听命令 ' ,
108 AOCTRL_CMD_COMMADDR_SET_ACK:u ' 应答设置终端IP地址和端口 ' ,
109 AOCTRL_CMD_APN_SET_ACK:u ' 应答设置APN消息 ' ,
110 AOCTRL_CMD_GETVERSION_ACK:u ' 应答读取终端版本消息 ' ,
111 AOCTRL_CMD_CLEAR_ALARMS_ACK:u ' 应答取消所有报警消息 ' ,
112 AOCTRL_CMD_CLEAR_MILES_ACK:u ' 应答里程清零消息 ' ,
113 AOCTRL_CMD_UPDATING_ACK :u ' 应答启动升级消息 ' ,
114 AOCTRL_CMD_INIT_MILES_ACK :u ' 应答初始化里程消息 ' ,
115 }
116
117
118 ALARM_TYPELIST = {
119 0:u ' 车辆断电 ' ,
120 1 :u ' 电子围栏入界报警 ' ,
121 2 :u ' 车辆劫警(SOS求助) ' ,
122 3 :u ' 车辆防盗器警报 ' ,
123 4 :u ' 车辆低速报警 ' ,
124 5 :u ' 车辆超速报警 ' ,
125 6 :u ' 电子围栏出界报警 '
126 }
127
128 def parseTime(dmy,hms):
129 d,mon,y = map(int, map(float,[dmy[: 2 ],dmy[ 2 : 4 ],dmy[ 4 :]]) )
130 h,min,s = map(int, map(float,[hms[: 2 ],hms[ 2 : 4 ],hms[ 4 :]]) )
131 # print d,mon,y,h,min,s
132 return time.mktime(( 2000 + y,mon,d,h,min,s,0,0,0))
133
134 # 简单的模拟gps接收解码器
135 # gps接收程序解析之后连接本地的TcpService端口,并传送过来
136 # 只有简单的gps数据,模拟端口打开
137 class MediaCodec_KS108:
138 def __init__ (self):
139 self.buf = ''
140 self.conn = None
141
142
143 def parse_gps(self,msg,s):
144 # gps 数据长度62个字符
145 msg[ ' gps ' ] = None # AOCTRL_CMD_SIMPLING_GPSDATA
146 if len(s) != 62 :
147 return
148 try :
149 yy = int(s[: 2 ])
150 mm = int(s[ 2 : 4 ])
151 dd = int(s[ 4 : 6 ])
152 av = s[ 6 ] # 是否有效
153 lat = int(s[ 7 : 9 ]) + float( s[ 9 : 16 ]) / 60 .
154 ns = s[ 16 ] # 维度标志 N/S
155 lon = int(s[ 17 : 20 ]) + float( s[ 20 : 27 ]) / 60 .
156 ew = s[ 27 ]
157 speed = float(s[ 28 : 33 ])
158 HH = int(s[ 33 : 35 ])
159 MM = int(s[ 35 : 37 ])
160 SS = int(s[ 37 : 39 ])
161 angle = float(s[ 39 : 45 ])
162 power = int(s[ 45 ])
163 acc = int(s[ 46 ])
164 mileflag = s[ 53 ]
165 miles = int(s[ 54 :], 16 )
166 gpstime = datetime.datetime( 2000 + yy,mm,dd,HH,MM,SS)
167 # GMT+8
168 gpstime = gpstime + datetime.timedelta(hours = 8 )
169 msg[ ' gps ' ] = {
170 ' time ' :gpstime,
171 ' lon ' :lon,
172 ' lat ' :lat,
173 ' speed ' :speed,
174 ' angle ' :angle,
175 ' power ' :power,
176 ' acc ' :acc,
177 ' miles ' :miles
178 }
179 except :
180 traceback.print_exc()
181 msg[ ' gps ' ] = None
182
183
184
185 def __parseMessage (self,s):
186 # @return: [object,] or [None,]
187 msg = { ' mid ' :self.conn.mid, ' cmd ' :0, ' seq ' : '' , ' params ' : '' , ' gps ' :None} # mid - 模块硬件编号
188
189
190 cmd = s[ 12 : 16 ]
191 msg[ ' msg ' ] = cmd
192 msg[ ' seq ' ] = s[: 12 ]
193
194 if cmd == ' BP00 ' and len(s) == 24 :
195 msg[ ' cmd ' ] = AOCTRL_CMD_SHACK_REQ
196 elif cmd == ' BP05 ' : # 注册信息
197 msg[ ' cmd ' ] = AOCTRL_CMD_REG_REQ
198 msg[ ' mid ' ] = s[ 16 : 31 ] #
199 self.conn.mid = msg[ ' mid ' ] # 设备连接上来这个是第一个消息包
200 # 将gps独立出单独的包
201 self.parse_gps(msg,s[ 31 :])
202 elif cmd == ' BS08 ' and len(s) == 24 :
203 msg[ ' cmd ' ] = AOCTRL_CMD_SAMPLING_TIMESET_ACK # 应答等时连续回传设置
204 msg[ ' samplewait ' ] = int(s[ 16 : 20 ], 16 )
205 hours = int(s[ 20 : 22 ], 16 )
206 minutes = int(s[ 22 : 24 ], 16 )
207 msg[ ' samplecycle ' ] = hours * 3600 + minutes * 60
208 msg[ ' params ' ] = " samplewait:%s,samplecycle:%s " % (msg[ ' samplewait ' ],msg[ ' samplecycle ' ])
209
210 elif cmd == ' BO01 ' :
211 msg[ ' cmd ' ] = AOCTRL_CMD_ALARM_REQ # 警报消息
212 msg[ ' alarm ' ] = int(s[ 16 ]) # 报警类型
213 msg[ ' params ' ] = ALARM_TYPELIST[msg[ ' alarm ' ]]
214 self.parse_gps(msg,s[ 17 :])
215 elif cmd == ' BP04 ' : # 点名应答
216 msg[ ' cmd ' ] = AOCTRL_CMD_NAMED_ACK # 应答点名信息
217 self.parse_gps(msg,s[ 16 :])
218 elif cmd == ' BR00 ' :
219 msg[ ' cmd ' ] = AOCTRL_CMD_SIMPLING_GPSDATA
220 self.parse_gps(msg,s[ 16 :])
221 elif cmd == ' BR02 ' :
222 msg[ ' cmd ' ] = AOCTRL_CMD_SIMPLING_END
223 self.parse_gps(msg,s[ 16 :])
224 elif cmd == ' BP12 ' and len(s) == 24 :
225 msg[ ' cmd ' ] = AOCTRL_CMD_SPEEDSET_ACK
226 msg[ ' hispeed ' ] = int(s[ 17 : 20 ])
227 msg[ ' lospeed ' ] = int(s[ 21 : 24 ]) # 低速限制
228 msg[ ' params ' ] = " hispeed:%s,lospeed:%s " % ( msg[ ' hispeed ' ],msg[ ' lospeed ' ] )
229
230 elif cmd == ' BV00 ' and len(s) == 17 :
231 msg[ ' cmd ' ] = AOCTRL_CMD_POWERCTRL_ACK # #应答电路控制
232 msg[ ' onoff ' ] = int(len(s[ 17 ]))
233 if msg[ ' onoff ' ] == 0:
234 msg[ ' params ' ] = u ' 已关电路 '
235 else :
236 msg[ ' params ' ] = ' 已开电路 '
237
238 elif cmd == ' BV01 ' and len(s) == 17 : # 应答油路控制
239 msg[ ' cmd ' ] = AOCTRL_CMD_OILCTRL_ACK
240 msg[ ' onoff ' ] = int(len(s[ 17 ]))
241 if msg[ ' onoff ' ] == 0:
242 msg[ ' params ' ] = u ' 已关油路 '
243 else :
244 msg[ ' params ' ] = ' 已开油路 '
245 elif cmd == ' BT00 ' :
246 msg[ ' cmd ' ] = AOCTRL_CMD_REBOOT_ACK
247 elif cmd == ' BR05 ' :
248 msg[ ' cmd ' ] = AOCTRL_CMD_ACCON_TIMESET_ACK # 应答设置ACC开发送数据间隔
249 elif cmd == ' BR06 ' :
250 msg[ ' cmd ' ] = AOCTRL_CMD_ACCOFF_TIMESET_ACK # 应答设置ACC关发送数据间隔
251 elif cmd == ' BU00 ' :
252 msg[ ' cmd ' ] = AOCTRL_CMD_BARRIER_SET_ACK
253 elif cmd == ' BR03 ' : # 获取终端所在位置消息
254 msg[ ' cmd ' ] = AOCTRL_CMD_GETLOCATION_ACK
255 self.parse_gps(msg,s[ 16 :])
256 elif cmd == ' BS20 ' :
257 msg[ ' cmd ' ] = AOCTRL_CMD_LISTEN_ACK # 应答监听命令
258 elif cmd == ' BP02 ' and len(s) == 16 :
259 msg[ ' cmd ' ] = AOCTRL_CMD_COMMADDR_SET_ACK # 应答设置终端IP地址和端口
260 elif cmd == ' BP03 ' :
261 msg[ ' cmd ' ] = AOCTRL_CMD_APN_SET_ACK # 应答设置APN消息
262 elif cmd == ' BP01 ' :
263 msg[ ' cmd ' ] = AOCTRL_CMD_GETVERSION_ACK
264 msg[ ' version ' ] = s[ 16 :]
265 elif cmd == ' BS21 ' :
266 msg[ ' cmd ' ] = AOCTRL_CMD_CLEAR_ALARMS_ACK # 应答取消所有报警消息
267 elif cmd == ' BS04 ' :
268 msg[ ' cmd ' ] = AOCTRL_CMD_CLEAR_MILES_ACK # 应答里程清零消息
269 elif cmd == ' BS05 ' :
270 msg[ ' cmd ' ] = AOCTRL_CMD_UPDATING_ACK # = 61 #应答启动升级消息
271 elif cmd == ' BS06 ' :
272 msg[ ' cmd ' ] = AOCTRL_CMD_INIT_MILES_ACK # 应答初始化里程消息
273 else :
274 msg = None
275 # print 'decode:',msg
276 return msg
277
278 # parse - codec 必须实现
279 # 对于某些设备的请求消息,这里必须进行默认的应答
280 # 如果出现大量数据包的要发送回设备的情况,考虑建立队列,用工作线程
281 # 慢慢发送,因为parse还在socket接收线程中
282 def parse(self,aom,d):
283 cmd = d[ ' cmd ' ]
284 s = ''
285 if cmd == AOCTRL_CMD_SHACK_REQ: # 握手回复
286 s = self.command(aom,{ ' cmd ' :AOCTRL_CMD_SHAKE_ACK, ' seq ' :d[ ' seq ' ]})
287 if cmd == AOCTRL_CMD_REG_REQ: # 设备注册
288 s = self.command(aom,{ ' cmd ' :AOCTRL_CMD_REG_ACK, ' seq ' :d[ ' seq ' ]})
289 if cmd == AOCTRL_CMD_ALARM_REQ:
290 s = self.command(aom,{ ' cmd ' :AOCTRL_CMD_ALARM_ACK, ' seq ' :d[ ' seq ' ], ' alarm ' :d[ ' alarm ' ]})
291 if s:
292 print ' send back to device: ' ,s
293 self.conn.handle.sendall(s)
294
295 def decode(self,s,conn):
296 # @return: packets,retry
297 # 解码出多个消息包,并返回是否
298 self.conn = conn
299 msglist = []
300 retry = True
301 print s
302 self.buf += s
303 while True:
304 p1 = self.buf.find( ' ( ' )
305 if p1 == - 1 :
306 self.buf = ''
307 break
308 self.buf = self.buf[p1:]
309
310 p2 = self.buf.find( ' ) ' )
311 if p2 == - 1 :
312 if len(self.buf) > 1024 :
313 return (),False # please break socket connection
314 break # push in more data
315 # find tail
316 msg = None
317 try :
318 msg = self. __parseMessage (self.buf[ 1 :p2])
319 except :
320 traceback.print_exc()
321 msg = None
322 if msg:
323 msglist.append(msg)
324 self.buf = self.buf[p2 + 1 :]
325 return msglist,retry
326
327
328
329 # 执行设备命令
330 def command(self,aom,msg):
331 # cmd - object (json decoded)
332 # @return: 返回命令控制消息
333 cmd = msg[ ' cmd ' ]
334 code = ''
335 params = ''
336 if not msg.has_key( ' seq ' ):
337 msg[ ' seq ' ] = ' 0 ' * 12
338
339 if cmd == AOCTRL_CMD_REG_ACK: # 注册响应
340 code = " (%sAP05) " % (msg[ ' seq ' ])
341
342 elif cmd == AOCTRL_CMD_SAMPLING_TIMESET: # 等时连续回传设置
343 wait = msg[ ' samplewait ' ] # 时间间隔
344 cycle = msg[ ' samplecycle ' ] # 总时长
345 hh = int(cycle / 3600 )
346 mm = (cycle - hh * 3600 ) / 60
347 mm = int(mm)
348 code = " (%sAR00%04x%02x%02x) " % ( ' 0 ' * 12 ,wait,hh,mm)
349 params = " wait:%s seconds, cycle: %s hours,%s mimuts " % (wait,hh,mm)
350 elif cmd == AOCTRL_CMD_ALARM_ACK: # 应答报警消息
351 alarm = msg[ ' alarm ' ]
352 code = " (%sAS01%d) " % (msg[ ' seq ' ],alarm)
353 params = ALARM_TYPELIST[alarm]
354 elif cmd == AOCTRL_CMD_NAMED : # 一次点名消息
355 code = " (%sAP00) " % ( ' 0 ' * 12 )
356 elif cmd == AOCTRL_CMD_SPEEDSET: # 设置车速上下限
357 hi = msg[ ' hispeed ' ]
358 lo = msg[ ' lospeed ' ]
359 code = " (%sAP12H%03dL%03d) " % ( ' 0 ' * 12 ,hi,lo)
360 params = " hi:%s,lo:%s " % (hi,lo)
361 elif cmd == AOCTRL_CMD_POWER_ONOFF: # 电路控制信号
362 onoff = msg[ ' onoff ' ]
363 code = " (%sAV00%d) " % ( ' 0 ' * 12 ,onoff) # “1”表示开电路,“0”表示关电路
364 params = " onoff:%s " % (onoff)
365 elif cmd == AOCTRL_CMD_OIL_ONOFF: # 油路控制信号
366 onoff = msg[ ' onoff ' ]
367 code = " (%sAV01%d) " % ( ' 0 ' * 12 ,onoff) # “1”表示开油路,“0”表示关油路
368 params = " onoff:%s " % (onoff)
369 elif cmd == AOCTRL_CMD_REBOOT: # 控制设备重启消息
370 code = " (%sAT00) " % ( ' 0 ' * 12 )
371 elif cmd == AOCTRL_CMD_ACC_ON_TIME: # 设置ACC开发送数据间隔
372 interval = msg[ ' interval ' ]
373 code = " (%sAR05%04x) " % ( ' 0 ' * 12 ,interval)
374 params = " interval:%s seconds " % (interval)
375 elif cmd == AOCTRL_CMD_ACC_OFF_TIME: # 设置ACC关发送数据间隔
376 interval = msg[ ' interval ' ]
377 code = " (%sAR06%04x) " % ( ' 0 ' * 12 ,interval)
378 params = " interval:%s seconds " % (interval)
379 elif cmd == AOCTRL_CMD_BARRIER_SET: # 设置电子围栏消息
380 pass
381 # code = "(%sAR06%04x)"%('0'*12,interval)
382 elif cmd == AOCTRL_CMD_GETLOCATION : # 应答获取终端所在位置消息
383 pass
384 elif cmd == AOCTRL_CMD_LISTEN_START: # 监听命令
385 phone = msg[ ' phone ' ]
386 code = " (%sAP15%s) " % ( ' 0 ' * 12 ,phone) # 让设备主动拨打电话
387 params = " phone:%s " % (phone)
388 elif cmd == AOCTRL_CMD_COMMADDR_SET: # 设置终端IP地址和端口
389 p1,p2,p3,p4 = map(int,msg[ ' ip ' ].strip().split( ' . ' ))
390 port = int(msg[ ' port ' ])
391 code = " (%sAP03%03d%03d%03d%03d%05d) " % ( ' 0 ' * 12 ,p1,p2,p3,p4,port)
392 params = " %s:%s " % (msg[ ' ip ' ],msg[ ' port ' ])
393 elif cmd == AOCTRL_CMD_APN_SET: # 设置APN消息
394 apn = msg[ ' apn ' ]
395 code = " (%sAP04%s) " % ( ' 0 ' * 12 ,apn)
396 params = " APN:%s " % (apn)
397 elif cmd == AOCTRL_CMD_GET_VERSION: # 读取终端版本消息
398 code = " (%sAP07) " % ( ' 0 ' * 12 )
399 elif cmd == AOCTRL_CMD_CLEAR_ALARMS: # 取消所有报警消息
400 code = " (%sAV02) " % ( ' 0 ' * 12 )
401 elif cmd == AOCTRL_CMD_CLEAR_MILES: # 里程清零消息
402 code = " (%sAX01) " % ( ' 0 ' * 12 )
403 elif cmd == AOCTRL_CMD_INIT_MILES: # 里程初始化消息
404 miles = msg[ ' miles ' ]
405 code = " (%sAX03%08x) " % ( ' 0 ' * 12 ,miles) # 为初始化里程值,8位16进制的ASCII值,单位为米
406 params = " miles:%s " % (miles)
407 elif cmd == AOCTRL_CMD_UPDATING: # 启动升级消息
408 ip = msg[ ' ip ' ]
409 port = msg[ ' port ' ]
410 code = " (%sAX02%s:%s) " % ( ' 0 ' * 12 ,ip,port)
411 params = " %s:%s " % (ip,port)
412 else :
413 return ''
414 # save to ctrl log
415 log = aom.gm.AO_CtrlLog()
416 log.ao = aom.ao.dbobj
417 log.cmd = cmd
418 log.time = datetime.datetime.now()
419 text = " %s: %s " % (AOCTRL_CMD_LIST[log.cmd],params)
420 log.comment = text[: 200 ]
421 log.save()
422
423 return code
424
425 # 将d数据写入db中
426 # 根据不同的数据进行hash分派 目前之后gps和告警信息进行分派
427 def save(self,aom,d):
428 log = aom.gm.AO_CtrlLog()
429 log.ao = aom.ao.dbobj
430 log.cmd = d[ ' cmd ' ]
431 log.time = datetime.datetime.now()
432 text = " %s: %s " % (AOCTRL_CMD_LIST[log.cmd],d[ ' params ' ])
433 log.comment = text[: 200 ]
434 log.save()
435 # 以下存储gps数据和设备状态数据
436 gps = d[ ' gps ' ]
437 if gps:
438 # save to db
439 g = aom.gm.AOMData_Gps()
440 g.ao = aom.ao.dbobj
441 g.savetime = gps[ ' time ' ]
442 g.lon = gps[ ' lon ' ]
443 g.lat = gps[ ' lat ' ]
444 g.speed = gps[ ' speed ' ]
445 g.angle = gps[ ' angle ' ]
446 g.power = gps[ ' power ' ]
447 g.acc = gps[ ' acc ' ]
448 g.miles = gps[ ' miles ' ]
449 g.save()
450 # for dispatch
451 t = time.mktime(g.savetime.timetuple())
452 s = { ' type ' :MediaDataType.GPS, ' hwid ' :aom.id,
453 ' lon ' :g.lon, ' lat ' :g.lat,
454 ' speed ' :g.speed, ' angle ' :g.angle,
455 ' satenum ' :0, ' sateused ' :0,
456 ' time ' :t,
457 ' power ' :g.power,
458 ' acc ' :g.acc,
459 ' miles ' :g.miles}
460 aom.dispatch(s) # 分派到 cached server
461
462 if d[ ' msg ' ] == ' BO01 ' : # 报警
463 alarm = aom.gm.AO_AlarmLog()
464 alarm.ao = aom.ao.dbobj
465 alarm.time = datetime.datetime.now()
466 alarm.type = d[ ' alarm ' ]
467 alarm.save()
468 # 分派报警
469 t = time.mktime(alarm.time.timetuple())
470 s = { ' type ' :MediaDataType.ALARM,
471 ' alarm ' :alarm.type,
472 ' comment ' :ALARM_TYPELIST[alarm.type]
473 }
474 aom.dispatch(s) # 分派到 cached server
475
476
2 # 解码器定义
3 # 2011.3.10 scott sw2us.com
4 # from aobject import *
5 import os,os.path,sys,time,datetime,copy
6 # import codec
7
8 # MediaCodecType = MediaDataType
9 class MediaDataType:
10 GPS = 1 << 0
11 AUDIO = 1 << 1
12 VIDEO = 1 << 2
13 IMAGE = 1 << 3
14 TEXT = 1 << 4
15 IODATA = 1 << 5
16 RAWBLOB = 1 << 6
17 COMMAND = 1 << 7 # 通用命令
18 ALARM = 1 << 8 # 报警信息
19 UNDEFINED = 0xff
20
21
22 AOCTRL_CMD_SHAKE_ACK = 1 # 应答握手信号信息
23 AOCTRL_CMD_REG_ACK = 2 # 终端注册响应消息
24 AOCTRL_CMD_SAMPLING_TIMESET = 3 # 等时连续回传设置
25 AOCTRL_CMD_ALARM_ACK = 4 # 应答报警消息
26 AOCTRL_CMD_NAMED = 5 # 一次点名消息
27 AOCTRL_CMD_SPEEDSET = 6 # 设置车速上下限
28 AOCTRL_CMD_POWER_ONOFF = 7 # 电路控制信号
29 AOCTRL_CMD_OIL_ONOFF = 8 # 油路控制信号
30 AOCTRL_CMD_REBOOT = 9 # 控制设备重启消息
31 AOCTRL_CMD_ACC_ON_TIME = 10 # 设置ACC开发送数据间隔
32 AOCTRL_CMD_ACC_OFF_TIME = 11 # 设置ACC关发送数据间隔
33 AOCTRL_CMD_BARRIER_SET = 12 # 设置电子围栏消息
34 AOCTRL_CMD_GETLOCATION = 13 # 应答获取终端所在位置消息
35 AOCTRL_CMD_LISTEN_START = 14 # 监听命令
36 AOCTRL_CMD_COMMADDR_SET = 15 # 设置终端IP地址和端口
37 AOCTRL_CMD_APN_SET = 16 # 设置APN消息
38 AOCTRL_CMD_GET_VERSION = 17 # 读取终端版本消息
39 AOCTRL_CMD_CLEAR_ALARMS = 18 # 取消所有报警消息
40 AOCTRL_CMD_CLEAR_MILES = 19 # 里程清零消息
41 AOCTRL_CMD_INIT_MILES = 20 # 里程初始化消息
42 AOCTRL_CMD_UPDATING = 21 # 启动升级消息
43
44 AOCTRL_CMD_SHACK_REQ = 31 # 握手信号消息
45 AOCTRL_CMD_REG_REQ = 32 # 终端注册信息
46 AOCTRL_CMD_SAMPLING_TIMESET_ACK = 32 # 应答等时连续回传设置
47 AOCTRL_CMD_ALARM_REQ = 33 # 警报消息
48 AOCTRL_CMD_NAMED_ACK = 34 # 应答点名信息
49 AOCTRL_CMD_SIMPLING_GPSDATA = 35 # 等时连续回传消息
50 AOCTRL_CMD_SIMPLING_END = 36 # 连续回传结束消息
51 AOCTRL_CMD_SPEEDSET_ACK = 37 # 应答设置车速上下限
52 AOCTRL_CMD_POWERCTRL_ACK = 38 # 应答电路控制
53 AOCTRL_CMD_OILCTRL_ACK = 39 # 应答油路控制
54 AOCTRL_CMD_REBOOT_ACK = 40 # 应答设备重启消息
55 AOCTRL_CMD_ACCON_TIMESET_ACK = 41 # 应答设置ACC开发送数据间隔
56 AOCTRL_CMD_ACCOFF_TIMESET_ACK = 42 # 应答设置ACC关发送数据间隔
57 AOCTRL_CMD_BARRIER_SET_ACK = 43 # 应答设置电子围栏消息
58 AOCTRL_CMD_GETLOCATION_ACK = 44 # 获取终端所在位置消息
59 AOCTRL_CMD_LISTEN_ACK = 45 # 应答监听命令
60 AOCTRL_CMD_COMMADDR_SET_ACK = 46 # 应答设置终端IP地址和端口
61 AOCTRL_CMD_APN_SET_ACK = 47 # 应答设置APN消息
62 AOCTRL_CMD_GETVERSION_ACK = 48 # 应答读取终端版本消息
63 AOCTRL_CMD_CLEAR_ALARMS_ACK = 49 # 应答取消所有报警消息
64 AOCTRL_CMD_CLEAR_MILES_ACK = 50 # 应答里程清零消息
65 AOCTRL_CMD_UPDATING_ACK = 61 # 应答启动升级消息
66 AOCTRL_CMD_INIT_MILES_ACK = 62 # 应答初始化里程消息
67
68 AOCTRL_CMD_LIST = {
69 AOCTRL_CMD_SHAKE_ACK:u ' 应答握手信号信息 ' ,
70 AOCTRL_CMD_REG_ACK:u ' 终端注册响应消息 ' ,
71 AOCTRL_CMD_SAMPLING_TIMESET:u ' 等时连续回传设置 ' ,
72 AOCTRL_CMD_ALARM_ACK:u ' 应答报警消息 ' ,
73 AOCTRL_CMD_NAMED:u ' 一次点名消息 ' ,
74 AOCTRL_CMD_SPEEDSET:u ' 设置车速上下限 ' ,
75
76 AOCTRL_CMD_POWER_ONOFF:u ' 电路控制信号 ' ,
77 AOCTRL_CMD_OIL_ONOFF:u ' 油路控制信号 ' ,
78 AOCTRL_CMD_REBOOT:u ' 控制设备重启消息 ' ,
79 AOCTRL_CMD_ACC_ON_TIME :u ' 设置ACC开发送数据间隔 ' ,
80 AOCTRL_CMD_ACC_OFF_TIME :u ' 设置ACC关发送数据间隔 ' ,
81 AOCTRL_CMD_BARRIER_SET :u ' 设置电子围栏消息 ' ,
82 AOCTRL_CMD_GETLOCATION :u ' 应答获取终端所在位置消息 ' ,
83 AOCTRL_CMD_LISTEN_START :u ' 监听命令 ' ,
84 AOCTRL_CMD_COMMADDR_SET :u ' 设置终端IP地址和端口 ' ,
85 AOCTRL_CMD_APN_SET :u ' 设置APN消息 ' ,
86 AOCTRL_CMD_GET_VERSION :u ' 读取终端版本消息 ' ,
87 AOCTRL_CMD_CLEAR_ALARMS :u ' 取消所有报警消息 ' ,
88 AOCTRL_CMD_CLEAR_MILES :u ' 里程清零消息 ' ,
89 AOCTRL_CMD_INIT_MILES :u ' 里程初始化消息 ' ,
90 AOCTRL_CMD_UPDATING :u ' 启动升级消息 ' ,
91
92 AOCTRL_CMD_SHACK_REQ :u ' 握手信号消息 ' ,
93 AOCTRL_CMD_REG_REQ :u ' 终端注册信息 ' ,
94 AOCTRL_CMD_SAMPLING_TIMESET_ACK :u ' 应答等时连续回传设置 ' ,
95 AOCTRL_CMD_ALARM_REQ :u ' 警报消息 ' ,
96 AOCTRL_CMD_NAMED_ACK :u ' 应答点名信息 ' ,
97 AOCTRL_CMD_SIMPLING_GPSDATA :u ' 等时连续回传消息 ' ,
98 AOCTRL_CMD_SIMPLING_END :u ' 连续回传结束消息 ' ,
99 AOCTRL_CMD_SPEEDSET_ACK :u ' 应答设置车速上下限 ' ,
100 AOCTRL_CMD_POWERCTRL_ACK :u ' 应答电路控制 ' ,
101 AOCTRL_CMD_OILCTRL_ACK :u ' 应答油路控制 ' ,
102 AOCTRL_CMD_REBOOT_ACK :u ' 应答设备重启消息 ' ,
103 AOCTRL_CMD_ACCON_TIMESET_ACK :u ' 应答设置ACC开发送数据间隔 ' ,
104 AOCTRL_CMD_ACCOFF_TIMESET_ACK:u ' 应答设置ACC关发送数据间隔 ' ,
105 AOCTRL_CMD_BARRIER_SET_ACK :u ' 应答设置电子围栏消息 ' ,
106 AOCTRL_CMD_GETLOCATION_ACK :u ' 获取终端所在位置消息 ' ,
107 AOCTRL_CMD_LISTEN_ACK :u ' 应答监听命令 ' ,
108 AOCTRL_CMD_COMMADDR_SET_ACK:u ' 应答设置终端IP地址和端口 ' ,
109 AOCTRL_CMD_APN_SET_ACK:u ' 应答设置APN消息 ' ,
110 AOCTRL_CMD_GETVERSION_ACK:u ' 应答读取终端版本消息 ' ,
111 AOCTRL_CMD_CLEAR_ALARMS_ACK:u ' 应答取消所有报警消息 ' ,
112 AOCTRL_CMD_CLEAR_MILES_ACK:u ' 应答里程清零消息 ' ,
113 AOCTRL_CMD_UPDATING_ACK :u ' 应答启动升级消息 ' ,
114 AOCTRL_CMD_INIT_MILES_ACK :u ' 应答初始化里程消息 ' ,
115 }
116
117
118 ALARM_TYPELIST = {
119 0:u ' 车辆断电 ' ,
120 1 :u ' 电子围栏入界报警 ' ,
121 2 :u ' 车辆劫警(SOS求助) ' ,
122 3 :u ' 车辆防盗器警报 ' ,
123 4 :u ' 车辆低速报警 ' ,
124 5 :u ' 车辆超速报警 ' ,
125 6 :u ' 电子围栏出界报警 '
126 }
127
128 def parseTime(dmy,hms):
129 d,mon,y = map(int, map(float,[dmy[: 2 ],dmy[ 2 : 4 ],dmy[ 4 :]]) )
130 h,min,s = map(int, map(float,[hms[: 2 ],hms[ 2 : 4 ],hms[ 4 :]]) )
131 # print d,mon,y,h,min,s
132 return time.mktime(( 2000 + y,mon,d,h,min,s,0,0,0))
133
134 # 简单的模拟gps接收解码器
135 # gps接收程序解析之后连接本地的TcpService端口,并传送过来
136 # 只有简单的gps数据,模拟端口打开
137 class MediaCodec_KS108:
138 def __init__ (self):
139 self.buf = ''
140 self.conn = None
141
142
143 def parse_gps(self,msg,s):
144 # gps 数据长度62个字符
145 msg[ ' gps ' ] = None # AOCTRL_CMD_SIMPLING_GPSDATA
146 if len(s) != 62 :
147 return
148 try :
149 yy = int(s[: 2 ])
150 mm = int(s[ 2 : 4 ])
151 dd = int(s[ 4 : 6 ])
152 av = s[ 6 ] # 是否有效
153 lat = int(s[ 7 : 9 ]) + float( s[ 9 : 16 ]) / 60 .
154 ns = s[ 16 ] # 维度标志 N/S
155 lon = int(s[ 17 : 20 ]) + float( s[ 20 : 27 ]) / 60 .
156 ew = s[ 27 ]
157 speed = float(s[ 28 : 33 ])
158 HH = int(s[ 33 : 35 ])
159 MM = int(s[ 35 : 37 ])
160 SS = int(s[ 37 : 39 ])
161 angle = float(s[ 39 : 45 ])
162 power = int(s[ 45 ])
163 acc = int(s[ 46 ])
164 mileflag = s[ 53 ]
165 miles = int(s[ 54 :], 16 )
166 gpstime = datetime.datetime( 2000 + yy,mm,dd,HH,MM,SS)
167 # GMT+8
168 gpstime = gpstime + datetime.timedelta(hours = 8 )
169 msg[ ' gps ' ] = {
170 ' time ' :gpstime,
171 ' lon ' :lon,
172 ' lat ' :lat,
173 ' speed ' :speed,
174 ' angle ' :angle,
175 ' power ' :power,
176 ' acc ' :acc,
177 ' miles ' :miles
178 }
179 except :
180 traceback.print_exc()
181 msg[ ' gps ' ] = None
182
183
184
185 def __parseMessage (self,s):
186 # @return: [object,] or [None,]
187 msg = { ' mid ' :self.conn.mid, ' cmd ' :0, ' seq ' : '' , ' params ' : '' , ' gps ' :None} # mid - 模块硬件编号
188
189
190 cmd = s[ 12 : 16 ]
191 msg[ ' msg ' ] = cmd
192 msg[ ' seq ' ] = s[: 12 ]
193
194 if cmd == ' BP00 ' and len(s) == 24 :
195 msg[ ' cmd ' ] = AOCTRL_CMD_SHACK_REQ
196 elif cmd == ' BP05 ' : # 注册信息
197 msg[ ' cmd ' ] = AOCTRL_CMD_REG_REQ
198 msg[ ' mid ' ] = s[ 16 : 31 ] #
199 self.conn.mid = msg[ ' mid ' ] # 设备连接上来这个是第一个消息包
200 # 将gps独立出单独的包
201 self.parse_gps(msg,s[ 31 :])
202 elif cmd == ' BS08 ' and len(s) == 24 :
203 msg[ ' cmd ' ] = AOCTRL_CMD_SAMPLING_TIMESET_ACK # 应答等时连续回传设置
204 msg[ ' samplewait ' ] = int(s[ 16 : 20 ], 16 )
205 hours = int(s[ 20 : 22 ], 16 )
206 minutes = int(s[ 22 : 24 ], 16 )
207 msg[ ' samplecycle ' ] = hours * 3600 + minutes * 60
208 msg[ ' params ' ] = " samplewait:%s,samplecycle:%s " % (msg[ ' samplewait ' ],msg[ ' samplecycle ' ])
209
210 elif cmd == ' BO01 ' :
211 msg[ ' cmd ' ] = AOCTRL_CMD_ALARM_REQ # 警报消息
212 msg[ ' alarm ' ] = int(s[ 16 ]) # 报警类型
213 msg[ ' params ' ] = ALARM_TYPELIST[msg[ ' alarm ' ]]
214 self.parse_gps(msg,s[ 17 :])
215 elif cmd == ' BP04 ' : # 点名应答
216 msg[ ' cmd ' ] = AOCTRL_CMD_NAMED_ACK # 应答点名信息
217 self.parse_gps(msg,s[ 16 :])
218 elif cmd == ' BR00 ' :
219 msg[ ' cmd ' ] = AOCTRL_CMD_SIMPLING_GPSDATA
220 self.parse_gps(msg,s[ 16 :])
221 elif cmd == ' BR02 ' :
222 msg[ ' cmd ' ] = AOCTRL_CMD_SIMPLING_END
223 self.parse_gps(msg,s[ 16 :])
224 elif cmd == ' BP12 ' and len(s) == 24 :
225 msg[ ' cmd ' ] = AOCTRL_CMD_SPEEDSET_ACK
226 msg[ ' hispeed ' ] = int(s[ 17 : 20 ])
227 msg[ ' lospeed ' ] = int(s[ 21 : 24 ]) # 低速限制
228 msg[ ' params ' ] = " hispeed:%s,lospeed:%s " % ( msg[ ' hispeed ' ],msg[ ' lospeed ' ] )
229
230 elif cmd == ' BV00 ' and len(s) == 17 :
231 msg[ ' cmd ' ] = AOCTRL_CMD_POWERCTRL_ACK # #应答电路控制
232 msg[ ' onoff ' ] = int(len(s[ 17 ]))
233 if msg[ ' onoff ' ] == 0:
234 msg[ ' params ' ] = u ' 已关电路 '
235 else :
236 msg[ ' params ' ] = ' 已开电路 '
237
238 elif cmd == ' BV01 ' and len(s) == 17 : # 应答油路控制
239 msg[ ' cmd ' ] = AOCTRL_CMD_OILCTRL_ACK
240 msg[ ' onoff ' ] = int(len(s[ 17 ]))
241 if msg[ ' onoff ' ] == 0:
242 msg[ ' params ' ] = u ' 已关油路 '
243 else :
244 msg[ ' params ' ] = ' 已开油路 '
245 elif cmd == ' BT00 ' :
246 msg[ ' cmd ' ] = AOCTRL_CMD_REBOOT_ACK
247 elif cmd == ' BR05 ' :
248 msg[ ' cmd ' ] = AOCTRL_CMD_ACCON_TIMESET_ACK # 应答设置ACC开发送数据间隔
249 elif cmd == ' BR06 ' :
250 msg[ ' cmd ' ] = AOCTRL_CMD_ACCOFF_TIMESET_ACK # 应答设置ACC关发送数据间隔
251 elif cmd == ' BU00 ' :
252 msg[ ' cmd ' ] = AOCTRL_CMD_BARRIER_SET_ACK
253 elif cmd == ' BR03 ' : # 获取终端所在位置消息
254 msg[ ' cmd ' ] = AOCTRL_CMD_GETLOCATION_ACK
255 self.parse_gps(msg,s[ 16 :])
256 elif cmd == ' BS20 ' :
257 msg[ ' cmd ' ] = AOCTRL_CMD_LISTEN_ACK # 应答监听命令
258 elif cmd == ' BP02 ' and len(s) == 16 :
259 msg[ ' cmd ' ] = AOCTRL_CMD_COMMADDR_SET_ACK # 应答设置终端IP地址和端口
260 elif cmd == ' BP03 ' :
261 msg[ ' cmd ' ] = AOCTRL_CMD_APN_SET_ACK # 应答设置APN消息
262 elif cmd == ' BP01 ' :
263 msg[ ' cmd ' ] = AOCTRL_CMD_GETVERSION_ACK
264 msg[ ' version ' ] = s[ 16 :]
265 elif cmd == ' BS21 ' :
266 msg[ ' cmd ' ] = AOCTRL_CMD_CLEAR_ALARMS_ACK # 应答取消所有报警消息
267 elif cmd == ' BS04 ' :
268 msg[ ' cmd ' ] = AOCTRL_CMD_CLEAR_MILES_ACK # 应答里程清零消息
269 elif cmd == ' BS05 ' :
270 msg[ ' cmd ' ] = AOCTRL_CMD_UPDATING_ACK # = 61 #应答启动升级消息
271 elif cmd == ' BS06 ' :
272 msg[ ' cmd ' ] = AOCTRL_CMD_INIT_MILES_ACK # 应答初始化里程消息
273 else :
274 msg = None
275 # print 'decode:',msg
276 return msg
277
278 # parse - codec 必须实现
279 # 对于某些设备的请求消息,这里必须进行默认的应答
280 # 如果出现大量数据包的要发送回设备的情况,考虑建立队列,用工作线程
281 # 慢慢发送,因为parse还在socket接收线程中
282 def parse(self,aom,d):
283 cmd = d[ ' cmd ' ]
284 s = ''
285 if cmd == AOCTRL_CMD_SHACK_REQ: # 握手回复
286 s = self.command(aom,{ ' cmd ' :AOCTRL_CMD_SHAKE_ACK, ' seq ' :d[ ' seq ' ]})
287 if cmd == AOCTRL_CMD_REG_REQ: # 设备注册
288 s = self.command(aom,{ ' cmd ' :AOCTRL_CMD_REG_ACK, ' seq ' :d[ ' seq ' ]})
289 if cmd == AOCTRL_CMD_ALARM_REQ:
290 s = self.command(aom,{ ' cmd ' :AOCTRL_CMD_ALARM_ACK, ' seq ' :d[ ' seq ' ], ' alarm ' :d[ ' alarm ' ]})
291 if s:
292 print ' send back to device: ' ,s
293 self.conn.handle.sendall(s)
294
295 def decode(self,s,conn):
296 # @return: packets,retry
297 # 解码出多个消息包,并返回是否
298 self.conn = conn
299 msglist = []
300 retry = True
301 print s
302 self.buf += s
303 while True:
304 p1 = self.buf.find( ' ( ' )
305 if p1 == - 1 :
306 self.buf = ''
307 break
308 self.buf = self.buf[p1:]
309
310 p2 = self.buf.find( ' ) ' )
311 if p2 == - 1 :
312 if len(self.buf) > 1024 :
313 return (),False # please break socket connection
314 break # push in more data
315 # find tail
316 msg = None
317 try :
318 msg = self. __parseMessage (self.buf[ 1 :p2])
319 except :
320 traceback.print_exc()
321 msg = None
322 if msg:
323 msglist.append(msg)
324 self.buf = self.buf[p2 + 1 :]
325 return msglist,retry
326
327
328
329 # 执行设备命令
330 def command(self,aom,msg):
331 # cmd - object (json decoded)
332 # @return: 返回命令控制消息
333 cmd = msg[ ' cmd ' ]
334 code = ''
335 params = ''
336 if not msg.has_key( ' seq ' ):
337 msg[ ' seq ' ] = ' 0 ' * 12
338
339 if cmd == AOCTRL_CMD_REG_ACK: # 注册响应
340 code = " (%sAP05) " % (msg[ ' seq ' ])
341
342 elif cmd == AOCTRL_CMD_SAMPLING_TIMESET: # 等时连续回传设置
343 wait = msg[ ' samplewait ' ] # 时间间隔
344 cycle = msg[ ' samplecycle ' ] # 总时长
345 hh = int(cycle / 3600 )
346 mm = (cycle - hh * 3600 ) / 60
347 mm = int(mm)
348 code = " (%sAR00%04x%02x%02x) " % ( ' 0 ' * 12 ,wait,hh,mm)
349 params = " wait:%s seconds, cycle: %s hours,%s mimuts " % (wait,hh,mm)
350 elif cmd == AOCTRL_CMD_ALARM_ACK: # 应答报警消息
351 alarm = msg[ ' alarm ' ]
352 code = " (%sAS01%d) " % (msg[ ' seq ' ],alarm)
353 params = ALARM_TYPELIST[alarm]
354 elif cmd == AOCTRL_CMD_NAMED : # 一次点名消息
355 code = " (%sAP00) " % ( ' 0 ' * 12 )
356 elif cmd == AOCTRL_CMD_SPEEDSET: # 设置车速上下限
357 hi = msg[ ' hispeed ' ]
358 lo = msg[ ' lospeed ' ]
359 code = " (%sAP12H%03dL%03d) " % ( ' 0 ' * 12 ,hi,lo)
360 params = " hi:%s,lo:%s " % (hi,lo)
361 elif cmd == AOCTRL_CMD_POWER_ONOFF: # 电路控制信号
362 onoff = msg[ ' onoff ' ]
363 code = " (%sAV00%d) " % ( ' 0 ' * 12 ,onoff) # “1”表示开电路,“0”表示关电路
364 params = " onoff:%s " % (onoff)
365 elif cmd == AOCTRL_CMD_OIL_ONOFF: # 油路控制信号
366 onoff = msg[ ' onoff ' ]
367 code = " (%sAV01%d) " % ( ' 0 ' * 12 ,onoff) # “1”表示开油路,“0”表示关油路
368 params = " onoff:%s " % (onoff)
369 elif cmd == AOCTRL_CMD_REBOOT: # 控制设备重启消息
370 code = " (%sAT00) " % ( ' 0 ' * 12 )
371 elif cmd == AOCTRL_CMD_ACC_ON_TIME: # 设置ACC开发送数据间隔
372 interval = msg[ ' interval ' ]
373 code = " (%sAR05%04x) " % ( ' 0 ' * 12 ,interval)
374 params = " interval:%s seconds " % (interval)
375 elif cmd == AOCTRL_CMD_ACC_OFF_TIME: # 设置ACC关发送数据间隔
376 interval = msg[ ' interval ' ]
377 code = " (%sAR06%04x) " % ( ' 0 ' * 12 ,interval)
378 params = " interval:%s seconds " % (interval)
379 elif cmd == AOCTRL_CMD_BARRIER_SET: # 设置电子围栏消息
380 pass
381 # code = "(%sAR06%04x)"%('0'*12,interval)
382 elif cmd == AOCTRL_CMD_GETLOCATION : # 应答获取终端所在位置消息
383 pass
384 elif cmd == AOCTRL_CMD_LISTEN_START: # 监听命令
385 phone = msg[ ' phone ' ]
386 code = " (%sAP15%s) " % ( ' 0 ' * 12 ,phone) # 让设备主动拨打电话
387 params = " phone:%s " % (phone)
388 elif cmd == AOCTRL_CMD_COMMADDR_SET: # 设置终端IP地址和端口
389 p1,p2,p3,p4 = map(int,msg[ ' ip ' ].strip().split( ' . ' ))
390 port = int(msg[ ' port ' ])
391 code = " (%sAP03%03d%03d%03d%03d%05d) " % ( ' 0 ' * 12 ,p1,p2,p3,p4,port)
392 params = " %s:%s " % (msg[ ' ip ' ],msg[ ' port ' ])
393 elif cmd == AOCTRL_CMD_APN_SET: # 设置APN消息
394 apn = msg[ ' apn ' ]
395 code = " (%sAP04%s) " % ( ' 0 ' * 12 ,apn)
396 params = " APN:%s " % (apn)
397 elif cmd == AOCTRL_CMD_GET_VERSION: # 读取终端版本消息
398 code = " (%sAP07) " % ( ' 0 ' * 12 )
399 elif cmd == AOCTRL_CMD_CLEAR_ALARMS: # 取消所有报警消息
400 code = " (%sAV02) " % ( ' 0 ' * 12 )
401 elif cmd == AOCTRL_CMD_CLEAR_MILES: # 里程清零消息
402 code = " (%sAX01) " % ( ' 0 ' * 12 )
403 elif cmd == AOCTRL_CMD_INIT_MILES: # 里程初始化消息
404 miles = msg[ ' miles ' ]
405 code = " (%sAX03%08x) " % ( ' 0 ' * 12 ,miles) # 为初始化里程值,8位16进制的ASCII值,单位为米
406 params = " miles:%s " % (miles)
407 elif cmd == AOCTRL_CMD_UPDATING: # 启动升级消息
408 ip = msg[ ' ip ' ]
409 port = msg[ ' port ' ]
410 code = " (%sAX02%s:%s) " % ( ' 0 ' * 12 ,ip,port)
411 params = " %s:%s " % (ip,port)
412 else :
413 return ''
414 # save to ctrl log
415 log = aom.gm.AO_CtrlLog()
416 log.ao = aom.ao.dbobj
417 log.cmd = cmd
418 log.time = datetime.datetime.now()
419 text = " %s: %s " % (AOCTRL_CMD_LIST[log.cmd],params)
420 log.comment = text[: 200 ]
421 log.save()
422
423 return code
424
425 # 将d数据写入db中
426 # 根据不同的数据进行hash分派 目前之后gps和告警信息进行分派
427 def save(self,aom,d):
428 log = aom.gm.AO_CtrlLog()
429 log.ao = aom.ao.dbobj
430 log.cmd = d[ ' cmd ' ]
431 log.time = datetime.datetime.now()
432 text = " %s: %s " % (AOCTRL_CMD_LIST[log.cmd],d[ ' params ' ])
433 log.comment = text[: 200 ]
434 log.save()
435 # 以下存储gps数据和设备状态数据
436 gps = d[ ' gps ' ]
437 if gps:
438 # save to db
439 g = aom.gm.AOMData_Gps()
440 g.ao = aom.ao.dbobj
441 g.savetime = gps[ ' time ' ]
442 g.lon = gps[ ' lon ' ]
443 g.lat = gps[ ' lat ' ]
444 g.speed = gps[ ' speed ' ]
445 g.angle = gps[ ' angle ' ]
446 g.power = gps[ ' power ' ]
447 g.acc = gps[ ' acc ' ]
448 g.miles = gps[ ' miles ' ]
449 g.save()
450 # for dispatch
451 t = time.mktime(g.savetime.timetuple())
452 s = { ' type ' :MediaDataType.GPS, ' hwid ' :aom.id,
453 ' lon ' :g.lon, ' lat ' :g.lat,
454 ' speed ' :g.speed, ' angle ' :g.angle,
455 ' satenum ' :0, ' sateused ' :0,
456 ' time ' :t,
457 ' power ' :g.power,
458 ' acc ' :g.acc,
459 ' miles ' :g.miles}
460 aom.dispatch(s) # 分派到 cached server
461
462 if d[ ' msg ' ] == ' BO01 ' : # 报警
463 alarm = aom.gm.AO_AlarmLog()
464 alarm.ao = aom.ao.dbobj
465 alarm.time = datetime.datetime.now()
466 alarm.type = d[ ' alarm ' ]
467 alarm.save()
468 # 分派报警
469 t = time.mktime(alarm.time.timetuple())
470 s = { ' type ' :MediaDataType.ALARM,
471 ' alarm ' :alarm.type,
472 ' comment ' :ALARM_TYPELIST[alarm.type]
473 }
474 aom.dispatch(s) # 分派到 cached server
475
476