alarm_buzzer.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. @CreateTime: 2022/06/16 10:48
  5. @Author: lxc
  6. @LastEditTime:
  7. @Desctiption:根据数据表存储的报警,判断蜂鸣器是否需要报警
  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 AlarmBuzzer:
  15. def __init__(self):
  16. self._storage = EventStorage()
  17. self._log = OutPutLog()
  18. def run(self):
  19. run_time = 0
  20. while True:
  21. this_time = time.time()
  22. if this_time - run_time >5:
  23. run_time = this_time
  24. self.judge_buzzer_alarm()
  25. def judge_buzzer_alarm(self):
  26. # 蜂鸣器状态:c809
  27. read_real_list = ["c809"]
  28. real_data_dict = self._storage.get_real_data(read_real_list)
  29. buzzer_status = real_data_dict["c809"] # 蜂鸣器当前状态,0:蜂鸣器关闭,1:蜂鸣器开启
  30. if buzzer_status is not None:
  31. buzzer_status = int(buzzer_status)
  32. get_set_buzzer_status = "SELECT buzzer_status FROM `alarm_set`;"
  33. set_buzzer_status = self._storage.execute_sql(get_set_buzzer_status) # 是否开启蜂鸣器报警功能,0=关闭,1=开启
  34. # print("-------------------", set_buzzer_status)
  35. if set_buzzer_status[0]["buzzer_status"] == 1:
  36. # 判断是否有报警点,如果有,则蜂鸣器报警
  37. get_alarm_sql = "SELECT id,CAST(create_time AS CHAR) AS create_time,CAST(update_time AS CHAR) AS update_time,point_name,data,buzzer_status_time,signal_type FROM alarm_tbl WHERE is_cancel=0;"
  38. alarm_data = self._storage.execute_sql(get_alarm_sql)
  39. if alarm_data:
  40. buzzer_status_time = alarm_data[0]["buzzer_status_time"]
  41. self.now_time = datetime.now()
  42. if buzzer_status is None or buzzer_status_time is None:
  43. # turn on the buzzer
  44. if self.send_command("open"):
  45. self.update_alarm_tbl()
  46. else:
  47. print("turn on the buzzer failed!")
  48. else:
  49. # 计算蜂鸣器保持开启或关闭状态的时长
  50. buzzer_hold_time = (self.now_time - buzzer_status_time).total_seconds()
  51. if buzzer_status == 0 and buzzer_hold_time > 60 * 10:
  52. # buzzer off and turn on the buzzer
  53. if self.send_command("open"):
  54. self.update_alarm_tbl()
  55. else:
  56. print("turn on the buzzer failed!")
  57. elif buzzer_status == 1 and buzzer_hold_time > 60 * 3:
  58. # buzzer on and turn off the buzzer
  59. if self.send_command("close"):
  60. self.update_alarm_tbl()
  61. else:
  62. print("turn off the buzzer failed!")
  63. elif buzzer_status == 1:
  64. # 没有报警点 判断蜂鸣器状态 如果蜂鸣器在响 关掉蜂鸣器
  65. if self.send_command("close"):
  66. print("turn off the buzzer success!")
  67. else:
  68. print("turn off the buzzer failed!")
  69. elif buzzer_status == 1:
  70. # 无论是否有报警,都关闭蜂鸣器
  71. if self.send_command("close"):
  72. print("turn off the buzzer success!")
  73. else:
  74. print("turn off the buzzer failed!")
  75. def send_command(self, status):
  76. '''给蜂鸣器发送指令,并更新数据库'''
  77. if status == "open":
  78. # turn on the buzzer
  79. command = {"device_id": 1, "start_addr": 0, "output_value": 65280, "function_code": 5, "res": 65280}
  80. elif status == "close":
  81. # turn off the buzzer
  82. command = {"device_id": 1, "start_addr": 0, "output_value": 0, "function_code": 5, "res": 0}
  83. elif status == "status":
  84. # views the buzzer status
  85. command = {"device_id": 1, "start_addr": 0, "length": 1, "function_code": 1}
  86. station_name = "buzzer"
  87. commend_result = Utility.available_connectors[station_name].send_command(command)
  88. return commend_result
  89. def update_alarm_tbl(self):
  90. '''更新数据库的报警信息'''
  91. update_sql = f"UPDATE alarm_tbl SET buzzer_status_time=\'{self.now_time}\' WHERE is_cancel=0;"
  92. update_result = self._storage.execute_update_sql(update_sql)
  93. # print(update_result)
  94. return update_result