#历史剩余充电时间统计 2021-10

from LIB.BACKEND.Tools import Tools
from LIB.BACKEND.DataPreProcess import DataPreProcess
import pandas as pd
import numpy as np
import os
import math
import datetime
from time import strftime,gmtime
from LIB.BACKEND import DBManager
dbManager = DBManager.DBManager()

#将时间戳由 "%Y-%m-%d %H:%M:%S" 切换为 sec
def timeconvert(df):  
    df.index=range(len(df))
    time=df.时间戳
    timeInSeries=[]
    time2=datetime.datetime.strptime(time[0],"%Y-%m-%d %H:%M:%S")
    for k in range(len(time)):
        time1=datetime.datetime.strptime(time[k],"%Y-%m-%d %H:%M:%S")    
        t=(time1-time2)
        timeInSeries.append(t.days*86400+t.seconds)
    df.loc[:,'相对时间']=pd.DataFrame(timeInSeries,columns=['相对时间'])
    return df

#构建新数据表统计剩余充电时间
def data_handle(df_in):
    #数据预处理
    df_in=timeconvert(df_in)  
    dfOut_temp=DataPreProcess.data_split_by_status(DataPreProcess, df_in, drive_interval_threshold=120, charge_interval_threshold=300,drive_stand_threshold=120, charge_stand_threshold=300)
    dfOut=dfOut_temp.copy()
    df_charge=dfOut[dfOut['data_status']=='charge']
    unique_status_idx=np.unique(df_charge.data_split_by_status.values)
    #计算剩余充电时间
    df_statisticsInfo=pd.DataFrame()
    for n in unique_status_idx:
        df_charge_buff=df_charge[df_charge.data_split_by_status==n]
        if(np.max(df_charge_buff['SOC[%]'].values[-1])==100):    #满充状态数据提取    
            times=df_charge_buff['相对时间'].values
            times1=times[:-1]
            times2=times[1:]
            delta=times2-times1
            if(np.max(delta)<50):
                times_inv=[times[-1]-t for t in times ]
                df_charge_buff.loc[:,'剩余充电时间[sec]']=times_inv
                times_hms=[]
                for k in list(range(len(times_inv))):
                    time_hms=strftime("%H:%M:%S", gmtime(df_charge_buff.loc[:,'剩余充电时间[sec]'].values[k]))
                    times_hms.append(time_hms)
                df_charge_buff.loc[:,'剩余充电时间[时分秒]']=times_hms
                df_dataset_temp=df_charge_buff.drop(['GSM信号','故障等级','故障代码','外电压','绝缘电阻','总输出状态','上锁状态','充电状态','加热状态','单体压差','单体均衡状态','相对时间','data_split_by_crnt','data_split_by_status','data_status'],axis=1)
                df_statisticsInfo=df_statisticsInfo.append(df_dataset_temp)
                #简化特征
                list_col=list(df_statisticsInfo)
                list1=[s for s in list_col if '单体电压' in s]
                list2=[s for s in list_col if '单体温度' in s]
                list3=[s for s in list_col if '其他温度' in s]
                list_v=['单体电压'+str(i) for i in range(1,len(list1)+1)] 
                list_T=['单体温度'+str(i) for i in range(1,len(list2)+1)] 
                A_cellVolt=df_statisticsInfo[list_v].values
                celVolt_max=np.max(A_cellVolt,axis=1)
                celVolt_min=np.min(A_cellVolt,axis=1)
                A_cellTemp=df_statisticsInfo[list_T].values
                celTemp_max=np.max(A_cellTemp,axis=1)
                celTemp_min=np.min(A_cellTemp,axis=1) 
                df_statisticsInfo.loc[:,'最高单体电压[V]']=celVolt_max
                df_statisticsInfo.loc[:,'最高单体温度[℃]']=celTemp_max
                df_statisticsInfo.loc[:,'最低单体温度[℃]']=celTemp_min
                df_statisticsInfo=df_statisticsInfo.drop(list_v,axis=1)
                df_statisticsInfo=df_statisticsInfo.drop(list_T,axis=1) 
                df_statisticsInfo=df_statisticsInfo.drop(['其他温度'+str(i) for i in range(1,len(list3)+1)],axis=1)
    return(df_statisticsInfo)

#Dataset
data_sn=pd.read_excel('sn-20210903.xlsx',sheet_name='sn-20210903')
fileNames=data_sn['sn']
df_dataset=pd.DataFrame()
for k in range(2): 
    df_data = dbManager.get_data(sn=fileNames[k], start_time='2021-10-01 00:00:00', end_time='2021-11-01 00:00:00', data_groups=['bms'])
    if len(df_data)==0:
        continue
    dataIn = df_data['bms']
    dataIn=data_handle(dataIn)
    dataIn['sn']=fileNames[k]
    df_dataset=df_dataset.append(dataIn)

df_dataset.to_csv('df_datatest.csv')

