hainiao.py 31 KB

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