From 2bd2e5ba0aad1fb3718ad3e9d4a75e60668367e2 Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 20 Jan 2024 21:41:47 +0100 Subject: [PATCH] Add Open Shock Serial --- OpenCS2hock/Configuration.cs | 10 ++++---- OpenCS2hock/OpenCS2hock.csproj | 2 +- OpenCS2hock/Setup.cs | 42 ++++++++++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/OpenCS2hock/Configuration.cs b/OpenCS2hock/Configuration.cs index faab969..9dfa7cf 100644 --- a/OpenCS2hock/Configuration.cs +++ b/OpenCS2hock/Configuration.cs @@ -27,10 +27,11 @@ public struct Configuration internal static Configuration GetConfigurationFromFile(string? path = null, ILogger? logger = null) { string settingsFilePath = path ?? "config.json"; + Configuration c; if (!File.Exists(settingsFilePath)) - Setup.Run().SaveConfiguration(); - - Configuration c = JsonConvert.DeserializeObject(File.ReadAllText(settingsFilePath), new CShocker.Shockers.ShockerJsonConverter()); + c = Setup.Run().SaveConfiguration(); + else + c = JsonConvert.DeserializeObject(File.ReadAllText(settingsFilePath), new CShocker.Shockers.ShockerJsonConverter()); if (!c.ConfigurationValid()) throw new Exception("Configuration validation failed."); foreach (Shocker cShocker in c.Shockers) @@ -38,10 +39,11 @@ public struct Configuration return c; } - internal void SaveConfiguration(string? path = null) + internal Configuration SaveConfiguration(string? path = null) { string settingsFilePath = path ?? "config.json"; File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this, Formatting.Indented)); + return this; } private bool ConfigurationValid() diff --git a/OpenCS2hock/OpenCS2hock.csproj b/OpenCS2hock/OpenCS2hock.csproj index a5b5a5a..3a47703 100644 --- a/OpenCS2hock/OpenCS2hock.csproj +++ b/OpenCS2hock/OpenCS2hock.csproj @@ -11,7 +11,7 @@ - + diff --git a/OpenCS2hock/Setup.cs b/OpenCS2hock/Setup.cs index 2cd49a5..57b2a35 100644 --- a/OpenCS2hock/Setup.cs +++ b/OpenCS2hock/Setup.cs @@ -105,12 +105,12 @@ public static class Setup { Console.WriteLine("Select API:"); Console.WriteLine("1) OpenShock (HTTP)"); - Console.WriteLine("2) OpenShock (Serial) NotImplemented"); //TODO + Console.WriteLine("2) OpenShock (Serial)"); Console.WriteLine("3) PiShock (HTTP)"); Console.WriteLine("4) PiShock (Serial) NotImplemented"); //TODO string? selectedChar = Console.ReadLine(); int selected; - while (!int.TryParse(selectedChar, out selected) || (selected != 1 && selected != 3)) + while (!int.TryParse(selectedChar, out selected) || selected < 1 || selected > 3) selectedChar = Console.ReadLine(); string apiUri, apiKey; @@ -128,6 +128,21 @@ public static class Setup newShocker = new OpenShockHttp(shockerIds, intensityRange, durationRange, apiKey, apiUri); newShocker.ShockerIds.AddRange(((OpenShockHttp)newShocker).GetShockers()); break; + case 2: //OpenShock (Serial) + apiUri = QueryString("OpenShock API-Endpoint (https://api.shocklink.net):", "https://api.shocklink.net"); + apiKey = QueryString("OpenShock API-Key:",""); + intensityRange = GetIntensityRange(); + durationRange = GetDurationRange(); + SerialShocker.SerialPortInfo serialPort = SelectSerialPort(); + newShocker = new OpenShockSerial(new Dictionary(), intensityRange, + durationRange, serialPort); + foreach (KeyValuePair kv in ((OpenShockSerial)newShocker) + .GetShockers(apiUri, apiKey)) + { + newShocker.ShockerIds.Add(kv.Key); + ((OpenShockSerial)newShocker).Model.Add(kv.Key, kv.Value); + } + break; case 3: //PiShock (HTTP) apiUri = QueryString("PiShock API-Endpoint (https://do.pishock.com/api/apioperate):", "https://do.pishock.com/api/apioperate"); apiKey = QueryString("PiShock API-Key:",""); @@ -141,7 +156,6 @@ public static class Setup newShocker = new PiShockHttp(shockerIds, intensityRange, durationRange, apiKey, username, shareCode, apiUri); break; // ReSharper disable thrice RedundantCaseLabel - case 2: //OpenShock (Serial) case 4: //PiShock (Serial) default: throw new NotImplementedException(); @@ -149,6 +163,25 @@ public static class Setup c.Shockers.Add(newShocker); } + private static SerialShocker.SerialPortInfo SelectSerialPort() + { + List serialPorts = SerialShocker.GetSerialPorts(); + + for(int i = 0; i < serialPorts.Count; i++) + Console.WriteLine($"{i}) {serialPorts[i]}"); + + Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:"); + string? selectedPortStr = Console.ReadLine(); + int selectedPort = -1; + while (!int.TryParse(selectedPortStr, out selectedPort) || selectedPort < 0 || selectedPort > serialPorts.Count - 1) + { + Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:"); + selectedPortStr = Console.ReadLine(); + } + + return serialPorts[selectedPort]; + } + private static void AddAction(ref Configuration c) { CS2Event triggerEvent = GetTrigger(); @@ -230,12 +263,13 @@ public static class Setup while (!int.TryParse(Console.ReadLine(), out selectedShocker) || selectedShocker < 0 || selectedShocker >= shockers.Count) Console.WriteLine("Select Shocker API:"); + Console.WriteLine("Select Shocker:"); for (int i = 0; i < shockers[selectedShocker].ShockerIds.Count; i++) Console.WriteLine($"{i}) {shockers[selectedShocker].ShockerIds[i]}"); int selectedIndex; while (!int.TryParse(Console.ReadLine(), out selectedIndex) || selectedIndex < 0 || selectedIndex >= shockers[selectedShocker].ShockerIds.Count) - Console.WriteLine("Select ID:"); + Console.WriteLine("Select Shocker:"); ids.Add(shockers[selectedShocker].ShockerIds[selectedIndex]);