main.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import json
  2. from MyUtils.MysqlUtils import MysqlUtils
  3. import datetime
  4. import time
  5. import pymysql
  6. import pytz
  7. SELECT_BUILDINGNAME_SQL = "SELECT local_name from object_data_building where id='%s' "
  8. SELECT_FLOORNAME_SQL = "SELECT local_name from object_data_floor where id='%s'"
  9. SELECT_SPACENAME_SQL = "SELECT local_name,floor_id,building_id from object_data_space where id = '%s'"
  10. SELECT_EQUIPMENTID_SQL = "SELECT from_id,to_id FROM object_data_relation where graph_id ='GtEquipforSpace001'"
  11. SELECT_EQUIPMENTNAME_SQL = "SELECT id,category,infos from object_data_equipment_infos where id = '%s'"
  12. SELECT_EQUIPNAME_SQL = "SELECT equip_name from object_data_category where equip_code = '%s'"
  13. DELETE_SQL = "delete from sagacloud_test.export_space_equipment"
  14. INSERT_SQL = "replace into sagacloud_test.export_space_equipment(buildingname,floor_name,space_name,equip_code,equip_name,equipment_id,local_name,equipment_point,meter,funcid) VALUES "
  15. equipment_points = {"FanGearNum","RunStatus","WaterValveSwitchStatus","ReturnAirTemp","WorkMode","FanGearSet","EquipSwitchSet","infoPointCode","WorkModeSet","ctm-info004","ctm-info003","AirSpeedGear","ModeSet","TempSet","AirSpeedGearSet","CoolCoilAirTemp","SupplyAirTempSet","ReturnAirTempSet","SupplyAirTemp","ctm-SeasonModeSe","FreshAirTemp","ctm-SeasonModeSet","LampGearSet","isPassengerPass"}
  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. # 读取配置文件信息
  24. with open("config.json", "r") as f:
  25. data = json.load(f)
  26. mysql = data["mysql"]
  27. database = mysql["database"]
  28. EquipforSpace_DICK = {}
  29. # #连接mysql
  30. MysqlUtil = MysqlUtils(**mysql)
  31. equipment_id_list= MysqlUtil.query(SELECT_EQUIPMENTID_SQL)
  32. for equipment_id in equipment_id_list:
  33. equip_id,space_id = equipment_id[0],equipment_id[1]
  34. EquipforSpace_DICK[equip_id] = space_id
  35. sqls = []
  36. for equip_id in EquipforSpace_DICK:
  37. print(equip_id)
  38. if len(MysqlUtil.query(SELECT_SPACENAME_SQL % (EquipforSpace_DICK[equip_id]))) == 1:
  39. space_name,floor_id,building_id = MysqlUtil.query(SELECT_SPACENAME_SQL % (EquipforSpace_DICK[equip_id]))[0]
  40. print(space_name,floor_id,building_id)
  41. building_name = MysqlUtil.query(SELECT_BUILDINGNAME_SQL % (building_id))[0][0]
  42. if floor_id is not None:
  43. floor_name = MysqlUtil.query(SELECT_FLOORNAME_SQL % (floor_id))[0][0]
  44. # SPACE_DICK[id] = [building_name,floor_name,space_name]
  45. else:
  46. floor_name = "NULL"
  47. # SPACE_DICK[id] = [building_name, floor_name, space_name]
  48. equipmentinfos = MysqlUtil.query(SELECT_EQUIPMENTNAME_SQL % (equip_id))
  49. if equipmentinfos:
  50. equipm_id,category,equipment_infos = equipmentinfos[0]
  51. equipment_name = MysqlUtil.query(SELECT_EQUIPNAME_SQL % (category))[0][0]
  52. equipment_infos = json.loads(equipment_infos)
  53. for equipment_point in equipment_infos:
  54. if "EquipLocalName" in equipment_infos:
  55. local_name = equipment_infos["EquipLocalName"]
  56. else:
  57. local_name = "NUll"
  58. if equipment_point in equipment_points:
  59. print(equipm_id,equipment_point,equipment_infos[equipment_point])
  60. infos = str(equipment_infos[equipment_point])
  61. if "-" in infos:
  62. meter = infos.split("-")[0]
  63. funcid = infos.split("-")[1]
  64. else:
  65. meter = infos
  66. funcid = "NUll"
  67. print(building_name,floor_name,space_name,category,equipment_name,equipm_id,local_name,equipment_point,meter,funcid)
  68. sql = "('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"%(building_name,floor_name,space_name,category,equipment_name,equipm_id,local_name,equipment_point,meter,funcid)
  69. sqls.append(sql)
  70. else:
  71. category = "NUll"
  72. equipment_name = "NUll"
  73. equipm_id = "NUll"
  74. local_name = "NUll"
  75. equipment_point = "NUll"
  76. meter = "NUll"
  77. funcid = "NUll"
  78. print(building_name, floor_name, space_name, category, equipment_name, equipm_id, local_name, equipment_point,
  79. meter, funcid)
  80. sql = "('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (
  81. building_name, floor_name, space_name, category, equipment_name, equipm_id, local_name, equipment_point,
  82. meter, funcid)
  83. sqls.append(sql)
  84. else:
  85. with open("log.log","w") as f:
  86. f.write(equip_id)
  87. if sqls:
  88. print("%s 清空数据库表"%datetime_now())
  89. MysqlUtil.update(DELETE_SQL)
  90. print("%s 数据库表清空完成"%datetime_now())
  91. print("%s 开始往数据库插入数据,共计%s条"%(datetime_now(),len(sqls)))
  92. for i in range(0,len(sqls),1000):
  93. sqlranges = sqls[i:i + 1000]
  94. sqlranges = INSERT_SQL + ",".join(sqlranges)
  95. MysqlUtil.update(sqlranges)
  96. print("%s 数据库插入成功"%datetime_now())
  97. MysqlUtil.close()