From 1daf9a175e6cbaf02546ffd448b26ce270a7fbe1 Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 20 Jan 2024 21:15:26 +0100 Subject: [PATCH] Use Http-API to get RF-IDs for OpenShockSerial --- CShocker.sln.DotSettings | 3 +- CShocker/CShocker.csproj | 2 +- CShocker/Shockers/APIS/OpenShockSerial.cs | 64 +++++++++++++++++------ TestApp/Program.cs | 2 +- 4 files changed, 53 insertions(+), 18 deletions(-) diff --git a/CShocker.sln.DotSettings b/CShocker.sln.DotSettings index 2bde85d..49254b1 100644 --- a/CShocker.sln.DotSettings +++ b/CShocker.sln.DotSettings @@ -1,4 +1,5 @@  True True - True \ No newline at end of file + True + True \ No newline at end of file diff --git a/CShocker/CShocker.csproj b/CShocker/CShocker.csproj index 2383eb7..4e9ea4e 100644 --- a/CShocker/CShocker.csproj +++ b/CShocker/CShocker.csproj @@ -7,7 +7,7 @@ Glax https://github.com/C9Glax/CShocker git - 1.3.2 + 1.3.3 diff --git a/CShocker/Shockers/APIS/OpenShockSerial.cs b/CShocker/Shockers/APIS/OpenShockSerial.cs index 756fd4b..03b2b19 100644 --- a/CShocker/Shockers/APIS/OpenShockSerial.cs +++ b/CShocker/Shockers/APIS/OpenShockSerial.cs @@ -1,7 +1,8 @@ -using System.Text.RegularExpressions; +using System.Net.Http.Headers; using CShocker.Ranges; using CShocker.Shockers.Abstract; using Microsoft.Extensions.Logging; +using Newtonsoft.Json.Linq; namespace CShocker.Shockers.APIS; @@ -27,27 +28,60 @@ public class OpenShockSerial : SerialShocker SerialPort.WriteLine(json); } - public Dictionary GetShockers() + public Dictionary GetShockers(string apiEndpoint, string apiKey) { - Dictionary ret = new(); - Regex shockerRex = new (@".*FetchDeviceInfo\(\): \[GatewayConnectionManager\] \[[a-z0-9\-]+\] rf=([0-9]{1,5}) model=([0,1])"); - this.Logger?.Log(LogLevel.Debug, "Restart"); - SerialPort.WriteLine("restart"); - while (SerialPort.ReadLine() is { } line && !line.Contains("Successfully verified auth token")) + HttpClient httpClient = new(); + HttpRequestMessage requestDevices = new (HttpMethod.Get, $"{apiEndpoint}/2/devices") { - this.Logger?.Log(LogLevel.Trace, line); - Match match = shockerRex.Match(line); - if (match.Success) - ret.Add(match.Groups[1].Value, Enum.Parse(match.Groups[2].Value)); - } - this.Logger?.Log(LogLevel.Debug, $"Shockers found: \n\t{string.Join("\n\t", ret)}"); + Headers = + { + UserAgent = { new ProductInfoHeaderValue("CShocker", "1") }, + Accept = { new MediaTypeWithQualityHeaderValue("application/json") } + } + }; + requestDevices.Headers.Add("OpenShockToken", apiKey); + HttpResponseMessage responseDevices = httpClient.Send(requestDevices); + + StreamReader deviceStreamReader = new(responseDevices.Content.ReadAsStream()); + string deviceJson = deviceStreamReader.ReadToEnd(); + this.Logger?.Log(LogLevel.Debug, $"{requestDevices.RequestUri} response: {responseDevices.StatusCode}\n{deviceJson}"); + JObject deviceListJObj = JObject.Parse(deviceJson); + List deviceIds = new(); + deviceIds.AddRange(deviceListJObj["data"]!.Children()["id"].Values()!); - return ret; + Dictionary models = new(); + foreach (string deviceId in deviceIds) + { + HttpRequestMessage requestShockers = new (HttpMethod.Get, $"{apiEndpoint}/2/devices/{deviceId}/shockers") + { + Headers = + { + UserAgent = { new ProductInfoHeaderValue("CShocker", "1") }, + Accept = { new MediaTypeWithQualityHeaderValue("application/json") } + } + }; + requestShockers.Headers.Add("OpenShockToken", apiKey); + HttpResponseMessage response = httpClient.Send(requestShockers); + + StreamReader shockerStreamReader = new(response.Content.ReadAsStream()); + string shockerJson = shockerStreamReader.ReadToEnd(); + this.Logger?.Log(LogLevel.Debug, $"{requestShockers.RequestUri} response: {response.StatusCode}\n{shockerJson}"); + JObject shockerListJObj = JObject.Parse(shockerJson); + for (int i = 0; i < shockerListJObj["data"]!.Children().Count(); i++) + { + models.Add( + shockerListJObj["data"]![i]!["rfId"]!.Value().ToString(), + Enum.Parse(shockerListJObj["data"]![i]!["model"]!.Value()!) + ); + } + } + + return models; } public enum ShockerModel : byte { - Caixianlin = 0, + CaiXianlin = 0, Petrainer = 1 } diff --git a/TestApp/Program.cs b/TestApp/Program.cs index a444ceb..b54f884 100644 --- a/TestApp/Program.cs +++ b/TestApp/Program.cs @@ -25,5 +25,5 @@ while (!int.TryParse(selectedPortStr, out selectedPort) || selectedPort < 0 || s } OpenShockSerial shockSerial = new (new Dictionary(), new IntensityRange(30,50), new DurationRange(1000,1000), serialPorts[selectedPort], logger); -Dictionary shockers = shockSerial.GetShockers(); +Dictionary shockers = shockSerial.GetShockers("https://api.shocklink.net", "kTXWKQN6dTm3LHy62Pzulf20mh0JLQiLd0zwKJcqNa9MFM0bWXfnf1O3Gzmhgd5o"); Console.ReadKey(); \ No newline at end of file