dianchuanServer.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # coding=utf-8
  2. import typing
  3. from apps.web.constant import DeviceCmdCode, MQTT_TIMEOUT
  4. from apps.web.core.adapter.zhixia2 import ChargingZHIXIA2Box
  5. from apps.web.core.device_define.cmCZ import DEFAULT_ACCOUNT_RULE
  6. from apps.web.core.exceptions import ServiceException
  7. from apps.web.core.networking import MessageSender
  8. if typing.TYPE_CHECKING:
  9. from apps.web.user.models import ConsumeRecord
  10. class DCServerBox(ChargingZHIXIA2Box):
  11. def _send_data(self, funCode, data, cmd=DeviceCmdCode.OPERATE_DEV_SYNC, timeout=MQTT_TIMEOUT.NORMAL):
  12. # 若是网关发送 则addr地址填充为0
  13. payload = {
  14. "IMEI": self.device.devNo,
  15. "funCode": funCode
  16. }
  17. if isinstance(data, dict):
  18. payload.update(data)
  19. else:
  20. payload["data"] = data
  21. result = MessageSender.send(
  22. device=self.device,
  23. cmd=cmd,
  24. payload=payload,
  25. timeout=timeout
  26. )
  27. if "rst" not in result:
  28. raise ServiceException({"result": 2, "description": u"串口通信异常(10001)"})
  29. if result["rst"] != 0:
  30. if result["rst"] == -1:
  31. raise ServiceException({"result": 2, "description": u"网络通信异常"})
  32. if result["rst"] == 1:
  33. raise ServiceException({"result": 2, "description": u"串口通信异常(10002)"})
  34. return result
  35. @property
  36. def deviceRule(self):
  37. rule = self.device.otherConf.get("rule", DEFAULT_ACCOUNT_RULE["rule"])
  38. for _item in rule:
  39. _item["price"] *= 100
  40. return rule
  41. @property
  42. def deviceDefaultPrice(self):
  43. defaultPrice = self.device.otherConf.get("defaultPrice", DEFAULT_ACCOUNT_RULE["defaultPrice"])
  44. return defaultPrice * 100
  45. def start_device_realiable(self, order): # type:(ConsumeRecord)->dict
  46. if order.is_on_point:
  47. raise ServiceException({"result": 2, "description": u"当前设备不支持远程上分,请直接给用户派币启动"})
  48. attachParas = order.attachParas
  49. package = order.package
  50. if attachParas is None:
  51. raise ServiceException({"result": 2, "description": u"请您选择合适的充电线路"})
  52. if "chargeIndex" not in attachParas:
  53. raise ServiceException({"result": 2, "description": u"请您选择合适的充电线路"})
  54. # portInfo = self._get_port_info(attachParas["chargeIndex"])
  55. # if portInfo["status"] != Const.DEV_WORK_STATUS_IDLE:
  56. # raise ServiceException({"result": 2, "description": u"当前端口正在工作中,请选择其他端口"})
  57. port = int(attachParas["chargeIndex"])
  58. rule = self.deviceRule
  59. defaultPrice = self.deviceDefaultPrice
  60. data = {
  61. 'order_type': "com_start",
  62. 'chargeTime': package["time"],
  63. "rule": rule,
  64. "defaultPrice": defaultPrice,
  65. "port": port,
  66. "order_id": order.orderNo,
  67. }
  68. return self._send_data(funCode="02", data=data)
  69. def analyze_event_data(self, data):
  70. pass