deploy.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. from LIB.MIDDLE.CellStateEstimation.InterShort.V1_0_0 import CBMSBatInterShort
  13. from LIB.MIDDLE.CellStateEstimation.Common import DBDownload
  14. from urllib import parse
  15. import pymysql
  16. import pdb
  17. dbManager = DBManager.DBManager()
  18. if __name__ == "__main__":
  19. # 时间设置
  20. now_time = datetime.datetime.now()
  21. pre_time = now_time + dateutil.relativedelta.relativedelta(days=-8)#上个月时间
  22. end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 00:00:00")
  23. start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 12:00:00")
  24. history_run_flag = False # 历史数据运行标志
  25. # 更新sn列表
  26. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  27. port=3306
  28. db='qixiang_oss'
  29. user='qixiang_oss'
  30. password='Qixiang2021'
  31. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  32. cursor = conn.cursor()
  33. cursor.execute("select sn, imei, add_time from app_device")
  34. res = cursor.fetchall()
  35. df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
  36. df_sn = df_sn.reset_index(drop=True)
  37. conn.close();
  38. # 数据库配置
  39. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  40. port = 3306
  41. user = 'qx_cas'
  42. password = parse.quote_plus('Qx@123456')
  43. database = 'qx_cas'
  44. db_engine = create_engine(
  45. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  46. user, password, host, port, database
  47. ))
  48. DbSession = sessionmaker(bind=db_engine)
  49. # 运行历史数据配置
  50. df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
  51. # 日志配置
  52. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  53. log_path = 'log/' + now_str
  54. if not os.path.exists(log_path):
  55. os.makedirs(log_path)
  56. log = Log.Mylog(log_name='uniform', log_level = 'info')
  57. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  58. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  59. logger = log.get_logger()
  60. logger.info("pid is {}".format(os.getpid()))
  61. # 算法参数
  62. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  63. port=3306
  64. db='qx_cas'
  65. user='qx_read'
  66. password='Qx@123456'
  67. tablename='cellStateEstimation_soh'
  68. for i in range(0, len(df_sn)):
  69. try:
  70. if df_sn.loc[i, 'imei'][5:9] == 'N640':
  71. celltype=1 #6040三元电芯
  72. elif df_sn.loc[i, 'imei'][5:9] == 'N440':
  73. celltype=2 #4840三元电芯
  74. elif df_sn.loc[i, 'imei'][5:9] == 'L660':
  75. celltype=99 # 6060锂电芯
  76. elif df_sn.loc[i, 'imei'][3:5] == 'LX' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  77. celltype=3 #力信 50ah三元电芯
  78. elif df_sn.loc[i, 'imei'][3:5] == 'CL' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  79. celltype=4 #CATL 50ah三元电芯
  80. else:
  81. logger.info("pid-{} celltype-{} SN: {} SKIP!".format(os.getpid(), "未知", sn))
  82. continue
  83. sn = df_sn.loc[i, 'sn']
  84. logger.info("pid-{} celltype-{} SN: {} START!".format(os.getpid(), celltype, sn))
  85. DBRead=DBDownload.DBDownload(host, port, db, user, password, 1)
  86. with DBRead as DBRead:
  87. df_soh=DBRead.getdata('time_st','time_sp','sn','method','soh','cellsoh', tablename=tablename, sn=sn, timename='',st='',sp='')
  88. dbManager = DBManager.DBManager()
  89. # 处理运行历史数据
  90. if (history_run_flag):
  91. this_sn = df_first_data_time[df_first_data_time['sn']==sn]
  92. if (len(this_sn) == 0):
  93. 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")
  94. else:
  95. first_data_time = df_first_data_time.loc[df_first_data_time[df_first_data_time['sn']==sn].index, 'first_data_time'].values[0]
  96. if pd.isnull(first_data_time):
  97. start_time = "2018-01-01 00:00:00"
  98. else:
  99. 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")
  100. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  101. df_bms = df_data['bms']
  102. BatInterShort=CBMSBatInterShort.BatInterShort(sn, celltype, df_bms, df_soh)
  103. df_res=BatInterShort.intershort()
  104. if not df_res.empty:
  105. df_res.columns = ['time_st', 'time_sp', 'sn', 'method', 'short_current', 'baltime']
  106. df_res.to_sql("cellStateEstimation_interShort",con=db_engine, if_exists="append",index=False)
  107. logger.info("pid-{} celltype-{} SN: {} DONE!".format(os.getpid(), celltype, sn))
  108. except:
  109. logger.error(traceback.format_exc)
  110. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)