main.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. toweek='Week'+time.strftime('%W')
  115. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  116. port=3306
  117. db='safety_platform'
  118. user='qx_algo_readonly'
  119. password='qx@123456'
  120. mode=2
  121. tablename1='all_fault_info'
  122. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  123. with DBRead as DBRead:
  124. df_fltinfo=DBRead.getdata('product_id','level','code','start_time','end_time','batpos','factory',tablename=tablename1,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  125. #............................获取数据................................
  126. for j in range(0,len(factory_info)):
  127. df_fltinfo=df_fltinfo[df_fltinfo['factory']==factory_info[j]]
  128. #............................获取时间................................
  129. end_time=datetime.datetime.now()
  130. # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d')
  131. start_time_week=end_time-datetime.timedelta(days=7)
  132. start_time_week=start_time_week.strftime('%Y-%m-%d')
  133. end_time=end_time.strftime('%Y-%m-%d')
  134. FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(df_fltinfo,start_time_week,end_time,factory_info[j])
  135. FaultLvlCount=DataSta.WeekInfoSta(df_fltinfo,start_time_week,end_time)
  136. for i in range(1,6):
  137. if not FaultLvlCount[FaultLvlCount['level']==i]['product_id'].empty:
  138. all_period_fault_info.loc[j,'level'+str(i)+'_count']=int(FaultLvlCount[FaultLvlCount['level']==i]['product_id'].values)
  139. else:
  140. all_period_fault_info.loc[j,'level'+str(i)+'_count']=int(0)
  141. all_period_fault_info.loc[j,'factory']=factory_info[j]
  142. all_period_fault_info.loc[j,'week']=toweek
  143. all_period_fault_info.loc[j,'solve_rate']=FltAlarmInfo.loc[0,'OprationManageRate']
  144. all_period_fault_info.fillna(0,inplace=False)
  145. task_on=0
  146. def DaTa_Sta_Minutes_Task():
  147. task_on=1
  148. factory_info=['骑享','金茂换电']
  149. #............................获取数据................................
  150. host='172.16.121.236'
  151. port=3306
  152. db='fastfun'
  153. user='readonly'
  154. password='Fast1234'
  155. mode=3
  156. tablename1='ff_battery_accum'
  157. tablename2='ff_location'
  158. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  159. with DBRead as DBRead:
  160. df_last_accum=DBRead.getdata('devcode','dsg_phaccum','dsg_ahaccum',tablename=tablename1,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  161. df_last_pos=DBRead.getdata('devcode','latitude','longitude',tablename=tablename2,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  162. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  163. port=3306
  164. db='qx_cas'
  165. user='qx_algo_readonly'
  166. password='qx@123456'
  167. mode=3
  168. tablename2='bat_first_data_time'
  169. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  170. with DBRead as DBRead:
  171. df_FirstDataTime=DBRead.getdata('sn','first_data_time',tablename=tablename2,factory='',sn='',timename='',st='',sp='')#dbdownload经过了改编
  172. #............................获取时间................................
  173. end_time=datetime.datetime.now()
  174. # end_time=datetime.datetime.strptime(end_time,'%Y-%m-%d')
  175. start_time=end_time-datetime.timedelta(days=1)
  176. start_time=start_time.strftime('%Y-%m-%d')
  177. end_time=end_time.strftime('%Y-%m-%d')
  178. #............................执行程序................................
  179. 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'])
  180. for j in range(0,len(factory_info)):
  181. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  182. port=3306
  183. db='safety_platform'
  184. user='qx_algo_readonly'
  185. password='qx@123456'
  186. mode=2
  187. tablename1='all_fault_info'
  188. DBRead=DBDw.DBDownload(host, port, db, user, password,mode)
  189. with DBRead as DBRead:
  190. df_fltinfo=DBRead.getdata('product_id','level','code','start_time','batpos',tablename=tablename1,factory=factory_info[j],sn='',timename='',st='',sp='')#dbdownload经过了改编
  191. FltAlarmInfo,Celltype=DataSta.SaftyWarningSta(df_fltinfo,start_time,end_time,factory_info[j])
  192. SatftyCount=DataSta.SftyWrngClsfy(df_fltinfo)
  193. MaxAccumAh,TotalAccumAh,MaxCycle,MaxRunningHour,TotalRunHour=DataSta.AccumInfo(df_last_accum,df_FirstDataTime,end_time,factory_info[j])
  194. all_location_info=DataSta.FltBatPosition(df_last_pos,df_fltinfo,factory_info[j])
  195. all_statistic_info.loc[j,'factory']=factory_info[j]
  196. all_statistic_info.loc[j,'total_alarm']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total']
  197. all_statistic_info.loc[j,'alarm_total_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day']
  198. all_statistic_info.loc[j,'alarm_close_today']=FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count']
  199. all_statistic_info.loc[j,'alarm_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_Data_day']-FltAlarmInfo.loc[0,'CS_Warning_day_Finish_Count']
  200. all_statistic_info.loc[j,'alarm_uregent_total_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day']
  201. all_statistic_info.loc[j,'alarm_uregent_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count']
  202. all_statistic_info.loc[j,'alarm_uregent_not_close_today']=FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day']-FltAlarmInfo.loc[0,'SftyPlt_EmgcyData_day_Finish_Count']
  203. all_statistic_info.loc[j,'run_time_total']=TotalRunHour
  204. all_statistic_info.loc[j,'dischrg_total']=TotalAccumAh
  205. all_statistic_info.loc[j,'odo_total']=0
  206. all_statistic_info.loc[j,'max_dischrg_one']=MaxAccumAh
  207. all_statistic_info.loc[j,'max_runtime_one']=MaxRunningHour
  208. all_statistic_info.loc[j,'max_cycle_one']=MaxCycle
  209. all_statistic_info.loc[j,'max_odo_one']=0
  210. all_statistic_info.loc[j,'alarm_close_total']=FltAlarmInfo.loc[0,'CS_Warning_Total_Finish_Count']
  211. all_statistic_info.loc[j,'alarm_total']=FltAlarmInfo.loc[0,'SftyPlt_Data_Total']
  212. CellType=Celltype.columns.tolist()
  213. CellType=','.join(CellType)
  214. all_statistic_info.loc[0,'cell_type']=str(CellType)
  215. CellTypeCount=Celltype.loc[0].tolist()
  216. CellTypeCount=[str(x) for x in CellTypeCount]
  217. CellTypeCount=','.join(CellTypeCount)
  218. all_statistic_info.loc[j,'cell_type_count']=str(CellTypeCount)
  219. all_statistic_info.loc[j,'cell_safety_risk_count']=SatftyCount.loc[0,'CellSaftyCount']
  220. all_statistic_info.loc[j,'data_safety_risk_count']=SatftyCount.loc[0,'DataSaftyCodeCount']
  221. all_statistic_info.loc[j,'status_safety_risk_count']=SatftyCount.loc[0,'StateSaftyCodeCount']
  222. all_statistic_info.loc[j,'hv_safety_risk_count']=SatftyCount.loc[0,'HvSaftyCodeCount']
  223. all_statistic_info.loc[j,'system_safety_risk_count']=SatftyCount.loc[0,'SysSaftyCodeCount']
  224. all_statistic_info.loc[j,'sample_safety_risk_count']=SatftyCount.loc[0,'SamplingSatyCount']
  225. all_statistic_info.loc[j,'controller_safety_risk_count']=SatftyCount.loc[0,'CtrlSaftyCodeCount']
  226. all_statistic_info.loc[j,'design_safety_risk_count']=SatftyCount.loc[0,'DsnSaftyCodeCount']
  227. end=time.time()
  228. #print(end-start)
  229. task_on=0
  230. print(all_statistic_info)
  231. all_statistic_info.to_excel('all_statistic_info.xlsx')
  232. #...............................................主函数.......................................................................................................................
  233. if __name__ == "__main__":
  234. global SNnums
  235. global task_on
  236. task_on=0
  237. excelpath=r'D:\ZLWORK\code\data_analyze_platform\USER\eric\SaftyCenter_CODE_V1\sn-20210903.xlsx'
  238. SNdata_6060 = pd.read_excel(excelpath, sheet_name='科易6060')
  239. SNdata_6040 = pd.read_excel(excelpath, sheet_name='科易6040')
  240. SNdata_4840 = pd.read_excel(excelpath, sheet_name='科易4840')
  241. SNdata_L7255 = pd.read_excel(excelpath, sheet_name='格林美-力信7255')
  242. SNdata_C7255 = pd.read_excel(excelpath, sheet_name='格林美-CATL7255')
  243. SNdata_U7255 = pd.read_excel(excelpath, sheet_name='优旦7255')
  244. SNnums_6060=SNdata_6060['SN号'].tolist()
  245. SNnums_6040=SNdata_6040['SN号'].tolist()
  246. SNnums_4840=SNdata_4840['SN号'].tolist()
  247. SNnums_L7255=SNdata_L7255['SN号'].tolist()
  248. SNnums_C7255=SNdata_C7255['SN号'].tolist()
  249. SNnums_U7255=SNdata_U7255['SN号'].tolist()
  250. SNnums=SNnums_L7255 + SNnums_C7255 + SNnums_6040 + SNnums_4840 + SNnums_U7255+ SNnums_6060
  251. SNnums=['TJMCL120502305038','TJMCL120502305032','TJMCL120502305022','TJMCL120502305026','TJMCL120502305032','TJMCL120502305044','TJMCL120502305048','TJMCL120502305012','TJMCL120502305010']
  252. #SNnums = ['MGMLXN750N218N004'] #SNnums_6040
  253. mylog=log.Mylog('log.txt','error')
  254. mylog.logcfg()
  255. #............................模块运行前,先读取数据库中所有结束时间为0的数据,需要从数据库中读取................
  256. host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
  257. port=3306
  258. db='safety_platform'
  259. user='qx_algo_readonly'
  260. password='qx@123456'
  261. mode=2
  262. tablename2='all_fault_info'
  263. DBRead = DBDw.DBDownload(host, port, db, user, password,mode)
  264. with DBRead as DBRead:
  265. df_Diag_Ram = DBRead.getdata('start_time','end_time','product_id','code','level','info','advice','Batpos','factory',tablename=tablename2,factory='',sn='',timename='',st='',sp='')
  266. # result=pd.read_csv(r'D:\Work\Code_write\data_analyze_platform\USER\01Screen_Problem\result.csv',encoding='gbk')
  267. #定时任务.......................................................................................................................................................................
  268. scheduler = BlockingScheduler()
  269. diag_cal()
  270. DaTa_Sta_Minutes_Task()
  271. DaTa_Sta_Week_Task()
  272. if task_on==0:
  273. scheduler.add_job(DaTa_Sta_Week_Task, 'interval', days=7, id='Week_Task')
  274. scheduler.add_job(diag_cal, 'interval', seconds=900, id='diag_job')
  275. scheduler.add_job(DaTa_Sta_Minutes_Task, 'interval', seconds=180, id='Hour_Task')
  276. try:
  277. scheduler.start()
  278. except Exception as e:
  279. scheduler.shutdown()
  280. print(repr(e))
  281. mylog.logopt(e)