offline.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import datetime
  4. import json
  5. import time
  6. from apps import serviceCache
  7. from apps.web.core.adapter.base import SmartBox, encode
  8. from apps.web.core.exceptions import ServiceException
  9. class OfflineSmartBox(SmartBox):
  10. def __init__(self, device):
  11. super(OfflineSmartBox, self).__init__(device)
  12. def get_port_status(self, force = False):
  13. raise ServiceException({'result': 2, 'description': u'设备处于网络离线状态,正在玩命找网络,建议您试试旁边其他设备,或者试试投硬币,或者稍后再试哦'})
  14. def check_dev_status(self, attachParas = None):
  15. if not self.device['status']:
  16. raise ServiceException({'result': 2, 'description': u'设备正在玩命找网络,建议您试试旁边其他设备,或者试试投硬币,或者稍后再试哦'})
  17. def __encode(self, package):
  18. now = datetime.datetime.today()
  19. time_stamp = now.minute * 60 + now.second
  20. pay_count = package['coins']
  21. return encode(self._device['devNo'], time_stamp, pay_count)
  22. def start_device(self, package, openId, attachParas):
  23. token_ids = serviceCache.get('tokens_%s' % self._device['devNo'], [])
  24. current_token_id = 'token_%s' % openId
  25. if current_token_id not in token_ids:
  26. token_ids.append(current_token_id)
  27. code_set = set()
  28. for token_id in token_ids:
  29. token = serviceCache.get(token_id, None)
  30. if token is not None:
  31. code_set.add(token['code'])
  32. if serviceCache.get(current_token_id, None):
  33. raise ServiceException({})
  34. code = self.__encode(package = package)
  35. while code in code_set:
  36. time.sleep(1)
  37. code = self.__encode(package = package)
  38. token = dict()
  39. token['code'] = code
  40. token['time_stamp'] = int(time.time())
  41. serviceCache.set('tokens_%s' % self._device['devNo'], json.dumps(token_ids))
  42. serviceCache.set(current_token_id, json.dumps(token), 300)
  43. return token