import datetime import pandas as pd import numpy as np import time from apscheduler.schedulers.blocking import BlockingScheduler from USER.SPF.alibaba.Common import log import CBMSBatSoc #...................................SOC计算函数...................................................................................................................... def soc_cal(): global df_ram global SNnums global df_bms global now_time end_time=now_time start_time=now_time-datetime.timedelta(seconds=11) now_time=now_time+datetime.timedelta(seconds=10) # start_time=start_time.strftime('%Y-%m-%d %H:%M:%S') # end_time=end_time.strftime('%Y-%m-%d %H:%M:%S') for sn in SNnums: # if 'PK500' in sn: # celltype=1 #6040三元电芯 # elif 'PK502' in sn: # celltype=2 #4840三元电芯 # elif 'K504B' in sn: # celltype=99 #60ah林磷酸铁锂电芯 # elif 'MGMLXN750' in sn: # celltype=3 #力信50ah三元电芯 # elif 'MGMCLN750' or 'UD' in sn: # celltype=4 #CATL 50ah三元电芯 # else: # print('SN:{},未找到对应电池类型!!!'.format(sn)) # continue # sys.exit() celltype=99 #读取原始数据库数据........................................................................................................................................................ df_bms1= df_bms[(df_bms['time']>=start_time) & (df_bms['time']50: # tablename='cellstateestimation_uniform_socvoltdiff' # with DBRead as DBRead: # df_socdiff=DBRead.getdata(param='time,sn,cellsoc_diff', tablename=tablename, sn=sn, timename='time', st=start_time, sp=end_time) # else: # df_socdiff=pd.DataFrame() df_socdiff=pd.DataFrame() # print(df_bms) # print(df_soh) #socram............................................................................................................................................................... if not df_bms1.empty: df_ram_sn=df_ram[df_ram['sn']==sn] if df_ram_sn.empty: with open(r'D:\01WorkSpace\python\data_analyze_platform\USER\SPF\alibaba\04BatSoc\\'+'soc_'+sn+'.txt','a') as file: file.write(str(tuple(df_ram_sn.columns))+'\n') BatSoc=CBMSBatSoc.BatSoc(sn,celltype,df_bms1,df_soh,df_ram_sn,df_socdiff) df_res, df_ram_sn=BatSoc.batsoc() if not df_ram_sn.empty: sn_index=df_ram.loc[df_ram['sn']==sn].index df_ram=df_ram.drop(index=sn_index) df_ram=df_ram.append(df_ram_sn) df_ram.reset_index(inplace=True,drop=True) #重置索引 #结果存入数据库........................................................... if not df_ram_sn.empty: with open(r'D:\01WorkSpace\python\data_analyze_platform\USER\SPF\alibaba\04BatSoc\\'+'soc_'+sn+'.txt','a') as file: file.write(str(tuple(df_ram_sn.iloc[-1][['time','sn','bms_soc', 'soc']]))+'\n') #...............................................主函数....................................................................................................................... if __name__ == "__main__": #所有SN号 SNnums= ['LS002'] #读取原始数据库数据........................................................................................................................................................ df_bms= pd.read_excel(r'D:\01WorkSpace\项目\Alibaba\data(1)\原始数据_2022-04-20_2022-05-20\力神_LS001.xlsx') now_time=datetime.datetime.strptime('2022-5-2 09:59:00','%Y-%m-%d %H:%M:%S') #log信息配置 mylog=log.Mylog('log_soc.txt','error') mylog.logcfg() #参数初始化......................................................................................................................................................................... column_name=['time', 'sn', 'bms_soc', 'soc','cellsoc','standingtime','rampackcrnt','ramcellvolt','kocellvoltmin','kocellvoltmax','ocvweight','as_accum','socstep'] df_ram=pd.DataFrame(columns=column_name) scheduler = BlockingScheduler() scheduler.add_job(soc_cal, 'interval', seconds=0.05, id='soc_job') try: scheduler.start() except Exception as e: scheduler.shutdown() print(repr(e)) mylog.logopt(e)