temp.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import datetime
  2. import json,pymysql
  3. import os
  4. import time
  5. from MyUtils.MysqlUtils import MysqlUtils
  6. from MyUtils.Dingtalk import send_message
  7. from MyUtils.DmprwdUtil import get_query_data
  8. from MyUtils.DateUtils import get_day
  9. import pytz
  10. 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 "
  11. DELETE_SQL = "DELETE FROM `energy_week_day` WHERE `project_id` = '%s' AND `date` >= '%s' AND `date` < '%s' "
  12. SELETE_COUNTLASTDATA_SQL = "SELECT count(*) FROM `energy_week_day` WHERE `project_id` = '%s' AND `date` >= '%s' AND `date` < '%s'"
  13. 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'"
  14. with open("config.json", "r") as f:
  15. data = json.load(f)
  16. mysql = data["mysql"]
  17. my_database = mysql["database"]
  18. buildings = data["buildings"]
  19. def datetime_now():
  20. # datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  21. #容器时间
  22. # tz = pytz.timezone('Asia/Shanghai') # 东八区
  23. datetime_now = datetime.datetime.fromtimestamp(int(time.time()),
  24. pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
  25. return datetime_now
  26. # #连接hbase
  27. MysqlUtil = MysqlUtils(**mysql)
  28. start_time = "20230724000000"
  29. end_time = "20230726000000"
  30. range_days = get_day(start_time,end_time)
  31. for i in range_days:
  32. yesterday,today = i[0],i[1]
  33. yesterday_date = yesterday[0:8]
  34. today_date = today[0:8]
  35. time_now = datetime.datetime.fromtimestamp(int(time.time()),
  36. pytz.timezone('Asia/Shanghai')).strftime('%H:%M:%S')
  37. # today = datetime.date.today().strftime("%Y%m%d")+"000000"
  38. # yesterday = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")+"000000"
  39. # today_date = datetime.date.today().strftime("%Y%m%d")
  40. # yesterday_date = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
  41. #
  42. # 循环项目查询
  43. for building in buildings:
  44. project_id = "Pj" + building["building"]
  45. groupcode = building["groupcode"]
  46. energyid = building["energyid"]
  47. rwdid = building["rwdid"]
  48. energy_total = rwdid["energy_total"]
  49. energy_light = rwdid["energy_light"]
  50. energy_ac_termial = rwdid["energy_ac_termial"]
  51. energy_cooling = rwdid["energy_cooling"]
  52. energy_heating = rwdid["energy_heating"]
  53. print("%s 开始查询%s项目数据" % (datetime_now(), project_id))
  54. # 获取能耗数据
  55. datas = get_query_data(yesterday, today, groupcode, project_id, energy_total, energy_light, energy_cooling,
  56. energy_heating, energy_ac_termial)
  57. if datas:
  58. # 删除昨天数据
  59. print("%s,开始删除%s项目%s的数据..." % (datetime_now(), project_id, yesterday))
  60. delete_sql = DELETE_SQL % (project_id, yesterday_date, today_date)
  61. MysqlUtil.update(delete_sql)
  62. objectids = [i["objectId"] for i in datas]
  63. print(objectids)
  64. sum_data_value_data = "0"
  65. energy_heating_data = "0"
  66. energy_ac_terminal_data = "0"
  67. energy_light_data = "0"
  68. energy_cooling_data = "0"
  69. for i in datas:
  70. print(i)
  71. # 总电耗
  72. if i["objectId"] == energy_total:
  73. sum_data_value_data = i["ipValue"]
  74. # 冷
  75. if i["objectId"] == energy_cooling:
  76. energy_cooling_data = i["ipValue"]
  77. # 热
  78. if i["objectId"] == energy_heating:
  79. energy_heating_data = i["ipValue"]
  80. # 空调末端
  81. if i["objectId"] == energy_ac_termial:
  82. energy_ac_terminal_data = i["ipValue"]
  83. # 照明
  84. if i["objectId"] == energy_light:
  85. energy_light_data = i["ipValue"]
  86. energy_other = float(sum_data_value_data) - float(energy_cooling_data) - float(energy_heating_data) - float(
  87. energy_light_data) - float(
  88. energy_ac_terminal_data)
  89. sql = "('%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (
  90. project_id, yesterday_date, energy_cooling_data, energy_heating_data, energy_ac_terminal_data,
  91. energy_light_data, energy_other,
  92. datetime_now(), datetime_now())
  93. inser_sql = INSERT_SQL % (my_database, "energy_week_day") + sql
  94. print("%s,开始插入%s项目%s数据..." % (datetime_now(), project_id, yesterday))
  95. MysqlUtil.update(inser_sql)
  96. else:
  97. print("%s,没有查询到%s项目%s的数据..." % (datetime_now(), project_id, yesterday))
  98. MysqlUtil.close()