diff --git a/OSCCollar/OSCCollar.cs b/OSCCollar/OSCCollar.cs index 3911431..37adc4f 100644 --- a/OSCCollar/OSCCollar.cs +++ b/OSCCollar/OSCCollar.cs @@ -33,7 +33,7 @@ public class OSCCollar private readonly string _ip; private readonly int _portReceive, _portSend; - private OSCQueryService _oscQueryService { get; init; } + private OSCQueryService? OscQueryService { get; set; } private OscServer Server { get; init; } private OscClient Client { get; init; } 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) { this._ip = ip; - var tcpPort = Extensions.GetAvailableTcpPort(); - 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("/avatar/parameters/GPS1", Attributes.AccessValues.WriteOnly); - this._oscQueryService.AddEndpoint("/avatar/parameters/GPS2", Attributes.AccessValues.WriteOnly); - this._oscQueryService.AddEndpoint("/avatar/parameters/GPS3", Attributes.AccessValues.WriteOnly); - this._oscQueryService.AddEndpoint("/avatar/parameters/Leash_Stretch", Attributes.AccessValues.WriteOnly); - this._oscQueryService.AddEndpoint("/avatar/parameters/Leash_Toggle", Attributes.AccessValues.WriteOnly); - this._portReceive = this._oscQueryService.OscPort; + this._portReceive = Extensions.GetAvailableUdpPort(); this._portSend = portSend; this._walkStretch = walkStretch; this._runStretch = runStretch; @@ -96,11 +78,10 @@ public class OSCCollar this.GPSConstantC = Math.Pow(GPS1.X, 2) + Math.Pow(GPS2.X, 2) - Math.Pow(GPS1.Y, 2) + Math.Pow(GPS2.Y, 2); 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); - 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($"GPS 1 x: {GPS1.X} y: {GPS1.Y}"); Console.WriteLine($"GPS 2 x: {GPS2.X} y: {GPS2.Y}"); @@ -121,14 +102,14 @@ public class OSCCollar Thread runningThread = new Thread(RunningThread); runningThread.Start(); - this._oscQueryService.RefreshServices(); + this.SetupOSCQuery(); } private void PrintOutput() { Console.Clear(); 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($"GPS 1:................{GPS1.Distance:00.00000}"); Console.WriteLine($"GPS 2:................{GPS2.Distance:00.00000}"); @@ -185,6 +166,28 @@ public class OSCCollar 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("/avatar/parameters/GPS1", Attributes.AccessValues.WriteOnly); + this.OscQueryService.AddEndpoint("/avatar/parameters/GPS2", Attributes.AccessValues.WriteOnly); + this.OscQueryService.AddEndpoint("/avatar/parameters/GPS3", Attributes.AccessValues.WriteOnly); + this.OscQueryService.AddEndpoint("/avatar/parameters/Leash_Stretch", Attributes.AccessValues.WriteOnly); + this.OscQueryService.AddEndpoint("/avatar/parameters/Leash_Toggle", Attributes.AccessValues.WriteOnly); + this.OscQueryService.RefreshServices(); + } + #region Handle OSC-Messages private void AllowMovingHandle(OscMessageValues messageValues) {