class CH10Packet:
    def __init__(self, packetData):
        offset = 0;
        self.msgFormat = packetData[offset] & 0x0F
        self.msgType = (packetData[offset] >> 4) & 0x0F
        offset += 1
        self.msgSeqNo = (packetData[offset+2] << 16) | (packetData[offset+1] << 8) | (packetData[offset])
        offset += 3
        self.uSync = (packetData[offset+1] << 8) | (packetData[offset])
        offset += 2
        self.channelID = (packetData[offset+1] << 8) | (packetData[offset])
        offset += 2
        self.pktLen = (packetData[offset+3] << 24) | (packetData[offset+2] << 16) | (packetData[offset+1] << 8) | (packetData[offset])
        offset += 4
        self.dataLen = (packetData[offset+3] << 24) | (packetData[offset+2] << 16) | (packetData[offset+1] << 8) | (packetData[offset])
        offset += 4
        self.dataTypeVer = (packetData[offset])
        offset += 1
        self.seqNumber = (packetData[offset])
        offset += 1
        self.pktFlags = (packetData[offset])
        offset += 1
        self.dataType = (packetData[offset])

        self.bIncludesSecondaryTimeHeader = False
        if (self.pktFlags / 128.0 >= 1.0):
            self.bIncludesSecondaryTimeHeader = True

        if self.dataType != 0X21:
            return
        
        if self.dataType == 0x00 or self.dataType == 0x01:
            return

        offset += 1
        self.aubyRelTime = (packetData[offset+5] << 40) | (packetData[offset+4] << 32) | (packetData[offset+3] << 24) | (packetData[offset+2] << 16) | (packetData[offset+1] << 8) | (packetData[offset])
        
        offset += 6
        self.checksum = (packetData[offset+1] << 8) | (packetData[offset])
        
        offset += 2
        if(self.bIncludesSecondaryTimeHeader):
            self.secondaryTimeNs = (packetData[offset+3] << 24) | (packetData[offset+2] << 16) | (packetData[offset+1] << 8) | (packetData[offset])
            offset += 4
            self.secondaryTimeSec = (packetData[offset+3] << 24) | (packetData[offset+2] << 16) | (packetData[offset+1] << 8) | (packetData[offset])
            offset += 4
            offset += 2 #rsv
            self.checksumSecondary = (packetData[offset+1] << 8) | (packetData[offset])
            offset += 2

        self.Ch10AnalogMode = packetData[offset] & 0x03
        self.Ch10AnalogLength = (packetData[offset] >> 2) & 0x3F
        offset += 1
        self.Ch10AnalogSubChannelID = (packetData[offset])
        offset += 1
        self.Ch10AnalogSubChannelCount = (packetData[offset])
        offset += 1
        self.Ch10AnalogFactor = packetData[offset] & 0x0F
        self.Ch10AnalogSAME = (packetData[offset] >> 4) & 0x0F
        offset += 1
        
        self.bIsTSRAIR = False
        if(self.Ch10AnalogSubChannelCount==18):
            self.bIsTSRAIR = True

        self.sampleData = list()
        self.sampleCount = ((self.dataLen - 4) / self.Ch10AnalogSubChannelCount)/2.0
        i = 1
        while i <= self.sampleCount:
            thisSample = list()
            ii = 1
            if(self.bIncludesSecondaryTimeHeader):
                val = float(self.secondaryTimeSec) + (float(self.secondaryTimeNs)/1000000000.0) + (i/8000.0)
                
                thisSample.append(val)
                    # datetime.datetime.utcfromtimestamp(self.secondaryTimeSec)
                    # .strftime('%Y-%m-%d %H:%M:%S') + f':{self.secondaryTimeNs}')
            else:
                thisSample.append(self.aubyRelTime)
            
            while ii <= self.Ch10AnalogSubChannelCount:
                thisSample.append(toSigned16(self.bIsTSRAIR,(packetData[offset+1] << 8) | (packetData[offset])))
                offset+=2
                ii += 1
            
            self.sampleData.append(thisSample)
            i += 1

class CH10TMATPacket:
    def __init__(self, packetData):
        offset = 0;
        self.Version = packetData[offset] & 0xFF
        if self.Version != 0x11:
            return
        offset += 1
        self.SetupRecordConfigurationChange = (packetData[offset]) & 0x01
        self.Format = (packetData[offset] & 0x02) >> 1
        offset = 40
        self.tmats = packetData[offset:len(packetData)+1].decode("utf-8")

def toSigned16(bIsTSRAIR, n):
    if bIsTSRAIR:
        n = n & 0xffff
        return (n ^ 0x8000) - 0x8000
    else:
        return n - 0x8000