main.py 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. import pytz
  9. 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 "
  10. DELETE_SQL = "DELETE FROM `energy_week_day` WHERE `project_id` = '%s' AND `date` >= '%s' AND `date` < '%s' "
  11. SELETE_COUNTLASTDATA_SQL = "SELECT count(*) FROM `energy_week_day` WHERE `project_id` = '%s' AND `date` >= '%s' AND `date` < '%s'"
  12. 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'"
  13. with open("config.json", "r") as f:
  14. data = json.load(f)
  15. mysql = data["mysql"]
  16. my_database = mysql["database"]
  17. dingding = data["dingding"]
  18. at_mobiles = data["at_mobiles"]
  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. building = "1101080259"
  27. def transfer_energy():
  28. print("同步%s项目数据" % (building))
  29. while True:
  30. project_id = "Pj" + building
  31. time_now = datetime.datetime.fromtimestamp(int(time.time()),
  32. pytz.timezone('Asia/Shanghai')).strftime('%H:%M:%S')
  33. am_set_time = "08:00:00"
  34. pm_set_time = "22:00:00"
  35. if time_now == am_set_time or time_now ==pm_set_time:#此处设置每天定时的时间
  36. today = datetime.date.today().strftime("%Y%m%d")+"000000"
  37. yesterday = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")+"000000"
  38. today_date = datetime.date.today().strftime("%Y%m%d")
  39. yesterday_date = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
  40. # #连接mysql
  41. MysqlUtil = MysqlUtils(**mysql)
  42. #获取能耗数据
  43. datas = get_query_data(yesterday,today)
  44. if datas:
  45. # 删除昨天数据
  46. print("%s,开始删除%s的数据..." % (datetime_now(), yesterday))
  47. delete_sql = DELETE_SQL % (project_id, yesterday_date, today_date)
  48. MysqlUtil.update(delete_sql)
  49. objectids = [i["objectId"] for i in datas]
  50. print(objectids)
  51. energy_cooling = "0"
  52. energy_heating = "0"
  53. energy_ac_terminal = "0"
  54. energy_light = "0"
  55. sum_data_value = "0"
  56. for i in datas:
  57. print(i)
  58. #总电耗
  59. if i["objectId"] == "Vo1101080259f2481ca3604644399f1dacb84e20adae":
  60. sum_data_value = i["ipValue"]
  61. #冷热源
  62. if i["objectId"] == "Vo1101080259e6dcf338d9be4bbf826f659b0b5a9ab2":
  63. energy_cooling = i["ipValue"]
  64. #空调末端
  65. if i["objectId"] == "Vo11010802590eaef68d3289452d86d89fbee721e6df":
  66. energy_ac_terminal = i["ipValue"]
  67. #照明
  68. if i["objectId"] == "Vo110108025953894df8d4ae4dbeb7c81ced7df3f83e":
  69. energy_light = i["ipValue"]
  70. energy_other = float(sum_data_value) - float(energy_cooling) - float(energy_heating)- float(energy_light) - float(energy_ac_terminal)
  71. sql = "('%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (
  72. project_id, yesterday_date, energy_cooling, energy_heating, energy_ac_terminal, energy_light, energy_other,
  73. datetime_now(), datetime_now())
  74. inser_sql = INSERT_SQL % (my_database, "energy_week_day") + sql
  75. print("%s,开始插入数据..." % datetime_now())
  76. MysqlUtil.update(inser_sql)
  77. else:
  78. print("%s,没有查询到数据...")
  79. MysqlUtil.close()
  80. if __name__ == '__main__':
  81. transfer_energy()