deploy_7255.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.Common import log
  13. from pandas.core.frame import DataFrame
  14. from urllib import parse
  15. import pymysql
  16. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import BatParam
  17. import datacompy
  18. from LIB.MIDDLE.SaftyCenter.Sorest import sor_est
  19. def cell_sor_test(df_sn,df_first_data_time):
  20. global df_diag_sor
  21. # 读取结果数据库
  22. host2='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  23. port2=3306
  24. db2='qx_cas'
  25. user2='qx_read'
  26. password2='Qx@123456'
  27. db_engine = create_engine(
  28. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  29. user, password, host, port, database
  30. ))
  31. start=time.time()
  32. end_time=datetime.datetime.now()
  33. start_time=end_time-datetime.timedelta(days=7)
  34. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  35. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  36. logger.info("cycle start !!!!!!!!!!!!!!!!!!!!")
  37. start_time = '2021-10-24 00:00:00'
  38. end_time = '2021-10-31 00:00:00'
  39. for i in range(0, len(df_sn)):
  40. try:
  41. sn = df_sn.loc[i, 'sn']
  42. # if df_sn.loc[i, 'imei'][5:9] == 'N640':
  43. # celltype=1 #6040三元电芯
  44. # elif df_sn.loc[i, 'imei'][5:9] == 'N440':
  45. # celltype=2 #4840三元电芯
  46. # elif df_sn.loc[i, 'imei'][5:9] == 'L660':
  47. # celltype=99 # 6060锂电芯
  48. # elif df_sn.loc[i, 'imei'][3:5] == 'LX' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  49. # celltype=3 #力信 50ah三元电芯
  50. # elif df_sn.loc[i, 'imei'][3:9] == 'CLL128':
  51. # celltype=100 # 重卡
  52. if df_sn.loc[i, 'imei'][3:5] == 'CL' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  53. celltype=4 #CATL 50ah三元电芯
  54. else:
  55. logger.info("pid-{} celltype-{} SN: {} SKIP!".format(os.getpid(), "未知", sn))
  56. continue
  57. logger.info("pid-{} celltype-{} SN: {} START!".format(os.getpid(), celltype, sn))
  58. this_sn = df_first_data_time[df_first_data_time['sn']==sn]
  59. if (len(this_sn) == 0):
  60. 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")
  61. else:
  62. first_data_time = df_first_data_time.loc[df_first_data_time[df_first_data_time['sn']==sn].index, 'first_data_time'].values[0]
  63. if pd.isnull(first_data_time):
  64. start_time = "2019-01-01 00:00:00"
  65. else:
  66. 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")
  67. param = BatParam.BatParam(celltype)
  68. #读取原始数据库数据........................................................................................................................................................
  69. dbManager = DBManager.DBManager()
  70. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  71. df_bms = df_data['bms']
  72. if not df_bms.empty:
  73. Diag_sorvol_temp = sor_est.sor_est(sn,celltype,df_bms)#电压内阻估计
  74. df_sor_add = Diag_sorvol_temp.sor_cal()
  75. if not df_sor_add.empty:
  76. df_sor_add = df_sor_add.drop_duplicates(subset = ['sn','time'], keep = 'first', inplace = False)
  77. df_sor_add['add_time'] = datetime.datetime.now()
  78. df_sor_add.to_sql("mechanism_sorout",con=db_engine, if_exists="append",index=False)
  79. logger.info("pid-{} celltype-{} SN: {} DONE!".format(os.getpid(), celltype, sn))
  80. except:
  81. logger.error(traceback.format_exc)
  82. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  83. logger.info("cycle DONE !!!!!!!!!!!!!!!!!!!!")
  84. db_engine.dispose()
  85. if __name__ == "__main__":
  86. # 时间设置
  87. # now_time = datetime.datetime.now()
  88. # pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)# 前一日
  89. # end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 00:00:00")
  90. # start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 00:00:00")
  91. history_run_flag = False # 历史数据运行标志
  92. # 更新sn列表
  93. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  94. port=3306
  95. db='qixiang_oss'
  96. user='qixiang_oss'
  97. password='Qixiang2021'
  98. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  99. cursor = conn.cursor()
  100. cursor.execute("select sn, imei, add_time from app_device")
  101. res = cursor.fetchall()
  102. df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
  103. df_sn = df_sn.reset_index(drop=True)
  104. conn.close();
  105. # 数据库配置
  106. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  107. port = 3306
  108. user = 'qx_cas'
  109. password = parse.quote_plus('Qx@123456')
  110. database = 'qx_cas'
  111. db_engine = create_engine(
  112. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  113. user, password, host, port, database
  114. ))
  115. DbSession = sessionmaker(bind=db_engine)
  116. # 运行历史数据配置
  117. df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
  118. # 日志配置
  119. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  120. log_path = 'log_7255/' + now_str
  121. if not os.path.exists(log_path):
  122. os.makedirs(log_path)
  123. log = Log.Mylog(log_name='saftyCenter_sorout7255', log_level = 'info')
  124. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  125. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  126. logger = log.get_logger()
  127. logger.info("pid is {}".format(os.getpid()))
  128. # 算法参数
  129. # host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  130. # port=3306
  131. # db='safety_platform'
  132. # user='qx_read'
  133. # password=parse.quote_plus('Qx@123456')
  134. # tablename='all_fault_info'
  135. # db_res_engine = create_engine(
  136. # "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  137. # user, password, host, port, db
  138. # ))
  139. #定时任务.......................................................................................................................................................................
  140. cell_sor_test(df_sn,df_first_data_time)
  141. # scheduler = BlockingScheduler()
  142. # scheduler.add_job(fun, 'interval', seconds=600, id='diag_job')
  143. # try:
  144. # scheduler.start()
  145. # except Exception as e:
  146. # scheduler.shutdown()
  147. # logger.error(str(e))