main.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import datetime
  2. import json,os
  3. import time
  4. from MyUtils.ZillionUtil import ZillionUtil
  5. from MyUtils.Dingtalk import send_message_markdown
  6. import pytz
  7. def datetime_now():
  8. # datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  9. #容器时间
  10. # tz = pytz.timezone('Asia/Shanghai') # 东八区
  11. datetime_now = datetime.datetime.fromtimestamp(int(time.time()),
  12. pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
  13. return datetime_now
  14. # 获取能耗数据
  15. def get_data_time(zillionUtil,hbase_database, hbase_table, building,meters,funcid):
  16. Criteria = {
  17. "building": building,
  18. "meter": {
  19. "in":meters
  20. },
  21. "funcid": funcid
  22. }
  23. datas = zillionUtil.select(hbase_database, hbase_table, Criteria)
  24. return datas
  25. with open("config.json", "r",encoding="utf-8") as f:
  26. data = json.load(f)
  27. hbase_database = data["metadata"]["database"]
  28. url = data["metadata"]["url"]
  29. building = data["building"]
  30. # funcid = data["funcid"]
  31. # points = data["points"]
  32. dingding = data["dingding"]
  33. points = {'1202': 'B2南新风机房浸水报警传感器',
  34. '1201': '2F空调机房浸水报警传感器',
  35. '1203': 'B2北新风机房浸水报警传感器',
  36. '1101': '制冷机房浸水报警传感器',
  37. '9002': '楼顶南新风机房浸水报警传感器',
  38. '9001': '楼顶北新风机房浸水报警传感器'}
  39. funcid = 11701
  40. # hbase_database = os.getenv("hbase_database")
  41. # url = os.getenv("url")
  42. # building = os.getenv("building")
  43. # dingding = os.getenv("dingding")
  44. # # funcid = os.getenv("funcid")
  45. # # points = os.getenv("points")
  46. #存报警
  47. alarm = {}
  48. zillionUtil = ZillionUtil(url)
  49. meters = [i for i in points]
  50. #存发送钉钉报警内容
  51. print("%s"%building)
  52. while True:
  53. messages = []
  54. datas = get_data_time(zillionUtil,hbase_database,"original_present",building,meters,funcid)
  55. for i in datas:
  56. value = i["data"]
  57. meter = i["meter"]
  58. receivetime = i["receivetime"]
  59. name = points[meter]
  60. if value == 1:
  61. #如果表之前未产生报警,直接发送报警,并将点位存在报警池中
  62. #如果之前已经有报警,不重复发送
  63. if meter not in alarm:
  64. message = (" - **空间名称:** %s \n" + " - **设备类名称:** 水浸传感器 \n" + " - **报警值:** %s \n" + " - **状态:** <font color=#FF0000>请及时查看处理</font> \n")%(name, value)
  65. messages.append(message)
  66. messages.append("___")
  67. alarm[meter] = value
  68. else:
  69. #如果报警恢复,删除报警池内容,发送恢复报警
  70. if meter in alarm:
  71. if alarm[meter] == 1:
  72. alarm.pop(meter)
  73. message = (" - **空间名称:** %s \n" + " - **设备类名称:** 水浸传感器 \n" + " - **报警值:** %s \n" + " - **状态:** 报警已恢复 \n") % (name, value)
  74. messages.append(message)
  75. messages.append("___")
  76. if messages:
  77. messages_info = "\n".join(messages)
  78. #如果有报警,发送钉钉
  79. title = "## 【中海财富中心】位移传感器报警 "
  80. messages_info = title +"\n" + messages_info
  81. print(messages_info)
  82. send_message_markdown(title,messages_info,dingding)
  83. messages.clear()
  84. time.sleep(60)