using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Team.Communicate.Client;

namespace LogoForceTestApp.Services
{
    public class ScanService : IScanService
    {
        private TeamTcpClient _teamTcpClient;

        public event EventHandler<string> ScanCompleted;

        public ScanService()
        {

        }
        public async Task<bool> InitAsync(string ip, int port)
        {

            _teamTcpClient?.Dispose();
            _teamTcpClient = new TeamTcpClient(ip, port) { IsAutoConnect = true };
            return await _teamTcpClient.ConnectAsync();
        }


        public string Read(string command)
        {
            return _teamTcpClient.SendAndReceiveString(command);
        }

        public Task<string> ReadAsync(string command)
        {
            return _teamTcpClient.SendAndReceiveStringAsync(command);
        }

        public void Close()
        {
            try
            {
                _teamTcpClient?.DisConnect();
                _teamTcpClient?.Dispose();
            }
            catch (Exception)
            {

            }
        }
    }
}