12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- # -*- coding: utf-8 -*-
- # !/usr/bin/env python
- import datetime
- import json
- import time
- from apps import serviceCache
- from apps.web.core.adapter.base import SmartBox, encode
- from apps.web.core.exceptions import ServiceException
- class OfflineSmartBox(SmartBox):
- def __init__(self, device):
- super(OfflineSmartBox, self).__init__(device)
- def get_port_status(self, force = False):
- raise ServiceException({'result': 2, 'description': u'设备处于网络离线状态,正在玩命找网络,建议您试试旁边其他设备,或者试试投硬币,或者稍后再试哦'})
- def check_dev_status(self, attachParas = None):
- if not self.device['status']:
- raise ServiceException({'result': 2, 'description': u'设备正在玩命找网络,建议您试试旁边其他设备,或者试试投硬币,或者稍后再试哦'})
- def __encode(self, package):
- now = datetime.datetime.today()
- time_stamp = now.minute * 60 + now.second
- pay_count = package['coins']
- return encode(self._device['devNo'], time_stamp, pay_count)
- def start_device(self, package, openId, attachParas):
- token_ids = serviceCache.get('tokens_%s' % self._device['devNo'], [])
- current_token_id = 'token_%s' % openId
- if current_token_id not in token_ids:
- token_ids.append(current_token_id)
- code_set = set()
- for token_id in token_ids:
- token = serviceCache.get(token_id, None)
- if token is not None:
- code_set.add(token['code'])
- if serviceCache.get(current_token_id, None):
- raise ServiceException({})
- code = self.__encode(package = package)
- while code in code_set:
- time.sleep(1)
- code = self.__encode(package = package)
- token = dict()
- token['code'] = code
- token['time_stamp'] = int(time.time())
- serviceCache.set('tokens_%s' % self._device['devNo'], json.dumps(token_ids))
- serviceCache.set(current_token_id, json.dumps(token), 300)
- return token
|