start.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import json
  2. from MyUtils.ZillionUtil import ZillionUtil
  3. from MyUtils.DateUtils import get_day
  4. import datetime
  5. import pymysql
  6. import pytz
  7. import time
  8. INSERT_SQL = "replace into %s.energy_week_day(project_id,date,energy_cooling,energy_heating,energy_ac_terminal,energy_light,energy_others,create_time,update_time) values"
  9. SELECT_SQL = "select c_building from energy_survey.t_research_bdratio_compute"
  10. DELETE_SQL = "DELETE FROM %s.energy_week_day WHERE `project_id` = '%s' AND `date` >= '%s' AND `date` < '%s';"
  11. # 获取能耗数据
  12. def get_hbase_data(hbase_database, hbase_table, buildingid, energyModelNodeSign, from_time, to_time):
  13. Criteria = {
  14. "building": buildingid,
  15. "energyModelNodeSign": energyModelNodeSign,
  16. "energyModelSign": buildingid + "EM01",
  17. "data_time": {
  18. "$gte": from_time,
  19. "$lt": to_time
  20. }
  21. }
  22. datas = zillionUtil.select(hbase_database, hbase_table, Criteria)
  23. return datas
  24. #计算建筑的能耗和
  25. def sum_values(hbase_database, hbase_table, buildinglist, energyModelNodeSign, day_start, day_end):
  26. data_values = []
  27. for building in buildinglist:
  28. datas = get_hbase_data(hbase_database, hbase_table, building, energyModelNodeSign, day_start, day_end)
  29. for i in datas:
  30. data_value = i["data_value"]
  31. data_values.append(data_value)
  32. if data_values:
  33. sum_data_value = sum(data_values)
  34. return sum_data_value
  35. # 当前时间
  36. # datetimenow = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
  37. #容器里python获取时间少8小时
  38. tz = pytz.timezone('Asia/Shanghai') #东八区
  39. datetimenow = datetime.datetime.fromtimestamp(int(time.time()),
  40. pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')
  41. now_time = datetime.datetime.now().strftime("%Y%m%d") + "000000"
  42. end_time = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y%m%d")
  43. # 读取配置文件信息
  44. with open("config.json", "r") as f:
  45. data = json.load(f)
  46. hbase_database = data["metadata"]["database"]
  47. url = data["metadata"]["url"]
  48. project = data["building"]["id"]
  49. energyModelNodeSigns = data["building"]["energyModels"]
  50. mysql = data["mysql"]
  51. database = mysql["database"]
  52. with open("config-time.json", "r") as fp:
  53. data_t = json.load(fp)
  54. from_time = data_t["from_time"]
  55. to_time = data_t["to_time"]
  56. hbase_table = "data_energydata_1d"
  57. if to_time == "now":
  58. to_time = end_time + "000000"
  59. days = get_day(from_time, to_time)
  60. try:
  61. # 连接mysql
  62. conn = pymysql.connect(**mysql)
  63. mysql_cur = conn.cursor()
  64. # 获取建筑列表
  65. mysql_cur.execute(SELECT_SQL)
  66. buildinglist = mysql_cur.fetchall()
  67. buildinglist = [i[0] for i in buildinglist]
  68. # #连接hbase
  69. zillionUtil = ZillionUtil(url)
  70. sqls = []
  71. for day in days:
  72. energy_cooling = "NULL"
  73. energy_heating = "NULL"
  74. energy_ac_terminal = "NULL"
  75. energy_light = "NULL"
  76. sum_value_data = "NULL"
  77. day_start, day_end = day[0], day[1]
  78. m_day_start, m_day_end = day[0][0:8], day[1][0:8]
  79. date = day_start[0:8]
  80. print("开始查询%s到%s的数据" % (m_day_start, m_day_end))
  81. for energyModelNodeSign in energyModelNodeSigns:
  82. #冷
  83. if energyModelNodeSign == "EI10102010101001":
  84. #计算建筑的能耗和
  85. energy_cooling = sum_values(hbase_database, hbase_table, buildinglist, energyModelNodeSign, day_start, day_end)
  86. #热
  87. if energyModelNodeSign == "EI10102010102001":
  88. energy_heating = sum_values(hbase_database, hbase_table, buildinglist, energyModelNodeSign, day_start,day_end)
  89. #空调末端
  90. if energyModelNodeSign == "EI10102010101001":
  91. energy_ac_terminal = sum_values(hbase_database, hbase_table, buildinglist, energyModelNodeSign, day_start,day_end)
  92. #照明
  93. if energyModelNodeSign == "EI1010202001":
  94. energy_light = sum_values(hbase_database, hbase_table, buildinglist, energyModelNodeSign, day_start,day_end)
  95. #总
  96. if energyModelNodeSign == "EI1001":
  97. sum_value_data = sum_values(hbase_database, hbase_table, buildinglist, energyModelNodeSign, day_start,day_end)
  98. if sum_value_data != None:
  99. if energy_cooling == None:
  100. energy_cooling = 0
  101. if energy_heating == None:
  102. energy_heating = 0
  103. if energy_ac_terminal == None:
  104. energy_ac_terminal = 0
  105. if energy_light == None:
  106. energy_light = 0
  107. energy_other = sum_value_data-energy_cooling-energy_heating-energy_ac_terminal-energy_light
  108. sqlline = "('%s','%s',%s,%s,%s,%s,%s,'%s','%s')" % (
  109. project, date,energy_cooling,energy_heating,energy_ac_terminal,energy_light,energy_other, datetimenow, datetimenow)
  110. sqls.append(sqlline)
  111. else:
  112. print("未查询到%s总电耗数据"%(date))
  113. print("开始删除%s到%s的数据" % (from_time, to_time))
  114. mysql_cur.execute(DELETE_SQL % (database, project, from_time[0:8], to_time[0:8]))
  115. conn.commit()
  116. # mysql插入数据
  117. print("开始往mysql插入数据...")
  118. #取出最后一条数据的时间
  119. last_data_time = eval(sqls[-1].split(",")[1])
  120. last_data_time = str(last_data_time)+"000000"
  121. for i in range(0, len(sqls), 1000):
  122. sqlranges = sqls[i:i + 1000]
  123. sqlranges = INSERT_SQL % (database) + ",".join(sqlranges)
  124. mysql_cur.execute(sqlranges)
  125. conn.commit()
  126. mysql_cur.close()
  127. conn.close()
  128. print("mysql数据插入成功,合计%s条..." % len(sqls))
  129. # 更新config文件时间
  130. print("更新config-time.json文件时间")
  131. data_t["from_time"] = last_data_time
  132. with open("config-time.json", "w") as f:
  133. json.dump(data_t, f)
  134. except Exception as e:
  135. print(e)