123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import numpy as np
- import pandas as pd
- import datetime
- import pymysql
- from apscheduler.schedulers.blocking import BlockingScheduler
- from USER.SPF.alibaba.Common import log
- import CBMSBatCtrl
- #...................................SOC计算函数......................................................................................................................
- def ctrl_cal():
- global df_ram_rlyctrl
- global df_ram_statectrl
- global now_time
- global df_data1,df_data2,df_data3
- end_time=now_time+datetime.timedelta(seconds=10)
- start_time=now_time
- now_time=now_time+datetime.timedelta(seconds=10)
- host='120.25.223.1'
- port=4901
- user='root'
- password='123456'
- db='ali'
- mysql = pymysql.connect (host=host, user=user, password=password, port=port, database=db)
- #log信息配置............................................................................................................................................................................
- mylog=log.Mylog('log_ctrl.txt','error')
- mylog.logcfg()
- sn_list=['KPD001','KPD002','KPD003','LS001','LS002']
- #celltype=1康普盾 99力神
- try:
- #读取所有康普盾电池包原始数据库最后一条数据...........................................................................
-
- df_bms1= df_data1[(df_data1['time']>=start_time) & (df_data1['time']<end_time)]
- df_bms1=df_bms1.reset_index(drop=True)
- cellvolt_name=['CellVolt'+str(x) for x in range(1,121)]
- df_volt=pd.DataFrame([x[0].split(",") for x in np.array(df_bms1[['cell_volt']])]).iloc[:,list(range(120))]
- df_volt.columns = cellvolt_name
- df_volt=df_volt.astype('float')
- df_bms1=pd.concat([df_bms1,df_volt],axis=1)
- df_bms1=df_bms1.iloc[-1].to_frame()
- df_bms1=pd.DataFrame(df_bms1.values.T,columns=df_bms1.index)
- df_bms2= df_data2[(df_data2['time']>=start_time) & (df_data2['time']<end_time)]
- df_bms2=df_bms2.reset_index(drop=True)
- cellvolt_name=['CellVolt'+str(x) for x in range(1,121)]
- df_volt=pd.DataFrame([x[0].split(",") for x in np.array(df_bms2[['cell_volt']])]).iloc[:,list(range(120))]
- df_volt.columns = cellvolt_name
- df_volt=df_volt.astype('float')
- df_bms2=pd.concat([df_bms2,df_volt],axis=1)
- df_bms2=df_bms2.iloc[-1].to_frame()
- df_bms2=pd.DataFrame(df_bms2.values.T,columns=df_bms2.index)
- df_bms3= df_data3[(df_data3['time']>=start_time) & (df_data3['time']<end_time)]
- df_bms3=df_bms3.reset_index(drop=True)
- cellvolt_name=['CellVolt'+str(x) for x in range(1,121)]
- df_volt=pd.DataFrame([x[0].split(",") for x in np.array(df_bms3[['cell_volt']])]).iloc[:,list(range(120))]
- df_volt.columns = cellvolt_name
- df_volt=df_volt.astype('float')
- df_bms3=pd.concat([df_bms3,df_volt],axis=1)
- df_bms3=df_bms3.iloc[-1].to_frame()
- df_bms3=pd.DataFrame(df_bms3.values.T,columns=df_bms3.index)
- df_bms=pd.concat([df_bms1,df_bms2,df_bms3])
- df_bms.reset_index(drop=True,inplace=True)
- # df_bms.dropna(axis=0, inplace=True)
- #读取所有未结束故障......................................................
- df_diag=pd.DataFrame({'sn':['KPD001','KPD002','KPD003'],'fault_code':[100,100,100]})
- #下行控制...................................................................................
- host1='120.25.223.1'
- port1=4902
- BatCrtl=CBMSBatCtrl.BatCrtl(host1,port1,df_ram_statectrl,df_ram_rlyctrl,df_diag,df_bms,sn_list)
- df_ram_rlyctrl, df_ram_statectrl=BatCrtl.bat_crtl()
- if not df_ram_statectrl.empty:
- with open(r'D:\01WorkSpace\python\data_analyze_platform\USER\SPF\alibaba\08BatCrtl\下行控制.txt','a',encoding="utf-8") as file:
- for j in range(len(df_ram_statectrl)):
- file.write(str(tuple(df_ram_statectrl.iloc[j]))+'\n')
- # print('done!!!')
-
- except Exception as e:
- print(repr(e))
- mylog.logopt(e)
- pass
- #...............................................主函数.......................................................................................................................
- if __name__ == "__main__":
- #log信息配置
- mylog=log.Mylog('log_ctrl.txt','error')
- mylog.logcfg()
- #参数初始化.........................................................................................................................................................................
- df_ram_rlyctrl=pd.DataFrame(columns=['time','sn','batrlyctrl'])
- # df_ram_statectrl=pd.DataFrame(columns=['time','sn','batstatectrl','done_flg'])
- df_ram_statectrl=pd.DataFrame({'time':[datetime.datetime.strptime('2022-04-28 02:02:48','%Y-%m-%d %H:%M:%S'),datetime.datetime.strptime('2022-04-28 04:26:10','%Y-%m-%d %H:%M:%S')],'sn':['KPD001','KPD002'],'batstatectrl':[0,1],'done_flg':[1,0]})
- df_ram_balctrl=pd.DataFrame(columns=['time','sn','batbalctrl'])
- df_data1= pd.read_excel(r'D:\01WorkSpace\项目\Alibaba\data(2)\原始数据_2022-04-26_2022-04-28\康普盾_KPD001.xlsx')
- df_data2= pd.read_excel(r'D:\01WorkSpace\项目\Alibaba\data(2)\原始数据_2022-04-26_2022-04-28\康普盾_KPD002.xlsx')
- df_data3= pd.read_excel(r'D:\01WorkSpace\项目\Alibaba\data(2)\原始数据_2022-04-26_2022-04-28\康普盾_KPD003.xlsx')
- now_time=datetime.datetime.strptime('2022-4-28 5:49:01','%Y-%m-%d %H:%M:%S')
- scheduler = BlockingScheduler()
- scheduler.add_job(ctrl_cal, 'interval', seconds=0.5, id='crtl_job')
-
- try:
- scheduler.start()
- except Exception as e:
- scheduler.shutdown()
- print(repr(e))
- mylog.logopt(e)
|