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


from io import BytesIO
import struct

class lcmt_allegro_status(object):

    __slots__ = ["utime", "num_joints", "joint_position_measured", "joint_velocity_estimated", "joint_position_commanded", "joint_torque_commanded"]

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

    __dimensions__ = [None, None, ["num_joints"], ["num_joints"], ["num_joints"], ["num_joints"]]

    def __init__(self):
        self.utime = 0
        """
        The timestamp in microseconds.
        LCM Type: int64_t
        """

        self.num_joints = 0
        """ LCM Type: int32_t """
        self.joint_position_measured = []
        """ LCM Type: double[num_joints] """
        self.joint_velocity_estimated = []
        """ LCM Type: double[num_joints] """
        self.joint_position_commanded = []
        """ LCM Type: double[num_joints] """
        self.joint_torque_commanded = []
        """ LCM Type: double[num_joints] """

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

    def _encode_one(self, buf):
        buf.write(struct.pack(">qi", self.utime, self.num_joints))
        buf.write(struct.pack('>%dd' % self.num_joints, *self.joint_position_measured[:self.num_joints]))
        buf.write(struct.pack('>%dd' % self.num_joints, *self.joint_velocity_estimated[:self.num_joints]))
        buf.write(struct.pack('>%dd' % self.num_joints, *self.joint_position_commanded[:self.num_joints]))
        buf.write(struct.pack('>%dd' % self.num_joints, *self.joint_torque_commanded[:self.num_joints]))

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

    @staticmethod
    def _decode_one(buf):
        self = lcmt_allegro_status()
        self.utime, self.num_joints = struct.unpack(">qi", buf.read(12))
        self.joint_position_measured = struct.unpack('>%dd' % self.num_joints, buf.read(self.num_joints * 8))
        self.joint_velocity_estimated = struct.unpack('>%dd' % self.num_joints, buf.read(self.num_joints * 8))
        self.joint_position_commanded = struct.unpack('>%dd' % self.num_joints, buf.read(self.num_joints * 8))
        self.joint_torque_commanded = struct.unpack('>%dd' % self.num_joints, buf.read(self.num_joints * 8))
        return self

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

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

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

