using HslCommunication.Core.Address;
using HslCommunication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.Profinet.Siemens;
using PVDEMCS.Common.DI;
using PVDEMCS.Common;

namespace PVDEMCS.Devices
{
    /// <summary>
    /// PLC通信接口
    /// </summary>
    public interface IPLCCommunicationService
    {
        /// <summary>
        /// 是否已连接
        /// </summary>
        bool IsConnected { get; }

        #region 同步
        /// <summary>
        /// 连接PLC（长连接）
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        Result Connection(string address, int port = 0);


        /// <summary>
        /// 关闭连接PLC（长连接）
        /// </summary>
        Result ColseConnection();

        /// <summary>
        /// 读取指定地址的byte[]值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Result<byte[]> ReadBytes(string address, ushort length);

        /// <summary>
        /// 读取指定地址的bool值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Result<bool> ReadBool(string address);

        /// <summary>
        /// 读取指定地址的byte值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Result<byte> ReadByte(string address);

        /// <summary>
        /// 读取指定地址的Int16值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Result<short> ReadInt16(string address);

        /// <summary>
        /// 读取指定地址的Int32值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Result<int> ReadInt32(string address);

        /// <summary>
        /// 读取指定地址的long值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Result<long> ReadLong(string address);

        /// <summary>
        /// 读取指定地址的Float值
        /// </summary>
        /// <returns></returns>
        Result<float> ReadFloat(string address);

        /// <summary>
        /// 读取指定地址的double值
        /// </summary>
        /// <returns></returns>
        Result<double> ReadDouble(string address);

        /// <summary>
        /// 写入bool值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Result Write(string address, bool value);

        /// <summary>
        /// 写入byte值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Result Write(string address, byte value);

        /// <summary>
        /// 写入Int16值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Result Write(string address, short value);

        /// <summary>
        /// 写入Int32值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Result Write(string address, int value);

        /// <summary>
        /// 写入float值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Result Write(string address, float value);

        /// <summary>
        /// 写入double值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Result Write(string address, double value);

        /// <summary>
        /// 写入long值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Result Write(string address, long value);

        #endregion

        #region 异步
        /// <summary>
        /// 连接PLC（长连接）
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        Task<Result> ConnectionAsync(string address, int port = 0);

        /// <summary>
        /// 关闭连接PLC（长连接）
        /// </summary>
        Task<Result> ColseConnectionAsyn();

        /// <summary>
        /// 读取指定地址的byte[]值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Task<Result<byte[]>> ReadBytesAsync(string address, ushort length);

        /// <summary>
        /// 读取指定地址的bool值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Task<Result<bool>> ReadBoolAsync(string address);

        /// <summary>
        /// 读取指定地址的byte值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Task<Result<byte>> ReadByteAsync(string address);

        /// <summary>
        /// 读取指定地址的Int16值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Task<Result<short>> ReadInt16Async(string address);

        /// <summary>
        /// 读取指定地址的Int32值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Task<Result<int>> ReadInt32Async(string address);

        /// <summary>
        /// 读取指定地址的long值
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        Task<Result<long>> ReadLongAsync(string address);

        /// <summary>
        /// 读取指定地址的Float值
        /// </summary>
        /// <returns></returns>
        Task<Result<float>> ReadFloatAsync(string address);

        /// <summary>
        /// 读取指定地址的double值
        /// </summary>
        /// <returns></returns>
        Task<Result<double>> ReadDoubleAsync(string address);

        /// <summary>
        /// 写入bool值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Task<Result> WriteAsync(string address, bool value);

        /// <summary>
        /// 写入byte值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Task<Result> WriteAsync(string address, byte value);

        /// <summary>
        /// 写入Int16值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Task<Result> WriteAsync(string address, short value);

        /// <summary>
        /// 写入Int32值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Task<Result> WriteAsync(string address, int value);

        /// <summary>
        /// 写入float值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Task<Result> WriteAsync(string address, float value);

        /// <summary>
        /// 写入double值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Task<Result> WriteAsync(string address, double value);

        /// <summary>
        /// 写入long值
        /// </summary>
        /// <param name="address">写入地址</param>
        /// <param name="value"></param>
        Task<Result> WriteAsync(string address, long value);

        #endregion
    }
}
