Move OSCQuerySetup to own Method

This commit is contained in:
Glax 2023-12-31 19:11:23 +01:00
parent 36520e8426
commit fcc584d37b

View File

@ -33,7 +33,7 @@ public class OSCCollar
private readonly string _ip; private readonly string _ip;
private readonly int _portReceive, _portSend; private readonly int _portReceive, _portSend;
private OSCQueryService _oscQueryService { get; init; } private OSCQueryService? OscQueryService { get; set; }
private OscServer Server { get; init; } private OscServer Server { get; init; }
private OscClient Client { get; init; } private OscClient Client { get; init; }
private float _leashStretch; private float _leashStretch;
@ -63,25 +63,7 @@ public class OSCCollar
private OSCCollar(string ip = "127.0.0.1", int portSend = 9000, double radius = 100, double calibrationX = 0, double calibrationY = 0, double walkStretch = 0.1, double runStretch = 0.4, bool skipSetup = true) private OSCCollar(string ip = "127.0.0.1", int portSend = 9000, double radius = 100, double calibrationX = 0, double calibrationY = 0, double walkStretch = 0.1, double runStretch = 0.4, bool skipSetup = true)
{ {
this._ip = ip; this._ip = ip;
var tcpPort = Extensions.GetAvailableTcpPort(); this._portReceive = Extensions.GetAvailableUdpPort();
var udpPort = Extensions.GetAvailableUdpPort();
this._oscQueryService = new OSCQueryServiceBuilder()
.WithHostIP(IPAddress.Parse(ip))
.WithOscIP(IPAddress.Parse(ip))
.WithDiscovery(new MeaModDiscovery())
.WithTcpPort(tcpPort)
.WithUdpPort(udpPort)
.WithServiceName("Collar")
.StartHttpServer()
.AdvertiseOSC()
.AdvertiseOSCQuery()
.Build();
this._oscQueryService.AddEndpoint<float>("/avatar/parameters/GPS1", Attributes.AccessValues.WriteOnly);
this._oscQueryService.AddEndpoint<float>("/avatar/parameters/GPS2", Attributes.AccessValues.WriteOnly);
this._oscQueryService.AddEndpoint<float>("/avatar/parameters/GPS3", Attributes.AccessValues.WriteOnly);
this._oscQueryService.AddEndpoint<float>("/avatar/parameters/Leash_Stretch", Attributes.AccessValues.WriteOnly);
this._oscQueryService.AddEndpoint<bool>("/avatar/parameters/Leash_Toggle", Attributes.AccessValues.WriteOnly);
this._portReceive = this._oscQueryService.OscPort;
this._portSend = portSend; this._portSend = portSend;
this._walkStretch = walkStretch; this._walkStretch = walkStretch;
this._runStretch = runStretch; this._runStretch = runStretch;
@ -97,10 +79,9 @@ public class OSCCollar
this.GPSConstantD = 2 * GPS3.X - 2 * GPS2.X; this.GPSConstantD = 2 * GPS3.X - 2 * GPS2.X;
this.GPSConstantE = Math.Pow(GPS2.X, 2) + Math.Pow(GPS3.X, 2) - Math.Pow(GPS2.Y, 2) + Math.Pow(GPS3.Y, 2); this.GPSConstantE = Math.Pow(GPS2.X, 2) + Math.Pow(GPS3.X, 2) - Math.Pow(GPS2.Y, 2) + Math.Pow(GPS3.Y, 2);
if (!skipSetup) if (!skipSetup)
{ {
Console.WriteLine($"OSC Port: {this._portReceive} TCP Port: {this._oscQueryService.TcpPort}"); Console.WriteLine($"OSC Port: {this._portReceive} TCP Port: {this.OscQueryService.TcpPort}");
Console.WriteLine("Position your GPS receivers:"); Console.WriteLine("Position your GPS receivers:");
Console.WriteLine($"GPS 1 x: {GPS1.X} y: {GPS1.Y}"); Console.WriteLine($"GPS 1 x: {GPS1.X} y: {GPS1.Y}");
Console.WriteLine($"GPS 2 x: {GPS2.X} y: {GPS2.Y}"); Console.WriteLine($"GPS 2 x: {GPS2.X} y: {GPS2.Y}");
@ -121,14 +102,14 @@ public class OSCCollar
Thread runningThread = new Thread(RunningThread); Thread runningThread = new Thread(RunningThread);
runningThread.Start(); runningThread.Start();
this._oscQueryService.RefreshServices(); this.SetupOSCQuery();
} }
private void PrintOutput() private void PrintOutput()
{ {
Console.Clear(); Console.Clear();
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine($"OSC Collar - Status: {(_allowMoving ? "enabled" : "disabled")} IP: {_ip} Send: {_portSend} Receive: {_portReceive} HTTP: {this._oscQueryService.TcpPort}"); Console.WriteLine($"OSC Collar - Status: {(_allowMoving ? "enabled" : "disabled")} IP: {_ip} Send-OSC: {_portSend} Receive-OSC: {_portReceive} OSC-HTTP: {this.OscQueryService?.TcpPort}");
Console.WriteLine("=============================="); Console.WriteLine("==============================");
Console.WriteLine($"GPS 1:................{GPS1.Distance:00.00000}"); Console.WriteLine($"GPS 1:................{GPS1.Distance:00.00000}");
Console.WriteLine($"GPS 2:................{GPS2.Distance:00.00000}"); Console.WriteLine($"GPS 2:................{GPS2.Distance:00.00000}");
@ -185,6 +166,28 @@ public class OSCCollar
Console.WriteLine(_debugValue); Console.WriteLine(_debugValue);
} }
private void SetupOSCQuery()
{
var tcpPort = Extensions.GetAvailableTcpPort();
this.OscQueryService = new OSCQueryServiceBuilder()
.WithHostIP(IPAddress.Parse(this._ip))
.WithOscIP(IPAddress.Parse(this._ip))
.WithDiscovery(new MeaModDiscovery())
.WithTcpPort(tcpPort)
.WithUdpPort(this._portReceive)
.WithServiceName("Collar")
.StartHttpServer()
.AdvertiseOSC()
.AdvertiseOSCQuery()
.Build();
this.OscQueryService.AddEndpoint<float>("/avatar/parameters/GPS1", Attributes.AccessValues.WriteOnly);
this.OscQueryService.AddEndpoint<float>("/avatar/parameters/GPS2", Attributes.AccessValues.WriteOnly);
this.OscQueryService.AddEndpoint<float>("/avatar/parameters/GPS3", Attributes.AccessValues.WriteOnly);
this.OscQueryService.AddEndpoint<float>("/avatar/parameters/Leash_Stretch", Attributes.AccessValues.WriteOnly);
this.OscQueryService.AddEndpoint<bool>("/avatar/parameters/Leash_Toggle", Attributes.AccessValues.WriteOnly);
this.OscQueryService.RefreshServices();
}
#region Handle OSC-Messages #region Handle OSC-Messages
private void AllowMovingHandle(OscMessageValues messageValues) private void AllowMovingHandle(OscMessageValues messageValues)
{ {