main.py 5.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import datetime
  2. import json
  3. import time
  4. from MyUtils.MysqlUtils import MysqlUtils
  5. from MyUtils.DmprwdUtil import get_query_data
  6. import pytz
  7. INSERT_SQL = "replace into %s.%s(project_id,date,energy_cooling,energy_heating,energy_ac_terminal,energy_light,energy_others,create_time,update_time) values "
  8. DELETE_SQL = "DELETE FROM `energy_week_day` WHERE `project_id` = '%s' AND `date` >= '%s' AND `date` < '%s' "
  9. SELETE_COUNTLASTDATA_SQL = "SELECT count(*) FROM `energy_week_day` WHERE `project_id` = '%s' AND `date` >= '%s' AND `date` < '%s'"
  10. SELETE_SUMLASTDATA_SQL = "SELECT SUM(energy_ac_terminal)+SUM(energy_heating)+SUM(energy_cooling)+SUM(energy_light)+sum(energy_others) as last_data FROM `energy_week_day` WHERE `project_id` = '%s' AND `date` >= '%s' AND `date` < '%s'"
  11. with open("config.json", "r") as f:
  12. data = json.load(f)
  13. mysql = data["mysql"]
  14. my_database = mysql["database"]
  15. buildings = data["buildings"]
  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. print("%s 等待下次执行..."%datetime_now())
  24. while True:
  25. time_now = datetime.datetime.fromtimestamp(int(time.time()),
  26. pytz.timezone('Asia/Shanghai')).strftime('%H:%M:%S')
  27. am_set_time = "08:00:00"
  28. pm_set_time = "22:00:00"
  29. if time_now == am_set_time or time_now == pm_set_time: # 此处设置每天定时的时间
  30. today = datetime.date.today().strftime("%Y%m%d") + "000000"
  31. yesterday = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d") + "000000"
  32. today_date = datetime.date.today().strftime("%Y%m%d")
  33. yesterday_date = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
  34. # #连接mysql
  35. MysqlUtil = MysqlUtils(**mysql)
  36. #循环项目查询
  37. for building in buildings:
  38. project_id = "Pj"+building["building"]
  39. groupcode = building["groupcode"]
  40. energyid = building["energyid"]
  41. rwdid = building["rwdid"]
  42. energy_total = rwdid["energy_total"]
  43. energy_light = rwdid["energy_light"]
  44. energy_ac_termial = rwdid["energy_ac_termial"]
  45. energy_cooling = rwdid["energy_cooling"]
  46. energy_heating = rwdid["energy_heating"]
  47. print("%s 开始查询%s项目数据"%(datetime_now(),project_id))
  48. # 获取能耗数据
  49. datas = get_query_data(yesterday, today,groupcode, project_id,energy_total,energy_light,energy_cooling,energy_heating,energy_ac_termial)
  50. if datas:
  51. # 删除昨天数据
  52. print("%s,开始删除%s项目%s的数据..." % (datetime_now(),project_id, yesterday))
  53. delete_sql = DELETE_SQL % (project_id, yesterday_date, today_date)
  54. MysqlUtil.update(delete_sql)
  55. objectids = [i["objectId"] for i in datas]
  56. print(objectids)
  57. sum_data_value_data = "0"
  58. energy_heating_data = "0"
  59. energy_ac_terminal_data = "0"
  60. energy_light_data = "0"
  61. energy_cooling_data = "0"
  62. for i in datas:
  63. print(i)
  64. # 总电耗
  65. if i["objectId"] == energy_total:
  66. sum_data_value_data = i["ipValue"]
  67. # 冷
  68. if i["objectId"] == energy_cooling:
  69. energy_cooling_data = i["ipValue"]
  70. # 热
  71. if i["objectId"] == energy_heating:
  72. energy_heating_data = i["ipValue"]
  73. # 空调末端
  74. if i["objectId"] == energy_ac_termial:
  75. energy_ac_terminal_data = i["ipValue"]
  76. # 照明
  77. if i["objectId"] == energy_light:
  78. energy_light_data = i["ipValue"]
  79. energy_other = float(sum_data_value_data) - float(energy_cooling_data) - float(energy_heating_data) - float(energy_light_data) - float(
  80. energy_ac_terminal_data)
  81. sql = "('%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (
  82. project_id, yesterday_date, energy_cooling_data, energy_heating_data, energy_ac_terminal_data, energy_light_data, energy_other,
  83. datetime_now(), datetime_now())
  84. inser_sql = INSERT_SQL % (my_database, "energy_week_day") + sql
  85. print("%s,开始插入%s项目%s数据..." % (datetime_now(),project_id,yesterday))
  86. MysqlUtil.update(inser_sql)
  87. else:
  88. print("%s,没有查询到%s项目%s的数据..."%(datetime_now(),project_id,yesterday))
  89. MysqlUtil.close()
  90. print("%s 完成%s的数据插入"%(datetime_now(),yesterday))
  91. time.sleep(2)