65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
using RobotNet.Script;
|
|
|
|
namespace RobotNet.ScriptManager.Connections;
|
|
|
|
public class UnixDevice : IUnixDevice
|
|
{
|
|
public static readonly UnixDevice Instance = new();
|
|
|
|
private UnixDevice() { }
|
|
public byte[] ReadDev(string name, int length)
|
|
{
|
|
if (Environment.OSVersion.Platform != PlatformID.Unix)
|
|
{
|
|
throw new PlatformNotSupportedException("This method is only supported on linux.");
|
|
}
|
|
if (length <= 0)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(length), "Length must be greater than zero.");
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(name) || name.Any(c => Path.GetInvalidFileNameChars().Contains(c)))
|
|
{
|
|
throw new ArgumentException("Invalid device name.", nameof(name));
|
|
}
|
|
|
|
var path = $"/dev/robotnet/{name}";
|
|
if (!File.Exists(path))
|
|
{
|
|
throw new FileNotFoundException($"Device '{name}' not found.", path);
|
|
}
|
|
|
|
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
|
|
var buffer = new byte[length];
|
|
var bytesRead = fs.Read(buffer, 0, length);
|
|
fs.Close();
|
|
|
|
return buffer;
|
|
}
|
|
|
|
public void WriteDev(string name, byte[] data)
|
|
{
|
|
if (Environment.OSVersion.Platform != PlatformID.Unix)
|
|
{
|
|
throw new PlatformNotSupportedException("This method is only supported on linux.");
|
|
}
|
|
if (data == null || data.Length == 0)
|
|
{
|
|
throw new ArgumentNullException(nameof(data), "Data cannot be null or empty.");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(name) || name.Any(c => Path.GetInvalidFileNameChars().Contains(c)))
|
|
{
|
|
throw new ArgumentException("Invalid device name.", nameof(name));
|
|
}
|
|
var path = $"/dev/robotnet/{name}";
|
|
if (!File.Exists(path))
|
|
{
|
|
throw new FileNotFoundException($"Device '{name}' not found.", path);
|
|
}
|
|
using var fs = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.Read);
|
|
fs.Write(data, 0, data.Length);
|
|
fs.Close();
|
|
}
|
|
}
|