hainiaov2.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. # -*- coding: utf-8 -*-
  2. #!/usr/bin/env python
  3. import time
  4. from apilib.utils_datetime import timestamp_to_dt
  5. from apps.web.constant import Const, DeviceCmdCode, MQTT_TIMEOUT
  6. from apps.web.core.adapter.base import SmartBox, fill_2_hexByte
  7. from apps.web.core.exceptions import ServiceException
  8. from apps.web.core.networking import MessageSender
  9. from apps.web.device.models import Device
  10. class ChargingHaiNiaoV2Box(SmartBox):
  11. def __init__(self, device):
  12. super(ChargingHaiNiaoV2Box, self).__init__(device)
  13. # 设备上报事件
  14. def analyze_event_data(self, data):
  15. cmdCode = data[6:8]
  16. if cmdCode == '86':
  17. portData = data[8:12]
  18. if portData == '0001':
  19. port = '1'
  20. reason = data[16:18]
  21. leftTime = str(int(data[36:44], 16) / 60)
  22. elif portData == '0002':
  23. port = '2'
  24. reason = data[18:20]
  25. leftTime = str(int(data[44:52], 16) / 60)
  26. elif portData == '0004':
  27. port = '3'
  28. reason = data[20:22]
  29. leftTime = str(int(data[52:60], 16) / 60)
  30. elif portData == '0008':
  31. port = '4'
  32. reason = data[22:24]
  33. leftTime = str(int(data[60:68], 16) / 60)
  34. elif portData == '0010':
  35. port = '5'
  36. reason = data[24:26]
  37. leftTime = str(int(data[68:76], 16) / 60)
  38. elif portData == '0020':
  39. port = '6'
  40. reason = data[26:28]
  41. leftTime = str(int(data[76:84], 16) / 60)
  42. elif portData == '0040':
  43. port = '7'
  44. reason = data[28:30]
  45. leftTime = str(int(data[84:92], 16) / 60)
  46. elif portData == '0080':
  47. port = '8'
  48. reason = data[30:32]
  49. leftTime = str(int(data[92:100], 16) / 60)
  50. elif portData == '0100':
  51. port = '9'
  52. reason = data[32:34]
  53. leftTime = str(int(data[100:108], 16) / 60)
  54. elif portData == '0200':
  55. port = '10'
  56. reason = data[34:36]
  57. leftTime = str(int(data[108:116], 16) / 60)
  58. else:
  59. port = '-1'
  60. reason = u'unknown'
  61. leftTime = '0'
  62. if reason == '00':
  63. desc = u'购买的充电时间用完了。'
  64. elif reason in ['04', '06', '07']:
  65. desc = u'管理人员可能远程断电了,或是插头被拔掉, 建议您根据已经充电的时间评估是否需要到现场换到其他端口充电。'
  66. elif reason in ['02', '03']:
  67. desc = u'警告!您的电池超功率,已经停止充电,为了公共安全,不建议您在该充电桩充电!提醒您,为了安全大功率的电池不要放入楼道、室内等位置充电哦。'
  68. elif reason == '05':
  69. desc = u'空载。'
  70. elif reason == '01':
  71. desc = u'电池没有充满!原因未知。'
  72. else:
  73. desc = u'成功!'
  74. return {'status': Const.DEV_WORK_STATUS_IDLE, 'cmdCode': cmdCode, 'port': str(int(port) - 1), 'leftTime': leftTime, 'reason': desc}
  75. def translate_funcode(self,funCode):
  76. funCodeDict = {
  77. '01':u'获取端口状态',
  78. '02':u'获取设备参数',
  79. '03':u'操作端口',
  80. '04':u'设置参数',
  81. }
  82. return funCodeDict.get(funCode,'')
  83. def translate_event_cmdcode(self,cmdCode):
  84. cmdDict = {
  85. '86':u'设备上报信息',
  86. }
  87. return cmdDict.get(cmdCode,'')
  88. def get_port_status_from_dev(self): # 获取端口状态
  89. devInfo = MessageSender.send(self.device, self.make_random_cmdcode(),
  90. {'IMEI': self._device['devNo'], "funCode": '01', 'data': '00'})
  91. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  92. if devInfo['rst'] == -1:
  93. raise ServiceException(
  94. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  95. elif devInfo['rst'] == 1:
  96. raise ServiceException(
  97. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  98. port2Status = int(devInfo['data'][8:10], 16) # 通道9~10的状态
  99. port8Status = int(devInfo['data'][10:12], 16) # 通道1~8的状态
  100. portStatusInfo = devInfo['data'][112:132]
  101. bin2Str = str(bin(port2Status))[2:]
  102. while len(bin2Str) < 8:
  103. bin2Str = '0' + bin2Str
  104. bin8Str = str(bin(port8Status))[2:]
  105. while len(bin8Str) < 8:
  106. bin8Str = '0' + bin8Str
  107. result = {}
  108. if bin2Str[6] == '1':
  109. result[str(10 - 1)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  110. elif bin2Str[6] == '0':
  111. result[str(10 - 1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  112. if bin2Str[7] == '1':
  113. result[str(9 - 1)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  114. elif bin2Str[7] == '0':
  115. result[str(9 - 1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  116. if bin8Str[0] == '1':
  117. result[str(8 - 1)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  118. elif bin8Str[0] == '0':
  119. result[str(8 - 1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  120. if bin8Str[1] == '1':
  121. result[str(7 - 1)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  122. elif bin8Str[1] == '0':
  123. result[str(7 - 1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  124. if bin8Str[2] == '1':
  125. result[str(6 - 1)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  126. elif bin8Str[2] == '0':
  127. result[str(6 - 1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  128. if bin8Str[3] == '1':
  129. result[str(5 - 1)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  130. elif bin8Str[3] == '0':
  131. result[str(5 - 1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  132. if bin8Str[4] == '1':
  133. result[str(4 - 1)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  134. elif bin8Str[4] == '0':
  135. result[str(4 - 1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  136. if bin8Str[5] == '1':
  137. result[str(3 - 1)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  138. elif bin8Str[5] == '0':
  139. result[str(3 - 1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  140. if bin8Str[6] == '1':
  141. result[str(2 - 1)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  142. elif bin8Str[6] == '0':
  143. result[str(2 - 1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  144. if bin8Str[7] == '1':
  145. result[str(1 - 1)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  146. elif bin8Str[7] == '0':
  147. result[str(1 - 1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  148. ii = 0
  149. while ii < 10:
  150. statusTemp = portStatusInfo[ii * 2:ii * 2 + 2]
  151. if statusTemp != '00':
  152. result[str(ii)] = {'status': Const.DEV_WORK_STATUS_FAULT}
  153. ii += 1
  154. allPorts, usedPorts, usePorts = self.get_port_static_info(result)
  155. Device.update_dev_control_cache(self._device['devNo'],
  156. {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts})
  157. # 这里存在多线程更新缓存的场景,可能性比较低,但是必须先取出来,然后逐个更新状态,然后再记录缓存
  158. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  159. for strPort, info in result.items():
  160. if ctrInfo.has_key(strPort):
  161. ctrInfo[strPort].update({'status': info['status']})
  162. else:
  163. ctrInfo[strPort] = info
  164. Device.update_dev_control_cache(self._device['devNo'], ctrInfo)
  165. return result
  166. def get_port_status(self, force = False): # 获取端口状态(缓存)
  167. if force:
  168. return self.get_port_status_from_dev()
  169. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  170. statusDict = {}
  171. allPorts = ctrInfo.get('allPorts',10)
  172. for ii in range(allPorts):
  173. tempDict = ctrInfo.get(str(ii),{})
  174. if tempDict.has_key('status'):
  175. statusDict[str(ii)] = {'status':tempDict.get('status')}
  176. elif tempDict.has_key('isStart'):
  177. if tempDict['isStart']:
  178. statusDict[str(ii)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  179. else:
  180. statusDict[str(ii)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  181. else:
  182. statusDict[str(ii)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  183. allPorts,usedPorts,usePorts = self.get_port_static_info(statusDict)
  184. Device.update_dev_control_cache(self._device['devNo'], {'allPorts':allPorts,'usedPorts':usedPorts,'usePorts':usePorts})
  185. return statusDict
  186. def get_dev_setting(self): # 获取设备参数
  187. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  188. {'IMEI': self._device['devNo'], "funCode": '02', 'data': '00'})
  189. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  190. if devInfo['rst'] == -1:
  191. raise ServiceException(
  192. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  193. elif devInfo['rst'] == 1:
  194. raise ServiceException(
  195. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  196. confData = devInfo['data'][8::]
  197. ewmTime = int(confData[0:4], 16)
  198. coinTime = int(confData[4:8], 16)
  199. icTime = int(confData[8:12], 16)
  200. oneMaxElec = float(int(confData[12:14], 16)) / 10
  201. oneStdElec = float(int(confData[14:16], 16)) / 10
  202. portParam = int(confData[16:18], 16)
  203. allMaxElec = float(int(confData[18:20], 16)) / 10
  204. lessElecShutDown = float(int(confData[20:22], 16)) / 10
  205. putCoins = int(confData[22:30], 16)
  206. putEwms = int(confData[30:38], 16)
  207. putCards = int(confData[38:46], 16)
  208. versionNum = int(confData[46:50], 16)
  209. binStr = str(bin(portParam))[2:]
  210. while len(binStr) < 8:
  211. binStr = '0' + binStr
  212. standbyDisplay = '0'
  213. shutDownMem = False
  214. freeMode = False
  215. fullClose = False
  216. defaultSetting = False
  217. allTimeClear = False
  218. funcSetting = '0'
  219. noElecShutDown = False
  220. if int(binStr[0]):
  221. standbyDisplay = '1'
  222. if int(binStr[1]):
  223. shutDownMem = True
  224. if int(binStr[2]):
  225. freeMode = True
  226. if int(binStr[3]):
  227. fullClose = True
  228. if int(binStr[4]):
  229. defaultSetting = True
  230. if int(binStr[5]):
  231. allTimeClear = True
  232. if int(binStr[6]):
  233. funcSetting = '1'
  234. if int(binStr[7]):
  235. noElecShutDown = True
  236. return {
  237. 'ewmTime': ewmTime,
  238. 'coinTime': coinTime,
  239. 'icTime': icTime,
  240. 'oneMaxElec': oneMaxElec,
  241. 'oneStdElec': oneStdElec,
  242. 'allMaxElec': allMaxElec,
  243. 'lessElecShutDown': lessElecShutDown,
  244. 'putCoins': putCoins,
  245. 'putEwms': putEwms,
  246. 'putCards': putCards,
  247. 'versionNum': versionNum,
  248. 'standbyDisplay': standbyDisplay,
  249. 'shutDownMem': shutDownMem,
  250. 'freeMode': freeMode,
  251. 'fullClose': fullClose,
  252. 'defaultSetting': defaultSetting,
  253. 'allTimeClear': allTimeClear,
  254. 'funcSetting': funcSetting,
  255. 'noElecShutDown': noElecShutDown,
  256. }
  257. def test(self, coins):
  258. port = hex(5)
  259. hexPort = fill_2_hexByte(port, 4)
  260. if hexPort == '0009':
  261. hexPort = '0100'
  262. elif hexPort == '000A':
  263. hexPort = '0200'
  264. elif hexPort == '0003':
  265. hexPort = '0004'
  266. elif hexPort == '0004':
  267. hexPort = '0008'
  268. elif hexPort == '0005':
  269. hexPort = '0010'
  270. elif hexPort == '0006':
  271. hexPort = '0020'
  272. elif hexPort == '0007':
  273. hexPort = '0040'
  274. elif hexPort == '0008':
  275. hexPort = '0080'
  276. hexTime = fill_2_hexByte(hex(1))
  277. data = hexPort + '01' + hexTime
  278. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {
  279. 'IMEI': self._device['devNo'], "funCode": '03', 'data': data
  280. })
  281. return devInfo
  282. def start_device(self, package, openId, attachParas):
  283. if attachParas is None:
  284. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路、电池类型信息'})
  285. if not attachParas.has_key('chargeIndex'):
  286. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路'})
  287. port = hex(int(attachParas['chargeIndex']) + 1)
  288. hexPort = fill_2_hexByte(port, 4)
  289. if hexPort == '0009':
  290. hexPort = '0100'
  291. elif hexPort == '000A':
  292. hexPort = '0200'
  293. elif hexPort == '0003':
  294. hexPort = '0004'
  295. elif hexPort == '0004':
  296. hexPort = '0008'
  297. elif hexPort == '0005':
  298. hexPort = '0010'
  299. elif hexPort == '0006':
  300. hexPort = '0020'
  301. elif hexPort == '0007':
  302. hexPort = '0040'
  303. elif hexPort == '0008':
  304. hexPort = '0080'
  305. needTime = int(package['time'])
  306. needCoins = int(package['coins'])
  307. unit = package.get('unit', u'分钟')
  308. if unit == u'小时':
  309. needTime = int(package['time']) * 60
  310. elif unit == u'天':
  311. needTime = int(package['time']) * 1440
  312. hexTime = fill_2_hexByte(hex(needTime))
  313. data = hexPort + '01' + hexTime
  314. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = {
  315. 'IMEI': self._device['devNo'],
  316. "funCode": '03',
  317. 'data': data
  318. }, timeout = MQTT_TIMEOUT.START_DEVICE)
  319. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  320. if devInfo['rst'] == -1:
  321. raise ServiceException(
  322. {'result': 2, 'description': u'充电桩正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
  323. elif devInfo['rst'] == 1:
  324. raise ServiceException({'result': 2, 'description': u'充电桩正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'})
  325. data = devInfo['data'][8::]
  326. usePort = int(attachParas['chargeIndex'])
  327. result = data[4:8]
  328. if result == '0000': # 代表失败
  329. newValue = {str(usePort): {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'充电站故障'}}
  330. Device.update_dev_control_cache(self._device['devNo'], newValue)
  331. raise ServiceException({'result': 2, 'description': u'充电站故障'})
  332. start_timestamp = int(time.time())
  333. finishedTime = start_timestamp + needTime * 60
  334. portDict = {
  335. 'startTime': timestamp_to_dt(start_timestamp).strftime('%Y-%m-%d %H:%M:%S'),
  336. 'status': Const.DEV_WORK_STATUS_WORKING,
  337. 'finishedTime': finishedTime,
  338. 'coins': float(needCoins),
  339. 'isStart': True,
  340. 'needTime': needTime,
  341. 'openId': openId,
  342. 'refunded': False,
  343. 'vCardId': self._vcard_id
  344. }
  345. Device.update_dev_control_cache(self._device['devNo'], {str(usePort): portDict})
  346. return devInfo
  347. # 设置充电时间
  348. def set_charging_time_setting(self, setConf):
  349. def send_msg_2_dev(data):
  350. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  351. {'IMEI': self._device['devNo'], "funCode": '04', 'data': data})
  352. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  353. if devInfo['rst'] == -1:
  354. raise ServiceException(
  355. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  356. elif devInfo['rst'] == 1:
  357. raise ServiceException(
  358. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  359. if devInfo['data'][14:16] == '01':
  360. raise ServiceException(
  361. {'result': 2, 'description': u'设置失败'})
  362. if setConf['ewmTime']:
  363. data = '01' + fill_2_hexByte(hex(int(setConf['ewmTime'])), 4)
  364. send_msg_2_dev(data)
  365. if setConf['coinTime']:
  366. data = '02' + fill_2_hexByte(hex(int(setConf['coinTime'])), 4)
  367. send_msg_2_dev(data)
  368. if setConf['icTime']:
  369. data = '03' + fill_2_hexByte(hex(int(setConf['icTime'])), 4)
  370. send_msg_2_dev(data)
  371. # 设置充电电流
  372. def set_charging_elec_setting(self, setConf):
  373. def send_msg_2_dev(data):
  374. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  375. {'IMEI': self._device['devNo'], "funCode": '04', 'data': data})
  376. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  377. if devInfo['rst'] == -1:
  378. raise ServiceException(
  379. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  380. elif devInfo['rst'] == 1:
  381. raise ServiceException(
  382. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  383. if devInfo['data'][14:16] == '01':
  384. raise ServiceException(
  385. {'result': 2, 'description': u'设置失败'})
  386. if setConf['oneMaxElec']:
  387. data = '04' + fill_2_hexByte(hex(int(setConf['oneMaxElec'])), 4)
  388. send_msg_2_dev(data)
  389. if setConf['oneStdElec']:
  390. data = '05' + fill_2_hexByte(hex(int(setConf['oneStdElec'])), 4)
  391. send_msg_2_dev(data)
  392. if setConf['allMaxElec']:
  393. data = '0E' + fill_2_hexByte(hex(int(setConf['allMaxElec'])), 4)
  394. send_msg_2_dev(data)
  395. if setConf['lessElecShutDown']:
  396. data = '0F' + fill_2_hexByte(hex(int(setConf['lessElecShutDown'])), 4)
  397. send_msg_2_dev(data)
  398. # 设置功能参数
  399. def set_func_param_setting(self, setConf):
  400. def send_msg_2_dev(data):
  401. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  402. {'IMEI': self._device['devNo'], "funCode": '04', 'data': data})
  403. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  404. if devInfo['rst'] == -1:
  405. raise ServiceException(
  406. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  407. elif devInfo['rst'] == 1:
  408. raise ServiceException(
  409. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  410. if devInfo['data'][14:16] == '01':
  411. raise ServiceException(
  412. {'result': 2, 'description': u'设置失败'})
  413. if setConf['funcSetting']:
  414. data = '07' + fill_2_hexByte(hex(int(setConf['funcSetting'])), 4)
  415. send_msg_2_dev(data)
  416. if setConf['standbyDisplay']:
  417. data = '0D' + fill_2_hexByte(hex(int(setConf['standbyDisplay'])), 4)
  418. send_msg_2_dev(data)
  419. # 设置空载自停
  420. def set_no_elec_shut_down(self, noElecShutDown=False):
  421. data = '06' + ('0001' if noElecShutDown else '0000')
  422. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  423. {'IMEI': self._device['devNo'], "funCode": '04', 'data': data})
  424. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  425. if devInfo['rst'] == -1:
  426. raise ServiceException(
  427. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  428. elif devInfo['rst'] == 1:
  429. raise ServiceException(
  430. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  431. if devInfo['data'][14:16] == '01':
  432. raise ServiceException(
  433. {'result': 2, 'description': u'设置失败'})
  434. # 设置充满自停
  435. def set_full_close(self, fullClose=False):
  436. data = '0A' + ('0001' if fullClose else '0000')
  437. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  438. {'IMEI': self._device['devNo'], "funCode": '04', 'data': data})
  439. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  440. if devInfo['rst'] == -1:
  441. raise ServiceException(
  442. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  443. elif devInfo['rst'] == 1:
  444. raise ServiceException(
  445. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  446. if devInfo['data'][14:16] == '01':
  447. raise ServiceException(
  448. {'result': 2, 'description': u'设置失败'})
  449. # 设置免费使用
  450. def set_free_mode(self, freeMode=False):
  451. data = '0B' + ('0001' if freeMode else '0000')
  452. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  453. {'IMEI': self._device['devNo'], "funCode": '04', 'data': data})
  454. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  455. if devInfo['rst'] == -1:
  456. raise ServiceException(
  457. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  458. elif devInfo['rst'] == 1:
  459. raise ServiceException(
  460. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  461. if devInfo['data'][14:16] == '01':
  462. raise ServiceException(
  463. {'result': 2, 'description': u'设置失败'})
  464. # 设置断电记忆
  465. def set_shut_down_mem(self, shutDownMem=False):
  466. data = '0C' + ('0001' if shutDownMem else '0000')
  467. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  468. {'IMEI': self._device['devNo'], "funCode": '04', 'data': data})
  469. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  470. if devInfo['rst'] == -1:
  471. raise ServiceException(
  472. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  473. elif devInfo['rst'] == 1:
  474. raise ServiceException(
  475. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  476. if devInfo['data'][14:16] == '01':
  477. raise ServiceException(
  478. {'result': 2, 'description': u'设置失败'})
  479. # 设置清空时间
  480. def set_all_time_clear(self, allTimeClear=False):
  481. data = '08' + ('0000' if allTimeClear else '0001')
  482. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  483. {'IMEI': self._device['devNo'], "funCode": '04', 'data': data})
  484. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  485. if devInfo['rst'] == -1:
  486. raise ServiceException(
  487. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  488. elif devInfo['rst'] == 1:
  489. raise ServiceException(
  490. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  491. if devInfo['data'][14:16] == '01':
  492. raise ServiceException(
  493. {'result': 2, 'description': u'设置失败'})
  494. # 设置恢复默认
  495. def set_default_setting(self, defaultSetting=False):
  496. data = '09' + ('0000' if defaultSetting else '0001')
  497. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  498. {'IMEI': self._device['devNo'], "funCode": '04', 'data': data})
  499. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  500. if devInfo['rst'] == -1:
  501. raise ServiceException(
  502. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  503. elif devInfo['rst'] == 1:
  504. raise ServiceException(
  505. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  506. if devInfo['data'][14:16] == '01':
  507. raise ServiceException(
  508. {'result': 2, 'description': u'设置失败'})
  509. # 远程停止某个端口的使用
  510. def active_deactive_port(self, port, active):
  511. hexPort = fill_2_hexByte(hex(int(port) + 1), 4)
  512. if hexPort == '0009':
  513. hexPort = '0100'
  514. elif hexPort == '000A':
  515. hexPort = '0200'
  516. elif hexPort == '0003':
  517. hexPort = '0004'
  518. elif hexPort == '0004':
  519. hexPort = '0008'
  520. elif hexPort == '0005':
  521. hexPort = '0010'
  522. elif hexPort == '0006':
  523. hexPort = '0020'
  524. elif hexPort == '0007':
  525. hexPort = '0040'
  526. elif hexPort == '0008':
  527. hexPort = '0080'
  528. if active:
  529. return
  530. else:
  531. data = hexPort + '00'
  532. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  533. {'IMEI': self._device['devNo'], "funCode": '03', 'data': data + '0000'})
  534. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  535. if devInfo['rst'] == -1:
  536. raise ServiceException(
  537. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  538. elif devInfo['rst'] == 1:
  539. raise ServiceException(
  540. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  541. def get_port_info(self, line):
  542. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  543. {'IMEI': self._device['devNo'], "funCode": '01', 'data': '00'})
  544. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  545. if devInfo['rst'] == -1:
  546. raise ServiceException(
  547. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  548. elif devInfo['rst'] == 1:
  549. raise ServiceException(
  550. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  551. leftTimeData = devInfo['data'][32:112]
  552. elecData = devInfo['data'][12:32]
  553. ii = int(line)
  554. leftTime = int(int(leftTimeData[ii * 8:ii * 8 + 8], 16) / 60)
  555. electricity = float(int(elecData[ii * 2:ii * 2 + 2], 16)) / 10
  556. return {'port': str(int(line) - 1), 'leftTime': leftTime, 'electricity': electricity}
  557. def set_device_function(self,request,lastSetConf):
  558. if request.POST.has_key('noElecShutDown'):
  559. # noElecShutDown = True if request.POST.get('noElecShutDown') == 'true' else False
  560. noElecShutDown = request.POST.get("noElecShutDown", False)
  561. lastSetConf.update({'noElecShutDown': noElecShutDown})
  562. self.set_no_elec_shut_down(lastSetConf['noElecShutDown'])
  563. if request.POST.has_key('fullClose'):
  564. # fullClose = True if request.POST.get('fullClose') == 'true' else False
  565. fullClose = request.POST.get("fullClose", False)
  566. lastSetConf.update({'fullClose': fullClose})
  567. self.set_full_close(lastSetConf['fullClose'])
  568. if request.POST.has_key('freeMode'):
  569. # freeMode = True if request.POST.get('freeMode') == 'true' else False
  570. freeMode = request.POST.get("freeMode", False)
  571. lastSetConf.update({'freeMode': freeMode})
  572. self.set_free_mode(lastSetConf['freeMode'])
  573. if request.POST.has_key('shutDownMem'):
  574. # shutDownMem = True if request.POST.get('shutDownMem') == 'true' else False
  575. shutDownMem = request.POST.get("shutDownMem", False)
  576. lastSetConf.update({'shutDownMem': shutDownMem})
  577. self.set_shut_down_mem(lastSetConf['shutDownMem'])
  578. if request.POST.has_key('allTimeClear'):
  579. # allTimeClear = True if request.POST.get('allTimeClear') == 'true' else False
  580. allTimeClear = request.POST.get("allTimeClear", False)
  581. lastSetConf.update({'allTimeClear': allTimeClear})
  582. self.set_all_time_clear(lastSetConf['allTimeClear'])
  583. if request.POST.has_key('defaultSetting'):
  584. # defaultSetting = True if request.POST.get('defaultSetting') == 'true' else False
  585. defaultSetting = request.POST.get("defaultSetting", False)
  586. lastSetConf.update({'defaultSetting': defaultSetting})
  587. self.set_default_setting(lastSetConf['defaultSetting'])
  588. def set_device_function_param(self,request,lastSetConf):
  589. ewmTime = request.POST.get('ewmTime', None)
  590. coinTime = request.POST.get('coinTime', None)
  591. icTime = request.POST.get('icTime', None)
  592. oneMaxElec = request.POST.get('oneMaxElec', None)
  593. oneStdElec = request.POST.get('oneStdElec', None)
  594. funcSetting = request.POST.get('funcSetting', False)
  595. standbyDisplay = request.POST.get('standbyDisplay', None)
  596. allMaxElec = request.POST.get('allMaxElec', None)
  597. lessElecShutDown = request.POST.get('lessElecShutDown', None)
  598. if ewmTime and coinTime and icTime:
  599. lastSetConf.update({'ewmTime': int(ewmTime)})
  600. lastSetConf.update({'coinTime': int(coinTime)})
  601. lastSetConf.update({'icTime': int(icTime)})
  602. self.set_charging_time_setting(lastSetConf)
  603. if oneMaxElec and oneStdElec and allMaxElec and lessElecShutDown:
  604. lastSetConf.update({'oneMaxElec': float(oneMaxElec) * 10})
  605. lastSetConf.update({'oneStdElec': float(oneStdElec) * 10})
  606. lastSetConf.update({'allMaxElec': float(allMaxElec) * 10})
  607. lastSetConf.update({'lessElecShutDown': float(lessElecShutDown) * 10})
  608. self.set_charging_elec_setting(lastSetConf)
  609. if funcSetting and standbyDisplay:
  610. lastSetConf.update({'funcSetting': True})
  611. lastSetConf.update({'standbyDisplay': True})
  612. self.set_func_param_setting(lastSetConf)