alarm.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """
  2. @Date :2021/5/21/00219:10:57
  3. @Desc :
  4. """
  5. from logging_config import logger
  6. import threading
  7. import time
  8. from event_storage import EventStorage
  9. class Alarm(threading.Thread):
  10. def __init__(self):
  11. super(Alarm, self).__init__()
  12. self._storage = EventStorage()
  13. self._save_frequency = 5
  14. self._last_save_time = 0
  15. def run(self) -> None:
  16. self.overrun_alarm()
  17. def get_real_time_data(self):
  18. """
  19. :return: data_dict {'c1': '064', 'c2': '0.1', 'c3': '20.3', 'c4': '43.2', 'c5': '1025.1', 'c6': '0.25', 'c81': '29.823', 'c82': '104.507', 'c83': '253.153'...}
  20. """
  21. point_info = self._storage.hardDiskStorage.get_point_info(point_tuple=None)
  22. keys_list = []
  23. for index in point_info:
  24. keys_list.append('c' + str(index['serial_number']))
  25. data_dict = self._storage.memoryStorage.get_value(keys_list)
  26. return data_dict
  27. def get_point_table(self):
  28. """
  29. 获取所有点的点表,并增加alarm_status属性
  30. :return: point_info 字典组成的列表
  31. """
  32. point_info = self._storage.hardDiskStorage.get_point_info(point_tuple=None)
  33. for obj in point_info:
  34. obj['alarm_status'] = 0
  35. return point_info
  36. def update_point_table(self, point_info):
  37. """
  38. 更新点表,主要更新报警上限和报警下限
  39. :param point_info: 更新前的点表
  40. :return: 更新后的点表
  41. """
  42. new = self._storage.hardDiskStorage.get_point_info(point_tuple=None)
  43. for i in range(0, len(new)):
  44. point_info[i]['alarm_low_limit'] = new[i]['alarm_low_limit']
  45. point_info[i]['alarm_up_limit'] = new[i]['alarm_up_limit']
  46. # 越限报警
  47. def overrun_alarm(self):
  48. logger.info('Over run alarm module is running!')
  49. try:
  50. point_info = self.get_point_table()
  51. while 1:
  52. self.update_point_table(point_info)
  53. data_dict = self.get_real_time_data()
  54. for index in point_info:
  55. key = 'c' + str(index['serial_number'])
  56. if data_dict[key]: # 数据不为空且报警状态为零
  57. data_dict[key] = float(data_dict[key])
  58. if index['alarm_low_limit'] is None or index['alarm_up_limit'] is None: # 未设置报警限值
  59. continue
  60. elif index['alarm_low_limit'] <= data_dict[key] <= index['alarm_up_limit']: # 在合理范围内
  61. index['alarm_status'] = 0
  62. else: # 数据越限
  63. if index['alarm_status'] == 0: # alarm_status == 0:表示第一次报警,存储报警信息
  64. alarm_unit = {'name': "'" + key + "'", 'data': data_dict[key]}
  65. table_name = "alarm_data_tbl" # 报警存储表名,可以通过配置文件配置
  66. alarm_time = time.strftime("%Y-%m-%d %H:%M:%S")
  67. self._storage.hardDiskStorage.insert_column_many(table_name, alarm_time, alarm_unit)
  68. index['alarm_status'] = 1
  69. elif index['alarm_status'] == 1: # alarm_status == 1:表示本次报警期间非第一次检测的越限
  70. continue
  71. time.sleep(1)
  72. except Exception as e:
  73. logger.error(e)
  74. def overrun_alarm_storage(self, table_name, save_time, item):
  75. pass
  76. # 变位报警
  77. def displacement_alarm(self):
  78. logger.info('[displacement_alarm] - Displacement alarm module is running!')
  79. point_info = self._storage.hardDiskStorage.get_point_info(point_tuple=None)
  80. keys_list = []
  81. for index in point_info:
  82. keys_list.append('c' + str(index['serial_number']))
  83. last_data_dict = self._storage.memoryStorage.get_value(keys_list)
  84. while 1:
  85. now_data_dict = self._storage.memoryStorage.get_value(keys_list)
  86. for index in point_info:
  87. key = 'c' + str(index['serial_number'])
  88. if index['signal_type'] == 'Switch' and now_data_dict[key]:
  89. if now_data_dict[key] != last_data_dict[key]:
  90. print(now_data_dict[key], last_data_dict[key])
  91. else:
  92. pass
  93. last_data_dict = now_data_dict
  94. print(last_data_dict)
  95. time.sleep(1)
  96. def displacement_alarm_storage(self):
  97. pass
  98. if __name__ == '__main__':
  99. alarm = Alarm().start()