main.py 17 KB

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