langxin.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. from django.core.cache import caches
  4. from apps.web.constant import Const, DeviceCmdCode, MQTT_TIMEOUT
  5. from apps.web.core.adapter.base import SmartBox, fill_2_hexByte, logger
  6. from apps.web.core.exceptions import ServiceException
  7. from apps.web.core.networking import MessageSender
  8. from apps.web.device.models import Device
  9. class ChargingLangXinBox(SmartBox):
  10. def __init__(self, device):
  11. super(ChargingLangXinBox, self).__init__(device)
  12. def translate_funcode(self, funCode):
  13. funCodeDict = {
  14. '01': u'获取端口数量',
  15. '02': u'移动支付',
  16. '0C': u'获取设备设置',
  17. '08': u'获取设备设置',
  18. '06': u'获取设备端口详情',
  19. '07': u'获取刷卡投币统计数据',
  20. '09': u'设置刷卡投币使能',
  21. '0A': u'端口使能',
  22. '0B': u'端口关闭',
  23. '16': u'设置设备参数',
  24. '20': u'设备重启',
  25. }
  26. return funCodeDict.get(funCode, '')
  27. def translate_event_cmdcode(self, cmdCode):
  28. cmdDict = {
  29. '03': u'投币上报',
  30. '04': u'刷卡上报',
  31. '05': u'充电结束',
  32. '10': u'刷卡上报',
  33. '0D': u'故障',
  34. }
  35. return cmdDict.get(cmdCode, '')
  36. # 获取端口状态(缓存)
  37. def get_port_status(self, force = False):
  38. if force:
  39. return self.get_port_status_from_dev()
  40. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  41. statusDict = {}
  42. staticPorts = self.get_port_number_from_type_code()
  43. allPorts = ctrInfo.get('allPorts', staticPorts)
  44. # allPorts 有的时候会为0, 这个时候强制设置为设备类型CODE对应的字段
  45. if allPorts == 0:
  46. allPorts = staticPorts
  47. for ii in range(allPorts):
  48. tempDict = ctrInfo.get(str(ii + 1), {})
  49. if tempDict.has_key('status'):
  50. statusDict[str(ii + 1)] = {'status': tempDict.get('status')}
  51. elif tempDict.has_key('isStart'):
  52. if tempDict['isStart']:
  53. statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_WORKING}
  54. else:
  55. statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_IDLE}
  56. else:
  57. statusDict[str(ii + 1)] = {'status': Const.DEV_WORK_STATUS_IDLE}
  58. allPorts, usedPorts, usePorts = self.get_port_static_info(statusDict)
  59. Device.update_dev_control_cache(self._device['devNo'],
  60. {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts})
  61. return statusDict
  62. # 获取端口状态
  63. def get_port_status_from_dev(self):
  64. devInfo = MessageSender.send(self.device, self.make_random_cmdcode(),
  65. {'IMEI': self._device['devNo'], "funCode": '01', 'data': '00'})
  66. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  67. if devInfo['rst'] == -1:
  68. raise ServiceException(
  69. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  70. elif devInfo['rst'] == 1:
  71. raise ServiceException(
  72. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  73. data = devInfo['data'][6::]
  74. result = {}
  75. portNum = int(data[0:2], 16)
  76. portData = data[2::]
  77. ii = 0
  78. while ii < portNum:
  79. port = ii + 1
  80. statusTemp = portData[ii * 2:ii * 2 + 2]
  81. if statusTemp == '01':
  82. status = {'status': Const.DEV_WORK_STATUS_IDLE}
  83. elif statusTemp == '02':
  84. status = {'status': Const.DEV_WORK_STATUS_WORKING}
  85. elif statusTemp == '03':
  86. status = {'status': Const.DEV_WORK_STATUS_FORBIDDEN}
  87. elif statusTemp == '04':
  88. status = {'status': Const.DEV_WORK_STATUS_FAULT}
  89. ii += 1
  90. result[str(port)] = status
  91. allPorts, usedPorts, usePorts = self.get_port_static_info(result)
  92. Device.update_dev_control_cache(self._device['devNo'],
  93. {'allPorts': allPorts, 'usedPorts': usedPorts, 'usePorts': usePorts})
  94. # 这里存在多线程更新缓存的场景,可能性比较低,但是必须先取出来,然后逐个更新状态,然后再记录缓存
  95. ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
  96. for strPort, info in result.items():
  97. if ctrInfo.has_key(strPort):
  98. ctrInfo[strPort].update({'status': info['status']})
  99. else:
  100. ctrInfo[strPort] = info
  101. Device.update_dev_control_cache(self._device['devNo'], ctrInfo)
  102. return result
  103. # 支付成功启动设备
  104. def start_device(self, package, openId, attachParas):
  105. if attachParas is None:
  106. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路、电池类型信息'})
  107. if not attachParas.has_key('chargeIndex'):
  108. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路'})
  109. devConf = caches['devmgr'].get('settingConf_%s' % (self._device['devNo']))
  110. if devConf is None:
  111. conf = self.get_dev_setting()
  112. caches['devmgr'].set('settingConf_%s' % (self._device['devNo']), conf, 24 * 3600)
  113. port = hex(int(attachParas['chargeIndex']))
  114. hexPort = fill_2_hexByte(port, 2)
  115. needTime = int(package['time'])
  116. needCoins = int(package['coins'])
  117. unit = package.get('unit', u'分钟')
  118. if unit == u'小时':
  119. needTime = int(package['time']) * 60
  120. elif unit == u'天':
  121. needTime = int(package['time']) * 1440
  122. hexTime = fill_2_hexByte(hex(needTime))
  123. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = {
  124. 'IMEI': self._device['devNo'],
  125. "funCode": '02',
  126. 'data': hexPort + hexTime
  127. }, timeout = MQTT_TIMEOUT.START_DEVICE)
  128. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  129. if devInfo['rst'] == -1:
  130. raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
  131. elif devInfo['rst'] == 1:
  132. raise ServiceException({'result': 2, 'description': u'充电桩正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'})
  133. data = devInfo['data'][6::]
  134. usePort = int(data[0:2], 16)
  135. result = data[2:4]
  136. if result == '00': # 成功
  137. pass
  138. elif result == '02':
  139. newValue = {str(usePort): {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'充电站故障'}}
  140. Device.update_dev_control_cache(self._device['devNo'], newValue)
  141. raise ServiceException({'result': 2, 'description': u'充电站故障'})
  142. elif result == '03':
  143. newValue = {str(usePort): {'status': Const.DEV_WORK_STATUS_WORKING, 'statusInfo': u''}}
  144. Device.update_dev_control_cache(self._device['devNo'], newValue)
  145. raise ServiceException({'result': 2, 'description': u'该端口正在使用中'})
  146. portCache = Device.get_dev_control_cache(self._device['devNo']).get(str(usePort), {})
  147. if portCache.get('status', Const.DEV_WORK_STATUS_IDLE) == Const.DEV_WORK_STATUS_WORKING:
  148. Device.update_dev_control_cache(
  149. self._device['devNo'],
  150. {
  151. str(usePort):
  152. {
  153. 'coins': portCache['coins'] + needCoins,
  154. 'status': Const.DEV_WORK_STATUS_WORKING,
  155. 'needTime': portCache['needTime'] + needTime,
  156. 'isStart': True,
  157. 'refunded': False,
  158. 'openId': openId,
  159. 'startTime': portCache['startTime']
  160. }
  161. })
  162. else:
  163. Device.update_dev_control_cache(
  164. self._device['devNo'],
  165. {
  166. str(usePort):
  167. {
  168. 'startTime': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
  169. 'status': Const.DEV_WORK_STATUS_WORKING,
  170. 'needTime': needTime,
  171. 'isStart': True,
  172. 'openId': openId,
  173. 'refunded': False,
  174. 'coins': needCoins,
  175. 'vCardId': self._vcard_id
  176. }
  177. })
  178. return devInfo
  179. # 获取设备配置参数
  180. def get_dev_setting(self):
  181. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  182. {'IMEI': self._device['devNo'], "funCode": '0C', 'data': '00'})
  183. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  184. if devInfo['rst'] == -1:
  185. raise ServiceException(
  186. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  187. elif devInfo['rst'] == 1:
  188. raise ServiceException(
  189. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  190. confData = devInfo['data'][6:-2]
  191. icMoney = int(confData[0:2], 16) / 10
  192. icTime = int(confData[2:4], 16) * 10
  193. tbTime = int(confData[4:6], 16) * 10
  194. stdPower = int(confData[6:8], 16) * 10
  195. maxPower = int(confData[8:10], 16) * 10
  196. dataDict1 = self.get_param_setting()
  197. dataDict2 = {'icMoney': icMoney,
  198. 'icTime': icTime,
  199. 'tbTime': tbTime,
  200. 'stdPower': stdPower,
  201. 'maxPower': maxPower}
  202. dataDict3 = self.get_dev_consume_count()
  203. return dict(dataDict1.items() + dataDict2.items() + dataDict3.items())
  204. # 设置设备参数
  205. def set_dev_setting(self, setConf):
  206. data = ''
  207. data += fill_2_hexByte(hex(int(setConf['icMoney']) * 10), 2) # 这个改不了, 默认的
  208. data += fill_2_hexByte(hex(int(setConf['icTime']) / 10), 2)
  209. data += fill_2_hexByte(hex(int(setConf['tbTime']) / 10), 2)
  210. data += fill_2_hexByte(hex(int(setConf['stdPower']) / 10), 2)
  211. data += fill_2_hexByte(hex(int(setConf['maxPower']) / 10), 2)
  212. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  213. {'IMEI': self._device['devNo'], "funCode": '08', 'data': data})
  214. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  215. if devInfo['rst'] == -1:
  216. raise ServiceException(
  217. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  218. elif devInfo['rst'] == 1:
  219. raise ServiceException(
  220. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  221. # 分析事件参数
  222. def analyze_event_data(self, data):
  223. cmdCode = data[4:6]
  224. if cmdCode == '03': # 投币上报
  225. coin = int(data[6:8], 16)
  226. return {'status': Const.DEV_WORK_STATUS_IDLE, 'cmdCode': cmdCode, 'coin': coin}
  227. if cmdCode == '04': # 刷卡上报
  228. money = int(data[6:8], 16)
  229. return {'status': Const.DEV_WORK_STATUS_IDLE, 'cmdCode': cmdCode, 'money': money}
  230. if cmdCode == '05': # 提交充电结束状态
  231. port = int(data[6:8], 16)
  232. leftTime = int(data[8:12], 16)
  233. reason = data[12:14]
  234. desc = ''
  235. if reason == '00':
  236. desc = u'电池没有充满!购买的充电时间用完了。每次充满电对您的电池保养有好处哦,建议您在个人中心,找到您正在使用的设备以及端口,付费后续充。'
  237. elif reason == '01':
  238. desc = u'可能是插头被拔掉,或者电瓶已经充满。系统判断为异常断电,由于电瓶车充电器种类繁多,可能存在误差。如有问题,请您及时联系商家协助解决问题并恢复充电。'
  239. elif reason == '02':
  240. desc = u'恭喜您!电池已经充满电!'
  241. elif reason == '03':
  242. desc = u'电池没有充满!设备或端口出现问题,为了安全起见,被迫停止工作。建议您根据已经充电的时间评估是否需要到现场换到其他端口充电。'
  243. elif reason == '04':
  244. desc = u'过载切断输出。'
  245. elif reason == '05':
  246. desc = u'警告!您的电池功率超过本机最大限制,已经停止充电,为了公共安全,不建议您在该充电桩充电!提醒您,为了安全,大功率的电池不要放入楼道、室内等位置充电哦。'
  247. return {'reasonCode':reason,'status':Const.DEV_WORK_STATUS_IDLE, 'cmdCode':cmdCode, 'port':port, 'leftTime':leftTime, 'reason':desc}
  248. elif cmdCode == '0D':
  249. port = int(data[6:8], 16)
  250. errCode = int(data[8:10], 16)
  251. if errCode == '01':
  252. return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'端口输出故障', 'cmdCode': cmdCode,
  253. 'port': port, 'FaultCode': errCode}
  254. elif errCode == '02':
  255. return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'机器整体充电功率过大', 'cmdCode': cmdCode,
  256. 'port': port, 'FaultCode': errCode}
  257. elif errCode == '03':
  258. return {'status': Const.DEV_WORK_STATUS_FAULT, 'statusInfo': u'电源故障', 'cmdCode': cmdCode, 'port': port,
  259. 'FaultCode': errCode}
  260. # 查询当前端口充电状态
  261. def get_port_info(self, line):
  262. data = fill_2_hexByte(hex(int(line)), 2)
  263. devInfo = MessageSender.send(self.device, self.make_random_cmdcode(),
  264. {'IMEI': self._device['devNo'], "funCode": '06', 'data': data})
  265. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  266. if devInfo['rst'] == -1:
  267. raise ServiceException(
  268. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  269. elif devInfo['rst'] == 1:
  270. raise ServiceException(
  271. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  272. data = devInfo['data'][6::]
  273. leftTime = int(data[2:6], 16)
  274. if data[6:10] == 'FFFF':
  275. power = 0
  276. else:
  277. power = int(data[6:10], 16)
  278. return {'port': line, 'leftTime': leftTime, 'power': power}
  279. # 查询消费总额数据
  280. def get_dev_consume_count(self):
  281. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  282. {'IMEI': self._device['devNo'], "funCode": '07', 'data': '00'})
  283. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  284. if devInfo['rst'] == -1:
  285. raise ServiceException(
  286. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  287. elif devInfo['rst'] == 1:
  288. raise ServiceException(
  289. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  290. data = devInfo['data'][6::]
  291. cardFee = int(data[0:4], 16)
  292. coinFee = int(data[4:8], 16)
  293. return {'cardFee': cardFee, 'coinFee': coinFee}
  294. # 设置IC卡、投币器是否可用
  295. def set_coin_card_enable(self, infoDict):
  296. data = ''
  297. if infoDict.has_key('putCoins'):
  298. data += '01'
  299. else:
  300. infoDict['putCoins'] = False
  301. data += '00'
  302. if infoDict.has_key('icCard'):
  303. data += '01'
  304. else:
  305. infoDict['icCard'] = False
  306. data += '00'
  307. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  308. {'IMEI': self._device['devNo'], "funCode": '09', 'data': data})
  309. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  310. if devInfo['rst'] == -1:
  311. raise ServiceException(
  312. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  313. elif devInfo['rst'] == 1:
  314. raise ServiceException(
  315. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  316. try:
  317. conf = Device.objects.get(devNo=self._device['devNo']).otherConf
  318. conf.update({'putCoins': infoDict['putCoins'], 'icCard': infoDict['icCard']})
  319. Device.get_collection().update({'devNo': self._device['devNo']}, {'$set': {'otherConf': conf}})
  320. except Exception, e:
  321. logger.error('update dev=%s coin enable ic enable e=%s' % (self._device['devNo'], e))
  322. # 锁定、解锁某一端口
  323. def lock_unlock_port(self, port, lock=True):
  324. lockStr = '00' if lock else '01'
  325. portStr = fill_2_hexByte(hex(int(port)), 2)
  326. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  327. {'IMEI': self._device['devNo'], "funCode": '0A', 'data': portStr + lockStr})
  328. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  329. if devInfo['rst'] == -1:
  330. raise ServiceException(
  331. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  332. elif devInfo['rst'] == 1:
  333. raise ServiceException(
  334. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  335. if lock:
  336. Device.update_dev_control_cache(self._device['devNo'],
  337. {str(port): {'status': Const.DEV_WORK_STATUS_FORBIDDEN}})
  338. else:
  339. Device.update_dev_control_cache(self._device['devNo'], {str(port): {'status': Const.DEV_WORK_STATUS_IDLE}})
  340. # 配合stop_charging_port使用
  341. def active_deactive_port(self, port, active):
  342. if active:
  343. raise ServiceException({'result': 2, 'description': u'该设备不支持直接打开端口'})
  344. self.stop_charging_port(port)
  345. devInfo = Device.get_dev_control_cache(self._device['devNo'])
  346. portCtrInfo = devInfo.get(str(port), {})
  347. portCtrInfo.update({'isStart': False, 'status': Const.DEV_WORK_STATUS_IDLE, 'needTime': 0, 'leftTime': 0,
  348. 'endTime': datetime.datetime.now().strftime(Const.DATETIME_FMT)})
  349. newValue = {str(port): portCtrInfo}
  350. Device.update_dev_control_cache(self._device['devNo'], newValue)
  351. # 远程停止某个端口的使用
  352. def stop_charging_port(self, port):
  353. portStr = fill_2_hexByte(hex(int(port)), 2)
  354. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  355. {'IMEI': self._device['devNo'], "funCode": '0B', 'data': portStr})
  356. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  357. if devInfo['rst'] == -1:
  358. raise ServiceException(
  359. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  360. elif devInfo['rst'] == 1:
  361. raise ServiceException(
  362. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  363. data = devInfo['data'][6::]
  364. port = int(data[0:2], 16)
  365. leftTime = int(data[2:6], 16)
  366. return {'port': port, 'leftTime': leftTime}
  367. # 个人中心远程停止端口
  368. def stop(self, port = None):
  369. infoDict = self.stop_charging_port(port)
  370. infoDict['remainder_time'] = infoDict['leftTime']
  371. return infoDict
  372. # 设置设备参数
  373. def set_param_setting(self, infoDict):
  374. data = ''
  375. if infoDict.has_key('autoStop'):
  376. if infoDict['autoStop']:
  377. data += '01'
  378. elif not infoDict['autoStop']:
  379. data += '00'
  380. else:
  381. infoDict['autoStop'] = False
  382. data += '00'
  383. section = ''
  384. if infoDict.has_key('funcParam'):
  385. if infoDict['funcParam'] == 0:
  386. section += '00'
  387. elif infoDict['funcParam'] == 1:
  388. section += '01'
  389. elif infoDict['funcParam'] == 2:
  390. section += '02'
  391. elif infoDict['funcParam'] == 3:
  392. section += '03'
  393. else:
  394. section += '00'
  395. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  396. {'IMEI': self._device['devNo'], "funCode": '16', 'data': data + '00' + section})
  397. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  398. if devInfo['rst'] == -1:
  399. raise ServiceException(
  400. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  401. elif devInfo['rst'] == 1:
  402. raise ServiceException(
  403. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  404. try:
  405. conf = Device.objects.get(devNo=self._device['devNo']).otherConf
  406. conf.update({'autoStop': infoDict['autoStop'], 'cardRefund': infoDict['cardRefund'],
  407. 'funcParam': infoDict['funcParam']})
  408. Device.get_collection().update({'devNo': self._device['devNo']}, {'$set': {'otherConf': conf}})
  409. except Exception, e:
  410. logger.error('update dev=%s coin enable ic enable e=%s' % (self._device['devNo'], e))
  411. # 获取设备参数
  412. def get_param_setting(self):
  413. try:
  414. dev = Device.objects.get(devNo=self._device['devNo'])
  415. except Exception, e:
  416. logger.error('get dev error = %s' % e)
  417. return {'autoStop': False, 'cardRefund': False, 'funcParam': 0}
  418. return {
  419. 'autoStop': dev['otherConf'].get('autoStop', False),
  420. 'cardRefund': dev['otherConf'].get('cardRefund', False),
  421. 'funcParam': dev['otherConf'].get('funcParam', 0)
  422. }
  423. # 设备重启
  424. def set_device_restart(self):
  425. devInfo = MessageSender.send(self.device, 220, {'IMEI': self._device['devNo'], "funCode": '20', 'data': '00'})
  426. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  427. if devInfo['rst'] == -1:
  428. raise ServiceException(
  429. {'result': 2, 'description': u'充电桩正在玩命找网络,请您稍候再试'})
  430. elif devInfo['rst'] == 1:
  431. raise ServiceException(
  432. {'result': 2, 'description': u'充电桩忙,无响应,请您稍候再试。也可能是您的设备版本过低,暂时不支持此功能'})
  433. def set_device_function(self, request, lastSetConf):
  434. if request.POST.has_key('putCoins'):
  435. # putCoins = True if request.POST.get('putCoins') == 'true' else False
  436. putCoins = request.POST.get("putCoins", False)
  437. lastSetConf.update({'putCoins': putCoins})
  438. self.set_coin_card_enable(lastSetConf)
  439. if request.POST.has_key('icCard'):
  440. # icCard = True if request.POST.get('icCard') == 'true' else False
  441. icCard = request.POST.get("icCard", False)
  442. lastSetConf.update({'icCard': icCard})
  443. self.set_coin_card_enable(lastSetConf)
  444. if request.POST.has_key('autoStop'):
  445. # autoStop = True if request.POST.get('autoStop') == 'true' else False
  446. autoStop = request.POST.get("autoStop", False)
  447. lastSetConf.update({'autoStop': autoStop})
  448. self.set_param_setting(lastSetConf)
  449. if request.POST.has_key('cardRefund'):
  450. # cardRefund = True if request.POST.get('cardRefund') == 'true' else False
  451. cardRefund = request.POST.get("cardRefund", False)
  452. lastSetConf.update({'cardRefund': cardRefund})
  453. self.set_param_setting(lastSetConf)
  454. if request.POST.has_key('reboot'):
  455. if request.POST.get('reboot'):
  456. self.set_device_restart()
  457. def set_device_function_param(self, request, lastSetConf):
  458. icMoney = request.POST.get('icMoney', None)
  459. icTime = request.POST.get('icTime', None)
  460. tbTime = request.POST.get('tbTime', None)
  461. stdPower = request.POST.get('stdPower', None)
  462. maxPower = request.POST.get('maxPower', None)
  463. autoStop = request.POST.get('autoStop', None)
  464. funcParam = request.POST.get('funcParam', 0)
  465. if icMoney:
  466. lastSetConf.update({'icMoney': int(icMoney)})
  467. if icTime:
  468. lastSetConf.update({'icTime': int(icTime)})
  469. if tbTime:
  470. lastSetConf.update({'tbTime': int(tbTime)})
  471. if stdPower:
  472. lastSetConf.update({'stdPower': int(stdPower)})
  473. if maxPower:
  474. lastSetConf.update({'maxPower': int(maxPower)})
  475. if autoStop is not None:
  476. lastSetConf.update({'autoStop': autoStop})
  477. if funcParam:
  478. lastSetConf.update({'funcParam': int(funcParam)})
  479. self.set_dev_setting(lastSetConf)
  480. self.set_param_setting(lastSetConf)
  481. def get_port_number_from_type_code(self):
  482. try:
  483. devTypeCode = self.device['devType']['code']
  484. if devTypeCode == Const.DEVICE_TYPE_CODE_CHARGING_LANGXIN:
  485. return 10
  486. elif devTypeCode == Const.DEVICE_TYPE_CODE_CHARGING_LANGXIN4:
  487. return 4
  488. elif devTypeCode == Const.DEVICE_TYPE_CODE_CHANGING_LANGXIN20:
  489. return 20
  490. else:
  491. return 10
  492. except Exception as e:
  493. return 10
  494. @property
  495. def isHaveStopEvent(self):
  496. return True