# -*- coding: utf-8 -*- # !/usr/bin/env python import time import uuid from apilib.utils_json import JsonResponse from apps.web.constant import Const, DeviceCmdCode, ErrorCode, DeviceErrorCodeDesc from apps.web.core.adapter.base import hexbyte_2_bin, SmartBox from apps.web.core.exceptions import ServiceException from apps.web.core.networking import MessageSender from apps.web.device.models import Device class PedicureBox(SmartBox): def __init__(self, device): super(PedicureBox, self).__init__(device) # 串口设备的状态检测,需要单独进行,和非串口的不一样 def check_dev_status(self, attachParas = None): devInfo = self.get_dev_info() if devInfo.has_key('rst') and devInfo['rst'] != 0: if devInfo['rst'] == ErrorCode.DEVICE_CONN_FAIL: raise ServiceException( {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)}) elif devInfo['rst'] == ErrorCode.BOARD_UART_TIMEOUT: raise ServiceException( {'result': 2, 'description': u'当前足疗机忙,无响应,请您稍候再试'}) if devInfo['isStart']: Device.update_dev_control_cache( self._device['devNo'], { 'status': Const.DEV_WORK_STATUS_WORKING, 'finishedTime': (int(time.time()) + 60) }) raise ServiceException({'result': 2, 'description': u'足疗机正在为您服务,结束后,才可以充值哦'}) else: Device.update_dev_control_cache(self._device['devNo'], {'status': Const.DEV_WORK_STATUS_IDLE}) if devInfo['status'] == Const.DEV_WORK_STATUS_FAULT: raise ServiceException({'result': 2, 'description': u'足疗机出现故障:%s,您找下管理员吧' % devInfo['statusInfo']}) # 足疗机的事件报文和get上来的报文一样,直接调用即可 def analyze_event_data(self, data): return self.analyze_get_data(data) # 解析get到的报文信息 def analyze_get_data(self, data): result = {} if 'F2F2F008' not in data: return result status = Const.DEV_WORK_STATUS_WORKING fault = '' # 解析返回的报文,报文格式F2F2... resultData = data[8:24] if resultData[0:2] == '00': isStart = False else: isStart = True hTime = resultData[2:4] mTime = resultData[4:6] sTime = resultData[6:8] leftTime = int(hTime, 16) * 3600 + int(mTime, 16) * 60 + int(sTime, 16) totalTime = int(resultData[8:12], 16) * 3600 + int(resultData[12:13], 16) hexFault = resultData[14:16] binFault = hexbyte_2_bin(hexFault) if binFault[0] == '1': status = Const.DEV_WORK_STATUS_FAULT fault = u'堵转故障' if binFault[1] == '1': fault = u'按摩脚霍尔传感器不归位' if binFault[2] == '1': fault = u'付款没有开机故障' if leftTime == 0 and fault == '': status = Const.DEV_WORK_STATUS_IDLE return {'status': status, 'fault': fault, 'isStart': isStart, 'totalTime': totalTime, 'leftTime': leftTime} def get_dev_info(self): result = MessageSender.send(self.device, DeviceCmdCode.PASSTHROUGH_OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], 'funCode': 'F0', 'data': 'F2F2F00100'}) if result['rst'] != 0: return {'rst': -1, 'isOffline': 1} result.update(self.analyze_get_data(result['data'])) return result def send_time(self, time): oxTime = hex(time)[2::] oxTime = '0000' + oxTime oxTime = oxTime[-4::] uuId = str(uuid.uuid4()).replace('-', '')[0:16] data = uuId + oxTime result = MessageSender.send(self.device, DeviceCmdCode.OPERATE_DEV_SYNC, {'IMEI': self._device['devNo'], 'funCode': '05', 'data': data}) if 'rst' in result and result['rst'] != 0: current_dev_type_name = self._device['devType']['name'] if current_dev_type_name == u'其他': current_dev_type_name = u'自助设备' description = u'当前' + current_dev_type_name + u'充值失败,可能是当前网络状况差,请稍候再试' raise ServiceException({'result': 2, 'description': description}) status = result['data'][24:26] if status == '00': raise ServiceException({'result': 2, 'description': u'设备启动异常,请联系客服'}) hour = int(result['data'][26:28], 16) minute = int(result['data'][28:30], 16) second = int(result['data'][30:32], 16) left_time = 3600 * hour + 60 * minute + second return result, left_time def start_device(self, package, openId, attachParas): unit = package.get('unit', u'分钟') if unit == u'分钟': duration = int(package['time']) elif unit == u'小时': duration = int(package['time']) * 60 elif unit == u'天': duration = int(package['time']) * 1440 else: duration = int(package['time']) dev_info, left_time = self.send_time(duration) if left_time == 0: Device.update_dev_control_cache(self._device['devNo'], {'status': Const.DEV_WORK_STATUS_IDLE}) return dev_info need_time = duration * 60 start_time = int(time.time()) service_cache = Device.get_dev_control_cache(self._device['devNo']) if 'finishedTime' not in service_cache or service_cache['finishedTime'] > int(time.time()): if 'startTime' in service_cache: start_time = service_cache['startTime'] if 'needTime' in service_cache: need_time = need_time + int(service_cache['needTime']) finished_time = start_time + need_time Device.update_dev_control_cache( self._device['devNo'], { 'openId': openId, 'status': Const.DEV_WORK_STATUS_WORKING, 'startTime': start_time, 'finishedTime': finished_time, 'leftTime': left_time }) return dev_info def support_count_down(self, openId = None, port = None): return True def count_down(self, request, dev, agent, group, devType, lastOpenId, port = None): dev_info = self.get_dev_info() if dev_info['rst'] != 0: return JsonResponse({'result': 0, 'description': u'足疗机网络不太好,再试试哦'}) result = { 'result': 1, 'description': '', 'payload': { 'surplus': 0.0, 'sum': 0.0, 'name': group['groupName'], 'address': group['address'], 'code': devType.get('code'), 'orderProcessing': False, 'logicalCode': dev['logicalCode'], 'user': 'me' if lastOpenId == request.user.openId else 'notme', 'agentFeatures': agent.features, } } if dev_info['leftTime'] == 0: Device.invalid_device_control_cache(self._device['devNo']) return JsonResponse(result) service_cache = Device.get_dev_control_cache(dev['devNo']) left_time = dev_info['leftTime'] need_time = left_time if 'startTime' in service_cache and 'finishedTime' in service_cache: need_time = service_cache['finishedTime'] - service_cache['startTime'] result.update({ 'surplus': left_time / 60.0, 'sum': need_time / 60.0, }) return result