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

try:
    import cStringIO.StringIO as BytesIO
except ImportError:
    from io import BytesIO
import struct

import oi.double_t

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
        self.count_publish = 0
        self.sender_id = ""
        self.tilt_degrees = 0.0
        self.tilt_counts = 0.0
        self.num_doubles = 0
        self.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)

    def decode(data):
        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)
    decode = staticmethod(decode)

    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
    _decode_one = staticmethod(_decode_one)

    _hash = None
    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
    _get_hash_recursive = staticmethod(_get_hash_recursive)
    _packed_fingerprint = None

    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
    _get_packed_fingerprint = staticmethod(_get_packed_fingerprint)

