diff --git a/OSCCollar/OSCCollar.cs b/OSCCollar/OSCCollar.cs index 738295b..3911431 100644 --- a/OSCCollar/OSCCollar.cs +++ b/OSCCollar/OSCCollar.cs @@ -1,5 +1,8 @@ -using BuildSoft.OscCore; +using System.Net; +using BuildSoft.OscCore; using Newtonsoft.Json.Linq; +using VRC.OSCQuery; +using Extensions = VRC.OSCQuery.Extensions; namespace VRC_Console; @@ -13,7 +16,6 @@ public class OSCCollar { var jObject = JObject.Parse(File.ReadAllText(configFilePath)); string ip = jObject.GetValue("ip")?.ToObject() ?? "127.0.0.1"; - int portReceive = jObject.GetValue("portReceive")?.ToObject() ?? 9001; int portSend = jObject.GetValue("portSend")?.ToObject() ?? 9000; double radius = jObject.GetValue("radius")?.ToObject() ?? 100; double calibrationX = jObject.GetValue("calibrationX")?.ToObject() ?? 0; @@ -21,7 +23,7 @@ public class OSCCollar double walkStretchDeadzone = jObject.GetValue("walkStretchDeadzone")?.ToObject() ?? 0.1; double runStretch = jObject.GetValue("runStretch")?.ToObject() ?? 0.4; bool skipSetup = jObject.GetValue("skipSetup")?.ToObject() ?? true; - var _ = new OSCCollar(ip, portReceive, portSend, radius, calibrationX, calibrationY, walkStretchDeadzone, runStretch, skipSetup); + var _ = new OSCCollar(ip, portSend, radius, calibrationX, calibrationY, walkStretchDeadzone, runStretch, skipSetup); }else { var _ = new OSCCollar(); @@ -31,6 +33,7 @@ public class OSCCollar private readonly string _ip; private readonly int _portReceive, _portSend; + private OSCQueryService _oscQueryService { get; init; } private OscServer Server { get; init; } private OscClient Client { get; init; } private float _leashStretch; @@ -57,10 +60,28 @@ public class OSCCollar private double GPSConstantD { get; init; } private double GPSConstantE { get; init; } - private OSCCollar(string ip = "127.0.0.1", int portReceive = 9001, 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._portReceive = portReceive; + 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._portSend = portSend; this._walkStretch = walkStretch; this._runStretch = runStretch; @@ -79,15 +100,17 @@ public class OSCCollar if (!skipSetup) { + 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}"); Console.WriteLine($"GPS 3 x: {GPS3.X} y: {GPS3.Y}"); Console.WriteLine($"Radius of each receiver (sphere): {radius * 2}"); + Console.ReadKey(); } - this.Server = new OscServer(portReceive); - this.Client = new OscClient(ip, portSend); + this.Server = new OscServer(this._portReceive); + this.Client = new OscClient(this._ip, this._portSend); this.Server.TryAddMethod("/avatar/parameters/GPS1", GPS1Handle); this.Server.TryAddMethod("/avatar/parameters/GPS2", GPS2Handle); this.Server.TryAddMethod("/avatar/parameters/GPS3", GPS3Handle); @@ -97,13 +120,15 @@ public class OSCCollar Thread runningThread = new Thread(RunningThread); runningThread.Start(); + + this._oscQueryService.RefreshServices(); } private void PrintOutput() { Console.Clear(); Console.ForegroundColor = ConsoleColor.White; - Console.WriteLine($"OSC Collar - Status: {(_allowMoving ? "enabled" : "disabled")} IP: {_ip} Send: {_portSend} Receive: {_portReceive}"); + Console.WriteLine($"OSC Collar - Status: {(_allowMoving ? "enabled" : "disabled")} IP: {_ip} Send: {_portSend} Receive: {_portReceive} HTTP: {this._oscQueryService.TcpPort}"); Console.WriteLine("=============================="); Console.WriteLine($"GPS 1:................{GPS1.Distance:00.00000}"); Console.WriteLine($"GPS 2:................{GPS2.Distance:00.00000}"); diff --git a/OSCCollar/OSCCollar.csproj b/OSCCollar/OSCCollar.csproj index 224f096..977234d 100644 --- a/OSCCollar/OSCCollar.csproj +++ b/OSCCollar/OSCCollar.csproj @@ -10,7 +10,15 @@ + + + + + ..\..\vrc-oscquery-lib\vrc-oscquery-lib\bin\Debug\net6.0\vrc-oscquery-lib.dll + + + diff --git a/VRC_Collar.sln.DotSettings.user b/VRC_Collar.sln.DotSettings.user new file mode 100644 index 0000000..a2ed6d6 --- /dev/null +++ b/VRC_Collar.sln.DotSettings.user @@ -0,0 +1,2 @@ + + True \ No newline at end of file