xuzhou.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. # TODO 目前采用主体相同协议拷贝的方式, 后续需要考虑更适合的方式
  4. # 修改zhixia.py问题的时候需要考虑是否修改aiyaxin5.py和zhixia2.py.
  5. # 三者协议一致, 但是因为加载reload问题, 目前必须拆开.
  6. import datetime
  7. import logging
  8. import time
  9. from decimal import Decimal
  10. from apilib.monetary import RMB
  11. from apilib.utils_datetime import timestamp_to_dt
  12. from apps.web.constant import DeviceCmdCode, Const, ErrorCode, MQTT_TIMEOUT
  13. from apps.web.core.adapter.base import SmartBox, fill_2_hexByte, make_six_bytes_session_id
  14. from apps.web.core.exceptions import ServiceException
  15. from apps.web.core.networking import MessageSender
  16. from apps.web.device.models import Device
  17. logger = logging.getLogger(__name__)
  18. class ChargingXUZHOUBox(SmartBox):
  19. def __init__(self, device):
  20. super(ChargingXUZHOUBox, self).__init__(device)
  21. def translate_funcode(self, funCode):
  22. funCodeDict = {
  23. '01': u'获取端口数量',
  24. '02': u'移动支付',
  25. '06': u'获取设备端口详情',
  26. '09': u'设置刷卡投币使能',
  27. '0B': u'端口关闭',
  28. '13': u'设置卡充满退费',
  29. '14': u'设置功率费率配置',
  30. '12': u'充值',
  31. }
  32. return funCodeDict.get(funCode, '')
  33. def translate_event_cmdcode(self, cmdCode):
  34. cmdDict = {
  35. '04': u'刷卡上报',
  36. '05': u'充电结束',
  37. '0D': u'故障',
  38. '12': u'卡充值',
  39. }
  40. return cmdDict.get(cmdCode, '')
  41. def test(self, coins):
  42. hexPort = fill_2_hexByte(hex(int(1)), 2)
  43. hexCoins = fill_2_hexByte(hex(1))#测试充值1毛钱的电量测试
  44. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  45. {'IMEI': self._device['devNo'], "funCode": '02',
  46. 'data': hexPort + '00' + hexCoins})
  47. return devInfo
  48. def start_device(self, package, openId, attachParas):
  49. if attachParas is None:
  50. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路、电池类型信息'})
  51. if not attachParas.has_key('chargeIndex'):
  52. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路'})
  53. port = hex(int(attachParas['chargeIndex']))
  54. hexPort = fill_2_hexByte(port, 2)
  55. needTime = int(package['time'])
  56. unit = package.get('unit', u'分钟')
  57. if unit == u'小时':
  58. needTime = int(package['time']) * 60
  59. elif unit == u'天':
  60. needTime = int(package['time']) * 1440
  61. coins = int ( 10 * float(package['coins']) )
  62. hexCoins = fill_2_hexByte(hex(coins))
  63. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  64. {'IMEI': self._device['devNo'], "funCode": '02',
  65. 'data': hexPort + '00' + hexCoins}, timeout = MQTT_TIMEOUT.START_DEVICE)
  66. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  67. if devInfo['rst'] == -1:
  68. raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
  69. elif devInfo['rst'] == 1:
  70. raise ServiceException({'result': 2, 'description': u'充电桩正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'})
  71. data = devInfo['data'][16::]
  72. usePort = int(attachParas['chargeIndex'])
  73. result = data[4:6]
  74. if result == '01': # 成功
  75. pass
  76. elif result == '02':
  77. newValue = {str(usePort): {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'充电站故障'}}
  78. Device.update_dev_control_cache(self._device['devNo'], newValue)
  79. raise ServiceException({'result': 2, 'description': u'充电站故障'})
  80. elif result == '03':
  81. newValue = {str(usePort): {'status': Const.DEV_WORK_STATUS_WORKING, 'statusInfo': u''}}
  82. Device.update_dev_control_cache(self._device['devNo'], newValue)
  83. raise ServiceException({'result': 2, 'description': u'该端口正在使用中'})
  84. start_timestamp = int(time.time())
  85. Device.update_dev_control_cache(
  86. self._device['devNo'],
  87. {
  88. str(usePort):
  89. {
  90. 'startTime': timestamp_to_dt(start_timestamp).strftime('%Y-%m-%d %H:%M:%S'),
  91. 'status': Const.DEV_WORK_STATUS_WORKING,
  92. 'finishedTime': start_timestamp + needTime * 60,
  93. 'coins': coins,
  94. 'needTime': needTime,
  95. 'isStart': True,
  96. 'openId': openId,
  97. 'refunded': False,
  98. 'vCardId': self._vcard_id
  99. }
  100. })
  101. return devInfo
  102. def get_card_charge_result_from_data(self,data):
  103. if data[4:6] != '16':
  104. logger.info('receive wrong card charge result data = %s' % data)
  105. return False,RMB(0)
  106. balance = RMB(1) * (int(data[26:30], 16) / 10.0)
  107. result = True if data[34:36] == '01' else False
  108. return result,balance
  109. def analyze_event_data(self, data):
  110. cmdCode = data[4:6]
  111. if cmdCode in ['05']:
  112. port = int(data[18:20], 16)
  113. leftMoney = int(data[20:24], 16)/10.0
  114. reason = data[24:26]
  115. if reason == '00':
  116. desc = u'您购买的充电时间用完了。'
  117. elif reason == '01':
  118. desc = u'可能是插头被拔掉,或者电瓶已经充满。系统判断为异常断电,由于电瓶车充电器种类繁多,可能存在误差。如有问题,请您及时联系商家协助解决问题并恢复充电。'
  119. elif reason == '02':
  120. desc = u'恭喜您!电池已经充满电!'
  121. elif reason == '03':
  122. desc = u'设备或端口出现问题,为了安全起见,被迫停止工作。建议您根据已经充电的时间评估是否需要到现场换到其他端口充电。'
  123. elif reason == '04':
  124. desc = u'警告!您的电器功率超过充电站的单路最大输出功率,强制切断输出!提醒您,为了公共安全大功率的电池千万不要放入楼道、室内等位置充电哦!'
  125. elif reason == '05':
  126. desc = u'刷卡退费结束了哦'
  127. else:
  128. desc = u''
  129. return {'status': Const.DEV_WORK_STATUS_IDLE, 'cmdCode': cmdCode, 'port': port, 'leftMoney': leftMoney,
  130. 'reason': desc, 'isStart': False,'reasonCode':reason}
  131. elif cmdCode == '0D':
  132. port = int(data[16:18], 16)
  133. errCode = data[18:20]
  134. if errCode == '01':
  135. return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'端口输出故障', 'cmdCode': cmdCode,
  136. 'port': port, 'FaultCode': errCode}
  137. elif errCode == '02':
  138. return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'负载充电功率过大', 'cmdCode': cmdCode,
  139. 'port': port, 'FaultCode': errCode}
  140. elif errCode == '03':
  141. return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'漏电报警', 'cmdCode': cmdCode, 'port': port,
  142. 'FaultCode': errCode}
  143. elif errCode == '04':
  144. return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'过压报警', 'cmdCode': cmdCode, 'port': port,
  145. 'FaultCode': errCode}
  146. elif errCode == '05':
  147. return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'过流报警', 'cmdCode': cmdCode, 'port': port,
  148. 'FaultCode': errCode}
  149. elif errCode == '06':
  150. return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'欠压报警', 'cmdCode': cmdCode, 'port': port,
  151. 'FaultCode': errCode}
  152. elif errCode == '07':
  153. return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'过温报警', 'cmdCode': cmdCode, 'port': port,
  154. 'FaultCode': errCode}
  155. elif errCode == '08':
  156. return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'烟雾报警', 'cmdCode': cmdCode, 'port': port,
  157. 'FaultCode': errCode}
  158. elif cmdCode == '12':
  159. cardNo = data[18:26]
  160. cardNo = str(int(cardNo, 16))
  161. balance = int(data[26:30], 16) / 10.0
  162. return {'cmdCode': cmdCode, 'balance': balance, 'cardNo': cardNo}
  163. elif cmdCode == '04': # 刷卡报文
  164. cardNo = data[18:26]
  165. cardNo = str(int(cardNo, 16))
  166. port = int(data[26:28],16)
  167. coins = int(data[28:32],16)/10.0
  168. balance = int(data[32:36], 16)/10.0
  169. status = data[36:38]
  170. return {'cmdCode': cmdCode, 'cardNo':cardNo, 'coins': coins, 'port':port, 'balance':balance,'status':status}
  171. def get_port_info(self, line):
  172. data = fill_2_hexByte(hex(int(line)), 2)
  173. devInfo = MessageSender.send(self.device, self.make_random_cmdcode(),
  174. {'IMEI': self._device['devNo'], "funCode": '06', 'data': data})
  175. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  176. if devInfo['rst'] == -1:
  177. raise ServiceException(
  178. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  179. elif devInfo['rst'] == 1:
  180. raise ServiceException(
  181. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  182. data = devInfo['data'][18::]
  183. port = int(data[0:2], 16)
  184. strStatus = int(data[2:4], 16)
  185. status = Const.DEV_WORK_STATUS_IDLE
  186. if strStatus == '02':
  187. status = Const.DEV_WORK_STATUS_WORKING
  188. elif strStatus == '03':
  189. status = Const.DEV_WORK_STATUS_FORBIDDEN
  190. elif strStatus == '04':
  191. status = Const.DEV_WORK_STATUS_FAULT
  192. leftMoney = int(data[4:8],16)/10.0
  193. duration = int(data[8:12],16)
  194. power = int(data[12:16],16)
  195. return {'port': port, 'status': status, 'leftMoney': leftMoney,'duration':duration,'power':power}
  196. # 访问设备,获取设备端口信息
  197. def get_port_status_from_dev(self):
  198. devInfo = MessageSender.send(self.device, self.make_random_cmdcode(),
  199. {'IMEI': self._device['devNo'], "funCode": '01', 'data': '00'})
  200. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  201. if devInfo['rst'] == -1:
  202. raise ServiceException(
  203. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  204. elif devInfo['rst'] == 1:
  205. raise ServiceException(
  206. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  207. data = devInfo['data'][16::]
  208. result = {}
  209. portNum = int(data[2:4], 16)
  210. portData = data[4::]
  211. ii = 0
  212. while ii < portNum:
  213. statusTemp = portData[ii * 2:ii * 2 + 2]
  214. if statusTemp == '01':
  215. status = {'status': Const.DEV_WORK_STATUS_IDLE}
  216. elif statusTemp == '02':
  217. status = {'status': Const.DEV_WORK_STATUS_WORKING}
  218. elif statusTemp == '03':
  219. status = {'status': Const.DEV_WORK_STATUS_FORBIDDEN}
  220. elif statusTemp == '04':
  221. status = {'status': Const.DEV_WORK_STATUS_FAULT}
  222. ii += 1
  223. result[str(ii)] = status
  224. allPorts, usedPorts, usePorts = self.get_port_static_info(result)
  225. Device.update_dev_control_cache(self._device['devNo'],
  226. {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts})
  227. # 这里存在多线程更新缓存的场景,可能性比较低,但是必须先取出来,然后逐个更新状态,然后再记录缓存
  228. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  229. for strPort, info in result.items():
  230. if ctrInfo.has_key(strPort):
  231. ctrInfo[strPort].update({'status': info['status']})
  232. else:
  233. ctrInfo[strPort] = info
  234. Device.update_dev_control_cache(self._device['devNo'], ctrInfo)
  235. return result
  236. def get_port_status(self, force = False):
  237. if force:
  238. return self.get_port_status_from_dev()
  239. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  240. if not ctrInfo.has_key('allPorts'):
  241. self.get_port_status_from_dev()
  242. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  243. allPorts = ctrInfo.get('allPorts', 10)
  244. statusDict = {}
  245. for ii in range(allPorts):
  246. tempDict = ctrInfo.get(str(ii + 1), {})
  247. if tempDict.has_key('status'):
  248. statusDict[str(ii + 1)] = {'status': tempDict.get('status')}
  249. elif tempDict.has_key('isStart'):
  250. if tempDict['isStart']:
  251. statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_WORKING}
  252. else:
  253. statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_IDLE}
  254. else:
  255. statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_IDLE}
  256. allPorts, usedPorts, usePorts = self.get_port_static_info(statusDict)
  257. Device.update_dev_control_cache(self._device['devNo'],
  258. {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts})
  259. return statusDict
  260. def active_deactive_port(self, port, active):
  261. if active:
  262. raise ServiceException({'result': 2, 'description': u'该设备不支持直接打开端口'})
  263. self.stop_charging_port(port)
  264. devInfo = Device.get_dev_control_cache(self._device['devNo'])
  265. portCtrInfo = devInfo.get(str(port), {})
  266. portCtrInfo.update({'isStart': False, 'status': Const.DEV_WORK_STATUS_IDLE, 'needTime': 0, 'leftTime': 0,
  267. 'endTime': datetime.datetime.now().strftime(Const.DATETIME_FMT)})
  268. newValue = {str(port): portCtrInfo}
  269. Device.update_dev_control_cache(self._device['devNo'], newValue)
  270. def stop_charging_port(self, port):
  271. portStr = fill_2_hexByte(hex(int(port)), 2)
  272. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  273. {'IMEI': self._device['devNo'], "funCode": '0B', 'data': portStr})
  274. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  275. if devInfo['rst'] == -1:
  276. raise ServiceException(
  277. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  278. elif devInfo['rst'] == 1:
  279. raise ServiceException(
  280. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  281. data = devInfo['data'][16::]
  282. port = int(data[2:4], 16)
  283. leftMoney = int(data[4:8], 16)
  284. return {'port': port, 'leftMoney': leftMoney}
  285. # 获取设备配置参数
  286. def get_dev_setting(self):
  287. resultDict = {}
  288. tempDict = self.get_gear_conf()
  289. resultDict.update(tempDict)
  290. tempDict = self.get_fullstop_cardrefund()
  291. resultDict.update(tempDict)
  292. return resultDict
  293. def get_fullstop_cardrefund(self):
  294. try:
  295. dev = Device.objects.get(devNo = self._device['devNo'])
  296. except Exception, e:
  297. logger.error('get dev error = %s' % e)
  298. return {'autoStop': False, 'cardRefund': False}
  299. return {'autoStop': dev['otherConf'].get('autoStop', False),
  300. 'cardRefund': dev['otherConf'].get('cardRefund', False)}
  301. def set_fullstop_cardrefund(self, infoDict):
  302. data = ''
  303. if infoDict['autoStop']:
  304. data += '01'
  305. else:
  306. data += '00'
  307. if infoDict['cardRefund']:
  308. data += '01'
  309. else:
  310. data += '00'
  311. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  312. {'IMEI': self._device['devNo'], "funCode": '13', 'data': data})
  313. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  314. if devInfo['rst'] == -1:
  315. raise ServiceException(
  316. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  317. elif devInfo['rst'] == 1:
  318. raise ServiceException(
  319. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  320. try:
  321. conf = Device.objects.get(devNo = self._device['devNo']).otherConf
  322. conf.update({'autoStop': infoDict['autoStop'], 'cardRefund': infoDict['cardRefund']})
  323. Device.get_collection().update({'devNo': self._device['devNo']}, {'$set': {'otherConf': conf}})
  324. except Exception, e:
  325. logger.error('update dev=%s coin enable ic enable e=%s' % (self._device['devNo'], e))
  326. def get_gear_conf(self):
  327. resultDict = {'time1': 0, 'time2': 0, 'time3': 0,
  328. 'time4': 0, 'time5': 0, 'time6': 0,
  329. 'time7': 0, 'time8': 0, 'time9': 0,
  330. 'fee1': 0, 'fee2': 0, 'fee3': 0,
  331. 'serviceFee': 0, 'outputPower': 0}
  332. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  333. {'IMEI': self._device['devNo'], "funCode": '15', 'data': '00'})
  334. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  335. if devInfo['rst'] == -1:
  336. raise ServiceException(
  337. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  338. elif devInfo['rst'] == 1:
  339. return resultDict
  340. confData = devInfo['data'][18::]
  341. time1 = int(confData[0:4], 16)
  342. time2 = int(confData[4:8], 16)
  343. time3 = int(confData[8:12], 16)
  344. time4 = int(confData[12:16], 16)
  345. time5 = int(confData[16:20], 16)
  346. time6 = int(confData[20:24], 16)
  347. time7 = int(confData[24:28], 16)
  348. time8 = int(confData[28:32], 16)
  349. time9 = int(confData[32:36], 16)
  350. fee1 = int(confData[36:38], 16)
  351. fee2 = int(confData[38:40], 16)
  352. fee3 = int(confData[40:42], 16)
  353. serviceFee = int(confData[42:44], 16)
  354. outputPower = int(confData[44:46], 16)
  355. result = {
  356. 'time1': time1, 'time2': time2, 'time3': time3,
  357. 'time4': time4, 'time5': time5, 'time6': time6,
  358. 'time7': time7, 'time8': time8, 'time9': time9,
  359. 'fee1': fee1, 'fee2': fee2, 'fee3': fee3,
  360. 'serviceFee':serviceFee,'outputPower':outputPower
  361. }
  362. return result
  363. def set_gear_conf(self, infoDict):
  364. data = ''
  365. data += fill_2_hexByte(hex(int(infoDict['time1'])), 4)
  366. data += fill_2_hexByte(hex(int(infoDict['time2'])), 4)
  367. data += fill_2_hexByte(hex(int(infoDict['time3'])), 4)
  368. data += fill_2_hexByte(hex(int(infoDict['time4'])), 4)
  369. data += fill_2_hexByte(hex(int(infoDict['time5'])), 4)
  370. data += fill_2_hexByte(hex(int(infoDict['time6'])), 4)
  371. data += fill_2_hexByte(hex(int(infoDict['time7'])), 4)
  372. data += fill_2_hexByte(hex(int(infoDict['time8'])), 4)
  373. data += fill_2_hexByte(hex(int(infoDict['time9'])), 4)
  374. data += fill_2_hexByte(hex(int(infoDict['fee1'])), 2)
  375. data += fill_2_hexByte(hex(int(infoDict['fee2'])), 2)
  376. data += fill_2_hexByte(hex(int(infoDict['fee3'])), 2)
  377. data += fill_2_hexByte(hex(int(infoDict['serviceFee'])), 2)
  378. data += fill_2_hexByte(hex(int(infoDict['outputPower'])), 2)
  379. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  380. {'IMEI': self._device['devNo'], "funCode": '14', 'data': data})
  381. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  382. if devInfo['rst'] == -1:
  383. raise ServiceException(
  384. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  385. elif devInfo['rst'] == 1:
  386. raise ServiceException(
  387. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  388. # 给实体卡充值
  389. def recharge_card(self, cardNo, money, orderNo=None):
  390. try:
  391. data = 'EE1012{}'.format(make_six_bytes_session_id())
  392. cardNo = fill_2_hexByte(hex(int(cardNo)), 8)
  393. data += cardNo + fill_2_hexByte(hex(int(money * 10)), 4) + '0001'
  394. devInfo = MessageSender.send(self.device, DeviceCmdCode.PASSTHROUGH_OPERATE_DEV_SYNC,
  395. {'IMEI': self._device['devNo'], 'data': data, 'funCode': '12'})
  396. if devInfo['rst'] != 0:
  397. if devInfo['rst'] == ErrorCode.DEVICE_CONN_FAIL:
  398. # 离线无法判断是否成功, 认为充值成功, 走售后解决
  399. return {
  400. 'result': ErrorCode.DEVICE_CONN_FAIL,
  401. 'description': u'当前充电桩正在玩命找网络,请您稍候再试'
  402. }, None
  403. elif devInfo['rst'] == ErrorCode.BOARD_UART_TIMEOUT:
  404. return {
  405. 'result': ErrorCode.BOARD_UART_TIMEOUT,
  406. 'description': u'当前充电桩忙,无响应,请您稍候再试'
  407. }, None
  408. else:
  409. return {
  410. 'result': devInfo['rst'],
  411. 'description': u'系统异常'
  412. }, None
  413. resultData = devInfo['data']
  414. if resultData[4:6] != '16':
  415. return {
  416. 'result': ErrorCode.PARAMETER_ERROR_TO_BOX,
  417. 'description': u'充值返回报文命令码不为16'
  418. }, None
  419. balance = RMB(int(resultData[26:30], 16)) * Decimal('0.1')
  420. result = True if resultData[34:36] == '01' else False
  421. if result:
  422. return {
  423. 'result': ErrorCode.SUCCESS,
  424. 'description': ''
  425. }, balance
  426. else:
  427. return {
  428. 'result': ErrorCode.IC_RECHARGE_FAIL,
  429. 'description': u'充值失败'
  430. }, balance
  431. except Exception as e:
  432. logger.exception(e)
  433. return {
  434. 'result': ErrorCode.EXCEPTION,
  435. 'description': e.message
  436. }, None
  437. def set_device_function(self, request, lastSetConf):
  438. if request.POST.has_key('autoStop'):
  439. # autoStop = True if request.POST.get('autoStop') == 'true' else False
  440. autoStop = request.POST.get("autoStop", False)
  441. lastSetConf.update({'autoStop': autoStop})
  442. self.set_fullstop_cardrefund(lastSetConf)
  443. if request.POST.has_key('cardRefund'):
  444. # cardRefund = True if request.POST.get('cardRefund') == 'true' else False
  445. cardRefund = request.POST.get("cardRefund", False)
  446. lastSetConf.update({'cardRefund': cardRefund})
  447. self.set_fullstop_cardrefund(lastSetConf)
  448. def set_device_function_param(self, request, lastSetConf):
  449. if request.POST.has_key('time1') and request.POST.has_key('time2'):
  450. lastSetConf.update({'time1': int(request.POST.get('time1', 0))})
  451. lastSetConf.update({'time2': int(request.POST.get('time2', 0))})
  452. lastSetConf.update({'time3': int(request.POST.get('time3', 0))})
  453. lastSetConf.update({'time4': int(request.POST.get('time4', 0))})
  454. lastSetConf.update({'time5': int(request.POST.get('time5', 0))})
  455. lastSetConf.update({'time6': int(request.POST.get('time6', 0))})
  456. lastSetConf.update({'time7': int(request.POST.get('time7', 0))})
  457. lastSetConf.update({'time8': int(request.POST.get('time8', 0))})
  458. lastSetConf.update({'time9': int(request.POST.get('time9', 0))})
  459. lastSetConf.update({'fee1': int(request.POST.get('fee1', 0))})
  460. lastSetConf.update({'fee2': int(request.POST.get('fee2', 0))})
  461. lastSetConf.update({'fee3': int(request.POST.get('fee3', 0))})
  462. lastSetConf.update({'serviceFee': int(request.POST.get('serviceFee', 0))})
  463. lastSetConf.update({'outputPower': int(request.POST.get('outputPower', 0))})
  464. self.set_gear_conf(lastSetConf)