import json from MyUtils.Dingtalk import send_message_markdown import datetime import time import pymysql import pytz import os SELECT_BUILDING_SQL = "SELECT zj_building_id,sg_building_id FROM sagacloud_setup.zj_sync_config_building" SELECT_STATE_SQL = "SELECT room_id from sagacloud_setup.zj_t_building_rooms where `type` = 1" #只对比类型为1的点位 SELECT_FC_SQL = "SELECT building_id,room_name,room_id from zj_fc.t_building_rooms" SELECT_SPACE_SQL = "SELECT id,building_id,local_name FROM sagacloud_object.object_data_space" TRUNCATE_BUILDING_ROOMS_SQL = "truncate table sagacloud_setup.zj_sync_building_rooms" INSERT_BUILDING_ROOMS_SQL = "INSERT into sagacloud_setup.zj_sync_building_rooms(building_id,building_name,room_name,floor_id,floor_name,`state`,`type`,create_time) values" SELECT_DIFF_FC_SQL="SELECT building_name,floor_id,floor_name,`state`,`type` from zj_fc.t_building_rooms WHERE building_id='%s' and room_name='%s'" UPDATE_SPACEID_SQL = "UPDATE sagacloud_setup.zj_t_building_rooms SET space_id = '%s' where room_id = '%s'" 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 # mysql插入数据 def insert_mysql(sqls): print("%s 开始往mysql插入数据..." % datetime_now()) for i in range(0, len(sqls), 1000): sqlranges = sqls[i:i + 1000] sqlranges = INSERT_BUILDING_ROOMS_SQL + ",".join(sqlranges) mysql_cur.execute(sqlranges) conn.commit() print("%s mysql数据插入成功,合计%s条..." % (datetime_now(), len(sqls))) # 读取配置文件信息 with open("config.json", "r") as f: data = json.load(f) mysql = data["mysql"] database = mysql["database"] dingding = data["dingding"] # mysql = { # "host": os.getenv("host"), # "port": int(os.getenv("port")), # "user": os.getenv("user"), # "passwd": os.getenv("passwd"), # "database": os.getenv("database") # } # database = os.getenv("database") # dingding = os.getenv("dingding") print("%s 等待开始检查..." % datetime_now()) while True: time_now = datetime.datetime.fromtimestamp(int(time.time()), pytz.timezone('Asia/Shanghai')).strftime('%H:%M:%S') # time_now = time.strftime("%H:%M:%S", time.localtime()) # 刷新 am_set_time = "05:00:00" if time_now == am_set_time: # 此处设置每天定时的时间 #钉钉推送消息 messages = [] #存储zj_fc表的空间名称 FC_NAMELIST_DICT = {} #存储space表的空间名称 SPACE_NAMELIST_DICT={} # 连接mysql conn = pymysql.connect(**mysql) mysql_cur = conn.cursor() # 获取建筑列表 mysql_cur.execute(SELECT_BUILDING_SQL) buildinglist = mysql_cur.fetchall() #获取需要对应的点位列表 mysql_cur.execute(SELECT_STATE_SQL) roomid_list = mysql_cur.fetchall() roomid_list = [i[0] for i in roomid_list] # 获取zj_fc空间名称 print("%s 获取zj_fc空间名称..."%datetime_now()) mysql_cur.execute(SELECT_FC_SQL) fc_namelist = mysql_cur.fetchall() for building in buildinglist: if building[0] and building[1] != None: for fc_name in fc_namelist: #判断空间是否需要对比 if fc_name[2] in roomid_list: if fc_name[0] == building[0]: FC_NAMELIST_DICT[building[1]+"_"+fc_name[1]] = building[0]+"_"+fc_name[1]+"_"+fc_name[2] # FC_NAMELIST_DICT[building[1]+"_"+fc_name[1]] = 1 else: message = (" - **类型:** 建筑关系未对应 \n" + " - **建筑ID:** %s(%s) \n")% (building[0],building[1]) print(message) messages.append(message) messages.append("___") # 获取space空间名称 print("%s 获取space空间名称..."%datetime_now()) mysql_cur.execute(SELECT_SPACE_SQL) space_namelist = mysql_cur.fetchall() for space in space_namelist: SPACE_NAMELIST_DICT[space[1]+"_"+space[2]] = space[0] # # print(FC_NAMELIST_DICT) # print(SPACE_NAMELIST_DICT) #对比空间名称,取出差异数据生成sql语句 sqls = [] print("%s 开始对比差异数据..."%datetime_now()) diff_namelist = set(FC_NAMELIST_DICT) - set(SPACE_NAMELIST_DICT) if diff_namelist != set(): for diff_name in diff_namelist: diff_building_rooms = FC_NAMELIST_DICT[diff_name] diff_building_id = str(diff_building_rooms).split("_")[0] diff_room_name = str(diff_building_rooms).split("_")[1] mysql_cur.execute(SELECT_DIFF_FC_SQL%(diff_building_id,diff_room_name)) diff_data = mysql_cur.fetchall()[0] building_name = diff_data[0] floor_id = diff_data[1] floor_name = diff_data[2] state = diff_data[3] type = diff_data[4] sqlline = "('%s','%s','%s','%s','%s','%s','%s','%s')"%(diff_building_id,building_name,diff_room_name,floor_id,floor_name,state,type,datetime_now()) sqls.append(sqlline) # message = (" - **building_id:** %s \n" + " - **building_name:** %s \n" + " - **room_name:** %s \n" + " - **type:** %s \n")%(diff_building_id, building_name,diff_room_name,type) # messages.append(message) # messages.append("___") print("%s 差异数据共计%s条..."%(datetime_now(),len(diff_namelist))) message = (" - **类型:** 空间名称未对应 \n" + " - **数量:** %s \n") % (len(diff_namelist)) messages.append(message) messages.append("___") #更新zj_t_building_rooms表的space_id print("%s 更新zj_t_building_rooms表的space_id..."%datetime_now()) for same_name in FC_NAMELIST_DICT: building_rooms = FC_NAMELIST_DICT[same_name] if same_name in SPACE_NAMELIST_DICT: space_id =SPACE_NAMELIST_DICT[same_name] room_id = str(building_rooms).split("_")[2] mysql_cur.execute(UPDATE_SPACEID_SQL % (space_id, room_id)) conn.commit() # 获取space空间名称 print("%s 清空MySQL表数据..."%datetime_now()) mysql_cur.execute(TRUNCATE_BUILDING_ROOMS_SQL) conn.commit() #往MySQL插入数据 insert_mysql(sqls) #关闭数据库 mysql_cur.close() conn.close() if messages: print("%s 发送钉钉报警"%datetime_now()) messages.append("详情查看MySQL数据库sagacloud_setup里的zj_sync_building_rooms表") messages_info = "\n".join(messages) # 如果有报警,发送钉钉 title = "## **【之江实验室】空间名称报警** " messages_info = title + "\n" +"___"+"\n"+ messages_info send_message_markdown(title, messages_info, dingding)