namespace RoboidControl {
///
/// Message for communicating the name of a thing
///
public class NameMsg : IMessage {
///
/// The message ID
///
public const byte Id = 0x91; // 145
///
/// The length of the message without the name string
///
public const byte length = 4;
///
/// The network ID of the thing
///
public byte networkId;
///
/// The ID of the thing
///
public byte thingId;
///
/// The length of the name, excluding null terminator
///
public byte len;
///
/// The name of the thing, not terminated with a null character
///
public string name = "";
///
/// Create a new message for sending
///
/// The network ID of the thing
/// The thing
public NameMsg(byte networkId, Thing thing) {
this.networkId = networkId;
this.thingId = thing.id;
this.name = thing.name;
}
///
/// Create a new message for sending
///
/// The network ID of the thing
/// The ID of the thing
/// The name of the thing
public NameMsg(byte networkId, byte thingId, string name) {
this.networkId = networkId;
this.thingId = thingId;
this.name = name;
}
/// @copydoc Passer::RoboidControl::IMessage::IMessage(byte[] buffer)
public NameMsg(byte[] buffer) {
byte ix = 1;
this.networkId = buffer[ix++];
this.thingId = buffer[ix++];
int strlen = buffer[ix++];
this.name = System.Text.Encoding.UTF8.GetString(buffer, (int)ix, strlen);
}
/// @copydoc Passer::RoboidControl::IMessage::Serialize
public override byte Serialize(ref byte[] buffer) {
if (buffer.Length < NameMsg.length + this.name.Length || this.name.Length == 0)
return 0;
byte ix = 0;
buffer[ix++] = NameMsg.Id;
buffer[ix++] = this.networkId;
buffer[ix++] = this.thingId;
int nameLength = this.name.Length;
buffer[ix++] = (byte)nameLength;
for (int nameIx = 0; nameIx < nameLength; nameIx++)
buffer[ix++] = (byte)this.name[nameIx];
return ix;
}
}
}