##
# @file   comm_structures.py
# @brief  communication structures definition
# @copyright
#
#
# @addtogroup
# @{

from comm_packing import *
from constants import *

class CommStructure(object):
    READ_ID = None
    WRITE_ID = None
    FULL_LENGTH = 0
    QUERY_LENGTH = 0


    def __str__(self):
        just = max([len(k) for k in self.__dict__.keys()]) + 1
        s = '%s: {\n    ' % self.__class__.__name__
        s += '\n    '.join(['%s: %s' % (str(k).ljust(just), str(self.__dict__[k])) for k in sorted(self.__dict__)])
        s += "\n}"
        return s


    def encode(self):
        raise NotImplementedError


    def decode(self):
        raise NotImplementedError


class SystemInfo(CommStructure):

    def __init__(self):
        # set default values
        self.fw_rev_major = None
        self.fw_rev_minor = None
        self.fw_rev_patch = None

    def decode(self, data):
        self.fw_rev_major = unpack_uint8(data[0])
        self.fw_rev_minor = unpack_uint8(data[1])
        self.fw_rev_patch = unpack_uint8(data[2])

class CounterStatus(CommStructure):

    def __init__(self):
        # set default values
        self.hall_count = 0
        self.revolutions = 0
        self.rpm = 0
        self.motor_enable = 0
        self.pps_ok = 0
        self.utc_ok = 0

    def decode(self, data):
        if len(data) >= 0:
            self.hall_count     = unpack_uint16(data[0:2])
            self.revolutions    = unpack_sint16(data[2:4])
            self.rpm            = unpack_uint16(data[4:6])
            self.motor_enable   = data[6]
            self.pps_ok         = data[7]
            self.utc_ok         = data[8]

class LogStatus(CommStructure):

    def __init__(self):
        # set default values
        self.hall_count = 0
        self.revolutions = 0
        self.timeStamp = 0
        self.timCounter = 0
        self.logCounter = 0

        self.Year = 0
        self.Month = 0
        self.Day = 0
        self.Hour = 0
        self.Minute = 0
        self.Second = 0
        self.ParamA = 0
        self.ParamB = 0

        self.pulse = 0

    def decode(self, data):
        if len(data) >= 0:
            self.hall_count = unpack_uint16(data[2:4])
            self.revolutions = unpack_sint16(data[4:6])
            self.timeStamp = data[6:30]
            self.timCounter = unpack_uint32(data[30:34])
            self.logCounter = unpack_uint16(data[34:36])

            self.Year = unpack_uint16(data[10:12])
            self.Month = unpack_uint16(data[13:15])
            self.Day = unpack_uint16(data[16:18])

            self.Hour = unpack_uint16(data[19:21])
            self.Minute = unpack_uint16(data[22:24])
            self.Second = unpack_uint16(data[25:27])
            self.ParamA = unpack_uint8(data[28])
            self.ParamB = unpack_uint8(data[29])
            self.pulse = unpack_uint16(data[36:38])

class ErrorInfo(CommStructure):

    def __init__(self):
        # set default values
        pass


    def decode(self, data):
        pass

class Counters(CommStructure):

    def __init__(self):
        # set default values
        self.Hall_Counter = 0


    def decode(self, data):
        self.Hall_Counter = unpack_uint32(data[0:4])



if __name__ == '__main__':
    pass

# @}
