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

"""
tilt_control_t.lcm
 Type definition for the published status of the tilt controller process. 
 Copyright 2019 MBARI

"""

from io import BytesIO
import struct

import oi

class tilt_control_t(object):

    __slots__ = ["time_unix_sec", "count_publish", "sender_id", "tilt_degrees", "tilt_counts", "num_doubles", "doubles"]

    __typenames__ = ["double", "int64_t", "string", "double", "double", "int16_t", "oi.double_t"]

    __dimensions__ = [None, None, None, None, None, None, ["num_doubles"]]

    def __init__(self):
        self.time_unix_sec = 0.0
        """
        Time since Unix Epoch
        LCM Type: double
        """

        self.count_publish = 0
        """
        LCM publish count
        LCM Type: int64_t
        """

        self.sender_id = ""
        """
        Self identifier for sender.
        LCM Type: string
        """

        self.tilt_degrees = 0.0
        """
        Start message specific items
        Tilt Sensor Related Items
        LCM Type: double
        """

        self.tilt_counts = 0.0
        """ LCM Type: double """
        self.num_doubles = 0
        """ LCM Type: int16_t """
        self.doubles = []
        """ LCM Type: oi.double_t[num_doubles] """

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

    def _encode_one(self, buf):
        buf.write(struct.pack(">dq", self.time_unix_sec, self.count_publish))
        __sender_id_encoded = self.sender_id.encode('utf-8')
        buf.write(struct.pack('>I', len(__sender_id_encoded)+1))
        buf.write(__sender_id_encoded)
        buf.write(b"\0")
        buf.write(struct.pack(">ddh", self.tilt_degrees, self.tilt_counts, self.num_doubles))
        for i0 in range(self.num_doubles):
            assert self.doubles[i0]._get_packed_fingerprint() == oi.double_t._get_packed_fingerprint()
            self.doubles[i0]._encode_one(buf)

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

    @staticmethod
    def _decode_one(buf):
        self = tilt_control_t()
        self.time_unix_sec, self.count_publish = struct.unpack(">dq", buf.read(16))
        __sender_id_len = struct.unpack('>I', buf.read(4))[0]
        self.sender_id = buf.read(__sender_id_len)[:-1].decode('utf-8', 'replace')
        self.tilt_degrees, self.tilt_counts, self.num_doubles = struct.unpack(">ddh", buf.read(18))
        self.doubles = []
        for i0 in range(self.num_doubles):
            self.doubles.append(oi.double_t._decode_one(buf))
        return self

    @staticmethod
    def _get_hash_recursive(parents):
        if tilt_control_t in parents: return 0
        newparents = parents + [tilt_control_t]
        tmphash = (0x5671fe8ab6567297+ oi.double_t._get_hash_recursive(newparents)) & 0xffffffffffffffff
        tmphash  = (((tmphash<<1)&0xffffffffffffffff) + (tmphash>>63)) & 0xffffffffffffffff
        return tmphash
    _packed_fingerprint = None

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

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

