123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #!/usr/bin/env python
- # encoding: utf-8
- """
- @CreateTime: 2022/08/23 14:53
- @Author: lxc
- @LastEditTime:
- @Desctiption: sct1501的PLC自动开关剖面功能:根据单点流速仪中的流速数值判断,大于0.3m/s时停止剖面,小于等于0.3m/s时开始剖面
- """
- from event_storage import EventStorage
- from log import OutPutLog
- import time
- from datetime import datetime
- from utility import Utility
- class AutoProfile1501:
- def __init__(self):
- self._storage = EventStorage()
- self._log = OutPutLog()
- def run(self):
- self._log.info("[AutoSwitchDevice] - AutoSwitchDevice module is running!")
- # c7:单点流速仪:流速(cm/s)
- # c88:PLC剖面功能(0=未开启,1=开启)
- self.point_list = ["c7", "c88"]
- run_time = 0
- while True:
- this_time = time.time()
- if this_time - run_time > 60:
- run_time = this_time
- self.now_time = datetime.now().strftime("%Y-%m-%d %H:%M:%M")
- real_data_dict = self._storage.get_real_data(self.point_list)
- flow_speed = real_data_dict['c7']
- profile_status = real_data_dict['c88']
- print(f"{self.now_time} [AutoProfile1501]: {flow_speed} {profile_status}")
- if flow_speed is None or profile_status is None:
- print(f"{self.now_time} [AutoProfile1501]: 参数读取失败,当前流速为{flow_speed}cm/s, 剖面状态为{profile_status}(0=关闭,1=开启)")
- else:
- flow_speed = float(flow_speed)
- profile_status = int(profile_status)
- if flow_speed > 0.3 and profile_status == 1:
- # 流速大于0.3m/s时且正在剖面中,停止剖面
- print(f"{self.now_time} [AutoProfile1501]: 当前流速为{flow_speed}cm/s, 停止剖面")
- self.control_device(station_name="sct1501", device_id=1, start_addr=3142, output_value=0, function_code=5)
- elif 0 <= flow_speed <= 0.3 and profile_status == 0:
- # 流速小于等于0.3m/s时且没有开启剖面,开启剖面
- print(f"{self.now_time} [AutoProfile1501]: 当前流速为{flow_speed}cm/s, 开启剖面")
- self.control_device(station_name="sct1501", device_id=1, start_addr=3142, output_value=1, function_code=5)
- def control_device(self, station_name, device_id, start_addr, output_value, function_code):
- """给设备发送指令"""
- command = [{"device_id": device_id, "start_addr": start_addr, "output_value": output_value, "function_code": function_code}]
- status = True
- t1 = time.time()
- while status:
- try:
- time.sleep(2)
- Utility.available_connectors[station_name].send_command(command)
- # 判断剖面状态是否修改成功
- real_data_dict = self._storage.get_real_data(self.point_list)
- flow_speed = real_data_dict['c7']
- profile_status = real_data_dict['c88']
- if flow_speed is None or profile_status is None:
- print(f"{self.now_time} [AutoProfile1501]: 修改剖面状态失败(参数读取失败): 当前流速为{flow_speed}cm/s, 剖面状态为{profile_status}(0=关闭,1=开启)")
- else:
- flow_speed = float(flow_speed)
- profile_status = int(profile_status)
- if profile_status == output_value:
- print(f"{self.now_time} [AutoProfile1501]: 修改剖面状态成功,当前流速为{flow_speed}cm/s, 当前剖面状态为{profile_status}(0=关闭,1=开启)!")
- status = False
-
- except Exception as e:
- print(f"{self.now_time} [AutoProfile1501]: {e}")
- finally:
- if time.time() - t1 >= 60:
- print(f"{self.now_time} [AutoProfile1501]: 修改剖面状态失败!")
- status = False
|