Example #1
0
        /// <summary>
        /// Creates a communication line, communication channel and devices.
        /// </summary>
        public static CommLine Create(LineConfig lineConfig, CoreLogic coreLogic, DriverHolder driverHolder)
        {
            // create communication line
            CommLine commLine = new CommLine(lineConfig, coreLogic);

            // create communication channel
            if (string.IsNullOrEmpty(lineConfig.Channel.TypeName))
            {
                ChannelLogic channelLogic = new ChannelLogic(commLine, lineConfig.Channel);
                commLine.channel = new ChannelWrapper(channelLogic, commLine.Log);
            }
            else if (driverHolder.GetDriver(lineConfig.Channel.Driver, out DriverLogic driverLogic))
            {
                ChannelLogic channelLogic = driverLogic.CreateChannel(commLine, lineConfig.Channel);
                commLine.channel = new ChannelWrapper(channelLogic, commLine.Log);
            }
            else
            {
                throw new ScadaException(Locale.IsRussian ?
                                         "Драйвер для создания канала связи не найден." :
                                         "Driver for creating communication channel not found.");
            }

            // create devices
            foreach (DeviceConfig deviceConfig in lineConfig.DevicePolling)
            {
                if (driverHolder.GetDriver(deviceConfig.Driver, out DriverLogic driverLogic))
                {
                    DeviceLogic deviceLogic = driverLogic.CreateDevice(commLine, deviceConfig);
                    commLine.AddDevice(deviceLogic);
                }
            }

            return(commLine);
        }
Example #2
0
        /// <summary>
        /// Creates a communication line, communication channel and devices.
        /// </summary>
        public static CommLine Create(LineConfig lineConfig, CoreLogic coreLogic, DriverHolder driverHolder)
        {
            // create communication line
            CommLine commLine = new CommLine(lineConfig, coreLogic);

            // create communication channel
            if (string.IsNullOrEmpty(lineConfig.Channel.Driver))
            {
                ChannelLogic channelLogic = new ChannelLogic(commLine, lineConfig.Channel); // stub
                commLine.channel = new ChannelWrapper(channelLogic, commLine.Log);
            }
            else if (driverHolder.GetDriver(lineConfig.Channel.Driver, out DriverLogic driverLogic))
            {
                ChannelLogic channelLogic = driverLogic.CreateChannel(commLine, lineConfig.Channel);
                commLine.channel = new ChannelWrapper(channelLogic, commLine.Log);
            }
            else
            {
                throw new ScadaException(Locale.IsRussian ?
                                         "Драйвер канала связи {0} не найден." :
                                         "Communication channel driver {0} not found.", lineConfig.Channel.Driver);
            }

            // create devices
            foreach (DeviceConfig deviceConfig in lineConfig.DevicePolling)
            {
                if (deviceConfig.Active && !coreLogic.DeviceExists(deviceConfig.DeviceNum))
                {
                    if (driverHolder.GetDriver(deviceConfig.Driver, out DriverLogic driverLogic))
                    {
                        DeviceLogic deviceLogic = driverLogic.CreateDevice(commLine, deviceConfig);

                        if (deviceLogic == null)
                        {
                            throw new ScadaException(Locale.IsRussian ?
                                                     "Не удалось создать устройство {0}." :
                                                     "Unable to create device {0}.", deviceConfig.Title);
                        }

                        commLine.AddDevice(deviceLogic);
                    }
                    else
                    {
                        throw new ScadaException(Locale.IsRussian ?
                                                 "Драйвер {0} для устройства {1} не найден." :
                                                 "Driver {0} for device {1} not found.",
                                                 deviceConfig.Driver, deviceConfig.Title);
                    }
                }
            }

            // prepare channel after adding devices
            commLine.channel.ChannelLogic.MakeReady();

            return(commLine);
        }