CShock/CShocker/Shockers/APIS/OpenShockSerial.cs

98 lines
4.1 KiB
C#
Raw Normal View History

using System.Net.Http.Headers;
2024-01-20 20:02:05 +01:00
using CShocker.Ranges;
2024-01-17 17:59:10 +01:00
using CShocker.Shockers.Abstract;
2024-01-17 04:11:30 +01:00
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
2024-01-17 04:11:30 +01:00
2024-01-17 22:16:40 +01:00
namespace CShocker.Shockers.APIS;
2024-01-17 04:11:30 +01:00
2024-01-17 04:36:38 +01:00
public class OpenShockSerial : SerialShocker
2024-01-17 04:11:30 +01:00
{
2024-01-20 20:25:31 +01:00
// ReSharper disable once MemberCanBePrivate.Global external usage
2024-01-20 20:02:05 +01:00
public readonly Dictionary<string, ShockerModel> Model;
private const int BaudRate = 115200;
public OpenShockSerial(Dictionary<string, ShockerModel> shockerIds, IntensityRange intensityRange, DurationRange durationRange, SerialPortInfo serialPortI, ILogger? logger = null) : base(shockerIds.Keys.ToList(), intensityRange, durationRange, serialPortI, BaudRate, ShockerApi.OpenShockSerial, logger)
2024-01-17 04:11:30 +01:00
{
2024-01-20 20:02:05 +01:00
this.Model = shockerIds;
2024-01-17 04:11:30 +01:00
}
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{
2024-01-20 20:19:25 +01:00
string json = "rftransmit {" +
2024-01-20 20:02:05 +01:00
$"\"model\":\"{Enum.GetName(Model[shockerId])!.ToLower()}\"," +
$"\"id\":{shockerId}," +
$"\"type\":\"{ControlActionToString(action)}\"," +
$"\"intensity\":{intensity}," +
$"\"durationMs\":{duration}" +
"}";
2024-01-20 20:25:31 +01:00
SerialPort.WriteLine(json);
2024-01-20 20:02:05 +01:00
}
public Dictionary<string, ShockerModel> GetShockers(string apiEndpoint, string apiKey)
2024-01-20 20:02:05 +01:00
{
HttpClient httpClient = new();
HttpRequestMessage requestDevices = new (HttpMethod.Get, $"{apiEndpoint}/2/devices")
2024-01-20 20:02:05 +01:00
{
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<string> deviceIds = new();
deviceIds.AddRange(deviceListJObj["data"]!.Children()["id"].Values<string>()!);
Dictionary<string, ShockerModel> 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<int>().ToString(),
Enum.Parse<ShockerModel>(shockerListJObj["data"]![i]!["model"]!.Value<string>()!)
);
}
2024-01-20 20:02:05 +01:00
}
return models;
2024-01-20 20:02:05 +01:00
}
public enum ShockerModel : byte
{
CaiXianlin = 0,
2024-01-20 20:02:05 +01:00
Petrainer = 1
}
2024-01-20 20:25:31 +01:00
private static string ControlActionToString(ControlAction action)
2024-01-20 20:02:05 +01:00
{
return action switch
{
ControlAction.Beep => "sound",
ControlAction.Vibrate => "vibrate",
ControlAction.Shock => "shock",
_ => "stop"
};
2024-01-17 04:11:30 +01:00
}
}