Beispiel #1
0
        public void Update_Device_Tags()
        {
            // prepare
            var dvConfig = new DeviceConfig
            {
                Name   = "plc01",
                Driver = "s7",
                Config = "host=127.0.0.1;rack=0;slot=0",
                Tags   = (new DeviceTagConfig[]
                {
                    new DeviceTagConfig
                    {
                        Name = "tag1",
                        TypeCode = TypeCode.Int32,
                        Mode = Modes.FromOTM, // write to plc
                        Address = "db801.dw0",
                        Rate = 50
                    },
                    new DeviceTagConfig
                    {
                        Name = "tag2",
                        TypeCode = TypeCode.Int32,
                        Mode = Modes.ToOTM, // read from plc
                        Address = "db800.dw12",
                        Rate = 50
                    }
                }).ToList()
            };

            var  buffer = new byte[13];
            byte dw10   = 99;

            var clientMock = new Mock <IS7Client>();

            // on the first call conected will be False
            clientMock.Setup(x => x.Connected).Returns(true);

            clientMock.Setup(x => x.ConnectTo("127.0.01", 0, 0))
            .Returns(0);

            clientMock.Setup(x => x.ErrorText(0)).Returns("No error");
            clientMock.Setup(x => x.DBRead(800, 0, 16, It.IsAny <byte[]>()))
            .Callback <int, int, int, byte[]>((dbNumber, start, lenght, buf) =>
            {
                // change second byte of dw13
                buf[13] = dw10;
            });
            clientMock.Setup(x => x.DBRead(801, 0, 4, It.IsAny <byte[]>()))
            .Callback <int, int, int, byte[]>((dbNumber, start, lenght, buf) =>
            {
                // change second byte of dw13
                //buf[13] = dw10;
            });

            var loggerMock = new Mock <ILogger>();
            var devPlc01   = new S7Device();

            devPlc01.Init(dvConfig, clientMock.Object, loggerMock.Object);
            devPlc01.ReadDeviceTags();

            var tag2 = -1;

            // update tag value to 0
            dw10 = 1;
            // assign a function to be executed when tag value is changed
            devPlc01.OnTagChangeAdd("tag2", (str, value) =>
            {
                tag2 = (int)value;
            });

            devPlc01.ReadDeviceTags();

            Assert.Equal(1, tag2);

            // update tag value to 1
            dw10 = 2;

            devPlc01.ReadDeviceTags();

            Assert.Equal(2, tag2);
        }
Beispiel #2
0
        public static IDictionary <string, IDevice> CreateDevices(List <DeviceConfig> devicesConfig, ILogger logger)
        {
            var devices = new Dictionary <string, IDevice>();

            if (devicesConfig != null)
            {
                foreach (var dvConfig in devicesConfig)
                {
                    if (string.IsNullOrWhiteSpace(dvConfig.Name))
                    {
                        var ex = new Exception("Invalid Device name in config. Name:" + dvConfig.Name);
                        ex.Data.Add("field", "Name");
                        throw ex;
                    }

                    logger.LogDebug($"Device {dvConfig?.Name}: Intializing with driver '{dvConfig.Driver}'");

                    switch (dvConfig.Driver)
                    {
                    case "s7":
                        var client = new S7Client()
                        {
                            PduSizeRequested = 960
                        };
                        var s7Device = new S7Device();
                        s7Device.Init(dvConfig, client, logger);
                        devices.Add(dvConfig.Name, s7Device);
                        logger.LogDebug($"Device {dvConfig?.Name}: Created");
                        break;

                    case "ptl":
                        var ptlDevice = new PtlDevice();
                        ptlDevice.Init(dvConfig, logger);
                        devices.Add(dvConfig.Name, ptlDevice);
                        logger.LogDebug($"Device {dvConfig?.Name}: Created");
                        break;

                    case "RabbitMq":
                        var rabbitMqDevice = new RabbitMqDevice();
                        rabbitMqDevice.Init(dvConfig, logger);
                        devices.Add(dvConfig.Name, rabbitMqDevice);
                        logger.LogDebug($"Device {dvConfig?.Name}: Created");
                        break;

                    default:
                        try
                        {
                            var pluginsNames = PluginLoadContext.GetDevicePlugins(logger);
                            var pluginName   = pluginsNames.Single(x => x.Name == dvConfig.Driver);
                            var device       = PluginLoadContext.LoadAndCreateDevicePlugin(pluginName.FileName, logger);
                            device.Init(dvConfig, logger);
                            devices.Add(dvConfig.Name, device);
                        }
                        catch (Exception ex)
                        {
                            var ex2 = new Exception("Invalid DeviceDriver in config. Driver:" + dvConfig.Driver, ex);
                            ex2.Data.Add("field", "Driver");
                            throw ex2;
                        }
                        break;
                    }
                }
            }

            return(devices);
        }