auto_profile_1501.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. @CreateTime: 2022/08/23 14:53
  5. @Author: lxc
  6. @LastEditTime:
  7. @Desctiption: sct1501的PLC自动开关剖面功能:根据单点流速仪中的流速数值判断,大于0.3m/s时停止剖面,小于等于0.3m/s时开始剖面
  8. """
  9. from event_storage import EventStorage
  10. from log import OutPutLog
  11. import time
  12. from datetime import datetime
  13. from utility import Utility
  14. class AutoProfile1501:
  15. def __init__(self):
  16. self._storage = EventStorage()
  17. self._log = OutPutLog()
  18. def run(self):
  19. self._log.info("[AutoSwitchDevice] - AutoSwitchDevice module is running!")
  20. # c7:单点流速仪:流速(cm/s)
  21. # c88:PLC剖面功能(0=未开启,1=开启)
  22. self.point_list = ["c7", "c88"]
  23. run_time = 0
  24. while True:
  25. this_time = time.time()
  26. if this_time - run_time > 60:
  27. run_time = this_time
  28. self.now_time = datetime.now().strftime("%Y-%m-%d %H:%M:%M")
  29. real_data_dict = self._storage.get_real_data(self.point_list)
  30. flow_speed = real_data_dict['c7']
  31. profile_status = real_data_dict['c88']
  32. print(f"{self.now_time} [AutoProfile1501]: {flow_speed} {profile_status}")
  33. if flow_speed is None or profile_status is None:
  34. print(f"{self.now_time} [AutoProfile1501]: 参数读取失败,当前流速为{flow_speed}cm/s, 剖面状态为{profile_status}(0=关闭,1=开启)")
  35. else:
  36. flow_speed = float(flow_speed)
  37. profile_status = int(profile_status)
  38. if flow_speed > 0.3 and profile_status == 1:
  39. # 流速大于0.3m/s时且正在剖面中,停止剖面
  40. print(f"{self.now_time} [AutoProfile1501]: 当前流速为{flow_speed}cm/s, 停止剖面")
  41. self.control_device(station_name="sct1501", device_id=1, start_addr=3142, output_value=0, function_code=5)
  42. elif 0 <= flow_speed <= 0.3 and profile_status == 0:
  43. # 流速小于等于0.3m/s时且没有开启剖面,开启剖面
  44. print(f"{self.now_time} [AutoProfile1501]: 当前流速为{flow_speed}cm/s, 开启剖面")
  45. self.control_device(station_name="sct1501", device_id=1, start_addr=3142, output_value=1, function_code=5)
  46. def control_device(self, station_name, device_id, start_addr, output_value, function_code):
  47. """给设备发送指令"""
  48. command = [{"device_id": device_id, "start_addr": start_addr, "output_value": output_value, "function_code": function_code}]
  49. status = True
  50. t1 = time.time()
  51. while status:
  52. try:
  53. time.sleep(2)
  54. Utility.available_connectors[station_name].send_command(command)
  55. # 判断剖面状态是否修改成功
  56. real_data_dict = self._storage.get_real_data(self.point_list)
  57. flow_speed = real_data_dict['c7']
  58. profile_status = real_data_dict['c88']
  59. if flow_speed is None or profile_status is None:
  60. print(f"{self.now_time} [AutoProfile1501]: 修改剖面状态失败(参数读取失败): 当前流速为{flow_speed}cm/s, 剖面状态为{profile_status}(0=关闭,1=开启)")
  61. else:
  62. flow_speed = float(flow_speed)
  63. profile_status = int(profile_status)
  64. if profile_status == output_value:
  65. print(f"{self.now_time} [AutoProfile1501]: 修改剖面状态成功,当前流速为{flow_speed}cm/s, 当前剖面状态为{profile_status}(0=关闭,1=开启)!")
  66. status = False
  67. except Exception as e:
  68. print(f"{self.now_time} [AutoProfile1501]: {e}")
  69. finally:
  70. if time.time() - t1 >= 60:
  71. print(f"{self.now_time} [AutoProfile1501]: 修改剖面状态失败!")
  72. status = False