
from LIB.BACKEND import DBManager
from LIB.MIDDLE.CellStateEstimation.Common import log
import datetime
import pandas as pd
import numpy as np
import os   
from DataPreProcessMGMC import DataPreProcess

#log信息配置
mylog=log.Mylog('log.txt','error')
mylog.logcfg()

dbManager = DBManager.DBManager()
dataSOH = pd.read_excel('sn-20210903.xlsx',sheet_name='MGMC')
fileNames = dataSOH['sn']
fileNames = list(fileNames)

data_GMGC=pd.DataFrame()
for k in range(len(fileNames)):
    try:    
        sn = fileNames[k]
        df_data = dbManager.get_data(sn=sn, start_time='2021-10-28 00:00:00', end_time='2021-11-04 00:00:00', data_groups=['bms','gps'])
        data_bms = df_data['bms']
        data_gps = df_data['gps']
        df_merge=pd.merge(data_bms, data_gps,how='outer',sort=True)
        df_merge['sn']=sn
        df_sheetCat=DataPreProcess.data_split_by_status(DataPreProcess,df_merge, drive_interval_threshold=120, charge_interval_threshold=300, drive_stand_threshold=120, charge_stand_threshold=300)
        data_charge=df_sheetCat[df_sheetCat['data_status']=='charge']
        unique_status_idx=np.unique(data_charge.data_split_by_status.values)
        data_I3=pd.DataFrame()
        for n in unique_status_idx:
            df_charge_buff=data_charge[data_charge.data_split_by_status==n]
            list_I=df_charge_buff['总电流[A]'].values.tolist()[1:8]
            if((3 in list_I) and (df_charge_buff['总电流[A]'].value_counts()[3]>5)): 
                data_I3=data_I3.append(df_charge_buff)
        data_GMGC=data_GMGC.append(data_I3)
        
    except Exception as e:
        print(repr(e))
        mylog.logopt(sn,e)
        pass  


