Files
LPR_Manager/LPR_Manager/Model/Device/LedController.cs
2026-02-03 11:33:58 +09:00

80 lines
2.3 KiB
C#

using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//using Inno.Communication.TCP;
using LPR_Manager.Model.Device.Interfaces;
using LPR_Manager.Model.Device.SubClass;
using LPR_Manager.Protocol.LED;
using LPR_Manager.Model;
namespace LPR_Manager.Model.Device
{
public class LedController : ILedController
{
// private readonly TcpClientConnector _connector = new();
private readonly string _ip;
private readonly int _port;
private readonly LEDProtocolType _protocolType;
private CancellationTokenSource? _cts;
private LEDTextSet _top = new();
private LEDTextSet _bottom = new();
public LedController(string ip, int port, LEDProtocolType protocolType = LEDProtocolType.YJMICRO)
{
_ip = ip;
_port = port;
_protocolType = protocolType;
}
// public bool IsConnected => _connector.IsConnected;
public async Task<bool> ConnectAsync()
{
try
{
_cts = new CancellationTokenSource();
// await _connector.ConnectAsync(_ip, _port, 2000, _cts.Token);
return true;
}
catch
{
return false;
}
}
public void Disconnect()
{
_cts?.Cancel();
// _connector.Dispose();
}
public void SetText(string top, string bottom, LEDColor topColor = LEDColor.GREEN, LEDColor bottomColor = LEDColor.YELLOW)
{
_top.Message = top;
_top.Color = topColor;
_bottom.Message = bottom;
_bottom.Color = bottomColor;
}
public void Send()
{
byte[] sendData;
if (_protocolType == LEDProtocolType.UJIN)
{
sendData = UJIN.GetMatrixMessage(_top, _bottom);
}
else
{
var cmd = YJMICRO.GetCommand(_top, _bottom);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
sendData = Encoding.GetEncoding(949).GetBytes(cmd);
}
if (sendData != null && sendData.Length > 0)
{
//_connector.WriteAsync(sendData, CancellationToken.None).Wait();
}
}
}
}