123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import time
- from apilib.utils_datetime import timestamp_to_dt
- from apilib.utils_json import JsonResponse
- from apps.web.constant import DeviceCmdCode, Const, MQTT_TIMEOUT
- from apps.web.core.adapter.base import SmartBox, fill_2_hexByte
- from apps.web.core.exceptions import ServiceException
- from apps.web.core.networking import MessageSender
- from apps.web.device.models import Device
- class ChuifengjiBox(SmartBox):
- def __init__(self, device):
- super(ChuifengjiBox, self).__init__(device)
- def test(self, coins):
- devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
- {'IMEI': self._device['devNo'], "funCode": 'C1',
- 'data': '01' + '01' + '00' + '01' + '00' + '0000'})
- return devInfo
- def get_port_info_from_dev(self, line):
- hexPort = fill_2_hexByte(hex(int(line)), 2)
- devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
- {'IMEI': self._device['devNo'], 'funCode': 'C1', 'data': hexPort + '01000000'})
- if devInfo.has_key('rst') and devInfo['rst'] != 0:
- if devInfo['rst'] == -1:
- raise ServiceException({'result': 2, 'description': u'设备正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
- elif devInfo['rst'] == 1:
- raise ServiceException({'result': 2, 'description': u'设备正忙没有响应,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
- data = devInfo['data']
- status = data[8:10]
- hour = int(data[10:12], 16)
- minute = int(data[12:14], 16)
- second = int(data[14:16], 16)
- leftTime = hour * 60 * 60 + minute * 60 + second
- if status == '01':
- return {'port': line, 'leftTime': leftTime, 'status': Const.DEV_WORK_STATUS_IDLE}
- elif status == '02':
- return {'port': line, 'leftTime': leftTime, 'status': Const.DEV_WORK_STATUS_WORKING}
- elif status == '03':
- return {'port': line, 'leftTime': leftTime, 'status': Const.DEV_WORK_STATUS_PAUSE}
- else:
- return {'port': line, 'leftTime': leftTime, 'status': Const.DEV_WORK_STATUS_WORKING}
- def get_port_info(self, line):
- result = {str(line): {}}
- if str(line) in ['1', '2']:
- ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
- infoFromDev = self.get_port_info_from_dev(line)
- infoFromCache = ctrInfo.get(str(line), {})
- infoFromCache.update(infoFromDev)
- return {str(line): infoFromDev}
- return result
- def get_port_status_from_dev(self):
- ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
- result = {}
- line1Info = self.get_port_info(1)
- ctrInfo.update({'1': {'status': line1Info['1']['status']}})
- result['1'] = {'status': line1Info['1']['status']}
- line2Info = self.get_port_info(2)
- ctrInfo.update({'2': {'status': line2Info['2']['status']}})
- result['2'] = {'status': line2Info['2']['status']}
- Device.update_dev_control_cache(self._device['devNo'], ctrInfo)
- return result
- def get_port_status(self, force = False):
- if force:
- return self.get_port_status_from_dev()
- portDict = {}
- portStatus = self.get_port_status_from_dev()
- portDict.update({'1': {'status': portStatus['1']['status']}})
- portDict.update({'2': {'status': portStatus['2']['status']}})
- return portDict
- def start_device(self, package, openId, attachParas):
- #: 首先检查设备是否在线
- unit = package.get('unit', u'分钟')
- coins = package.get('price')
- port = int(attachParas.get('chargeIndex'))
- hexPort = fill_2_hexByte(hex(port), 2)
- if unit == u'分钟': # 如果单位为分钟,就认为是充电
- needTimeInput = int(package['time'])
- needTime = needTimeInput
- hour, minute = needTimeInput / 60, needTimeInput % 60
- hexHour, hexMinute, hexSecond = fill_2_hexByte(hex(hour), 2), fill_2_hexByte(hex(minute), 2), '00'
- elif unit == u'秒': # 如果单位是秒,就认为是接水
- needTimeInput = int(package['time'])
- needTime = needTimeInput / 60.0
- hour = needTimeInput / 3600
- minute = (needTimeInput % 3600) / 60
- second = (needTimeInput % 3600) % 60
- hexHour, hexMinute, hexSecond = fill_2_hexByte(hex(hour), 2), fill_2_hexByte(hex(minute),
- 2), fill_2_hexByte(hex(second),
- 2)
- else:
- raise ServiceException({'result': 2, 'description': u'套餐单位错误,无法启动哦'})
- devInfo = MessageSender.send(device = self.device,
- cmd = DeviceCmdCode.OPERATE_DEV_SYNC,
- payload = {
- 'IMEI': self._device['devNo'],
- "funCode": 'C1',
- 'data': hexPort + '01' + hexHour + hexMinute + hexSecond + '0000'
- }, timeout = MQTT_TIMEOUT.START_DEVICE)
- if devInfo.has_key('rst') and devInfo['rst'] != 0:
- if devInfo['rst'] == -1:
- raise ServiceException({'result': 2, 'description': u'设备正在玩命找网络,您的金币还在,重试不需要重新付款,建议您试试旁边其他设备,或者稍后再试哦'})
- elif devInfo['rst'] == 1:
- raise ServiceException({'result': 2, 'description': u'设备正在忙,无响应,您的金币还在,重试不需要重新付款,请试试其他线路,或者请稍后再试哦'})
- data = devInfo['data']
- leftSeconds = int(data[10:12], 16) * 3600 + int(data[12:14], 16) * 60 + int(data[14:16], 16)
- start_timestamp = int(time.time())
- devInfo['finishedTime'] = start_timestamp + leftSeconds
- value = {
- 'openId': openId,
- 'startTime': timestamp_to_dt(start_timestamp).strftime('%Y-%m-%d %H:%M:%S'),
- 'leftSeconds': leftSeconds,
- 'isStart': True,
- 'needTime': needTime,
- 'coins': coins,
- 'vCardId': self._vcard_id,
- 'status': Const.DEV_WORK_STATUS_WORKING,
- 'finishedTime': devInfo['finishedTime']
- }
- Device.update_dev_control_cache(self._device['devNo'], {str(port): value})
- return devInfo
- def count_down(self, request, dev, agent, group, devType, lastOpenId, port = None):
- ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
- if port is None:
- port1Info = ctrInfo.get('1', {})
- port2Info = ctrInfo.get('2', {})
- if port1Info.has_key('openId') and port1Info['openId'] == request.user.openId and port1Info['status'] in [
- Const.DEV_WORK_STATUS_WORKING, Const.DEV_WORK_STATUS_PAUSE]:
- port = '1'
- if port2Info.has_key('openId') and port2Info['openId'] == request.user.openId and port2Info['status'] in [
- Const.DEV_WORK_STATUS_WORKING, Const.DEV_WORK_STATUS_PAUSE]:
- port = '2'
- if port is None:
- return JsonResponse({'result': 0, 'description': u'请您重新扫码', 'payload': {}})
- resultDict = self.get_port_info_from_dev(port)
- portCache = ctrInfo.get(port, {})
- status = 'idle'
- if resultDict['status'] == Const.DEV_WORK_STATUS_WORKING:
- status = 'working'
- elif resultDict['status'] == Const.DEV_WORK_STATUS_PAUSE:
- status = 'pause'
- surplus = resultDict['leftTime'] / 60.0
- sumtime = portCache.get('leftSeconds', surplus) / 60.0
- orderProcessing = False
- if surplus == 0.0 and sumtime == 0.0:
- orderProcessing = True
- return JsonResponse(
- {
- 'result': 1,
- 'description': '',
- 'payload': {
- 'workStatus': status,
- 'surplus': surplus,
- 'sum': sumtime,
- 'name': group['groupName'],
- 'address': group['address'],
- 'code': devType.get('code'),
- 'orderProcessing': orderProcessing,
- 'logicalCode': dev['logicalCode'],
- 'user': 'me' if lastOpenId == request.user.openId else 'notme',
- 'agentFeatures': agent.features,
- }
- })
- def stop(self, port = None):
- hexPort = fill_2_hexByte(hex(int(port)), 2)
- devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
- {'IMEI': self._device['devNo'], "funCode": 'C1',
- 'data': hexPort + '04' + '0000000000'})
- if devInfo.has_key('rst') and devInfo['rst'] != 0:
- return JsonResponse({"result": 0, "description": u'网络连接异常,停止设备失败,请您重新尝试停掉设备', "payload": ''})
- data = devInfo['data']
- leftSeconds = int(data[10:12], 16) * 3600 + int(data[12:14], 16) * 60 + int(data[14:16], 16)
- devInfo['remainder_time'] = round(leftSeconds / 60.0, 2) #
- return devInfo
- def pause(self, openId, isContinue, port = None):
- if port is None:
- ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
- port1Info = ctrInfo.get('1', {})
- port2Info = ctrInfo.get('2', {})
- if port1Info.has_key('openId') and port1Info['openId'] == openId and port1Info['status'] in [
- Const.DEV_WORK_STATUS_WORKING, Const.DEV_WORK_STATUS_PAUSE]:
- port = '1'
- if port2Info.has_key('openId') and port2Info['openId'] == openId and port2Info['status'] in [
- Const.DEV_WORK_STATUS_WORKING, Const.DEV_WORK_STATUS_PAUSE]:
- port = '2'
- if port is None:
- return JsonResponse({'result': 0, 'description': u'请您重新扫码', 'payload': {}})
- hexPort = fill_2_hexByte(hex(int(port)), 2)
- if isContinue:
- tempData = '03'
- else:
- tempData = '02'
- devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
- {'IMEI': self._device['devNo'], "funCode": 'C1',
- 'data': hexPort + tempData + '0000000000'})
- if devInfo.has_key('rst') and devInfo['rst'] != 0:
- return JsonResponse({"result": 0, "description": u'网络连接异常,停止设备失败,请您重新尝试停掉设备', "payload": ''})
- data = devInfo['data']
- leftSeconds = int(data[10:12], 16) * 3600 + int(data[12:14], 16) * 60 + int(data[14:16], 16)
- devInfo['remainder_time'] = round(leftSeconds / 60.0, 2) #
- return devInfo
- def get_dev_setting(self):
- devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
- {'IMEI': self._device['devNo'], "funCode": 'C4', 'data': '00'})
- data = devInfo['data']
- startMoney = int(data[6:8], 16)
- startTime = int(data[8:10], 16)
- addTime = int(data[10:12], 16)
- coinMoney = int(data[12:16], 16)
- memorySwitch = True if int(data[16:18], 16) else False
- pulseSwitch = True if int(data[18:20], 16) else False
- return {'startMoney': startMoney, 'startTime': startTime, 'addTime': addTime, 'coinMoney': coinMoney,
- 'memorySwitch': memorySwitch, 'pulseSwitch': pulseSwitch}
- # 获取设备配置参数
- def set_dev_setting(self, setConf):
- data = fill_2_hexByte(hex(setConf['startMoney']), 2)
- data += fill_2_hexByte(hex(setConf['startTime']), 2)
- data += fill_2_hexByte(hex(setConf['addTime']), 2)
- if setConf['coinMoney'] == 0:
- data += '00'
- else:
- data += 'FF'
- data += '01' if setConf['memorySwitch'] else '00'
- data += '01' if setConf['pulseSwitch'] else '00'
- data += '0000'
- devInfo = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC,
- {'IMEI': self._device['devNo'], "funCode": 'C3', 'data': data})
- if devInfo.has_key('rst') and devInfo['rst'] != 0:
- return JsonResponse({"result": 0, "description": u'网络连接异常,停止设备失败,请您重新尝试停掉设备', "payload": ''})
- def set_device_function_param(self, request, lastSetConf):
- if request.POST.has_key('startMoney'):
- lastSetConf.update({'startMoney': int(request.POST.get('startMoney', 0))})
- lastSetConf.update({'startTime': int(request.POST.get('startTime', 0))})
- lastSetConf.update({'addTime': int(request.POST.get('addTime', 0))})
- self.set_dev_setting(lastSetConf)
- if request.POST.has_key('memorySwitch'):
- lastSetConf.update({'memorySwitch': int(request.POST.get('memorySwitch', 0))})
- self.set_dev_setting(lastSetConf)
- if request.POST.has_key('pulseSwitch'):
- lastSetConf.update({'pulseSwitch': int(request.POST.get('pulseSwitch', 0))})
- self.set_dev_setting(lastSetConf)
- def support_count_down(self, openId = None, port = None):
- if openId is None and port is None:
- return True
- ctrInfo = Device.get_dev_control_cache(self._device['devNo'])
- try:
- port1Info = ctrInfo.get('1', {})
- port2Info = ctrInfo.get('2', {})
- if port1Info.has_key('openId') and port1Info['openId'] == openId and port1Info['status'] in [
- Const.DEV_WORK_STATUS_WORKING, Const.DEV_WORK_STATUS_PAUSE]:
- return True
- if port2Info.has_key('openId') and port2Info['openId'] == openId and port2Info['status'] in [
- Const.DEV_WORK_STATUS_WORKING, Const.DEV_WORK_STATUS_PAUSE]:
- return True
- except Exception, e:
- return False
- return False
- def set_device_function(self, request, lastSetConf):
- if request.POST.has_key('coinMoney'):
- # coinMoney = 0 if request.POST.get('coinMoney') == 'true' else 1
- coinMoney = int(not request.get("coinMoney", False))
- lastSetConf.update({'coinMoney': coinMoney})
- self.set_dev_setting(lastSetConf)
|