using RTCommunicatorBase;
using TLVProtocal;
using UniversalNetFrame451.IO;

namespace Test2;

internal class Program
{
    static void Main(string[] args)
    {
        Test test = new();
        test.Initialize();
        test.Open("127.0.0.1", 50052);
        while (true)
        {
            byte mini8 = Convert.ToByte(Console.ReadLine());
            byte channel = Convert.ToByte(Console.ReadLine());

            test.Send(mini8,channel);
        }
    }
}

internal class Test : ITlvProvider
{
    private ITlvCommunicatorServer? _server;


    public bool Initialize()
    {
        if (this._server is not null)
            return false;

        this._server = TlvFactory.GetTlvServer();
        this._server.Initialize(this);
        return true;
    }

    public bool Open(string IP, ushort port)
    {
        if (this._server is null)
            return false;

        return this._server.Open(IP, port);

    }

    public void Received(TlvData data)
    {
    }

    public TlvData RequestReply(TlvData tlvData)
    {
        return default!;
    }

    public void Connected(TcpConnection connection)
    {
        Console.WriteLine("Connected");
    }

    public void Disconnected(TcpConnection connection)
    {
        Console.WriteLine("Disconnected");
    }

    public bool Send(byte mini8, byte channel)
    {
        ST_ALARM alarm = new()
        {
            Mini8Index = mini8,
            ChannelIndex = channel,
            PV = 100,
            Caps = 120,
            Floor = 80,
            AlarmType = AlarmType.CapsOverFlow,
        };
        return this._server?.Send(1, alarm) == true;
    }
}