import datetime import json,os import time from MyUtils.ZillionUtil import ZillionUtil from MyUtils.Dingtalk import send_message_markdown import pytz def datetime_now(): # datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S") #容器时间 # tz = pytz.timezone('Asia/Shanghai') # 东八区 datetime_now = datetime.datetime.fromtimestamp(int(time.time()), pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S') return datetime_now # 获取能耗数据 def get_data_time(zillionUtil,hbase_database, hbase_table, building,meters,funcid): Criteria = { "building": building, "meter": { "in":meters }, "funcid": funcid } datas = zillionUtil.select(hbase_database, hbase_table, Criteria) return datas with open("config.json", "r",encoding="utf-8") as f: data = json.load(f) hbase_database = data["metadata"]["database"] url = data["metadata"]["url"] building = data["building"] # funcid = data["funcid"] # points = data["points"] dingding = data["dingding"] points = {'1202': 'B2南新风机房浸水报警传感器', '1201': '2F空调机房浸水报警传感器', '1203': 'B2北新风机房浸水报警传感器', '1101': '制冷机房浸水报警传感器', '9002': '楼顶南新风机房浸水报警传感器', '9001': '楼顶北新风机房浸水报警传感器'} funcid = 11701 # hbase_database = os.getenv("hbase_database") # url = os.getenv("url") # building = os.getenv("building") # dingding = os.getenv("dingding") # # funcid = os.getenv("funcid") # # points = os.getenv("points") #存报警 alarm = {} zillionUtil = ZillionUtil(url) meters = [i for i in points] #存发送钉钉报警内容 print("%s"%building) while True: messages = [] datas = get_data_time(zillionUtil,hbase_database,"original_present",building,meters,funcid) for i in datas: value = i["data"] meter = i["meter"] receivetime = i["receivetime"] name = points[meter] if value == 1: #如果表之前未产生报警,直接发送报警,并将点位存在报警池中 #如果之前已经有报警,不重复发送 if meter not in alarm: message = (" - **空间名称:** %s \n" + " - **设备类名称:** 水浸传感器 \n" + " - **报警值:** %s \n" + " - **状态:** 请及时查看处理 \n")%(name, value) messages.append(message) messages.append("___") alarm[meter] = value else: #如果报警恢复,删除报警池内容,发送恢复报警 if meter in alarm: if alarm[meter] == 1: alarm.pop(meter) message = (" - **空间名称:** %s \n" + " - **设备类名称:** 水浸传感器 \n" + " - **报警值:** %s \n" + " - **状态:** 报警已恢复 \n") % (name, value) messages.append(message) messages.append("___") if messages: messages_info = "\n".join(messages) #如果有报警,发送钉钉 title = "## 【中海财富中心】位移传感器报警 " messages_info = title +"\n" + messages_info print(messages_info) send_message_markdown(title,messages_info,dingding) messages.clear() time.sleep(60)