Beispiel #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);
        }
Beispiel #2
0
        private ChannelWrapper channel;                                    // the communication channel


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        private CommLine(LineConfig lineConfig, CoreLogic coreLogic)
        {
            LineConfig      = lineConfig ?? throw new ArgumentNullException(nameof(lineConfig));
            this.coreLogic  = coreLogic ?? throw new ArgumentNullException(nameof(coreLogic));
            infoFileName    = Path.Combine(coreLogic.AppDirs.LogDir, CommUtils.GetLineLogFileName(CommLineNum, ".txt"));
            devices         = new List <DeviceWrapper>();
            deviceMap       = new Dictionary <int, DeviceWrapper>();
            deviceByNumAddr = new Dictionary <int, DeviceLogic>();
            deviceByStrAddr = new Dictionary <string, DeviceLogic>();
            commands        = new Queue <TeleCommand>();
            priorityPoll    = new Queue <DeviceWrapper>();

            thread               = null;
            terminated           = false;
            lineStatus           = ServiceStatus.Undefined;
            lastInfoLength       = 0;
            maxDeviceTitleLength = 0;
            channel              = null;

            Title      = CommUtils.GetLineTitle(CommLineNum, lineConfig.Name);
            SharedData = null;
            Log        = new LogFile(LogFormat.Simple)
            {
                FileName = Path.Combine(coreLogic.AppDirs.LogDir, CommUtils.GetLineLogFileName(CommLineNum, ".log")),
                Capacity = coreLogic.Config.GeneralOptions.MaxLogSize
            };
        }
Beispiel #3
0
        private CoreLogic coreLogic; // the Communicator logic instance


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public Manager()
        {
            log       = LogStub.Instance;
            coreLogic = null;
            AppDirs   = new CommDirs();
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
        }
Beispiel #4
0
        private volatile bool terminated;     // necessary to stop the thread

        public CommandReader(CoreLogic coreLogic)
        {
            this.coreLogic = coreLogic ?? throw new ArgumentNullException(nameof(coreLogic));
            cmdDir         = coreLogic.AppDirs.CmdDir;
            log            = coreLogic.Log;

            thread     = null;
            terminated = false;
        }
Beispiel #5
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);
        }
Beispiel #6
0
        /// <summary>
        /// Starts the service.
        /// </summary>
        public bool StartService()
        {
#if DEBUG
            System.Diagnostics.Debugger.Launch();
#endif

            string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            AppDirs.Init(exeDir);

            LogFile logFile = new LogFile(LogFormat.Full, Path.Combine(AppDirs.LogDir, CommUtils.LogFileName))
            {
                Capacity = int.MaxValue
            };

            log = logFile;
            log.WriteBreak();

            if (!Locale.LoadCulture(Path.Combine(exeDir, "..", "Config", InstanceConfig.DefaultFileName),
                                    out string errMsg))
            {
                log.WriteError(errMsg);
            }

            log.WriteAction(Locale.IsRussian ?
                            "Коммуникатор {0} запущен" :
                            "Communicator {0} started", CommUtils.AppVersion);

            if (AppDirs.CheckExistence(out errMsg))
            {
                LocalizeApp(AppDirs.LangDir);
                string     configFileName = Path.Combine(AppDirs.ConfigDir, CommConfig.DefaultFileName);
                CommConfig config         = new CommConfig();
                coreLogic = new CoreLogic(config, AppDirs, log);

                if (config.Load(configFileName, out errMsg) &&
                    coreLogic.StartProcessing())
                {
                    logFile.Capacity = config.GeneralOptions.MaxLogSize;
                    return(true);
                }
                else if (!string.IsNullOrEmpty(errMsg))
                {
                    log.WriteError(errMsg);
                }
            }
            else
            {
                log.WriteError(errMsg);
            }

            log.WriteError(CommonPhrases.ExecutionImpossible);
            return(false);
        }
Beispiel #7
0
        /// <summary>
        /// Starts the service.
        /// </summary>
        public bool StartService()
        {
#if DEBUG
            System.Diagnostics.Debugger.Launch();
#endif

            // load instance configuration
            AppDirs.Init(Assembly.GetExecutingAssembly());
            InstanceConfig instanceConfig = new InstanceConfig();
            Locale.SetCultureToEnglish();

            if (instanceConfig.Load(InstanceConfig.GetConfigFileName(AppDirs.InstanceDir), out string errMsg))
            {
                Locale.SetCulture(instanceConfig.Culture);
                AppDirs.UpdateLogDir(instanceConfig.LogDir);
            }
            else
            {
                Console.WriteLine(errMsg);
                Locale.SetCultureToDefault();
            }

            // initialize log
            LogFile logFile = new LogFile(LogFormat.Full, Path.Combine(AppDirs.LogDir, CommUtils.LogFileName))
            {
                Capacity = int.MaxValue
            };

            log = logFile;
            log.WriteBreak();

            // prepare to start service
            log.WriteAction(Locale.IsRussian ?
                            "Коммуникатор {0} запущен" :
                            "Communicator {0} started", EngineUtils.AppVersion);

            storageWrapper = new StorageWrapper(new StorageContext
            {
                App     = ServiceApp.Comm,
                AppDirs = AppDirs,
                Log     = log
            }, instanceConfig);

            CommConfig appConfig = new CommConfig();

            if (AppDirs.CheckExistence(out errMsg) &&
                LocalizeApp() &&
                storageWrapper.InitStorage() &&
                appConfig.Load(storageWrapper.Storage, CommConfig.DefaultFileName, out errMsg))
            {
                // start service
                logFile.CapacityMB = appConfig.GeneralOptions.MaxLogSize;
                coreLogic          = new CoreLogic(appConfig, AppDirs, storageWrapper.Storage, log);

                if (coreLogic.StartProcessing())
                {
                    return(true);
                }
            }
            else if (!string.IsNullOrEmpty(errMsg))
            {
                log.WriteError(errMsg);
            }

            log.WriteError(CommonPhrases.ExecutionImpossible);
            return(false);
        }
Beispiel #8
0
        private readonly CoreLogic coreLogic; // the Communicator logic instance


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public CommContext(CoreLogic coreLogic)
        {
            this.coreLogic = coreLogic ?? throw new ArgumentNullException(nameof(coreLogic));
        }