Arduino Ant now works.

This commit is contained in:
Pascal Serrarens 2024-12-09 12:06:12 +01:00
parent d3cb4c1e47
commit aebe4c0f8e
3 changed files with 35 additions and 81 deletions

View File

@ -10,7 +10,7 @@ namespace Passer.Control {
public string ipAddress;
public int port;
public byte networkId;
public byte networkId = 0;
public readonly ConcurrentQueue<IMessage> messageQueue = new();
@ -23,20 +23,32 @@ namespace Passer.Control {
}
static public List<Client> clients = new List<Client>();
public static Client NewClient() {
Client client = new();
clients.Add(client);
client.networkId = 0;
//// These static functions are deprecated
//public static Client NewClient() {
// Client client = new();
// clients.Add(client);
// client.networkId = 0;
return client;
}
// return client;
//}
public static Client NewUDPClient(UdpClient udpClient, string ipAddress, int port) {
Client client = NewClient();
client.ipAddress = null;
client.port = port;
client.udpClient = udpClient;
return client;
//public static Client NewUDPClient(UdpClient udpClient, string ipAddress, int port) {
// Client client = NewClient();
// client.ipAddress = null;
// client.port = port;
// client.udpClient = udpClient;
// return client;
//}
//public Client() {
//}
public Client(UdpClient udpClient, int port) {
this.udpClient = udpClient;
this.ipAddress = null;
this.port = port;
clients.Add(this);
}
public void ProcessMessage(IMessage msg) {

View File

@ -195,15 +195,13 @@ namespace Passer.Control {
#region Thing
public class ThingMsg : IMessage {
public const byte length = 5;
public const byte length = 4;
public const byte Id = 0x80;
public byte networkId;
public byte thingId;
public byte thingType;
public byte parentId;
public ThingMsg(byte networkId, byte thingId, byte thingType, byte parentId) {
this.networkId = networkId;
public ThingMsg(byte thingId, byte thingType, byte parentId) {
this.thingId = thingId;
this.thingType = thingType;
this.parentId = parentId;
@ -212,23 +210,22 @@ namespace Passer.Control {
public override byte[] Serialize() {
byte[] data = new byte[ThingMsg.length];
data[0] = ThingMsg.Id;
data[1] = this.networkId;
data[2] = this.thingId;
data[3] = this.thingType;
data[4] = this.parentId;
byte ix = 0;
data[ix++] = ThingMsg.Id;
data[ix++] = this.thingId;
data[ix++] = this.thingType;
data[ix] = this.parentId;
return data;
}
public override void Deserialize(byte[] data) {
uint ix = 0;
this.networkId = data[ix++];
this.thingId = data[ix++];
this.thingType = data[ix++];
this.parentId = data[ix];
}
public static bool Send(Client client, byte thingId, byte thingType, byte parentId) {
ThingMsg msg = new(client.networkId, thingId, thingType, parentId);
ThingMsg msg = new(thingId, thingType, parentId);
return SendMsg(client, msg);
}
public static async Task<bool> Receive(Stream dataStream, Client client, byte packetSize) {
@ -250,7 +247,6 @@ namespace Passer.Control {
public class NameMsg : IMessage {
public const byte Id = 0x91; // 145
public const byte length = 3;
public byte networkId = 0;
public byte thingId;
public byte len;
public string name;

View File

@ -1,4 +1,3 @@
using System.IO;
using System.Threading.Tasks;
@ -8,12 +7,9 @@ namespace Passer.Control {
public static async Task ReceiveData(Stream dataStream, Client client) {
while (true) {
byte packetSize = (byte)dataStream.ReadByte();
if (packetSize == 0xFF)
//Debug.Log("Receive timeout");
// Timeout
;
else
if (packetSize != 0xFF)
await ReceiveData(dataStream, client, packetSize);
// else timeout
}
}
@ -64,55 +60,5 @@ namespace Passer.Control {
await ReceiveData(dataStream, client, packetSize);
}
}
//public static void ProcessMessage(ISiteServer site, Client client, IMessage msg) {
// client.ProcessMessage(site, client, msg);
// switch (msg) {
// case ClientMsg clientMsg:
// site.ProcessClient(client, clientMsg);
// break;
// case NetworkIdMsg networkId:
// site.ProcessNetworkId(client, networkId);
// break;
// case InvestigateMsg investigate:
// site.ProcessInvestigate(client, investigate);
// break;
// case ThingMsg thing:
// site.ProcessThing(client, thing);
// break;
// case NameMsg name:
// site.ProcessName(client, name);
// break;
// case ModelUrlMsg modelUrl:
// site.ProcessModelUrl(client, modelUrl);
// break;
// case PoseMsg pose:
// site.ProcessPose(client, pose);
// break;
// case CustomMsg custom:
// site.ProcessCustom(client, custom);
// break;
// case TextMsg text:
// site.ProcessText(client, text);
// break;
// case DestroyMsg destroy:
// site.ProcessDestroy(client, destroy);
// break;
// }
//}
}
//public interface ISiteServer {
// public void ProcessClient(Client client, ClientMsg clientMsg);
// public void ProcessNetworkId(Client client, NetworkIdMsg networkId);
// public void ProcessInvestigate(Client client, InvestigateMsg investigate);
// public void ProcessThing(Client client, ThingMsg thing);
// public void ProcessName(Client client, NameMsg name);
// public void ProcessModelUrl(Client client, ModelUrlMsg modelUrl);
// public void ProcessPose(Client client, PoseMsg pose);
// public void ProcessCustom(Client client, CustomMsg custom);
// public void ProcessText(Client client, TextMsg text);
// public void ProcessDestroy(Client client, DestroyMsg destroy);
//}
}