deploy.py 9.3 KB

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