from LIB.BACKEND import DBManager
import cal_mileage
from LIB.MIDDLE.CellStateEstimation.Common import log
import datetime
import pandas as pd

dbManager = DBManager.DBManager()

now_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')   #type: str
now_time=datetime.datetime.strptime(now_time,'%Y-%m-%d %H:%M:%S')     #type: datetime
start_time=now_time-datetime.timedelta(days=1)
end_time=str(now_time)
start_time=str(start_time)

dataSOH = pd.read_excel('sn-20210903.xlsx',sheet_name='sn-20210903')
fileNames = dataSOH['sn']
fileNames = list(fileNames)
l = len(fileNames)

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


for k in range(l):
    try:
        sn = fileNames[k]
        df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms','gps'])
        data_bms = df_data['bms']
        data_gps = df_data['gps']

        #...............每日累积里程............................................................................
        if len(data_bms['时间戳'])>0 and len(data_gps['时间戳'])>0:
            df_res = cal_mileage.calcul_mileage(sn,data_bms,data_gps)
            df_res.to_csv('Mileage_'+sn+'.csv')
        
    except Exception as e:
        print(repr(e))
        mylog.logopt(sn,e)
        pass
