# -*- coding: cp936 -*-

from ctypes import *


## TMsgSaveEngine ver1
class ver1TMsgSaveEngine(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [("mainChNo", c_ubyte), ("chType", c_ubyte), ("channelNoInfoRow", c_ubyte), ("channelNoInfoCol", c_ubyte), ("channelNoInfoIndex", c_ubyte),
                ("engineState", c_ubyte), ("testId", c_uint),
                ("stepNum", c_ushort), ("stepNo", c_ushort), ("isAlarmClosed", c_ubyte),
                ("dfDataIndex", c_uint), ("platformPointVolRec", c_ubyte * 3),
                ("logIndex", c_uint), ("startDFFlag", c_ubyte), ("sampleStatus", c_ubyte),
                ("detalVolFlag", c_ubyte), ("detalVolZeroPoint", c_int), ("totalTime", c_ulonglong),
                ("phaseInCCCVC", c_ubyte), ("testTime", c_ulonglong), ("lastSampleVol", c_int),
                ("sampleCount", c_uint), ("sampleOmitted", c_ubyte), ("sampleTimeChange", c_ulonglong),
                ("volMax", c_int), ("timeChange", c_ulonglong), ("savedVoltage", c_int),
                ("savedCurrent", c_int), ("chgCap", c_longlong), ("dchgCap", c_longlong),
                ("chgEnergy", c_longlong), ("dchgEnergy", c_longlong), ("waitDCRStepFlag", c_ubyte),
                ("mchDCRFlag", c_ubyte), ("achDCRFlag", c_ubyte), ("dcrStartStepFlag", c_ubyte),
                ("dcrEndStepFlag", c_ubyte), ("dcrCutOffFlag", c_ubyte), ("mchDCRk", c_uint),
                ("mchChDFDCR", c_int * 5), ("cycleNum", c_uint), ("cycleStackSize", c_ubyte),
                ("auxChNum", c_ubyte), ("uniteData", c_ubyte * 10), ("latestParaRunTime", c_uint),
                ("latestParaRunNo", c_uint), ("lastErrCode", c_ubyte), ("CCCStepCnt", c_ubyte),
                ("stepSwitchNum", c_uint)]

###ver2 与 ver1 相比，在("CCCStepCnt", c_ubyte)和("stepSwitchNum", c_uint)之间多了
###("ctrlStepStatus", c_ubyte), ("ctrlStepSetVal", c_ubyte), 同时后面多了一些内容（见ver2）。
    
## TMsgSaveEngine ver2
class ver2TMsgSaveEngine(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [("mainChNo", c_ubyte), ("chType", c_ubyte), ("channelNoInfoRow", c_ubyte), ("channelNoInfoCol", c_ubyte), ("channelNoInfoIndex", c_ubyte),
                ("engineState", c_ubyte), ("testId", c_uint),
                ("stepNum", c_ushort), ("stepNo", c_ushort), ("isAlarmClosed", c_ubyte),
                ("dfDataIndex", c_uint), ("platformPointVolRec", c_ubyte * 3),
                ("logIndex", c_uint), ("startDFFlag", c_ubyte), ("sampleStatus", c_ubyte),
                ("detalVolFlag", c_ubyte), ("detalVolZeroPoint", c_int), ("totalTime", c_ulonglong),
                ("phaseInCCCVC", c_ubyte), ("testTime", c_ulonglong), ("lastSampleVol", c_int),
                ("sampleCount", c_uint), ("sampleOmitted", c_ubyte), ("sampleTimeChange", c_ulonglong),
                ("volMax", c_int), ("timeChange", c_ulonglong), ("savedVoltage", c_int),
                ("savedCurrent", c_int), ("chgCap", c_longlong), ("dchgCap", c_longlong),
                ("chgEnergy", c_longlong), ("dchgEnergy", c_longlong), ("waitDCRStepFlag", c_ubyte),
                ("mchDCRFlag", c_ubyte), ("achDCRFlag", c_ubyte), ("dcrStartStepFlag", c_ubyte),
                ("dcrEndStepFlag", c_ubyte), ("dcrCutOffFlag", c_ubyte), ("mchDCRk", c_uint),
                ("mchChDFDCR", c_int * 5), ("cycleNum", c_uint), ("cycleStackSize", c_ubyte),
                ("auxChNum", c_ubyte), ("uniteData", c_ubyte * 10), ("latestParaRunTime", c_uint),
                ("latestParaRunNo", c_uint), ("lastErrCode", c_ubyte), ("CCCStepCnt", c_ubyte),
                ("ctrlStepStatus", c_ubyte), ("ctrlStepSetVal", c_ubyte), ("stepSwitchNum", c_uint),
                ("volMin", c_int), ("currentMax", c_int), ("currentMin", c_int),
                ("globalVariable", c_float * 100)]


## TMsgSaveEngine 之后是循环变量栈　动态变化的,与TMsgSaveEngine中的cycleStackSize相关
## len = cycleStackSize * (c_ushort + c_uint + c_ushort)


##TAchInfoOfEngineState 动态，与TMsgSaveEngine中的 auxChNum 相关　
## len = sizeof(ver2TAchInfoOfEngineState)
class ver123TAchInfoOfEngineState(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [("chNo", c_ubyte), ("chType", c_ubyte), ("chTypeDetail", c_ubyte),
                ("chMap", c_ubyte * 3), ("varAuxIndex", c_uint), ("varAChTimeChange", c_uint),
                ("varSavedAChVoltage", c_int), ("varSavedAChTemperature", c_short),
                ("varTIIIndex", c_uint), ("tiiSample", c_ushort * 4)]
                
##ATL参数相关 长度为 sizeof(TATLProtectVar) = 6664 Byte

## mESWriteSer 4Byte 和　CRC32校验值

## ver2 与 ver3 的区别在于，ver3中没有了ver2中的ATL相关的参数，除了　atlVolJumpValueLast 4Byte　atlLatestCCCCap 8Byte　两个之外．







## TMsgSaveEngine ver3  与ver2相比多了 ah wh
class ver3TMsgSaveEngine(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [("mainChNo", c_ubyte), ("chType", c_ubyte), ("channelNoInfoRow", c_ubyte), ("channelNoInfoCol", c_ubyte), ("channelNoInfoIndex", c_ubyte),
                ("engineState", c_ubyte), ("testId", c_uint),
                ("stepNum", c_ushort), ("stepNo", c_ushort), ("isAlarmClosed", c_ubyte),
                ("dfDataIndex", c_uint), ("platformPointVolRec", c_ubyte * 3),
                ("logIndex", c_uint), ("startDFFlag", c_ubyte), ("sampleStatus", c_ubyte),
                ("detalVolFlag", c_ubyte), ("detalVolZeroPoint", c_int), ("totalTime", c_ulonglong),
                ("phaseInCCCVC", c_ubyte), ("testTime", c_ulonglong), ("lastSampleVol", c_int),
                ("sampleCount", c_uint), ("sampleOmitted", c_ubyte), ("sampleTimeChange", c_ulonglong),
                ("volMax", c_int), ("timeChange", c_ulonglong), ("savedVoltage", c_int),
                ("savedCurrent", c_int), ("chgCap", c_longlong), ("dchgCap", c_longlong),
                ("chgEnergy", c_longlong), ("dchgEnergy", c_longlong), ("waitDCRStepFlag", c_ubyte),
                ("mchDCRFlag", c_ubyte), ("achDCRFlag", c_ubyte), ("dcrStartStepFlag", c_ubyte),
                ("dcrEndStepFlag", c_ubyte), ("dcrCutOffFlag", c_ubyte), ("mchDCRk", c_uint),
                ("mchChDFDCR", c_int * 5), ("cycleNum", c_uint), ("cycleStackSize", c_ubyte),
                ("auxChNum", c_ubyte), ("uniteData", c_ubyte * 10), ("latestParaRunTime", c_uint),
                ("latestParaRunNo", c_uint), ("lastErrCode", c_ubyte), ("CCCStepCnt", c_ubyte),
                ("ctrlStepStatus", c_ubyte), ("ctrlStepSetVal", c_ubyte), ("stepSwitchNum", c_uint),
                ("volMin", c_int), ("currentMax", c_int), ("currentMin", c_int), ("ah", c_longlong),
                ("wh", c_longlong), ("globalVariable", c_float * 100)]



## TMsgSaveEngine ver4  与ver3相比无变化,只是保存时的个别数据的单位发生了变化
class ver4TMsgSaveEngine(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [("dataVer", c_uint),
                ("mainChNo", c_ubyte), ("chType", c_ubyte), ("channelNoInfoRow", c_ubyte), ("channelNoInfoCol", c_ubyte), ("channelNoInfoIndex", c_ubyte),
                ("engineState", c_ubyte), ("testId", c_uint),
                ("stepNum", c_ushort), ("stepNo", c_ushort), ("isAlarmClosed", c_ubyte),
                ("dfDataIndex", c_uint), ("platformPointVolRec", c_ubyte * 3),
                ("logIndex", c_uint), ("startDFFlag", c_ubyte), ("sampleStatus", c_ubyte),
                ("detalVolFlag", c_ubyte), ("detalVolZeroPoint", c_int), ("totalTime", c_ulonglong),
                ("phaseInCCCVC", c_ubyte), ("testTime", c_ulonglong), ("lastSampleVol", c_int),
                ("sampleCount", c_uint), ("sampleOmitted", c_ubyte), ("sampleTimeChange", c_ulonglong),
                ("volMax", c_int), ("timeChange", c_ulonglong), ("savedVoltage", c_int),
                ("savedCurrent", c_int), ("chgCap", c_longlong), ("dchgCap", c_longlong),
                ("chgEnergy", c_longlong), ("dchgEnergy", c_longlong), ("waitDCRStepFlag", c_ubyte),
                ("mchDCRFlag", c_ubyte), ("achDCRFlag", c_ubyte), ("dcrStartStepFlag", c_ubyte),
                ("dcrEndStepFlag", c_ubyte), ("dcrCutOffFlag", c_ubyte), ("mchDCRk", c_uint),
                ("mchChDFDCR", c_int * 5), ("cycleNum", c_uint), ("cycleStackSize", c_ubyte),
                ("auxChNum", c_ubyte), ("uniteData", c_ubyte * 10), ("latestParaRunTime", c_uint),
                ("latestParaRunNo", c_uint), ("lastErrCode", c_ubyte), ("CCCStepCnt", c_ubyte),
                ("ctrlStepStatus", c_ubyte), ("ctrlStepSetVal", c_ubyte), ("stepSwitchNum", c_uint),
                ("volMin", c_int), ("currentMax", c_int), ("currentMin", c_int), ("ah", c_longlong),
                ("wh", c_longlong), ("globalVariable", c_float * 100)]


##ver3到ver4变化的内容为:("chgCap", c_longlong), ("dchgCap", c_longlong),
##                ("chgEnergy", c_longlong), ("dchgEnergy", c_longlong),
##                ("ah", c_longlong), ("wh", c_longlong)
## 单位由mAS变为了mAmS,因此需要将旧数据*1000处理.




'''
5比4在TMsgSaveEngine最后多了     TBOOL nd96PlcNeedLeakpState;
                                TBOOL nd96PlcIgnoreLeakpState;
                                TBOOL nd96PlcLeakedAir;
'''
class ver5TMsgSaveEngine(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [("dataVer", c_uint),
                ("mainChNo", c_ubyte), ("chType", c_ubyte), ("channelNoInfoRow", c_ubyte), ("channelNoInfoCol", c_ubyte), ("channelNoInfoIndex", c_ubyte),
                ("engineState", c_ubyte), ("testId", c_uint),
                ("stepNum", c_ushort), ("stepNo", c_ushort), ("isAlarmClosed", c_ubyte),
                ("dfDataIndex", c_uint), ("platformPointVolRec", c_ubyte * 3),
                ("logIndex", c_uint), ("startDFFlag", c_ubyte), ("sampleStatus", c_ubyte),
                ("detalVolFlag", c_ubyte), ("detalVolZeroPoint", c_int), ("totalTime", c_ulonglong),
                ("phaseInCCCVC", c_ubyte), ("testTime", c_ulonglong), ("lastSampleVol", c_int),
                ("sampleCount", c_uint), ("sampleOmitted", c_ubyte), ("sampleTimeChange", c_ulonglong),
                ("volMax", c_int), ("timeChange", c_ulonglong), ("savedVoltage", c_int),
                ("savedCurrent", c_int), ("chgCap", c_longlong), ("dchgCap", c_longlong),
                ("chgEnergy", c_longlong), ("dchgEnergy", c_longlong), ("waitDCRStepFlag", c_ubyte),
                ("mchDCRFlag", c_ubyte), ("achDCRFlag", c_ubyte), ("dcrStartStepFlag", c_ubyte),
                ("dcrEndStepFlag", c_ubyte), ("dcrCutOffFlag", c_ubyte), ("mchDCRk", c_uint),
                ("mchChDFDCR", c_int * 5), ("cycleNum", c_uint), ("cycleStackSize", c_ubyte),
                ("auxChNum", c_ubyte), ("uniteData", c_ubyte * 10), ("latestParaRunTime", c_uint),
                ("latestParaRunNo", c_uint), ("lastErrCode", c_ubyte), ("CCCStepCnt", c_ubyte),
                ("ctrlStepStatus", c_ubyte), ("ctrlStepSetVal", c_ubyte), ("stepSwitchNum", c_uint),
                ("volMin", c_int), ("currentMax", c_int), ("currentMin", c_int), ("ah", c_longlong),
                ("wh", c_longlong), ("globalVariable", c_float * 100),
                ("nd96PlcNeedLeakpState", c_ubyte), ("nd96PlcIgnoreLeakpState", c_ubyte),
                ("nd96PlcLeakedAir", c_ubyte)]


'''
6与5相同
'''

'''

7比5 6多了 nd96PlcLeakedAir 之后的内容

'''


class TPressCtrlPara(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [("flag", c_ubyte), ("csvtVacuum", c_uint), ("csvtValveOpenTime", c_uint),
                ("csvtValveCloseTime", c_uint), ("csvtVacuumMethod", c_ubyte), ("csvtVacuumType", c_ubyte)]


class ver7TMsgSaveEnginePre(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [("dataVer", c_uint),
                ("mainChNo", c_ubyte), ("chType", c_ubyte), ("channelNoInfoRow", c_ubyte), ("channelNoInfoCol", c_ubyte), ("channelNoInfoIndex", c_ubyte),
                ("engineState", c_ubyte), ("testId", c_uint),
                ("stepNum", c_ushort), ("stepNo", c_ushort), ("isAlarmClosed", c_ubyte),
                ("dfDataIndex", c_uint), ("platformPointVolRec", c_ubyte * 3),
                ("logIndex", c_uint), ("startDFFlag", c_ubyte), ("sampleStatus", c_ubyte),
                ("detalVolFlag", c_ubyte), ("detalVolZeroPoint", c_int), ("totalTime", c_ulonglong),
                ("phaseInCCCVC", c_ubyte), ("testTime", c_ulonglong), ("lastSampleVol", c_int),
                ("sampleCount", c_uint), ("sampleOmitted", c_ubyte), ("sampleTimeChange", c_ulonglong),
                ("volMax", c_int), ("timeChange", c_ulonglong), ("savedVoltage", c_int),
                ("savedCurrent", c_int), ("chgCap", c_longlong), ("dchgCap", c_longlong),
                ("chgEnergy", c_longlong), ("dchgEnergy", c_longlong), ("waitDCRStepFlag", c_ubyte),
                ("mchDCRFlag", c_ubyte), ("achDCRFlag", c_ubyte), ("dcrStartStepFlag", c_ubyte),
                ("dcrEndStepFlag", c_ubyte), ("dcrCutOffFlag", c_ubyte), ("mchDCRk", c_uint),
                ("mchChDFDCR", c_int * 5), ("cycleNum", c_uint), ("cycleStackSize", c_ubyte),
                ("auxChNum", c_ubyte), ("uniteData", c_ubyte * 10), ("latestParaRunTime", c_uint),
                ("latestParaRunNo", c_uint)]


class ver7TMsgSaveEngine(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [('prev7data', ver7TMsgSaveEnginePre), ("lastErrCode", c_ubyte * 2), ("CCCStepCnt", c_ubyte),
                ("ctrlStepStatus", c_ubyte), ("ctrlStepSetVal", c_ubyte), ("stepSwitchNum", c_uint),
                ("volMin", c_int), ("currentMax", c_int), ("currentMin", c_int), ("ah", c_longlong),
                ("wh", c_longlong), ("globalVariable", c_float * 100),
                ("nd96PlcNeedLeakpState", c_ubyte), ("nd96PlcIgnoreLeakpState", c_ubyte),
                ("nd96PlcLeakedAir", c_ubyte),
                ("reserveStopCount", c_uint), ("appointStepCount", c_uint),
                ("tempRunTime", c_uint), ("latestPressCtrlPara", TPressCtrlPara), ("stepType", c_ubyte)]
    



''' ver8使用的结构 '''    

class TReservePause(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [('reserveStep', c_ushort), ('reserveCycle', c_uint)]    


class TReservePauseData(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [('reservedata', TReservePause), ('reserveType', c_ubyte)]    
    

class TResePauseInfo(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [('num', c_ubyte), ('info', TReservePauseData * 5),
                ('overTime', c_uint), ('firstArrive', c_ubyte)]    

class ver8ifloat(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [("ifloat", c_float * 24)]
    
#在globalVariable中也比原来多了24个内容,注意
class ver8ChangeStart(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [('prev7data', ver7TMsgSaveEnginePre), ("lastErrCode", c_ubyte * 2), ("CCCStepCnt", c_ubyte),
                ("ctrlStepStatus", c_ubyte), ("ctrlStepSetVal", c_ubyte), ("stepSwitchNum", c_uint),
                ("volMin", c_int), ("currentMax", c_int), ("currentMin", c_int), ("ah", c_longlong),
                ("wh", c_longlong), ("globalVariable", c_float * 124),
                ("nd96PlcNeedLeakpState", c_ubyte), ("nd96PlcIgnoreLeakpState", c_ubyte),
                ("nd96PlcLeakedAir", c_ubyte)]

class ver8Tail(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [("tempRunTime", c_uint), ("latestPressCtrlPara", TPressCtrlPara), ("stepType", c_ubyte),
                ('usedWXLevel', c_ubyte)]

class ver8TMsgSaveEngine(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [('beforeData', ver8ChangeStart), ("resePauseInfo", TResePauseInfo),
                ("v8tail", ver8Tail)]    
    
#ver9中比8多了 globalVariable 149-124=25个
class ver9ChangeStart(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [('prev7data', ver7TMsgSaveEnginePre), ("lastErrCode", c_ubyte * 2), ("CCCStepCnt", c_ubyte),
                ("ctrlStepStatus", c_ubyte), ("ctrlStepSetVal", c_ubyte), ("stepSwitchNum", c_uint),
                ("volMin", c_int), ("currentMax", c_int), ("currentMin", c_int), ("ah", c_longlong),
                ("wh", c_longlong), ("globalVariable", c_float * 124)]
    
#ver9 whole
class ver9whole(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [('first', ver9ChangeStart), ('second', c_float * 25), ("nd96PlcNeedLeakpState", c_ubyte),
                ("nd96PlcIgnoreLeakpState", c_ubyte), ("nd96PlcLeakedAir", c_ubyte),
                ("resePauseInfo", TResePauseInfo), ("v8tail", ver8Tail)]

    
#9与10相同
    

    


