namespace Passer.RoboidControl { /// /// Message providing generic information about a Thing /// public class ThingMsg : IMessage { /// /// The message ID /// public const byte id = 0x80; /// /// The length of the message /// public const byte length = 5; /// /// The network ID of the thing /// public byte networkId; /// /// The ID of the thing /// public byte thingId; /// /// The Thing.Type of the thing /// public byte thingType; /// /// The parent of the thing in the hierarachy. This is null for root Things /// public byte parentId; /// /// Create a message for sending /// /// The network ID of the thing /// The thing public ThingMsg(byte networkId, Thing thing) { this.networkId = networkId; this.thingId = thing.id; this.thingType = thing.type; if (thing.parent != null) this.parentId = thing.parent.id; else this.parentId = 0; } /// /// Create a message for sending /// /// The network ID of the thing /// The ID of the thing /// The type of thing /// The parent of the thing public ThingMsg(byte networkId, byte thingId, byte thingType, byte parentId) { this.networkId = networkId; this.thingId = thingId; this.thingType = thingType; this.parentId = parentId; } /// /// Create a message for receiving /// /// The byte array to parse public ThingMsg(byte[] buffer) { uint ix = 1; this.networkId = buffer[ix++]; this.thingId = buffer[ix++]; this.thingType = buffer[ix++]; this.parentId = buffer[ix]; } /// /// Serialize the message into a byte array /// /// The buffer to serialize into /// The length of the message in the bufer. 0 when the buffer was too small public override byte Serialize(ref byte[] buffer) { if (buffer.Length < ThingMsg.length) return 0; byte ix = 0; buffer[ix++] = ThingMsg.id; buffer[ix++] = this.networkId; buffer[ix++] = this.thingId; buffer[ix++] = this.thingType; buffer[ix++] = this.parentId; return ThingMsg.length; } } }