import CBMSBatUniform
import log

#coding=utf-8
import os
import sys
import datetime
import pandas as pd
from LIB.BACKEND import DBManager, Log
# from LIB.MIDDLE import SignalMonitor
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import time, datetime
from LIB.MIDDLE.soh import NCMSoh_20210716 as NCMSoh
from LIB.MIDDLE.soh import LFPSoh_20210711 as LFPSoh
from urllib import parse

dbManager = DBManager.DBManager()
if __name__ == "__main__":
    SNdata_6040 = pd.read_excel('骑享资产梳理-20210621.xlsx', sheet_name='6040骑享')
    SNdata_6060 = pd.read_excel('骑享资产梳理-20210621.xlsx', sheet_name='6060')
    SNdata_4840 = pd.read_excel('骑享资产梳理-20210621.xlsx', sheet_name='4840骑享')
    SNdata_7250 = pd.read_excel('骑享资产梳理-20210621.xlsx', sheet_name='7250')
    SNnums_6060=SNdata_6060['SN号']
    SNnums_6040=SNdata_6040['SN号']
    SNnums_4840=SNdata_4840['SN号']
    SNnums_7250=SNdata_7250['SN号']

    SNnums=SNnums_6040.tolist()+SNnums_6060.tolist()+SNnums_4840.tolist()+SNnums_7250.tolist()
    now_time=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    now_time=datetime.datetime.strptime(now_time,'%Y-%m-%d %H:%M:%S')
    start_time=now_time-datetime.timedelta(days=5)
    end_time=str(now_time)
    start_time=str(start_time)

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

    for sn in SNnums:
        try:
            if 'PK500' in sn:
                celltype=1 #6040三元电芯
            elif 'PK502' in sn:
                celltype=2 #4840三元电芯
            elif 'PK504' in sn:
                celltype=99    #60ah林磷酸铁锂电芯
            elif 'MGMLXN750' in sn:
                celltype=3 #力信50ah三元电芯
            elif 'MGMCLN750' in sn: 
                celltype=4 #CATL 50ah三元电芯
            else:
                print('未找到对应电池编号！！！')
                sys.exit()
            
            # sn='PK50001A100000035'
            # start_time='2021-08-10 9:49:37'
            # end_time='2021-08-29 19:49:37'

            dbManager = DBManager.DBManager()
            df_data = dbManager.get_data(sn=sn, start_time=start_time, end_time=end_time, data_groups=['bms'])
            df_bms = df_data['bms']
            # df_bms.to_csv('BMS_'+sn+'.csv',encoding='GB18030')

            BatUniform=CBMSBatUniform.BatUniform(sn,celltype,df_bms)
            df_res=BatUniform.batuniform()
            df_res.to_csv('CBMS_Uniform_'+sn+'.csv',encoding='GB18030')
        
        
        except IndexError as e:
            print(repr(e))
            mylog.logopt(sn,e)
            pass
