kaiyuanxingneng.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. # -*- coding: utf-8 -*-
  2. from apps.web.core.adapter.base import *
  3. from apps.web.device.models import Device
  4. class ChargingKyxnBox(SmartBox):
  5. def __init__(self, device):
  6. super(ChargingKyxnBox, self).__init__(device)
  7. def analyze_event_data(self,data):
  8. cmdCode = data[6:8]
  9. backData = data[8::]
  10. if cmdCode == '04':#充电状态
  11. voltage = int(backData[16:20],16)/10.0
  12. electric = int(backData[20:24],16)/10.0
  13. line = int(backData[24:26],16) + 1
  14. costTime = int(backData[26:30],16)
  15. return {'voltage':voltage,'isStart':True,'electric':electric,'costTime':costTime,'port':line}
  16. elif cmdCode == '05':#结束状态
  17. binTemp = hexbyte_2_bin(backData[16:18])
  18. endReason = u'桩端结束' if binTemp[-1] == '0' else u'服务器结束'
  19. line = int(binTemp[0:-1],2) + 1
  20. faultTemp = backData[18:20]
  21. faultCode = ''
  22. if faultTemp == '02':
  23. faultCode = Const.CHARGING_KYXN_DCGZ
  24. elif faultTemp == '04':
  25. faultCode = Const.CHARGING_KYXN_CDZGZ
  26. elif faultTemp == '06':
  27. faultCode = Const.CHARGING_KYXN_QTGZ
  28. return {'isStart':False,'reason':endReason,'faultCode':faultCode,'port':line}
  29. else:
  30. return None
  31. def check_dev_status(self,attachParas=None):
  32. if not attachParas.has_key('chargeIndex'):
  33. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路'})
  34. #查询是否已经接上了电池,如果没有接上电池,就要报错
  35. #动作
  36. lineBin = hexbyte_2_bin(hex(int(attachParas['chargeIndex'])-1))
  37. temp1=lineBin[1::] + '0'
  38. cmd = '00000000' + decimal_2_hexByte(int(temp1,2))
  39. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  40. {'IMEI': self._device['devNo'], "funCode": "06", "data": cmd})
  41. if 'rst' in devInfo and devInfo['rst'] != 0:
  42. if devInfo['rst'] == -1:
  43. raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,建议您试试旁边其他设备,或者稍后再试哦'})
  44. elif devInfo['rst'] == 1:
  45. raise ServiceException({'result': 2, 'description': u'充电桩正在忙,无响应,请试试其他线路,或者请稍后再试哦'})
  46. data = devInfo['data']
  47. devResult = data[16:18]
  48. binResult = hexbyte_2_bin(devResult)
  49. isNormal = True if binResult[-1] == '0' else False
  50. if not isNormal:
  51. raise ServiceException(
  52. {'result': 2, 'description': u'您未接电池,或者电池反接了,请您检查后再试哦'})
  53. #凯源新能的可以往端口加钱,所以,端口即使被占用,可以仍然使用
  54. def is_port_can_use(self,port):
  55. portDict = self.get_port_status()
  56. statusInfo = portDict.get(str(port))
  57. if statusInfo.get('status','') != Const.DEV_WORK_STATUS_IDLE:
  58. return False,u'设备当前线路不是空闲状态,无法使用哦'
  59. return True,u''
  60. def lock_unlock_port(self,port,lock):
  61. dev = Device.objects.get(devNo = self._device['devNo'])
  62. portKey = 'port%s' % port
  63. if lock:
  64. dev.otherConf.update({portKey:Const.DEV_WORK_STATUS_FORBIDDEN})
  65. else:
  66. dev.otherConf.update({portKey:Const.DEV_WORK_STATUS_IDLE})
  67. dev.save()
  68. if lock:
  69. self.active_deactive_port(port, False)
  70. if lock:
  71. Device.update_dev_control_cache(self._device['devNo'], {str(port):{'status':Const.DEV_WORK_STATUS_FORBIDDEN}})
  72. else:
  73. Device.update_dev_control_cache(self._device['devNo'], {str(port):{'status':Const.DEV_WORK_STATUS_IDLE}})
  74. def get_port_info(self,line):
  75. devCtrInfo = Device.get_dev_control_cache(self._device['devNo'])
  76. portInfo = devCtrInfo.get(line,None)
  77. if portInfo is None:
  78. return {'status':Const.DEV_WORK_STATUS_IDLE}
  79. return portInfo
  80. def get_port_status(self, force = False):
  81. resultDict = {}
  82. otherConf = Device.objects.get(devNo = self._device['devNo']).otherConf
  83. nowTime = datetime.datetime.now()
  84. devCtrInfo = Device.get_dev_control_cache(self._device['devNo'])
  85. for ii in range(8):
  86. resultDict[str(ii+1)] = {'status':Const.DEV_WORK_STATUS_FAULT}
  87. portInfo = devCtrInfo.get(str(ii+1),None)
  88. if portInfo is None:
  89. resultDict[str(ii+1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  90. continue
  91. if portInfo.get('isStart',False):
  92. startTime = to_datetime(portInfo['startTime'])
  93. usedTime = int((nowTime - startTime).total_seconds()/60.0)
  94. if usedTime > portInfo['needTime']:
  95. resultDict[str(ii+1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  96. else:
  97. resultDict[str(ii+1)] = {'status':Const.DEV_WORK_STATUS_WORKING}
  98. else:
  99. if portInfo.get('faultCode'):
  100. resultDict[str(ii+1)] = {'status':Const.DEV_WORK_STATUS_FAULT}
  101. else:
  102. resultDict[str(ii+1)] = {'status':Const.DEV_WORK_STATUS_IDLE}
  103. resultDict[str(ii+1)] = {'status':Const.DEV_WORK_STATUS_FAULT}
  104. portKey = 'port%s' % (ii + 1)
  105. confStatus = otherConf.get(portKey,None)
  106. if confStatus == Const.DEV_WORK_STATUS_FORBIDDEN:
  107. resultDict[str(ii + 1)] = {'status':Const.DEV_WORK_STATUS_FORBIDDEN}
  108. allPorts,usedPorts,usePorts = self.get_port_static_info(resultDict)
  109. Device.update_dev_control_cache(self._device['devNo'], {'allPorts':allPorts,'usedPorts':usedPorts,'usePorts':usePorts})
  110. return resultDict
  111. def start_device(self, package, openId, attachParas):
  112. if attachParas is None:
  113. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路、电池类型信息'})
  114. if not attachParas.has_key('chargeIndex'):
  115. raise ServiceException({'result': 2, 'description': u'请您选择合适的充电线路'})
  116. if not attachParas.has_key('batteryType'):
  117. raise ServiceException({'result': 2, 'description': u'请您选择合适的电池类型'})
  118. if not attachParas.has_key('voltage'):
  119. raise ServiceException({'result': 2, 'description': u'请您选择合适的电池电压'})
  120. #动作
  121. lineBin = hexbyte_2_bin(hex(int(attachParas['chargeIndex'])-1))
  122. temp1=lineBin[1::] + '0'
  123. cmd = '00000000' + decimal_2_hexByte(int(temp1,2))
  124. #电池类型
  125. if attachParas['batteryType'] == u'锂电池':
  126. cmd += '01'
  127. else:
  128. cmd += '02'
  129. #电压
  130. vTemp = fill_2_hexByte(hex(int(attachParas['voltage'])*10))
  131. cmd += vTemp
  132. #充电币
  133. coins = int(package['coins'])
  134. cTemp = fill_2_hexByte(hex(int(coins)))
  135. cmd += cTemp
  136. devInfo = MessageSender.send(device = self.device, cmd = DeviceCmdCode.OPERATE_DEV_SYNC, payload = {
  137. 'IMEI': self._device['devNo'],
  138. "funCode": "0D", "data": cmd
  139. }, timeout = MQTT_TIMEOUT.START_DEVICE)
  140. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  141. if devInfo['rst'] == -1:
  142. raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦。'})
  143. elif devInfo['rst'] == 1:
  144. raise ServiceException({'result': 2, 'description': u'充电桩正在忙,无响应,您的金币还在,请试试其他线路,或者请稍后再试哦'})
  145. data = devInfo['data']
  146. devResult = data[16:18]
  147. binResult = hexbyte_2_bin(devResult)
  148. isStart = True if binResult[-1] == '0' else False
  149. lineNum = int(binResult[0:-1],2) + 1
  150. if not isStart:
  151. raise ServiceException(
  152. {'result': 2, 'description': u'充电桩没有开始工作,请检查您的线路是否被占用,或者您的输入选择是否正确'})
  153. #刷新状态
  154. needTime = int(package['time'])
  155. newValue = {str(lineNum):{'startTime':datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
  156. 'coins':float(coins),'isStart':isStart,'voltage':attachParas['voltage'],'refunded':False,
  157. 'batteryType':attachParas['batteryType'],'needTime':needTime,'openId':openId,
  158. 'vCardId':self._vcard_id}}
  159. Device.update_dev_control_cache(self._device['devNo'], newValue)
  160. unit = package.get('unit','')
  161. if unit == u'次':
  162. devInfo['finished_time'] = int(time.time()) + 24*60*60
  163. return devInfo
  164. def active_deactive_port(self,port,active,duration=None):
  165. devCtrInfo = Device.get_dev_control_cache(self._device['devNo'])
  166. portInfo = devCtrInfo.get(str(port),{})
  167. lineBin = hexbyte_2_bin(hex(int(port)-1))
  168. temp1=lineBin[1::] + '0' if active else '1'
  169. cmd = '00000000' + decimal_2_hexByte(int(temp1,2))
  170. #电池类型
  171. if portInfo.get('batteryType','') == u'锂电池':
  172. cmd += '01'
  173. else:
  174. cmd += '02'
  175. #电压
  176. vTemp = fill_2_hexByte(hex(int(portInfo.get('voltage',36))*10))
  177. cmd += vTemp
  178. #充电币
  179. coins = int(portInfo.get('coins',8))
  180. cTemp = fill_2_hexByte(hex(int(coins)))
  181. cmd += cTemp
  182. devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
  183. {'IMEI': self._device['devNo'], "funCode": "0D", "data": cmd})
  184. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  185. if devInfo['rst'] == -1:
  186. raise ServiceException({'result': 2, 'description': u'充电桩正在玩命找网络,建议您试试旁边其他设备,或者稍后再试哦'})
  187. elif devInfo['rst'] == 1:
  188. raise ServiceException({'result': 2, 'description': u'充电桩正在忙,无响应,请试试其他线路,或者请稍后再试哦'})
  189. data = devInfo['data']
  190. devResult = data[16:18]
  191. binResult = hexbyte_2_bin(devResult)
  192. isStart = True if binResult[-1] == '0' else False
  193. lineNum = int(binResult[0:-1],2) + 1
  194. if isStart:
  195. raise ServiceException(
  196. {'result': 2, 'description': u'充电桩端口没有成功停掉,请稍候再试哦'})
  197. #刷新状态
  198. portInfo.update({'endTime':datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),'isStart':False,'status':Const.DEV_WORK_STATUS_IDLE})
  199. newValue = {str(lineNum):portInfo}
  200. Device.update_dev_control_cache(self._device['devNo'], newValue)
  201. if not active:#关闭充电桩的端口
  202. devInfo = Device.get_dev_control_cache(self._device['devNo'])
  203. portCtrInfo = devInfo.get(str(port),{})
  204. portCtrInfo.update({'isStart':False,'status':Const.DEV_WORK_STATUS_IDLE,'needTime':0,'leftTime':0,'endTime':datetime.datetime.now().strftime(Const.DATETIME_FMT)})
  205. newValue = {str(port):portCtrInfo}
  206. Device.update_dev_control_cache(self._device['devNo'], newValue)
  207. return devInfo