using System;
using S7.Net;

namespace MES.Utility.Network.S7netplus.ORM
{
    public class S7Client : IDisposable
    {
        public S7Helper Plc;
        public bool IsConnected;
        public S7Client(CpuType cpu, string ip)
        {
            try
            {
                Plc = new S7Helper(cpu, ip);
                IsConnected = true;
            }
            catch (Exception ex)
            {
                IsConnected = false;
            }
        }

        public S7Read<T> Readable<T>() where T : class, new()
        {
            return new S7Read<T>(this);
        }

        public S7Write<T> Writeable<T>(T entity) where T : class, new()
        {
            return new S7Write<T>(this, entity);
        }

        public void Dispose()
        {
            try
            {
                if (Plc != null)
                {
                    Plc.Dispose();
                    Plc = null;
                }

            }
            catch { }
        }
    }
}