Add Open Shock Serial
This commit is contained in:
parent
d59543297a
commit
2bd2e5ba0a
@ -27,10 +27,11 @@ public struct Configuration
|
|||||||
internal static Configuration GetConfigurationFromFile(string? path = null, ILogger? logger = null)
|
internal static Configuration GetConfigurationFromFile(string? path = null, ILogger? logger = null)
|
||||||
{
|
{
|
||||||
string settingsFilePath = path ?? "config.json";
|
string settingsFilePath = path ?? "config.json";
|
||||||
|
Configuration c;
|
||||||
if (!File.Exists(settingsFilePath))
|
if (!File.Exists(settingsFilePath))
|
||||||
Setup.Run().SaveConfiguration();
|
c = Setup.Run().SaveConfiguration();
|
||||||
|
else
|
||||||
Configuration c = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText(settingsFilePath), new CShocker.Shockers.ShockerJsonConverter());
|
c = JsonConvert.DeserializeObject<Configuration>(File.ReadAllText(settingsFilePath), new CShocker.Shockers.ShockerJsonConverter());
|
||||||
if (!c.ConfigurationValid())
|
if (!c.ConfigurationValid())
|
||||||
throw new Exception("Configuration validation failed.");
|
throw new Exception("Configuration validation failed.");
|
||||||
foreach (Shocker cShocker in c.Shockers)
|
foreach (Shocker cShocker in c.Shockers)
|
||||||
@ -38,10 +39,11 @@ public struct Configuration
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SaveConfiguration(string? path = null)
|
internal Configuration SaveConfiguration(string? path = null)
|
||||||
{
|
{
|
||||||
string settingsFilePath = path ?? "config.json";
|
string settingsFilePath = path ?? "config.json";
|
||||||
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this, Formatting.Indented));
|
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(this, Formatting.Indented));
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool ConfigurationValid()
|
private bool ConfigurationValid()
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CS2GSI" Version="1.0.7" />
|
<PackageReference Include="CS2GSI" Version="1.0.7" />
|
||||||
<PackageReference Include="CShocker" Version="1.2.5" />
|
<PackageReference Include="CShocker" Version="1.3.6" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -105,12 +105,12 @@ public static class Setup
|
|||||||
{
|
{
|
||||||
Console.WriteLine("Select API:");
|
Console.WriteLine("Select API:");
|
||||||
Console.WriteLine("1) OpenShock (HTTP)");
|
Console.WriteLine("1) OpenShock (HTTP)");
|
||||||
Console.WriteLine("2) OpenShock (Serial) NotImplemented"); //TODO
|
Console.WriteLine("2) OpenShock (Serial)");
|
||||||
Console.WriteLine("3) PiShock (HTTP)");
|
Console.WriteLine("3) PiShock (HTTP)");
|
||||||
Console.WriteLine("4) PiShock (Serial) NotImplemented"); //TODO
|
Console.WriteLine("4) PiShock (Serial) NotImplemented"); //TODO
|
||||||
string? selectedChar = Console.ReadLine();
|
string? selectedChar = Console.ReadLine();
|
||||||
int selected;
|
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();
|
selectedChar = Console.ReadLine();
|
||||||
|
|
||||||
string apiUri, apiKey;
|
string apiUri, apiKey;
|
||||||
@ -128,6 +128,21 @@ public static class Setup
|
|||||||
newShocker = new OpenShockHttp(shockerIds, intensityRange, durationRange, apiKey, apiUri);
|
newShocker = new OpenShockHttp(shockerIds, intensityRange, durationRange, apiKey, apiUri);
|
||||||
newShocker.ShockerIds.AddRange(((OpenShockHttp)newShocker).GetShockers());
|
newShocker.ShockerIds.AddRange(((OpenShockHttp)newShocker).GetShockers());
|
||||||
break;
|
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<string, OpenShockSerial.ShockerModel>(), intensityRange,
|
||||||
|
durationRange, serialPort);
|
||||||
|
foreach (KeyValuePair<string, OpenShockSerial.ShockerModel> 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)
|
case 3: //PiShock (HTTP)
|
||||||
apiUri = QueryString("PiShock API-Endpoint (https://do.pishock.com/api/apioperate):", "https://do.pishock.com/api/apioperate");
|
apiUri = QueryString("PiShock API-Endpoint (https://do.pishock.com/api/apioperate):", "https://do.pishock.com/api/apioperate");
|
||||||
apiKey = QueryString("PiShock API-Key:","");
|
apiKey = QueryString("PiShock API-Key:","");
|
||||||
@ -141,7 +156,6 @@ public static class Setup
|
|||||||
newShocker = new PiShockHttp(shockerIds, intensityRange, durationRange, apiKey, username, shareCode, apiUri);
|
newShocker = new PiShockHttp(shockerIds, intensityRange, durationRange, apiKey, username, shareCode, apiUri);
|
||||||
break;
|
break;
|
||||||
// ReSharper disable thrice RedundantCaseLabel
|
// ReSharper disable thrice RedundantCaseLabel
|
||||||
case 2: //OpenShock (Serial)
|
|
||||||
case 4: //PiShock (Serial)
|
case 4: //PiShock (Serial)
|
||||||
default:
|
default:
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
@ -149,6 +163,25 @@ public static class Setup
|
|||||||
c.Shockers.Add(newShocker);
|
c.Shockers.Add(newShocker);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static SerialShocker.SerialPortInfo SelectSerialPort()
|
||||||
|
{
|
||||||
|
List<SerialShocker.SerialPortInfo> 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)
|
private static void AddAction(ref Configuration c)
|
||||||
{
|
{
|
||||||
CS2Event triggerEvent = GetTrigger();
|
CS2Event triggerEvent = GetTrigger();
|
||||||
@ -230,12 +263,13 @@ public static class Setup
|
|||||||
while (!int.TryParse(Console.ReadLine(), out selectedShocker) || selectedShocker < 0 || selectedShocker >= shockers.Count)
|
while (!int.TryParse(Console.ReadLine(), out selectedShocker) || selectedShocker < 0 || selectedShocker >= shockers.Count)
|
||||||
Console.WriteLine("Select Shocker API:");
|
Console.WriteLine("Select Shocker API:");
|
||||||
|
|
||||||
|
Console.WriteLine("Select Shocker:");
|
||||||
for (int i = 0; i < shockers[selectedShocker].ShockerIds.Count; i++)
|
for (int i = 0; i < shockers[selectedShocker].ShockerIds.Count; i++)
|
||||||
Console.WriteLine($"{i}) {shockers[selectedShocker].ShockerIds[i]}");
|
Console.WriteLine($"{i}) {shockers[selectedShocker].ShockerIds[i]}");
|
||||||
|
|
||||||
int selectedIndex;
|
int selectedIndex;
|
||||||
while (!int.TryParse(Console.ReadLine(), out selectedIndex) || selectedIndex < 0 || selectedIndex >= shockers[selectedShocker].ShockerIds.Count)
|
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]);
|
ids.Add(shockers[selectedShocker].ShockerIds[selectedIndex]);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user