from LIB.MIDDLE.CellStateEstimation.Common import log
from LIB.BACKEND import DBManager
dbManager = DBManager.DBManager()
import pandas as pd
import anomalySparsePCA

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

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

data_res=pd.DataFrame()
for k in range(3):
    try:
        sn = fileNames[k]
        df_data = dbManager.get_data(sn=sn, start_time='2021-01-01 00:00:00', end_time='2021-11-01 00:00:00', data_groups=['bms'])
        data_bms = df_data['bms']

        #...............训练模型............................................................................
        if len(data_bms['时间戳'])>0:
            data_stat=anomalySparsePCA.anomaly(data_bms)
            data_stat.to_csv('result_'+sn+'.csv',encoding='gbk')
            data_stat['sn']=sn
            data_res=data_res.append(data_stat)
              
    except Exception as e:
        print(repr(e))
        mylog.logopt(sn,e)
        pass 