"""LCM type definitions
This file automatically generated by lcm.
DO NOT MODIFY BY HAND!!!!
"""

"""

Copyright 2019 MBARI.                                                    //
MBARI Proprietary Information. All rights reserved.                      //

"""

from io import BytesIO
import struct

class lcm_header_t(object):

    __slots__ = ["publisher", "timestamp", "sequence"]

    __typenames__ = ["string", "double", "int64_t"]

    __dimensions__ = [None, None, None]

    def __init__(self):
        self.publisher = ""
        """
        a string that uniquely identifies the process
        publishing the data ( e.g. hostname:process_name(pid) )
        LCM Type: string
        """

        self.timestamp = 0.0
        """
        epoch time in seconds
        LCM Type: double
        """

        self.sequence = 0
        """
        sequence number of published packet
        LCM Type: int64_t
        """


    def encode(self):
        buf = BytesIO()
        buf.write(lcm_header_t._get_packed_fingerprint())
        self._encode_one(buf)
        return buf.getvalue()

    def _encode_one(self, buf):
        __publisher_encoded = self.publisher.encode('utf-8')
        buf.write(struct.pack('>I', len(__publisher_encoded)+1))
        buf.write(__publisher_encoded)
        buf.write(b"\0")
        buf.write(struct.pack(">dq", self.timestamp, self.sequence))

    @staticmethod
    def decode(data: bytes):
        if hasattr(data, 'read'):
            buf = data
        else:
            buf = BytesIO(data)
        if buf.read(8) != lcm_header_t._get_packed_fingerprint():
            raise ValueError("Decode error")
        return lcm_header_t._decode_one(buf)

    @staticmethod
    def _decode_one(buf):
        self = lcm_header_t()
        __publisher_len = struct.unpack('>I', buf.read(4))[0]
        self.publisher = buf.read(__publisher_len)[:-1].decode('utf-8', 'replace')
        self.timestamp, self.sequence = struct.unpack(">dq", buf.read(16))
        return self

    @staticmethod
    def _get_hash_recursive(parents):
        if lcm_header_t in parents: return 0
        tmphash = (0x628deda648a19ac5) & 0xffffffffffffffff
        tmphash  = (((tmphash<<1)&0xffffffffffffffff) + (tmphash>>63)) & 0xffffffffffffffff
        return tmphash
    _packed_fingerprint = None

    @staticmethod
    def _get_packed_fingerprint():
        if lcm_header_t._packed_fingerprint is None:
            lcm_header_t._packed_fingerprint = struct.pack(">Q", lcm_header_t._get_hash_recursive([]))
        return lcm_header_t._packed_fingerprint

    def get_hash(self):
        """Get the LCM hash of the struct"""
        return struct.unpack(">Q", lcm_header_t._get_packed_fingerprint())[0]

