libplctag / libplctag.NET

A .NET wrapper for libplctag.
https://libplctag.github.io/
Mozilla Public License 2.0
197 stars 49 forks source link

Try to decouple NotifyValueChangedTag from application #334

Closed patopat closed 1 year ago

patopat commented 1 year ago

Hi,

From your example class NotifyValueChangedTag<M, T>, I would like decouple the Plc access and Plc client in order to facilitate the unit testing. I created PlcAccess.cs that implements an interface IPlcAccess, and PlcClient.cs

In PlcClient.cs, I declared :

public void SubscribeNotificationVar1(EventHandler<EventArgs> eventHandler) => _con.SubscribeNotificationChangedTag<MyTagPlcMapper, MyTagPlc>("Main.var1", eventHandler);

public void SubscribeNotificationVar2(EventHandler<EventArgs> eventHandler) => _con.SubscribeNotificationChangedTag<MyTagPlcMapper, MyTagPlc>("Main.var2", eventHandler);

In PlcAccess.cs, I declared :

        public void SubscribeNotificationChangedTag<M, T>(string name, EventHandler<EventArgs> eventHandler) where M : IPlcMapper<T>, new()
        {
            var valueChangedTag = new NotifyValueChangedTag<M, T>()
            {
                Name = name,
                Gateway = _gateway,
                Path = _path,
                PlcType = PlcType.ControlLogix,
                Protocol = Protocol.ab_eip,
                AutoSyncReadInterval = TimeSpan.FromMilliseconds(1000)
            };

            valueChangedTag.ValueChanged += eventHandler;

            valueChangedTag.Initialize();
        }

In IPlcAccess.cs :

    public interface IPlcAccess
    {
        public void SubscribeNotificationChangedTag<M, T>(string name, EventHandler<EventArgs> eventHandler) where M : IPlcMapper<T>, new();
    }

This doesn't work because the valueChangedTag is not saved, do you have an idea for an implementation that meets the decoupling ?

Regards, Pat