main.py 16 KB

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