deploy_7255.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. df_sn_done = pd.read_csv("SNnums_C7255_析锂状态.csv")
  42. sn_done = list(set(df_sn_done['sn']))
  43. for i in range(0, len(df_sn)):
  44. try:
  45. sn = df_sn.loc[i, 'sn']
  46. # if sn in sn_done:
  47. # logger.info(sn+"已计算,跳过")
  48. # continue
  49. # if df_sn.loc[i, 'imei'][5:9] == 'N640':
  50. # celltype=1 #6040三元电芯
  51. # elif df_sn.loc[i, 'imei'][5:9] == 'N440':
  52. # celltype=2 #4840三元电芯
  53. # elif df_sn.loc[i, 'imei'][5:9] == 'L660':
  54. # celltype=99 # 6060锂电芯
  55. # elif df_sn.loc[i, 'imei'][3:5] == 'LX' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  56. # celltype=3 #力信 50ah三元电芯
  57. if df_sn.loc[i, 'imei'][3:5] == 'CL' and df_sn.loc[i, 'imei'][5:9] == 'N750':
  58. celltype=4 #CATL 50ah三元电芯
  59. # if df_sn.loc[i, 'imei'][3:9] == 'CLL128':
  60. # celltype=100 #CATL 50ah三元电芯
  61. else:
  62. logger.info("pid-{} celltype-{} SN: {} SKIP!".format(os.getpid(), "未知", sn))
  63. continue
  64. # 处理运行历史数据
  65. this_sn = df_first_data_time[df_first_data_time['sn']==sn]
  66. if (len(this_sn) == 0):
  67. 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")
  68. else:
  69. first_data_time = df_first_data_time.loc[df_first_data_time[df_first_data_time['sn']==sn].index, 'first_data_time'].values[0]
  70. if pd.isnull(first_data_time):
  71. start_time = "2021-11-17 00:00:00"
  72. else:
  73. 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")
  74. logger.info("pid-{} celltype-{} SN: {} START!".format(os.getpid(), celltype, sn))
  75. param=QX_BatteryParam.BatteryInfo(celltype)
  76. #读取原始数据库数据........................................................................................................................................................
  77. dbManager = DBManager.DBManager()
  78. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  79. df_bms = df_data['bms']
  80. df_Diag_lipltd = pd.DataFrame(columns = ['sn','time','liplated', 'liplated_amount'])
  81. df_Diag_lipltd_add = pd.DataFrame(columns = ['sn','time','liplated', 'liplated_amount'])
  82. #析锂诊断................................................................................................................................................................
  83. if not df_bms.empty:
  84. Diag_lipltd_temp = Li_plated.Liplated_test(sn,celltype,df_bms)#析锂检测
  85. df_Diag_lipltd_add = Diag_lipltd_temp.liplated_detect()
  86. if not df_Diag_lipltd_add.empty:
  87. df_Diag_lipltd_temp = df_Diag_lipltd.append(df_Diag_lipltd_add)
  88. df_Diag_lipltd = df_Diag_lipltd_temp.drop_duplicates(subset = ['sn','time'], keep = 'first', inplace = False)
  89. df_Diag_lipltd.sort_values(by = ['sn'], axis = 0, ascending=True,inplace=True)#对故障信息按照时间进行排序
  90. df_Diag_lipltd['add_time'] = datetime.datetime.now()
  91. df_Diag_lipltd.to_sql("mechanism_liplated",con=db_engine, if_exists="append",index=False)
  92. logger.info("pid-{} celltype-{} SN: {} DONE!".format(os.getpid(), celltype, sn))
  93. except:
  94. logger.error(traceback.format_exc)
  95. logger.error(u"{} :{},{} 任务运行错误\n".format(sn,start_time,end_time), exc_info=True)
  96. logger.info("cycle DONE !!!!!!!!!!!!!!!!!!!!")
  97. db_engine.dispose()
  98. if __name__ == "__main__":
  99. # 时间设置
  100. # now_time = datetime.datetime.now()
  101. # pre_time = now_time + dateutil.relativedelta.relativedelta(days=-1)# 前一日
  102. # end_time=datetime.datetime.strftime(now_time,"%Y-%m-%d 00:00:00")
  103. # start_time=datetime.datetime.strftime(pre_time,"%Y-%m-%d 00:00:00")
  104. history_run_flag = False # 历史数据运行标志
  105. # 更新sn列表
  106. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  107. port=3306
  108. db='qixiang_oss'
  109. user='qixiang_oss'
  110. password='Qixiang2021'
  111. conn = pymysql.connect(host=host, port=port, user=user, password=password, database=db)
  112. cursor = conn.cursor()
  113. cursor.execute("select sn, imei, add_time from app_device")
  114. res = cursor.fetchall()
  115. df_sn = pd.DataFrame(res, columns=['sn', 'imei', 'add_time'])
  116. df_sn = df_sn.reset_index(drop=True)
  117. conn.close();
  118. # 数据库配置
  119. host = 'rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  120. port = 3306
  121. user = 'qx_cas'
  122. password = parse.quote_plus('Qx@123456')
  123. database = 'qx_cas'
  124. db_engine = create_engine(
  125. "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  126. user, password, host, port, database
  127. ))
  128. DbSession = sessionmaker(bind=db_engine)
  129. # 运行历史数据配置
  130. df_first_data_time = pd.read_sql("select * from bat_first_data_time", db_engine)
  131. db_engine.dispose()
  132. # 日志配置
  133. now_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).replace(":","_")
  134. log_path = 'log7255/' + now_str
  135. if not os.path.exists(log_path):
  136. os.makedirs(log_path)
  137. log = Log.Mylog(log_name='saftyCenter_liplated7255', log_level = 'info')
  138. log.set_file_hl(file_name='{}/info.log'.format(log_path), log_level='info', size=1024* 1024 * 100)
  139. log.set_file_hl(file_name='{}/error.log'.format(log_path), log_level='error', size=1024* 1024 * 100)
  140. logger = log.get_logger()
  141. logger.info("pid is {}".format(os.getpid()))
  142. # 算法参数
  143. # host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  144. # port=3306
  145. # db='safety_platform'
  146. # user='qx_read'
  147. # password=parse.quote_plus('Qx@123456')
  148. # tablename='all_fault_info'
  149. # db_res_engine = create_engine(
  150. # "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(
  151. # user, password, host, port, db
  152. # ))
  153. #定时任务.......................................................................................................................................................................
  154. cell_platd_test(df_sn, df_first_data_time)
  155. # scheduler = BlockingScheduler()
  156. # scheduler.add_job(fun, 'interval', seconds=600, id='diag_job')
  157. # try:
  158. # scheduler.start()
  159. # except Exception as e:
  160. # scheduler.shutdown()
  161. # logger.error(str(e))