deploy_7255_other.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 LIB.MIDDLE.SaftyCenter.Common import QX_BatteryParam
  15. from LIB.MIDDLE.SaftyCenter.diagfault.SC_SamplingSafty import SamplingSafty
  16. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload
  17. from urllib import parse
  18. import pymysql
  19. import pdb
  20. from apscheduler.schedulers.blocking import BlockingScheduler
  21. import datacompy
  22. from LIB.MIDDLE.SaftyCenter.Liplated import Li_plated
  23. from LIB.MIDDLE.SaftyCenter.diagfault.SC_SamplingSafty import SamplingSafty
  24. def cell_platd_test(df_sn, df_first_data_time):
  25. # 读取结果数据库
  26. host2='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  27. port2=3306
  28. db2='qx_cas'
  29. user2='qx_read'
  30. password2='Qx@123456'
  31. db_engine = create_engine(
  32. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  33. user, password, host, port, database
  34. ))
  35. start=time.time()
  36. end_time=datetime.datetime.now()
  37. start_time=end_time-datetime.timedelta(days=90)
  38. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  39. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  40. logger.info("cycle start !!!!!!!!!!!!!!!!!!!!")
  41. for i in range(0, len(df_sn)):
  42. try:
  43. if df_sn.loc[i, 'imei'][5:9] == 'N640':
  44. celltype=1 #6040三元电芯
  45. elif df_sn.loc[i, 'imei'][5:9] == 'N440':
  46. celltype=2 #4840三元电芯
  47. elif df_sn.loc[i, 'imei'][5:9] == 'L660':
  48. celltype=99 # 6060锂电芯
  49. elif df_sn.loc[i, 'imei'][3:5] == 'LX' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  50. celltype=3 #力信 50ah三元电芯
  51. # elif df_sn.loc[i, 'imei'][3:5] == 'CL' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  52. # celltype=4 #CATL 50ah三元电芯
  53. else:
  54. logger.info("pid-{} celltype-{} SN: {} SKIP!".format(os.getpid(), "未知", sn))
  55. continue
  56. sn = df_sn.loc[i, 'sn']
  57. logger.info("pid-{} celltype-{} SN: {} START!".format(os.getpid(), celltype, sn))
  58. param=QX_BatteryParam.BatteryInfo(celltype)
  59. #读取原始数据库数据........................................................................................................................................................
  60. dbManager = DBManager.DBManager()
  61. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  62. df_bms = df_data['bms']
  63. df_Diag_lipltd = pd.DataFrame(columns = ['sn','time','liplated', 'liplated_amount'])
  64. df_Diag_lipltd_add = pd.DataFrame(columns = ['sn','time','liplated', 'liplated_amount'])
  65. df_diag_sor = pd.DataFrame(columns = ['sn','time','sorout_confr', 'sorout_amplt'])
  66. df_diag_vol = pd.DataFrame(columns = ['sn','time','volout_confr', 'volout_amplt'])
  67. df_diag_volsor = pd.DataFrame(columns = ['sn','time','volsorout_confr', 'volsorout_amplt'])
  68. #析锂诊断................................................................................................................................................................
  69. if not df_bms.empty:
  70. Diag_lipltd_temp = Li_plated.Liplated_test(sn,celltype,df_bms)#析锂检测
  71. df_Diag_lipltd_add = Diag_lipltd_temp.liplated_detect()
  72. # Diag_sorvol_temp = vol_sor_est.vol_sor_est(sn,celltype,df_bms)#电压内阻估计
  73. # [df_diag_sor_add, df_diag_vol_add, df_diag_sorvol_add] = Diag_sorvol_temp.volsor_cal()
  74. if not df_Diag_lipltd_add.empty:
  75. df_Diag_lipltd_temp = df_Diag_lipltd.append(df_Diag_lipltd_add)
  76. df_Diag_lipltd = df_Diag_lipltd_temp.drop_duplicates(subset = ['sn','time'], keep = 'first', inplace = False)
  77. df_Diag_lipltd.reset_index(drop = True)
  78. df_Diag_lipltd.sort_values(by = ['sn'], axis = 0, ascending=True,inplace=True)#对故障信息按照时间进行排序
  79. df_Diag_lipltd['add_time'] = datetime.datetime.now()
  80. df_Diag_lipltd.to_sql("mechanism_liplated",con=db_engine, if_exists="append",index=False)
  81. # if not df_diag_sor_add.empty:
  82. # df_diag_sor_temp = df_diag_sor.append(df_diag_sor_add)
  83. # df_diag_sor = df_diag_sor_temp.drop_duplicates(subset = ['sn','time'], keep = 'first', inplace = False)
  84. # df_diag_sor.reset_index(drop = True)
  85. # df_diag_sor.sort_values(by = ['sn'], axis = 0, ascending=True,inplace=True)#对故障信息按照时间进行排序
  86. # df_diag_sor['add_time'] = datetime.datetime.now()
  87. # df_diag_sor.to_sql("mechanism_sorout",con=db_engine, if_exists="append",index=False)
  88. # if not df_diag_vol_add.empty:
  89. # df_diag_vol_temp = df_diag_vol.append(df_diag_vol_add)
  90. # df_diag_vol = df_diag_vol_temp.drop_duplicates(subset = ['sn','time'], keep = 'first', inplace = False)
  91. # df_diag_vol.reset_index(drop = True)
  92. # df_diag_vol.sort_values(by = ['sn'], axis = 0, ascending=True,inplace=True)#对故障信息按照时间进行排序
  93. # df_diag_vol['add_time'] = datetime.datetime.now()
  94. # df_diag_vol.to_sql("mechanism_volout",con=db_engine, if_exists="append",index=False)
  95. # if not df_diag_sorvol_add.empty:
  96. # df_diag_volsor_temp = df_diag_volsor.append(df_diag_sorvol_add)
  97. # df_diag_volsor = df_diag_volsor_temp.drop_duplicates(subset = ['sn','time'], keep = 'first', inplace = False)
  98. # df_diag_volsor.reset_index(drop = True)
  99. # df_diag_volsor.sort_values(by = ['sn'], axis = 0, ascending=True,inplace=True)#对故障信息按照时间进行排序
  100. # df_diag_volsor['add_time'] = datetime.datetime.now()
  101. # df_diag_volsor.to_sql("mechanism_volsorout",con=db_engine, if_exists="append",index=False)
  102. logger.info("pid-{} celltype-{} SN: {} DONE!".format(os.getpid(), celltype, sn))
  103. except:
  104. logger.error(traceback.format_exc)
  105. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  106. logger.info("cycle DONE !!!!!!!!!!!!!!!!!!!!")
  107. db_engine.dispose()
  108. if __name__ == "__main__":
  109. # 时间设置
  110. # now_time = datetime.datetime.now()
  111. # pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)# 前一日
  112. # end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 00:00:00")
  113. # start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 00:00:00")
  114. history_run_flag = False # 历史数据运行标志
  115. # 更新sn列表
  116. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  117. port=3306
  118. db='qixiang_oss'
  119. user='qixiang_oss'
  120. password='Qixiang2021'
  121. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  122. cursor = conn.cursor()
  123. cursor.execute("select sn, imei, add_time from app_device")
  124. res = cursor.fetchall()
  125. df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
  126. df_sn = df_sn.reset_index(drop=True)
  127. conn.close();
  128. # 数据库配置
  129. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  130. port = 3306
  131. user = 'qx_cas'
  132. password = parse.quote_plus('Qx@123456')
  133. database = 'qx_cas'
  134. db_engine = create_engine(
  135. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  136. user, password, host, port, database
  137. ))
  138. DbSession = sessionmaker(bind=db_engine)
  139. # 运行历史数据配置
  140. df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
  141. db_engine.dispose()
  142. # 日志配置
  143. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  144. log_path = 'log_3month/' + now_str
  145. if not os.path.exists(log_path):
  146. os.makedirs(log_path)
  147. log = Log.Mylog(log_name='saftyCenter_liplatedAll', log_level = 'info')
  148. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  149. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  150. logger = log.get_logger()
  151. logger.info("pid is {}".format(os.getpid()))
  152. # 算法参数
  153. # host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  154. # port=3306
  155. # db='safety_platform'
  156. # user='qx_read'
  157. # password=parse.quote_plus('Qx@123456')
  158. # tablename='all_fault_info'
  159. # db_res_engine = create_engine(
  160. # "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  161. # user, password, host, port, db
  162. # ))
  163. #定时任务.......................................................................................................................................................................
  164. cell_platd_test(df_sn, df_first_data_time)
  165. # scheduler = BlockingScheduler()
  166. # scheduler.add_job(fun, 'interval', seconds=600, id='diag_job')
  167. # try:
  168. # scheduler.start()
  169. # except Exception as e:
  170. # scheduler.shutdown()
  171. # logger.error(str(e))