cmCZ.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # coding=utf-8
  2. from apps.web.core.device_define.cmCZ import DEV_PREFIX
  3. from apps.web.device.models import Device, DeviceDict
  4. from apps.web.eventer import EventBuilder
  5. from apps.web.eventer.base import WorkEvent
  6. from apps.web.eventer.cmCZSub import cmCZSubEventWorker, cmCZSubComNetPayAckEvent, cmCZSubIdStartAckEvent, cmCZSubAckEventProcessorIntf
  7. class builder(EventBuilder):
  8. """
  9. gateway是代理人的角色 目前网关没有事件 因此所有信息都转到sub
  10. """
  11. def __getEvent__(self, device_event):
  12. if "order_id" in device_event:
  13. addr = device_event["addr"]
  14. devNo = "{}{}".format(DEV_PREFIX, addr)
  15. sub = Device.get_dev(devNo)
  16. if device_event["order_type"] == "com_start":
  17. return cmCZSubComNetPayAckEvent(sub.deviceAdapter, device_event)
  18. if device_event["order_type"] == "id_start":
  19. return cmCZSubIdStartAckEvent(sub.deviceAdapter, device_event, pre_processor=cmCZSubAckEventProcessorIntf())
  20. elif "subs" in device_event:
  21. return CMCZGateway(self.device.deviceAdapter, device_event)
  22. else:
  23. addr = int(device_event["data"][6: 14], 16)
  24. devNo = "{}{}".format(DEV_PREFIX, addr)
  25. sub = Device.get_dev(devNo)
  26. event_data = sub.deviceAdapter.analyze_event_data(device_event["data"])
  27. return cmCZSubEventWorker(sub.deviceAdapter, event_data)
  28. class CMCZGateway(WorkEvent):
  29. """
  30. 无用的 主板没有实现该协议
  31. """
  32. def do(self, **args):
  33. if not self.event_data["subs"]:
  34. return
  35. for sub, signal in self.event_data["subs"].items():
  36. dev = Device.get_dev("{}{}".format(DEV_PREFIX, sub)) # type: DeviceDict
  37. if not dev:
  38. continue
  39. # 信号上报的时候 也将整机上报上去
  40. other = dev.otherConf
  41. if "master" not in other:
  42. other["master"] = self.device.devNo
  43. Device.objects.filter(devNo=dev.devNo).first().update(
  44. otherConf=other,
  45. coreVer=self.device.coreVer,
  46. softVer=self.device.softVer,
  47. hwVer=self.device.hwVer,
  48. driverCode=self.device.driverCode,
  49. driverVersion=self.device.driverVersion
  50. )
  51. Device.invalid_device_cache(dev.devNo)
  52. if signal:
  53. dev.set_online(30)
  54. else:
  55. dev.set_offline()