main.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 QX_BatteryParam
  14. from LIB.MIDDLE.SaftyCenter.Common import DBDownload as DBDw
  15. from DataStatistics import DataSta
  16. from DiagDataMerge import DiagDataMerge
  17. #...................................电池包电芯安全诊断函数......................................................................................................................
  18. def diag_cal():
  19. global SNnums
  20. global df_Diag_Ram
  21. global start
  22. #..................................设置时间..........................................................
  23. start=time.time()
  24. end_time=datetime.datetime.now()
  25. start_time=end_time-datetime.timedelta(seconds=900)#15分钟跑一次,一次取15分钟数据
  26. start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
  27. end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
  28. for sn in SNnums:#SN遍历
  29. print(sn)
  30. if 'PK500' in sn:
  31. celltype=1 #6040三元电芯
  32. elif 'PK502' in sn:
  33. celltype=2 #4840三元电芯
  34. elif 'K504B' in sn:
  35. celltype=99 #60ah林磷酸铁锂电芯
  36. elif 'MGMLXN750' in sn:
  37. celltype=3 #力信50ah三元电芯
  38. elif 'MGMCLN750' or 'UD' in sn:
  39. celltype=4 #CATL 50ah三元电芯
  40. else:
  41. print('SN:{},未找到对应电池类型!!!'.format(sn))
  42. continue
  43. # sys.exit()
  44. param=QX_BatteryParam.BatteryInfo(celltype)
  45. #读取原始数据库数据........................................................................................................................................................
  46. dbManager = DBManager.DBManager()
  47. df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
  48. df_bms = df_data['bms']
  49. #读取结果数据库数据........................................................................................................................................................
  50. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  51. port=3306
  52. db='qx_cas'
  53. user='qx_read'
  54. password='Qx@123456'
  55. mode=1
  56. tablename1='cellstateestimation_soh'
  57. tablename2='cellstateestimation_uniform_socvoltdiff'
  58. DBRead=DBDownload.DBDownload(host, port, db, user, password,mode)
  59. with DBRead as DBRead:
  60. df_soh=DBRead.getdata('time_st,sn,soh,cellsoh', tablename=tablename1, sn=sn, timename='time_sp', st=start_time, sp=end_time)
  61. df_uniform=DBRead.getdata('time,sn,cellsoc_diff,cellmin_num,cellmax_num', tablename=tablename2, sn=sn, timename='time', st=start_time, sp=end_time)
  62. #读取电池当前运营状态.....................................................................................................................................................
  63. host='rm-bp10j10qy42bzy0q7.mysql.rds.aliyuncs.com'
  64. port=3306
  65. db='qixiang_manage'
  66. user='qx_query'
  67. password='@Qx_query'
  68. mode=4
  69. tablename1='py_battery'
  70. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  71. with DBRead as DBRead:
  72. df_OprtnSta=DBRead.getdata('qrcode','status', tablename=tablename1, sn=sn, timename='', st='', sp='',factory='')#取最后一条运营状态
  73. #电池诊断.....................................................................................................................................................................
  74. CellFltInfo=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
  75. df_Diag_Ram_sn = df_Diag_Ram.loc[df_Diag_Ram['product_id']==sn]#历史故障
  76. 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之外的故障
  77. CellFltInfo = df_Diag_Ram_sn.drop('Batpos',axis=1)
  78. #获取当前故障电池的历史故障信息↑↑↑↑↑↑↑↑↑↑↑.......................................................................................................................................
  79. if not df_bms.empty:
  80. df_Diag_Batdiag_update_xq=SamplingSafty.main(sn,param,df_bms,CellFltInfo)#学琦计算故障
  81. batDiag=CBMSBatDiag.BatDiag(sn,celltype,df_bms, df_soh, df_uniform, CellFltInfo)#鹏飞计算
  82. df_Diag_Batdiag_update=batDiag.diag()
  83. # df_Diag_Batdiag_update=BatDiag.diag()
  84. else:
  85. df_Diag_Batdiag_update_xq=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  86. df_Diag_Batdiag_update=DataFrame(columns=['start_time','end_time','product_id','code','level','info','advice','Batpos'])
  87. df_Diag_Ram,df_Diag_Ram_add,df_Diag_Ram_Update=DiagDataMerge.DetaMerge(df_Diag_Ram_sn,df_Diag_Batdiag_update_xq,df_Diag_Batdiag_update,df_OprtnSta,df_Diag_Ram_sn_else)
  88. df_Diag_Ram.to_csv('df_Diag_Ram.csv')
  89. df_Diag_Ram_add.to_csv('df_Diag_Ram_add.csv')
  90. df_Diag_Ram_Update.to_csv('df_Diag_Ram_add.csv')
  91. #.................................统计程序...............................
  92. def DaTa_Sta_Week_Task():
  93. all_period_fault_info=DataFrame(columns=['factory','week','level1_count','level2_count','level3_count','level4_count','level5_count','solve_rate'])
  94. #............................获取数据................................
  95. toweek='Week'+time.strftime('%W')
  96. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  97. port=3306
  98. db='safety_platform'
  99. user='qx_read'
  100. password='Qx@123456'
  101. mode=2
  102. tablename1='all_fault_info'
  103. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  104. with DBRead as DBRead:
  105. df_fltinfo=DBRead.getdata('product_id','level','code','start_time','end_time','batpos',tablename=tablename1,factory='骑享',sn='',timename='',st='',sp='')#dbdownload经过了改编
  106. #............................获取时间................................
  107. end_time=datetime.datetime.now()
  108. # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d')
  109. start_time=end_time-datetime.timedelta(days=7)
  110. start_time=start_time.strftime('%Y-%m-%d')
  111. end_time=end_time.strftime('%Y-%m-%d')
  112. FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(df_fltinfo,start_time,end_time)
  113. FaultLvlCount=DataSta.WeekInfoSta(df_fltinfo,start_time,end_time)
  114. for i in range(1,6):
  115. if not FaultLvlCount[FaultLvlCount['level']==i]['product_id'].empty:
  116. all_period_fault_info.loc[0,'level'+str(i)+'_count']=int(FaultLvlCount[FaultLvlCount['level']==i]['product_id'].values)
  117. else:
  118. all_period_fault_info.loc[0,'level'+str(i)+'_count']=int(0)
  119. all_period_fault_info.loc[0,'factory']='骑享'
  120. all_period_fault_info.loc[0,'week']=toweek
  121. all_period_fault_info.loc[0,'solve_rate']=FltAlarmInfo.loc[0,'OprationManageRate']
  122. all_period_fault_info.fillna(0,inplace=False)
  123. all_period_fault_info.to_csv('all_period_fault_info.csv')
  124. def DaTa_Sta_Minutes_Task():
  125. #............................获取数据................................
  126. host='172.16.121.236'
  127. port=3306
  128. db='fastfun'
  129. user='readonly'
  130. password='Fast1234'
  131. mode=3
  132. tablename1='ff_battery_accum'
  133. tablename2='ff_location'
  134. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  135. with DBRead as DBRead:
  136. df_last_accum=DBRead.getdata('devcode','dsg_phaccum','dsg_ahaccum',tablename=tablename1,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  137. df_last_pos=DBRead.getdata('devcode','latitude','longitude',tablename=tablename2,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  138. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  139. port=3306
  140. db='qx_cas'
  141. user='qx_read'
  142. password='Qx@123456'
  143. mode=3
  144. tablename2='bat_first_data_time'
  145. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  146. with DBRead as DBRead:
  147. df_FirstDataTime=DBRead.getdata('sn','first_data_time',tablename=tablename2,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  148. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  149. port=3306
  150. db='safety_platform'
  151. user='qx_read'
  152. password='Qx@123456'
  153. mode=2
  154. tablename1='all_fault_info'
  155. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  156. with DBRead as DBRead:
  157. df_fltinfo=DBRead.getdata('product_id','level','code','start_time','batpos',tablename=tablename1,factory='骑享',sn='',timename='',st='',sp='')#dbdownload经过了改编
  158. #............................获取时间................................
  159. end_time=datetime.datetime.now()
  160. # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d')
  161. start_time=end_time-datetime.timedelta(days=1)
  162. start_time=start_time.strftime('%Y-%m-%d')
  163. end_time=end_time.strftime('%Y-%m-%d')
  164. #............................执行程序................................
  165. FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(df_fltinfo,start_time,end_time)
  166. SatftyCount=DataSta.SftyWrngClsfy(df_fltinfo)
  167. MaxAccumAh,TotalAccumAh,MaxCycle,MaxRunningHour,TotalRunHour=DataSta.AccumInfo(df_last_accum,df_FirstDataTime,end_time)
  168. df_Diag_Ram_Pos=DataSta.FltBatPosition(df_last_pos,df_Diag_Ram)
  169. all_statistic_info=DataFrame(columns=['factory','total_alarm','alarm_total_today','alarm_not_close_today','alarm_close_today','alarm_uregent_total_today','alarm_uregent_close_today','alarm_uregent_not_close_today','alarm_close_total','run_time_total','dischrg_total','odo_total','max_dischrg_one','max_runtime_one','max_cycle_one','max_odo_one','alarm_close_total','alarm_total','cell_type','cell_type_count','cell_safety_risk_count','data_safety_risk_count','status_safety_risk_count','hv_safety_risk_count','system_safety_risk_count','sample_safety_risk_count','controller_safety_risk_count','design_safety_risk_count'])
  170. all_statistic_info.loc[0,'factory']='骑享'
  171. all_statistic_info.loc[0,'total_alarm']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total']
  172. all_statistic_info.loc[0,'alarm_total_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day']
  173. all_statistic_info.loc[0,'alarm_close_today']=FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count']
  174. all_statistic_info.loc[0,'alarm_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day']-FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count']
  175. all_statistic_info.loc[0,'alarm_uregent_total_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day']
  176. all_statistic_info.loc[0,'alarm_uregent_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count']
  177. all_statistic_info.loc[0,'alarm_uregent_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day']-FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count']
  178. all_statistic_info.loc[0,'run_time_total']=TotalRunHour
  179. all_statistic_info.loc[0,'dischrg_total']=TotalAccumAh
  180. all_statistic_info.loc[0,'odo_total']=0
  181. all_statistic_info.loc[0,'max_dischrg_one']=MaxAccumAh
  182. all_statistic_info.loc[0,'max_runtime_one']=MaxRunningHour
  183. all_statistic_info.loc[0,'max_cycle_one']=MaxCycle
  184. all_statistic_info.loc[0,'max_odo_one']=0
  185. all_statistic_info.loc[0,'alarm_close_total']=FltAlarmInfo.loc[0,'CS_Warning_Total_Finish_Count']
  186. all_statistic_info.loc[0,'alarm_total']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total']
  187. CellType=Celltype.columns.tolist()
  188. CellType=','.join(CellType)
  189. all_statistic_info.loc[0,'cell_type']=str(CellType)
  190. CellTypeCount=Celltype.loc[0].tolist()
  191. CellTypeCount=[str(x) for x in CellTypeCount]
  192. CellTypeCount=','.join(CellTypeCount)
  193. all_statistic_info.loc[0,'cell_type_count']=str(CellTypeCount)
  194. all_statistic_info.loc[0,'cell_safety_risk_count']=SatftyCount.loc[0,'CellSaftyCount']
  195. all_statistic_info.loc[0,'data_safety_risk_count']=SatftyCount.loc[0,'DataSaftyCodeCount']
  196. all_statistic_info.loc[0,'status_safety_risk_count']=SatftyCount.loc[0,'StateSaftyCodeCount']
  197. all_statistic_info.loc[0,'hv_safety_risk_count']=SatftyCount.loc[0,'HvSaftyCodeCount']
  198. all_statistic_info.loc[0,'system_safety_risk_count']=SatftyCount.loc[0,'SysSaftyCodeCount']
  199. all_statistic_info.loc[0,'sample_safety_risk_count']=SatftyCount.loc[0,'SamplingSatyCount']
  200. all_statistic_info.loc[0,'controller_safety_risk_count']=SatftyCount.loc[0,'CtrlSaftyCodeCount']
  201. all_statistic_info.loc[0,'design_safety_risk_count']=SatftyCount.loc[0,'DsnSaftyCodeCount']
  202. all_statistic_info.to_csv('all_statistic_info.csv')
  203. end=time.time()
  204. print(end-start)
  205. #...............................................主函数.......................................................................................................................
  206. if __name__ == "__main__":
  207. global SNnums
  208. excelpath=r'D:\Platform\platform_python\data_analyze_platform\USER\sxq\SaftyCenter_CODE_V1_1\sn-20210903.xlsx'
  209. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  210. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  211. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  212. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  213. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  214. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  215. SNnums_6060=SNdata_6060['SN号'].tolist()
  216. SNnums_6040=SNdata_6040['SN号'].tolist()
  217. SNnums_4840=SNdata_4840['SN号'].tolist()
  218. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  219. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  220. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  221. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
  222. # SNnums=['MGMCLN750N215I005','PK504B10100004341','PK504B00100004172','MGMLXN750N2189014']
  223. #SNnums = ['MGMLXN750N21B5004'] #SNnums_6040
  224. mylog=log.Mylog('log_diag.txt','error')
  225. mylog.logcfg()
  226. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
  227. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  228. port=3306
  229. db='safety_platform'
  230. user='qx_read'
  231. password='Qx@123456'
  232. mode=2
  233. tablename2='all_fault_info'
  234. DBRead = DBDw.DBDownload(host, port, db, user, password,mode)
  235. with DBRead as DBRead:
  236. df_Diag_Ram = DBRead.getdata('start_time','end_time','product_id','code','level','info','advice','Batpos',tablename=tablename2,factory='骑享',sn='',timename='',st='',sp='')
  237. # result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result.csv',encoding='gbk')
  238. diag_cal()
  239. DaTa_Sta_Minutes_Task()
  240. DaTa_Sta_Week_Task()
  241. #定时任务.......................................................................................................................................................................
  242. scheduler = BlockingScheduler()
  243. scheduler.add_job(diag_cal, 'interval', seconds=900, id='diag_job')
  244. scheduler.add_job(DaTa_Sta_Week_Task, 'interval',start_date='2021-11-15 08:00:00', days=7, id='Week_Task')
  245. scheduler.add_job(DaTa_Sta_Minutes_Task, 'interval', seconds=180, id='Minutes_Task')
  246. try:
  247. scheduler.start()
  248. except Exception as e:
  249. scheduler.shutdown()
  250. print(repr(e))
  251. mylog.logopt(e)