deploy.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. __author__ = 'lmstack'
  2. #coding=utf-8
  3. import os
  4. import datetime
  5. import pandas as pd
  6. from LIB.BACKEND import DBManager, Log
  7. from sqlalchemy import create_engine
  8. from sqlalchemy.orm import sessionmaker
  9. import time, datetime
  10. import dateutil.relativedelta
  11. import traceback
  12. <<<<<<< HEAD
  13. from LIB.MIDDLE.odo.DailyMileageEstimation.V1_0_3 import cal_mileage
  14. =======
  15. from LIB.MIDDLE.odo.DailyMileageEstimation.V1_0_4 import calcul_mileage as cal_mileage
  16. >>>>>>> dev
  17. from urllib import parse
  18. import pymysql
  19. import pdb
  20. dbManager = DBManager.DBManager()
  21. if __name__ == "__main__":
  22. # 时间设置
  23. now_time = datetime.datetime.now()
  24. pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)# 前一日
  25. end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 00:00:00")
  26. start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 00:00:00")
  27. history_run_flag = False # 历史数据运行标志
  28. # 更新sn列表
  29. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  30. port=3306
  31. db='qixiang_oss'
  32. user='qixiang_oss'
  33. password='Qixiang2021'
  34. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  35. cursor = conn.cursor()
  36. cursor.execute("select sn, imei, add_time from app_device")
  37. res = cursor.fetchall()
  38. df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
  39. df_sn = df_sn.reset_index(drop=True)
  40. conn.close();
  41. # 数据库配置
  42. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  43. port = 3306
  44. user = 'qx_cas'
  45. password = parse.quote_plus('Qx@123456')
  46. database = 'qx_cas'
  47. db_engine = create_engine(
  48. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  49. user, password, host, port, database
  50. ))
  51. <<<<<<< HEAD
  52. DbSession = sessionmaker(bind=db_engine)
  53. =======
  54. # 获取sn --- 协议关系
  55. host = '172.16.121.236'
  56. port = 3306
  57. user = 'root'
  58. password = 'FastFun1234,'
  59. database = 'fastfun'
  60. db_pro_engine = create_engine(
  61. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  62. user, password, host, port, database
  63. ))
  64. df_protocol = pd.read_sql("select devcode,family_id from ff_device", db_pro_engine)
  65. db_pro_engine.dispose()
  66. >>>>>>> dev
  67. # 运行历史数据配置
  68. df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
  69. # 日志配置
  70. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  71. log_path = 'log/' + now_str
  72. if not os.path.exists(log_path):
  73. os.makedirs(log_path)
  74. log = Log.Mylog(log_name='odo_dailyMileageEstimation', log_level = 'info')
  75. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  76. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  77. logger = log.get_logger()
  78. logger.info("pid is {}".format(os.getpid()))
  79. # 算法参数
  80. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  81. port=3306
  82. db='qx_cas'
  83. user='qx_read'
  84. password='Qx@123456'
  85. tablename='cellStateEstimation_soh'
  86. for i in range(0, len(df_sn)):
  87. try:
  88. sn = df_sn.loc[i, 'sn']
  89. logger.info("pid-{} SN: {} START!".format(os.getpid(), sn))
  90. dbManager = DBManager.DBManager()
  91. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms','gps'])
  92. data_bms = df_data['bms']
  93. data_gps = df_data['gps']
  94. #...............每日累积里程............................................................................
  95. <<<<<<< HEAD
  96. if len(data_bms['时间戳'])>0 and len(data_gps['时间戳'])>0:
  97. =======
  98. if not data_bms.empty and not data_gps.empty and len(data_bms['时间戳'])>0 and len(data_gps['时间戳'])>0:
  99. protocol = df_protocol[df_protocol['devcode']==sn]['family_id']
  100. if len(protocol)>0 and str(int(protocol.values[0])) == '13':
  101. data_bms['总电流[A]'] = -data_bms['总电流[A]']
  102. >>>>>>> dev
  103. df_res = cal_mileage.calcul_mileage(sn,data_bms,data_gps)
  104. # df_res.to_csv('Mileage_'+sn+'.csv')
  105. # # 处理运行历史数据
  106. # if (history_run_flag):
  107. # this_sn = df_first_data_time[df_first_data_time['sn']==sn]
  108. # if (len(this_sn) == 0):
  109. # start_time = pd.to_datetime(str(df_sn.loc[df_sn[df_sn['sn']==sn].index, 'add_time'].values[0])).strftime("%Y-%m-%d 00:00:00")
  110. # else:
  111. # first_data_time = df_first_data_time.loc[df_first_data_time[df_first_data_time['sn']==sn].index, 'first_data_time'].values[0]
  112. # if pd.isnull(first_data_time):
  113. # start_time = "2018-01-01 00:00:00"
  114. # else:
  115. # start_time = pd.to_datetime(str(df_first_data_time.loc[df_first_data_time[df_first_data_time['sn']==sn].index, 'first_data_time'].values[0])).strftime("%Y-%m-%d 00:00:00")
  116. if not df_res.empty:
  117. <<<<<<< HEAD
  118. =======
  119. >>>>>>> dev
  120. df_res.columns = ['time', 'sn', 'current', 'voltage', 'soc', 'charging_status', 'latitude', 'longitude', 'energy_consump', 'mileage']
  121. df_res.to_sql("odo_dailyMileageEstimation",con=db_engine, if_exists="append",index=False)
  122. logger.info("pid-{} SN: {} DONE!".format(os.getpid(), sn))
  123. except:
  124. logger.error(traceback.format_exc)
  125. <<<<<<< HEAD
  126. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  127. =======
  128. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  129. db_engine.dispose()
  130. >>>>>>> dev