start.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import json
  2. from MyUtils.Dingtalk import send_message_markdown
  3. import datetime
  4. import time
  5. import pymysql
  6. import pytz
  7. import os
  8. SELECT_BUILDING_SQL = "SELECT zj_building_id,sg_building_id FROM sagacloud_setup.zj_sync_config_building"
  9. SELECT_STATE_SQL = "SELECT room_id from sagacloud_setup.zj_t_building_rooms where `type` = 1" #只对比类型为1的点位
  10. SELECT_FC_SQL = "SELECT building_id,room_name,room_id from zj_fc.t_building_rooms"
  11. SELECT_SPACE_SQL = "SELECT id,building_id,local_name FROM sagacloud_object.object_data_space"
  12. TRUNCATE_BUILDING_ROOMS_SQL = "truncate table sagacloud_setup.zj_sync_building_rooms"
  13. 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"
  14. 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'"
  15. UPDATE_SPACEID_SQL = "UPDATE sagacloud_setup.zj_t_building_rooms SET space_id = '%s' where room_id = '%s'"
  16. def datetime_now():
  17. # datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  18. #容器时间
  19. # tz = pytz.timezone('Asia/Shanghai') # 东八区
  20. datetime_now = datetime.datetime.fromtimestamp(int(time.time()),
  21. pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
  22. return datetime_now
  23. # mysql插入数据
  24. def insert_mysql(sqls):
  25. print("%s 开始往mysql插入数据..." % datetime_now())
  26. for i in range(0, len(sqls), 1000):
  27. sqlranges = sqls[i:i + 1000]
  28. sqlranges = INSERT_BUILDING_ROOMS_SQL + ",".join(sqlranges)
  29. mysql_cur.execute(sqlranges)
  30. conn.commit()
  31. print("%s mysql数据插入成功,合计%s条..." % (datetime_now(), len(sqls)))
  32. # 读取配置文件信息
  33. with open("config.json", "r") as f:
  34. data = json.load(f)
  35. mysql = data["mysql"]
  36. database = mysql["database"]
  37. dingding = data["dingding"]
  38. # mysql = {
  39. # "host": os.getenv("host"),
  40. # "port": int(os.getenv("port")),
  41. # "user": os.getenv("user"),
  42. # "passwd": os.getenv("passwd"),
  43. # "database": os.getenv("database")
  44. # }
  45. # database = os.getenv("database")
  46. # dingding = os.getenv("dingding")
  47. print("%s 等待开始检查..." % datetime_now())
  48. while True:
  49. time_now = datetime.datetime.fromtimestamp(int(time.time()),
  50. pytz.timezone('Asia/Shanghai')).strftime('%H:%M:%S')
  51. # time_now = time.strftime("%H:%M:%S", time.localtime()) # 刷新
  52. am_set_time = "05:00:00"
  53. if time_now == am_set_time: # 此处设置每天定时的时间
  54. #钉钉推送消息
  55. messages = []
  56. #存储zj_fc表的空间名称
  57. FC_NAMELIST_DICT = {}
  58. #存储space表的空间名称
  59. SPACE_NAMELIST_DICT={}
  60. # 连接mysql
  61. conn = pymysql.connect(**mysql)
  62. mysql_cur = conn.cursor()
  63. # 获取建筑列表
  64. mysql_cur.execute(SELECT_BUILDING_SQL)
  65. buildinglist = mysql_cur.fetchall()
  66. #获取需要对应的点位列表
  67. mysql_cur.execute(SELECT_STATE_SQL)
  68. roomid_list = mysql_cur.fetchall()
  69. roomid_list = [i[0] for i in roomid_list]
  70. # 获取zj_fc空间名称
  71. print("%s 获取zj_fc空间名称..."%datetime_now())
  72. mysql_cur.execute(SELECT_FC_SQL)
  73. fc_namelist = mysql_cur.fetchall()
  74. for building in buildinglist:
  75. if building[0] and building[1] != None:
  76. for fc_name in fc_namelist:
  77. #判断空间是否需要对比
  78. if fc_name[2] in roomid_list:
  79. if fc_name[0] == building[0]:
  80. FC_NAMELIST_DICT[building[1]+"_"+fc_name[1]] = building[0]+"_"+fc_name[1]+"_"+fc_name[2]
  81. # FC_NAMELIST_DICT[building[1]+"_"+fc_name[1]] = 1
  82. else:
  83. message = (" - **类型:** 建筑关系未对应 \n" + " - **建筑ID:** <font color=#FF0000>%s(%s)</font> \n")% (building[0],building[1])
  84. print(message)
  85. messages.append(message)
  86. messages.append("___")
  87. # 获取space空间名称
  88. print("%s 获取space空间名称..."%datetime_now())
  89. mysql_cur.execute(SELECT_SPACE_SQL)
  90. space_namelist = mysql_cur.fetchall()
  91. for space in space_namelist:
  92. SPACE_NAMELIST_DICT[space[1]+"_"+space[2]] = space[0]
  93. #
  94. # print(FC_NAMELIST_DICT)
  95. # print(SPACE_NAMELIST_DICT)
  96. #对比空间名称,取出差异数据生成sql语句
  97. sqls = []
  98. print("%s 开始对比差异数据..."%datetime_now())
  99. diff_namelist = set(FC_NAMELIST_DICT) - set(SPACE_NAMELIST_DICT)
  100. if diff_namelist != set():
  101. for diff_name in diff_namelist:
  102. diff_building_rooms = FC_NAMELIST_DICT[diff_name]
  103. diff_building_id = str(diff_building_rooms).split("_")[0]
  104. diff_room_name = str(diff_building_rooms).split("_")[1]
  105. mysql_cur.execute(SELECT_DIFF_FC_SQL%(diff_building_id,diff_room_name))
  106. diff_data = mysql_cur.fetchall()[0]
  107. building_name = diff_data[0]
  108. floor_id = diff_data[1]
  109. floor_name = diff_data[2]
  110. state = diff_data[3]
  111. type = diff_data[4]
  112. 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())
  113. sqls.append(sqlline)
  114. # 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)
  115. # messages.append(message)
  116. # messages.append("___")
  117. print("%s 差异数据共计%s条..."%(datetime_now(),len(diff_namelist)))
  118. message = (" - **类型:** 空间名称未对应 \n" + " - **数量:** <font color=#FF0000>%s</font> \n") % (len(diff_namelist))
  119. messages.append(message)
  120. messages.append("___")
  121. #更新zj_t_building_rooms表的space_id
  122. print("%s 更新zj_t_building_rooms表的space_id..."%datetime_now())
  123. for same_name in FC_NAMELIST_DICT:
  124. building_rooms = FC_NAMELIST_DICT[same_name]
  125. if same_name in SPACE_NAMELIST_DICT:
  126. space_id =SPACE_NAMELIST_DICT[same_name]
  127. room_id = str(building_rooms).split("_")[2]
  128. mysql_cur.execute(UPDATE_SPACEID_SQL % (space_id, room_id))
  129. conn.commit()
  130. # 获取space空间名称
  131. print("%s 清空MySQL表数据..."%datetime_now())
  132. mysql_cur.execute(TRUNCATE_BUILDING_ROOMS_SQL)
  133. conn.commit()
  134. #往MySQL插入数据
  135. insert_mysql(sqls)
  136. #关闭数据库
  137. mysql_cur.close()
  138. conn.close()
  139. if messages:
  140. print("%s 发送钉钉报警"%datetime_now())
  141. messages.append("<font color=#0080FF>详情查看MySQL数据库sagacloud_setup里的zj_sync_building_rooms表</font>")
  142. messages_info = "\n".join(messages)
  143. # 如果有报警,发送钉钉
  144. title = "## **【之江实验室】空间名称报警** "
  145. messages_info = title + "\n" +"___"+"\n"+ messages_info
  146. send_message_markdown(title, messages_info, dingding)