zhijinji.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. from apps.web.core.adapter.base import *
  4. from apps.web.device.models import Device, StockRecord
  5. class TissueBox(SmartBox):
  6. def __init__(self, device):
  7. super(TissueBox, self).__init__(device)
  8. def analyze_event_data(self,data):
  9. if data['faultCode'] == 1:
  10. return {'status':Const.DEV_WORK_STATUS_FAULT,'statusInfo':u'缺纸告警'}
  11. elif data['faultCode'] ==Const.DEV_WORK_STATUS_FAULT:
  12. return {'status':Const.DEV_WORK_STATUS_FAULT,'statusInfo':u'电机故障'}
  13. return {'status':Const.DEV_WORK_STATUS_IDLE,'statusInfo':''}
  14. def check_dev_status(self,attachParas=None):
  15. if not self.device.need_fetch_online:
  16. raise ServiceException(
  17. {'result': 2, 'description': DeviceErrorCodeDesc.get(ErrorCode.DEVICE_CONN_CHECK_FAIL)})
  18. if self.device.online == DeviceOnlineStatus.DEV_STATUS_ONLINE:
  19. retry = 3
  20. timeout = 12
  21. else:
  22. retry = 2
  23. timeout = 10
  24. devInfo = MessageSender.send(self.device, DeviceCmdCode.GET_DEVINFO, {'IMEI': self._device['devNo']},
  25. timeout = timeout, retry = retry)
  26. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  27. if devInfo['rst'] == -1:
  28. raise ServiceException(
  29. {'result': 2, 'description': u'当前纸巾机正在玩命找网络,请您稍候再试'})
  30. elif devInfo['rst'] == 1:
  31. raise ServiceException(
  32. {'result': 2, 'description': u'当前纸巾机忙,无响应,请您稍候再试'})
  33. if devInfo.get('isTissueEmpty', False):
  34. Device.update_field(
  35. dev_no = self._device['devNo'],
  36. update = True,
  37. quantity = 0)
  38. raise ServiceException(
  39. {'result': 2, 'description': u'您来迟了一步,当前纸巾机已经没有纸巾了,建议您试试周围其他机子哦'})
  40. #请求设备出纸巾,count表示出的数目
  41. def sendOutPaper(self,count):
  42. result = MessageSender.send(self.device, DeviceCmdCode.PAY_MONEY,
  43. {'app_pay': int(count), 'IMEI': self._device['devNo']}, timeout = MQTT_TIMEOUT.START_DEVICE)
  44. if result.has_key('rst') and result['rst'] != 0:
  45. if result['rst'] == -1:
  46. description = u'当前纸巾机开小差了,可能是网络信号差,您稍候试试,或者找找周边的其他纸巾机'
  47. raise ServiceException({'result': 2, 'description': description})
  48. elif result['rst'] == 1:
  49. description = u'当前纸巾机繁忙无响应,您稍后再试'
  50. raise ServiceException({'result': 2, 'description': description})
  51. return {'rst':0,'IMEI':self._device['devNo'],'out':count}
  52. #免费领纸巾
  53. def start_free(self, token, count = 1):
  54. result = self.sendOutPaper(count)
  55. #更新设备的库存信息
  56. self._device['consumptionQuantity'] += result['out']
  57. self._device['quantity'] -= result['out']
  58. if self._device['quantity'] <= 0:
  59. self._device['quantity'] = 0
  60. Device.update_field(dev_no = self._device['devNo'],
  61. update = True,
  62. consumptionQuantity = self._device['consumptionQuantity'],
  63. quantity = self._device['quantity'])
  64. # 用户成功消耗纸巾,需要把库存记录到数据库中
  65. payload = {
  66. 'token': token,
  67. 'logicCode': self._device['logicalCode'],
  68. 'imei': self._device['devNo'],
  69. 'stockType':'adFree',
  70. 'stockTime': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
  71. 'number':count,
  72. 'more':u'免费送'
  73. }
  74. StockRecord(**payload).save()
  75. return result
  76. def start_device(self, package, openId, attachParas):
  77. #: 首先检查设备是否在线,是否有库存
  78. coins = int(package['coins'])
  79. devInfo = self.sendOutPaper(coins)
  80. # 更新设备的库存信息
  81. self._device['consumptionQuantity'] += devInfo['out']
  82. self._device['quantity'] -= devInfo['out']
  83. if self._device['quantity'] <= 0:
  84. self._device['quantity'] = 0
  85. Device.update_field(
  86. dev_no = self._device['devNo'],
  87. update = True,
  88. consumptionQuantity = self._device['consumptionQuantity'],
  89. quantity = self._device['quantity'])
  90. #用户成功消耗纸巾,需要把库存记录到数据库中
  91. try:
  92. stockTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  93. StockRecord.get_collection().insert({'logicCode':self._device['logicalCode'],'imei':self._device['devNo'],
  94. 'stockType':'consume','stockTime':stockTime,'number':devInfo['out']})
  95. except Exception ,e:
  96. logger.info('record tissue device=%s consume record exception=%s' % (self._device['devNo'],e))
  97. Device.update_dev_control_cache(self._device['devNo'], {'openId':openId,'vCardId':self._vcard_id})
  98. return devInfo
  99. def get_dev_setting(self):
  100. devInfo = MessageSender.send(self.device, DeviceCmdCode.GET_DEVINFO, {'IMEI': self._device['devNo']})
  101. if devInfo.has_key('rst') and devInfo['rst'] != 0:
  102. if devInfo['rst'] == -1:
  103. raise ServiceException(
  104. {'result': 2, 'description': u'当前纸巾机正在玩命找网络,请您稍候再试'})
  105. elif devInfo['rst'] == 1:
  106. raise ServiceException(
  107. {'result': 2, 'description': u'当前纸巾机忙,无响应,请您稍候再试'})
  108. return {'stop_delay': devInfo['stop_delay'], 'tissue_timeout': devInfo['tissue_timeout']}
  109. def set_dev_setting(self, stop_delay, tissue_timeout):
  110. result = MessageSender.send(self.device, 202, {
  111. "cmd": 202,
  112. "IMEI": self._device['devNo'],
  113. "motor_set":
  114. {
  115. "stop_delay": stop_delay,
  116. "tissue_timeout": tissue_timeout
  117. }
  118. })
  119. if result['rst'] == -1:
  120. raise ServiceException(
  121. {'result': 2, 'description': u'当前纸巾机正在玩命找网络,请您稍候再试'})
  122. elif result['rst'] == 1:
  123. raise ServiceException(
  124. {'result': 2, 'description': u'当前纸巾机忙,无响应,请您稍候再试'})
  125. return
  126. def set_device_function_param(self,request,lastSetConf):
  127. setting = {
  128. 'stop_delay': request.POST.get('stop_delay'),
  129. 'tissue_timeout': request.POST.get('tissue_timeout')
  130. }
  131. from apps.web.device.validation import DeviceSettingSchema
  132. setting = DeviceSettingSchema.TISSUE_BOX(setting)
  133. self.set_dev_setting(**setting)