main.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import datetime
  2. import json
  3. import time
  4. from MyUtils.ZillionUtil import ZillionUtil
  5. from MyUtils.MysqlUtils import MysqlUtils
  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. def datetime_now():
  10. # datetime_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
  11. #容器时间
  12. # tz = pytz.timezone('Asia/Shanghai') # 东八区
  13. datetime_now = datetime.datetime.fromtimestamp(int(time.time()),
  14. pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
  15. return datetime_now
  16. # 获取能耗数据
  17. def get_data_time(zillionUtil,hbase_database, hbase_table, building,meters, from_time, to_time):
  18. Criteria = {
  19. "building": building,
  20. "meter": {
  21. "in":meters
  22. },
  23. "funcid": 10101,
  24. "data_time": {
  25. "$gte": from_time,
  26. "$lt": to_time
  27. }
  28. }
  29. datas = zillionUtil.select(hbase_database, hbase_table, Criteria)
  30. return datas
  31. # 获取点位表
  32. def get_pointlists(zillionUtil,hbase_database, hbase_table, building):
  33. Criteria = {
  34. "building": building,
  35. }
  36. datas = zillionUtil.select(hbase_database, hbase_table, Criteria)
  37. return datas
  38. with open("config.json", "r") as f:
  39. data = json.load(f)
  40. hbase_database = data["metadata"]["database"]
  41. url = data["metadata"]["url"]
  42. buildings = data["building"]
  43. set_time = data["set_time"]
  44. mysql = data["mysql"]
  45. my_database = mysql["database"]
  46. print("同步项目列表:%s"%buildings)
  47. print(set_time)
  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. if time_now == set_time:
  53. # #连接hbase
  54. zillionUtil = ZillionUtil(url)
  55. # #连接hbase
  56. MysqlUtil = MysqlUtils(**mysql)
  57. for building in buildings:
  58. pointlists = get_pointlists(zillionUtil,hbase_database,"dy_pointlist",building)
  59. meters_10101 = []
  60. for i in pointlists:
  61. if i["funcid"] == 10101:
  62. meters_10101.append(i["meter"])
  63. print("%s,%s项目点位列表%s"%(datetime_now(),building,meters_10101))
  64. project_id = "Pj" + building
  65. yesterday = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
  66. today = datetime.date.today().strftime("%Y%m%d")
  67. datas = get_data_time(zillionUtil,hbase_database,"data_servicedata_1d",building,meters_10101,yesterday,today)
  68. if datas:
  69. data_values = [float(i["data_value"]) for i in datas]
  70. total_energy = sum(data_values)
  71. sql = "('%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (
  72. project_id, yesterday,"0","0","0","0",total_energy, datetime_now(), datetime_now())
  73. print("%s,开始删除项目%s在%s的数据..." % (datetime_now(), building,yesterday))
  74. delete_sql = DELETE_SQL % (project_id, yesterday, today)
  75. MysqlUtil.update(delete_sql)
  76. inser_sql = INSERT_SQL % (my_database, "energy_week_day") + sql
  77. print("%s,开始插入项目%s在%s的数据..." % (datetime_now(),building,yesterday))
  78. MysqlUtil.update(inser_sql)
  79. print("%s,%s项目的%s数据插入成功..."%(datetime_now(),building,yesterday))
  80. else:
  81. print("%s,未查询到项目%s在%s的数据..."%(datetime_now(),building,yesterday))
  82. else:
  83. time.sleep(2)
  84. print("%s,等待下次程序执行" % (datetime_now()))
  85. MysqlUtil.close()