CShock/CShocker/Devices/APIs/PiShockSerial.cs

47 lines
1.5 KiB
C#
Raw Normal View History

using System.IO.Ports;
using CShocker.Devices.Abstract;
using CShocker.Devices.Additional;
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;
namespace CShocker.Devices.APIs;
2024-01-17 04:11:30 +01:00
public class PiShockSerial : PiShockApi
2024-01-17 04:11:30 +01:00
{
2024-01-20 20:02:12 +01:00
private const int BaudRate = 115200;
public SerialPortInfo SerialPortI;
private readonly SerialPort _serialPort;
public PiShockSerial(DeviceApi apiType, SerialPortInfo serialPortI, ILogger? logger = null) : base(apiType, logger)
2024-01-17 04:11:30 +01:00
{
this.SerialPortI = serialPortI;
this._serialPort = new SerialPort(this.SerialPortI.PortName, BaudRate);
2024-01-17 04:11:30 +01:00
throw new NotImplementedException();
}
protected override void ControlInternal(ControlAction action, Shocker shocker, int intensity, int duration)
2024-01-17 04:11:30 +01:00
{
2024-01-20 20:02:12 +01:00
string json = "{" +
"\"cmd\": \"operate\"," +
"\"value\":{" +
$"\"op\": \"{ControlActionToOp(action)}\"," +
$"\"duration\": {duration}," +
$"\"intensity\": {intensity}," +
$"\"id\": " +
"}" +
"}";
_serialPort.WriteLine(json);
2024-01-20 20:02:12 +01:00
}
private static string ControlActionToOp(ControlAction action)
{
return action switch
{
ControlAction.Beep => "",
ControlAction.Vibrate => "vibrate",
ControlAction.Shock => "",
_ => ""
};
2024-01-17 04:11:30 +01:00
}
}