main.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #from test.spf.BatDiag import CBMSBatDiag, Log
  2. import CBMSBatDiag
  3. import QX_BatteryParam
  4. from SC_SamplingSafty import SamplingSafty
  5. import datetime
  6. import pandas as pd
  7. from LIB.BACKEND import DBManager, Log
  8. from sqlalchemy import create_engine
  9. import time, datetime
  10. from apscheduler.schedulers.blocking import BlockingScheduler
  11. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import DBDownload
  12. from LIB.MIDDLE.CellStateEstimation.Common.V1_0_1 import log
  13. from pandas.core.frame import DataFrame
  14. import datacompy
  15. import GetFeiShuData
  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=120)
  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. for sn in SNnums:
  26. if 'PK500' in sn:
  27. celltype=1 #6040三元电芯
  28. elif 'PK502' in sn:
  29. celltype=2 #4840三元电芯
  30. elif 'K504B' in sn:
  31. celltype=99 #60ah林磷酸铁锂电芯
  32. elif 'MGMLXN750' in sn:
  33. celltype=3 #力信50ah三元电芯
  34. elif 'MGMCLN750' or 'UD' in sn:
  35. celltype=4 #CATL 50ah三元电芯
  36. else:
  37. print('SN:{},未找到对应电池类型!!!'.format(sn))
  38. continue
  39. # sys.exit()
  40. param=QX_BatteryParam.BatteryInfo(celltype)
  41. # sn='PK50201A000002039'
  42. # celltype=2
  43. # start_time='2021-05-02 09:12:26'
  44. # end_time='2021-06-03 19:12:26'
  45. # # df_bms= pd.read_csv(r'D:\Platform\platform_python\data_analyze_platform\USER\01qixiang\98Download\\'+'BMS_'+sn+'.csv',encoding='GB18030')
  46. #读取原始数据库数据........................................................................................................................................................
  47. dbManager = DBManager.DBManager()
  48. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  49. df_bms = df_data['bms']
  50. df_bms.to_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\\''BMS_'+sn+'.csv',encoding='GB18030')
  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. tablename3='cellstateestimation_soc'
  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. # df_soc=DBRead.getdata('time','sn','packsoc', tablename=tablename3, sn=sn)
  66. #电池诊断................................................................................................................................................................
  67. #BatDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform)
  68. #df_res=BatDiag.diag()
  69. #df_Diag_Ram_old=df_Diag_Ram
  70. df_Diag_Ram_Update=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  71. CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  72. if not df_bms.empty:
  73. CellFltInfo=df_Diag_Ram[df_Diag_Ram['product_id']==sn]
  74. df_Diag_Ram_Update=SamplingSafty.main(sn,param,df_bms,CellFltInfo)#学琦计算故障
  75. BatDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform, CellFltInfo)#鹏飞计算
  76. df_res=BatDiag.diag()
  77. df_Diag_Ram_Update=df_Diag_Ram_Update.append(df_res)
  78. #------------------------------合并两者故障,并将同一sn号下的车辆故障放一起----------------------------------------------
  79. df_read_Yunw = GetFeiShuData.getFeiShuDATA()#运维表格数据
  80. set_diff_df = pd.concat([df_Diag_Ram_Update,df_read_Yunw,df_read_Yunw]).drop_duplicates(subset=['product_id'],keep=False)#新增故障的sn,报出故障减去原文档中的sn
  81. same_sn = df_read_Yunw[(df_read_Yunw['维修信息']== '需正常返仓') | (df_read_Yunw['维修信息']== '需紧急返仓')]#筛选待修改和需返回车辆
  82. set_same_df = df_Diag_Ram_Update.loc[df_Diag_Ram_Update['product_id'].isin(same_sn['product_id'])]#筛选待修改和需返回车辆
  83. df_temp = set_diff_df.append(set_same_df)#新增及待改进车辆
  84. set_same_df_else = pd.concat([df_Diag_Ram_Update,df_temp,df_temp]).drop_duplicates(subset=['product_id'],keep=False)#寻找旧sn中新增故障的sn
  85. #先取sn相同的车辆,然后对比时间,保留故障码时间晚于运营记录的信息
  86. set_same_df_oth = pd.DataFrame()
  87. for i in set_same_df_else['product_id']:
  88. if max(df_Diag_Ram_Update.loc[df_Diag_Ram_Update['product_id'] == i]['start_time']) > max(df_read_Yunw.loc[df_read_Yunw['product_id'] == i]['发生时间']):
  89. temp_df = df_Diag_Ram_Update.loc[[df_Diag_Ram_Update['product_id'] == i]['start_time'] > max(df_read_Yunw.loc[df_read_Yunw['product_id'] == i]['发生时间'])]#故障时间晚于运维记录时间的sn故障
  90. set_same_df_oth = set_same_df_oth.append(temp_df)
  91. else:
  92. pass
  93. df_diag_frame = pd.concat([set_diff_df,same_sn,set_same_df_oth])#筛选新增sn故障、维修信息依旧故障sn、旧sn中新增故障
  94. df_tempnum = df_diag_frame.groupby(["product_id"]).size()#获取每个sn的故障总数
  95. col1 = df_tempnum[df_tempnum>1].reset_index()[["product_id"]]#多故障sn号
  96. col2 = df_tempnum[df_tempnum==1].reset_index()[["product_id"]]#单故障sn号
  97. df_temp1 = pd.merge(col1,df_diag_frame,on=["product_id"])#多故障码数据筛选
  98. df_temp1.sort_values(by = "start_time", axis = 0, ascending=True,inplace=True)#对故障信息按照sn号进行排序
  99. df_temp2 = pd.merge(col2,df_diag_frame,on=["product_id"])#单故障码数据筛选
  100. df_temp3 = pd.concat([df_temp1,df_temp2], ignore_index=True)#多故障及单故障合并
  101. df_Diag_Ram_Update = df_temp3#计算故障信息
  102. #-------------------------------故障--------------------------------------------
  103. if not df_Diag_Ram_Update.empty:
  104. sn_index=df_Diag_Ram[df_Diag_Ram['product_id']==sn].index
  105. df_Diag_Ram=df_Diag_Ram.drop(index=sn_index)
  106. df_Diag_Ram=df_Diag_Ram.append(df_Diag_Ram_Update)
  107. df_Diag_Ram.reset_index(inplace=True,drop=True)
  108. Diag_Ram_Dif=datacompy.Compare(df_Diag_Ram_Update,CellFltInfo,join_columns=['product_id','end_time','code'])
  109. Diag_Ram_Dif=Diag_Ram_Dif.df1_unq_rows
  110. if len(Diag_Ram_Dif)>0:
  111. Diag_Ram_Dif_New=Diag_Ram_Dif[Diag_Ram_Dif['end_time']=='0000-00-00 00:00:00']
  112. Diag_Ram_Dif_Finish=df_Diag_Ram[df_Diag_Ram['end_time']!='0000-00-00 00:00:00']
  113. if len(Diag_Ram_Dif_New)>0:
  114. result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\result.csv',encoding='gbk')
  115. result=result.append(Diag_Ram_Dif_New)
  116. result.to_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\result.csv',index=False,encoding='GB18030')
  117. if len(Diag_Ram_Dif_Finish)>0:
  118. result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\result.csv',encoding='gbk')
  119. Diag_Ram_Dif_Finish=Diag_Ram_Dif_Finish.reset_index(drop=True)
  120. for i in range(0,len(Diag_Ram_Dif_Finish)):
  121. result.loc[result[result[result['product_id']==Diag_Ram_Dif_Finish.loc[i,'product_id']]['code']==Diag_Ram_Dif_Finish.loc[i,'code']].index,'end_time']=Diag_Ram_Dif_Finish.loc[i,'end_time']
  122. result.to_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\result.csv',index=False,encoding='GB18030')
  123. end=time.time()
  124. print(end-start)
  125. # print(df_soh)
  126. #...................................电池包电芯安全诊断函数......................................................................................................................
  127. def shortdiag_cal():
  128. global SNnums
  129. global df_Diag_Ram
  130. start=time.time()
  131. end_time=datetime.datetime.now()
  132. start_time=end_time-datetime.timedelta(days=30)
  133. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  134. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  135. for sn in SNnums:
  136. if 'PK500' in sn:
  137. celltype=1 #6040三元电芯
  138. elif 'PK502' in sn:
  139. celltype=2 #4840三元电芯
  140. elif 'K504B' in sn:
  141. celltype=99 #60ah林磷酸铁锂电芯
  142. elif 'MGMLXN750' in sn:
  143. celltype=3 #力信50ah三元电芯
  144. elif 'MGMCLN750' or 'UD' in sn:
  145. celltype=4 #CATL 50ah三元电芯
  146. else:
  147. print('SN:{},未找到对应电池类型!!!'.format(sn))
  148. continue
  149. # sys.exit()
  150. #读取结果数据库数据........................................................................................................................................................
  151. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  152. port=3306
  153. db='qx_cas'
  154. user='qx_read'
  155. password='Qx@123456'
  156. mode=2
  157. tablename4='cellstateestimation_intershort'
  158. DBRead=DBDownload.DBDownload(host, port, db, user, password, mode) #mode==1取数据库最后一行数据
  159. with DBRead as DBRead:
  160. df_short=DBRead.getdata('time_sp,sn,short_current', tablename=tablename4, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  161. #电池诊断................................................................................................................................................................
  162. ShortDiag=CBMSBatDiag.ShortDiag(sn,celltype, df_short)
  163. df_res=ShortDiag.shortdiag()
  164. df_res.to_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\\'+'CBMS_diag_'+sn+'.csv',encoding='GB18030')
  165. print(df_res)
  166. end=time.time()
  167. print(end-start)
  168. # print(df_soh)
  169. #...............................................主函数.......................................................................................................................
  170. if __name__ == "__main__":
  171. excelpath=r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\sn-20210903.xlsx'
  172. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  173. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  174. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  175. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  176. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  177. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  178. SNnums_6060=SNdata_6060['SN号'].tolist()
  179. SNnums_6040=SNdata_6040['SN号'].tolist()
  180. SNnums_4840=SNdata_4840['SN号'].tolist()
  181. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  182. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  183. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  184. # SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
  185. SNnums=['MGMCLN750N215I005','PK504B10100004341','PK504B00100004172','MGMLXN750N2189014']
  186. #SNnums = SNnums_6060
  187. mylog=log.Mylog('log_diag.txt','error')
  188. mylog.logcfg()
  189. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
  190. result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\01智联运维故障显示\result.csv',encoding='gbk')
  191. df_Diag_Ram=result[result['end_time']=='0000-00-00 00:00:00']
  192. print('----------------输入--------')
  193. print(df_Diag_Ram)
  194. print('-------done-----------')
  195. #定时任务.......................................................................................................................................................................
  196. scheduler = BlockingScheduler()
  197. scheduler.add_job(diag_cal, 'interval', seconds=120, id='diag_job')
  198. scheduler.add_job(shortdiag_cal, 'interval', days=7, id='shortdiag_job')
  199. try:
  200. scheduler.start()
  201. except Exception as e:
  202. scheduler.shutdown()
  203. print(repr(e))
  204. mylog.logopt(e)