import sys
import numpy as np
import pandas as pd
import string
import os
from pandas import Series
from matplotlib import pyplot as plt
from pandas.core.frame import DataFrame
from pandas.core.indexes.base import Index
from pymysql import NULL
from LIB.BACKEND import DBManager
import datetime
import time
import string
import re



class SamplingSafty:
    def __init__(self):
        pass
    def main(sn,param,bms_info,df_Diag_Ram_in):
        df_Diag_Ram_Update_inside=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
        df_Diag_Ram_Update_inside=df_Diag_Ram_Update_inside.append(df_Diag_Ram_in)
        global QuitErrCount 
        VolStarkCount=[0 for i in range(param.CellVoltNums)]
        VolCount=0
        QuitErrCount=[0 for i in range(param.FaultCount)]
        df_Diag_Ram=DataFrame(columns=['start_time', 'end_time', 'product_id', 'code', 'level', 'info','advice'])
        st_tp=[]
        #--------------该电池的所有当前故障-------------

        VoltageNum=['单体电压'+str(i) for i in range(1,param.CellVoltNums+1)]#单体电压替换cellvolt_
        CellVoltage=bms_info[VoltageNum]/1000
        CellMaxVoltage=CellVoltage.max(axis=1)
        CellMinVoltage=CellVoltage.min(axis=1)
        InVMaxBatNo=CellVoltage[CellVoltage>=param.CellOVlmt].dropna(axis=0,how='all',inplace=False)
        InVMinBatNo=CellVoltage[CellVoltage<=param.CellUVlmt].dropna(axis=0,how='all',inplace=False)
        #date替换为'时间戳'
        if len(InVMaxBatNo):
            indexValue=InVMaxBatNo.index.values
            df_Diag_Ram.loc[len(df_Diag_Ram)]=[bms_info.loc[indexValue[0],'时间戳'],'0000-00-00 00:00:00',sn,50,3,'单体电压大于{:.2f}V,采样无效'.format(param.CellOVlmt),'建议返厂维修']
            for i in range(1,len(indexValue)):
                if indexValue[i]- indexValue[i-1]>10:
                    df_Diag_Ram.loc[len(df_Diag_Ram)-1,'end_time']=bms_info.loc[indexValue[i-1],'时间戳']
                    df_Diag_Ram.loc[len(df_Diag_Ram)]=[bms_info.loc[indexValue[i],'时间戳'],'0000-00-00 00:00:00',sn,50,3,'单体电压大于{:.2f}V,采样无效'.format(param.CellOVlmt),'建议返厂维修']                         
        if len(InVMinBatNo):
            indexValue=InVMinBatNo.index.values
            df_Diag_Ram.loc[len(df_Diag_Ram)]=[bms_info.loc[indexValue[0],'时间戳'],'0000-00-00 00:00:00',sn,50,3,'电压小于{:.2f}V,采样无效'.format(param.CellUVlmt),'建议返厂维修']
            for i in range(1,len(indexValue)):
                if indexValue[i]- indexValue[i-1]>10:
                    df_Diag_Ram.loc[len(df_Diag_Ram)-1,'end_time']=bms_info.loc[indexValue[i-1],'时间戳']
                    df_Diag_Ram.loc[len(df_Diag_Ram)]=[bms_info.loc[indexValue[i],'时间戳'],'0000-00-00 00:00:00',sn,50,3,'电压小于{:.2f}V,采样无效'.format(param.CellUVlmt),'建议返厂维修']           
        MedianVol=CellVoltage.median(axis=1).tolist()
        MediaVolGap=abs(CellVoltage.sub(MedianVol,axis=0))
        OutlineVol=MediaVolGap[MediaVolGap>=param.AvgVolGap][CellMaxVoltage<param.CellOVlmt][CellMinVoltage>param.CellUVlmt][abs(bms_info['总电流[A]'])<1].dropna(axis=0,how='all',inplace=False)
        #OutlineVol=OutlineVol[CellMaxVoltage<param.CellOVlmt][CellMinVoltage>param.CellUVlmt].dropna(axis=0,how='all',inplace=False)
        
        
        if len(OutlineVol):
            indexValue=OutlineVol.index.values
            df_Diag_Ram.loc[len(df_Diag_Ram)]=[bms_info.loc[indexValue[0],'时间戳'],'0000-00-00 00:00:00',sn,50,3,sn,10,3,'电池电压发生偏移或发生断线','建议返厂维修']
            for i in range(1,len(indexValue)):
                if indexValue[i]- indexValue[i-1]>10:
                    df_Diag_Ram.loc[len(df_Diag_Ram)-1,'end_time']=bms_info.loc[indexValue[i-1],'时间戳']
                    df_Diag_Ram.loc[len(df_Diag_Ram)]=[bms_info.loc[indexValue[i],'时间戳'],'0000-00-00 00:00:00',sn,10,3,'电池电压发生偏移或发生断线','建议返厂维修'] 
        CellTemp=bms_info['单体温度1']
        InVMaxTempBatNo=CellTemp[CellTemp>=param.PackOTlmt].dropna(axis=0,how='all',inplace=False)
        InVMinTempBatNo=CellTemp[CellTemp<=param.PackUTlmt].dropna(axis=0,how='all',inplace=False)
        if len(InVMaxTempBatNo):
            indexValue=InVMaxTempBatNo.index.values
            df_Diag_Ram.loc[len(df_Diag_Ram)]=[bms_info.loc[indexValue[0],'时间戳'],'0000-00-00 00:00:00',sn,50,3,'电池温度大于{:.0f}摄氏度,采样无效'.format(param.PackOTlmt),'请立即确认状态']
            for i in range(1,len(indexValue)):
                if indexValue[i]- indexValue[i-1]>10:
                    df_Diag_Ram.loc[len(df_Diag_Ram)-1,'end_time']=bms_info.loc[indexValue[i-1],'时间戳']
                    df_Diag_Ram.loc[len(df_Diag_Ram)]=[bms_info.loc[indexValue[i],'时间戳'],'0000-00-00 00:00:00',sn,50,3,'电池温度大于{:.0f}摄氏度,采样无效'.format(param.PackOTlmt),'请立即确认状态'] 
        if len(InVMinTempBatNo):
            indexValue=InVMinTempBatNo.index.values
            df_Diag_Ram.loc[len(df_Diag_Ram)]=[bms_info.loc[indexValue[0],'时间戳'],'0000-00-00 00:00:00',sn,50,3,'电池温度小于{:.0f}摄氏度,采样无效'.format(param.PackUTlmt),'建议返厂维修']
            for i in range(1,len(indexValue)):
                if indexValue[i]- indexValue[i-1]>10:
                    df_Diag_Ram.loc[len(df_Diag_Ram)-1,'end_time']=bms_info.loc[indexValue[i-1],'时间戳']
                    df_Diag_Ram.loc[len(df_Diag_Ram)]=[bms_info.loc[indexValue[i],'时间戳'],'0000-00-00 00:00:00',sn,50,3,'电池温度小于{:.0f}摄氏度,采样无效'.format(param.PackUTlmt),'建议返厂维修'] 
        return df_Diag_Ram