kuaichong.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import logging
  4. from apps.web.constant import Const, DeviceErrorCodeDesc, ErrorCode, DeviceOnlineStatus, DeviceCmdCode, MQTT_TIMEOUT
  5. import time
  6. import simplejson as json
  7. from apps.web.core.adapter.base import OnlineSmartBox
  8. from apps.web.core.exceptions import ServiceException
  9. from apps.web.core.networking import MessageSender
  10. logger = logging.getLogger(__name__)
  11. class GprsQuickChargeBox(OnlineSmartBox):
  12. def __init__(self, device):
  13. super(GprsQuickChargeBox, self).__init__(device)
  14. def is_port_can_use(self, port, canAdd = False):
  15. return True, u''
  16. def get_port_status(self, force = False):
  17. return {
  18. '1': {'status': Const.DEV_WORK_STATUS_IDLE},
  19. '2': {'status': Const.DEV_WORK_STATUS_IDLE}
  20. }
  21. def check_dev_status(self, attachParas = None):
  22. if 'chargeIndex' not in attachParas:
  23. raise ServiceException(
  24. {'result': 2, 'description': u'请扫描带有端口编号的二维码图片'})
  25. if not self.device.need_fetch_online:
  26. raise ServiceException(
  27. {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)})
  28. if self.device.online == DeviceOnlineStatus.DEV_STATUS_ONLINE:
  29. retry = 3
  30. timeout = 12
  31. else:
  32. retry = 2
  33. timeout = 10
  34. dev_info = MessageSender.send(device = self.device, cmd = DeviceCmdCode.GET_DEVINFO,
  35. payload = {'IMEI': self._device['devNo'], 'fields': ['power1', 'power2']},
  36. timeout = timeout, retry = retry)
  37. if 'rst' not in dev_info:
  38. raise ServiceException(
  39. {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)})
  40. if dev_info['rst'] == ErrorCode.DEVICE_SUCCESS:
  41. port = int(attachParas['chargeIndex'])
  42. if port == 1 and dev_info['power1'] == 'false':
  43. raise ServiceException(
  44. {'result': 0, 'description': u'1号端口空载,请将电瓶接到指定的端口'})
  45. if port == 2 and dev_info['power2'] == 'false':
  46. raise ServiceException(
  47. {'result': 0, 'description': u'2号端口空载,请将电瓶接到指定的端口'})
  48. else:
  49. raise ServiceException(
  50. {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)})
  51. def test(self, coins):
  52. if not self._device.online:
  53. raise ServiceException({'result': 2, 'description': u'设备离线'})
  54. result = MessageSender.send(device = self.device, cmd = DeviceCmdCode.GET_DEVINFO,
  55. payload = {'IMEI': self._device['devNo'], 'fields': ['power1', 'power2']})
  56. if result['rst'] == 0:
  57. if result['power1'] == 'true':
  58. return MessageSender.net_pay(self.device, coins, port = 1)
  59. elif result['power2'] == 'true':
  60. return MessageSender.net_pay(self.device, coins, port = 2)
  61. else:
  62. raise ServiceException({'result': 0, 'description': u'端口空载'})
  63. return result
  64. def start_device(self, package, openId, attachParas):
  65. pay_count = int(package['coins'])
  66. port = attachParas.get('chargeIndex', None) if attachParas else None
  67. if not port:
  68. raise ServiceException({'result': 2, 'description': u'参数错误'})
  69. result = MessageSender.net_pay(self.device, pay_count, timeout = MQTT_TIMEOUT.START_DEVICE, port = int(port))
  70. if result['rst'] != 0:
  71. logger.debug('gprs quick charge failed to start, result = %s' % (json.dumps(result),))
  72. current_dev_type_name = self.device.devTypeName
  73. if current_dev_type_name == u'其他':
  74. current_dev_type_name = u'自助设备'
  75. if result['rst'] == ErrorCode.BOARD_FAULT:
  76. description = u'当前' + current_dev_type_name + u'故障,建议您试试旁边其他设备'
  77. elif result['rst'] == ErrorCode.PORT_NO_LOAD:
  78. description = u'端口空载,请接负载到指定的端口'
  79. elif result['rst'] == ErrorCode.DEVICE_CONN_FAIL:
  80. description = u'当前' + current_dev_type_name + u'正在玩命找网络,您的金币还在,重试不需要重新付款再试尝试不会扣费,建议您试试旁边其他设备,或者试试投硬币,或者稍后再试哦'
  81. else:
  82. description = DeviceErrorCodeDesc.get(result['rst'])
  83. raise ServiceException({'result': 2, 'description': description})
  84. try:
  85. duration = self.get_duration(package = package)
  86. result['finishedTime'] = (int(time.time()) + duration)
  87. except Exception as e:
  88. logger.exception('error = %s' % e)
  89. return result