jiuhengIC.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. import logging
  4. from apps.common.utils import int_to_hex
  5. from apps.web.constant import DeviceCmdCode
  6. from apps.web.core.adapter.base import SmartBox, SendManager
  7. from apps.web.core.exceptions import ServiceException
  8. logger = logging.getLogger(__name__)
  9. class JiuHengICRechargeBox(SmartBox):
  10. def start_device(self, package, openId, attachParas):
  11. # todo 简单实现
  12. cardNo = "00000000"
  13. cardCst = "0000"
  14. cardOpe = "02"
  15. payload = {
  16. "funCode": "22",
  17. "data": cardNo + cardCst + cardOpe
  18. }
  19. with SendManager(visitor="client") as sender1:
  20. result1 = sender1.send(
  21. device=self.device,
  22. cmd=DeviceCmdCode.OPERATE_DEV_SYNC,
  23. payload=payload,
  24. timeout=60
  25. )
  26. sender1.rst = result1
  27. cardNoHex = '00000000'
  28. balanceHex = int_to_hex(int(float(package['coins']) * 10), lens=4)
  29. cardOpe = "01"
  30. payload = {
  31. "funCode": "22",
  32. "data": cardNoHex + balanceHex + cardOpe
  33. }
  34. with SendManager(visitor="client") as sender:
  35. result = sender.send(
  36. device=self.device,
  37. cmd=DeviceCmdCode.OPERATE_DEV_SYNC,
  38. payload=payload,
  39. timeout=60
  40. )
  41. sender.rst = result
  42. rstCardNo = result['data'][8:16]
  43. if rstCardNo == '00000000':
  44. raise ServiceException({'result': 2, 'description': u'未检测到充值卡, 请稍后再试'})
  45. return result