main.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import CBMSBatDiag
  2. from SC_SamplingSafty import SamplingSafty
  3. import datetime
  4. import pandas as pd
  5. from LIB.BACKEND import DBManager, Log
  6. from sqlalchemy import create_engine
  7. import time, datetime
  8. from apscheduler.schedulers.blocking import BlockingScheduler
  9. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload as DBDownload
  10. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  11. from pandas.core.frame import DataFrame
  12. import datacompy
  13. from LIB.MIDDLE.SaftyCenter.Common import FeiShuData
  14. from LIB.MIDDLE.SaftyCenter.Common import QX_BatteryParam
  15. from LIB.MIDDLE.SaftyCenter.Common import DBDownload as DBDw
  16. #...................................电池包电芯安全诊断函数......................................................................................................................
  17. def diag_cal():
  18. global SNnums
  19. global df_Diag_Ram
  20. start=time.time()
  21. end_time=datetime.datetime.now()
  22. start_time=end_time-datetime.timedelta(seconds=300)
  23. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  24. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  25. df_read_Yunw = FeiShuData.getFeiShuDATA()#运维表格数据
  26. df_read_Yunw.rename(columns={'电池编码':'product_id'},inplace=True)
  27. df_read_Yunw.rename(columns={'内容描述':'info'},inplace=True)
  28. df_read_Yunw.rename(columns={'发生时间':'start_time'},inplace=True)
  29. df_read_Yunw.rename(columns={'维修信息':'advice'},inplace=True)
  30. for sn in SNnums:
  31. print(sn)
  32. if 'PK500' in sn:
  33. celltype=1 #6040三元电芯
  34. elif 'PK502' in sn:
  35. celltype=2 #4840三元电芯
  36. elif 'K504B' in sn:
  37. celltype=99 #60ah林磷酸铁锂电芯
  38. elif 'MGMLXN750' in sn:
  39. celltype=3 #力信50ah三元电芯
  40. elif 'MGMCLN750' or 'UD' in sn:
  41. celltype=4 #CATL 50ah三元电芯
  42. else:
  43. print('SN:{},未找到对应电池类型!!!'.format(sn))
  44. continue
  45. # sys.exit()
  46. param=QX_BatteryParam.BatteryInfo(celltype)
  47. #读取原始数据库数据........................................................................................................................................................
  48. dbManager = DBManager.DBManager()
  49. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  50. df_bms = df_data['bms']
  51. #读取结果数据库数据........................................................................................................................................................
  52. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  53. port=3306
  54. db='qx_cas'
  55. user='qx_read'
  56. password='Qx@123456'
  57. mode=1
  58. tablename1='cellstateestimation_soh'
  59. tablename2='cellstateestimation_uniform_socvoltdiff'
  60. #电池诊断................................................................................................................................................................
  61. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  62. with DBRead as DBRead:
  63. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  64. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellmin_num,cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time)
  65. #电池诊断................................................................................................................................................................
  66. CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  67. df_Diag_Ram = df_Diag_Ram.drop_duplicates(subset=['product_id','code','start_time','Batpos','info'],keep='first')#sn之外的故障
  68. df_Diag_Ram_sn = df_Diag_Ram.loc[df_Diag_Ram['product_id']==sn]#历史故障
  69. df_Diag_Ram_sn_else = pd.concat([df_Diag_Ram,df_Diag_Ram_sn,df_Diag_Ram_sn]).drop_duplicates(subset=['product_id','code','start_time','Batpos','info'],keep=False)#sn之外的故障
  70. CellFltInfo = df_Diag_Ram_sn.drop('Batpos',axis=1)
  71. df_Diag_Ram_add = pd.DataFrame()
  72. df_Diag_Ram_Update_change = pd.DataFrame()
  73. if not df_bms.empty:
  74. df_Diag_Batdiag_update_xq=SamplingSafty.main(sn,param,df_bms,CellFltInfo)#学琦计算故障
  75. BatDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform, CellFltInfo)#鹏飞计算
  76. df_Diag_Batdiag_update=BatDiag.diag()
  77. df_Diag_Cal_Update_add = pd.concat([df_Diag_Batdiag_update_xq,df_Diag_Batdiag_update])#重新计算的该SN下的故障
  78. df_Diag_Cal_Update_temp = df_Diag_Cal_Update_add.drop_duplicates(subset=['product_id','start_time','end_time','code','info'], keep='first', inplace=False, ignore_index=False)#去除相同故障
  79. df_Diag_cal_early_unfix = pd.DataFrame()
  80. df_Diag_Cal_finish = pd.DataFrame()
  81. df_Diag_cal_early_fix = pd.DataFrame()
  82. if not df_Diag_Cal_Update_temp.empty:
  83. #------------------------------合并两者故障,并将同一sn号下的车辆故障放一起----------------------------------------------
  84. df_Diag_Cal_Update = df_Diag_Cal_Update_temp#替换上一行
  85. df_Diag_Cal_finish = df_Diag_Cal_Update.loc[df_Diag_Cal_Update['end_time'] != '0000-00-00 00:00:00']
  86. df_Diag_Cal_new = df_Diag_Cal_Update.loc[df_Diag_Cal_Update['end_time'] == '0000-00-00 00:00:00']
  87. df_Diag_Cal_finish['Batpos'] = 1
  88. df_Diag_Cal_new['Batpos'] = 0
  89. df_feishu_sta = df_read_Yunw.loc[(df_read_Yunw['product_id'] == sn)]#飞书中该sn车辆状态
  90. if df_feishu_sta.empty:#飞书中没有该sn记录故障的新增
  91. df_Diag_cal_early_unfix = df_Diag_Cal_new#如果为新出故障,则直接记录在df_diag_frame中
  92. else:
  93. df_Diag_cal_later = df_Diag_Cal_new.loc[pd.to_datetime(df_Diag_Cal_new['start_time']) > max(pd.to_datetime(df_feishu_sta['start_time']))]#故障表中故障时间晚于飞书记录时间的新增
  94. df_Diag_cal_early = pd.concat([df_Diag_Cal_new,df_Diag_cal_later,df_Diag_cal_later]).drop_duplicates(subset=['product_id','code','start_time'],keep=False)#故障表中故障时间早于飞书记录时间
  95. df_feishu_sta_latest = df_feishu_sta.loc[pd.to_datetime(df_feishu_sta['start_time']) == max(pd.to_datetime(df_feishu_sta['start_time']))]#飞书中该SN下的最新故障
  96. df_feishu_diag_unfix = (df_feishu_sta_latest['advice'] == '需正常返仓') | (df_feishu_sta_latest['advice'] == '需紧急返仓')
  97. if any(df_feishu_diag_unfix):
  98. df_Diag_cal_early_unfix = df_Diag_Cal_new
  99. else:
  100. df_Diag_cal_early_fix = df_Diag_cal_early
  101. df_Diag_cal_early_unfix = df_Diag_cal_later
  102. if not df_Diag_cal_early_fix.empty:
  103. df_Diag_cal_early_fix['Batpos'] = 1
  104. df_Diag_Ram_Update = pd.concat([df_Diag_cal_early_unfix,df_Diag_cal_early_fix,df_Diag_Cal_finish])
  105. df_Diag_Ram_Update['start_time'] = pd.to_dateime(df_Diag_Ram_Update['start_time'])
  106. df_Diag_Ram_Update.sort_values(by = ['start_time'], axis = 0, ascending=True,inplace=True)#该sn下当次诊断的故障状态
  107. df_Diag_Ram_add = pd.concat([df_Diag_Ram_Update,df_Diag_Ram_sn,df_Diag_Ram_sn]).drop_duplicates(subset=['start_time','code'],keep=False)#此次判断中新增故障
  108. df_Diag_Ram_Update_old = pd.concat([df_Diag_Ram_Update,df_Diag_Ram_add,df_Diag_Ram_add]).drop_duplicates(subset=['start_time','code'],keep=False)#此次判断中新增故障
  109. df_Diag_Ram_Update_change = pd.concat([df_Diag_Ram_Update_old,df_Diag_Ram_sn,df_Diag_Ram_sn]).drop_duplicates(subset=['start_time','code','Batpos'],keep=False)#此次判断中新增故障
  110. df_Diag_Ram_sav = df_Diag_Ram_Update.loc[df_Diag_Ram_Update['Batpos'] == 0]
  111. df_Diag_Ram = pd.concat([df_Diag_Ram_sn_else,df_Diag_Ram_sav])
  112. if len(df_Diag_Ram_sav) > 0:#历史及现有故障
  113. df_Diag_Ram.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\04故障诊断\01Screen_Problem\result.csv',index=False,encoding='GB18030')
  114. if len(df_Diag_Ram_add) > 0:#新增故障
  115. df_Diag_Ram_add.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\04故障诊断\01Screen_Problem\result_add.csv',index=False,encoding='GB18030')
  116. if len(df_Diag_Ram_Update_change) > 0:#更改故障
  117. df_Diag_Ram_Update_change.to_csv(r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\04故障诊断\01Screen_Problem\result_change.csv',index=False,encoding='GB18030')
  118. end=time.time()
  119. print(end-start)
  120. #...............................................主函数.......................................................................................................................
  121. if __name__ == "__main__":
  122. global SNnums
  123. excelpath=r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\04故障诊断\01Screen_Problem\sn-20210903.xlsx'
  124. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  125. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  126. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  127. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  128. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  129. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  130. SNnums_6060=SNdata_6060['SN号'].tolist()
  131. SNnums_6040=SNdata_6040['SN号'].tolist()
  132. SNnums_4840=SNdata_4840['SN号'].tolist()
  133. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  134. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  135. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  136. #SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
  137. # SNnums=['MGMCLN750N215I005','PK504B10100004341','PK504B00100004172','MGMLXN750N2189014']
  138. SNnums = ['MGMLXN750N21B5004'] #SNnums_6040
  139. mylog=log.Mylog('log_diag.txt','error')
  140. mylog.logcfg()
  141. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
  142. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  143. port=3306
  144. db='safety_platform'
  145. user='qx_read'
  146. password='Qx@123456'
  147. mode=2
  148. tablename2='all_fault_info'
  149. DBRead = DBDw.DBDownload(host, port, db, user, password,mode)
  150. with DBRead as DBRead:
  151. df_Diag_Ram = DBRead.getdata('start_time','end_time','product_id','code','level','info','advice','Batpos',tablename=tablename2,factory='骑享',sn='',timename='',st='',sp='')
  152. # result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result.csv',encoding='gbk')
  153. #定时任务.......................................................................................................................................................................
  154. scheduler = BlockingScheduler()
  155. scheduler.add_job(diag_cal, 'interval', seconds=300, id='diag_job')
  156. try:
  157. scheduler.start()
  158. except Exception as e:
  159. scheduler.shutdown()
  160. print(repr(e))
  161. mylog.logopt(e)