sijiang20.py 26 KB

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