Add OpenShockSerial
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
using CShocker.Ranges;
|
||||
using System.Text.RegularExpressions;
|
||||
using CShocker.Ranges;
|
||||
using CShocker.Shockers.Abstract;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
@ -6,13 +7,57 @@ namespace CShocker.Shockers.APIS;
|
||||
|
||||
public class OpenShockSerial : SerialShocker
|
||||
{
|
||||
public OpenShockSerial(List<string> shockerIds, IntensityRange intensityRange, DurationRange durationRange, ILogger? logger = null) : base(shockerIds, intensityRange, durationRange, ShockerApi.OpenShockSerial, logger)
|
||||
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)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
this.Model = shockerIds;
|
||||
}
|
||||
|
||||
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
string json = "{" +
|
||||
$"\"model\":\"{Enum.GetName(Model[shockerId])!.ToLower()}\"," +
|
||||
$"\"id\":{shockerId}," +
|
||||
$"\"type\":\"{ControlActionToString(action)}\"," +
|
||||
$"\"intensity\":{intensity}," +
|
||||
$"\"durationMs\":{duration}" +
|
||||
"}";
|
||||
serialPort.WriteLine(json);
|
||||
}
|
||||
|
||||
public Dictionary<string, ShockerModel> GetShockers()
|
||||
{
|
||||
Dictionary<string, ShockerModel> 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"))
|
||||
{
|
||||
this.Logger?.Log(LogLevel.Trace, line);
|
||||
Match match = shockerRex.Match(line);
|
||||
if (match.Success)
|
||||
ret.Add(match.Groups[1].Value, Enum.Parse<ShockerModel>(match.Groups[2].Value));
|
||||
}
|
||||
this.Logger?.Log(LogLevel.Debug, $"Shockers found: \n\t{string.Join("\n\t", ret)}");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public enum ShockerModel : byte
|
||||
{
|
||||
Caixianlin = 0,
|
||||
Petrainer = 1
|
||||
}
|
||||
|
||||
private string ControlActionToString(ControlAction action)
|
||||
{
|
||||
return action switch
|
||||
{
|
||||
ControlAction.Beep => "sound",
|
||||
ControlAction.Vibrate => "vibrate",
|
||||
ControlAction.Shock => "shock",
|
||||
_ => "stop"
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user