Exemplo n.º 1
0
        protected ModbusPoll modbusPoll;   // implements device polling


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public DevModbusLogic(ICommContext commContext, ILineContext lineContext, DeviceConfig deviceConfig)
            : base(commContext, lineContext, deviceConfig)
        {
            transMode   = TransMode.RTU;
            deviceModel = null;
            modbusPoll  = null;
        }
Exemplo n.º 2
0
        private int lastInfoLength;       // the last info text length


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public DeviceLogic(ICommContext commContext, ILineContext lineContext, DeviceConfig deviceConfig)
        {
            CommContext  = commContext ?? throw new ArgumentNullException(nameof(commContext));
            LineContext  = lineContext ?? throw new ArgumentNullException(nameof(lineContext));
            DeviceConfig = deviceConfig ?? throw new ArgumentNullException(nameof(deviceConfig));
            AppDirs      = commContext.AppDirs;
            Log          = lineContext.LineConfig.LineOptions.DetailedLog ? lineContext.Log : LogStub.Instance;
            AssemblyName asmName = GetType().Assembly.GetName();

            DriverName         = ScadaUtils.RemoveFileNameSuffixes(asmName.Name) + " " + asmName.Version;
            LastRequestOK      = false;
            ReqRetries         = lineContext.LineConfig.LineOptions.ReqRetries;
            IsBound            = lineContext.LineConfig.IsBound && deviceConfig.IsBound;
            DeviceNum          = deviceConfig.DeviceNum;
            Title              = CommUtils.GetDeviceTitle(DeviceNum, deviceConfig.Name);
            NumAddress         = deviceConfig.NumAddress;
            StrAddress         = deviceConfig.StrAddress;
            PollingOptions     = deviceConfig.PollingOptions;
            CanSendCommands    = false;
            ConnectionRequired = false;
            DeviceStatus       = DeviceStatus.Undefined;
            LastSessionTime    = DateTime.MinValue;
            LastCommandTime    = DateTime.MinValue;
            DeviceTags         = new DeviceTags();
            DeviceData         = new DeviceData(deviceConfig.DeviceNum);
            DeviceStats        = new DeviceStats();

            terminated     = false;
            connection     = null;
            lastInfoLength = 0;
        }
Exemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DriverLogic(ICommContext commContext)
 {
     CommContext = commContext ?? throw new ArgumentNullException(nameof(commContext));
     AppDirs     = commContext.AppDirs;
     Storage     = commContext.Storage;
     Log         = commContext.Log;
 }
Exemplo n.º 4
0
        private Connection connection;    // the device connection


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public DeviceLogic(ICommContext commContext, ILineContext lineContext, DeviceConfig deviceConfig)
        {
            CommContext    = commContext ?? throw new ArgumentNullException(nameof(commContext));
            LineContext    = lineContext ?? throw new ArgumentNullException(nameof(lineContext));
            DeviceConfig   = deviceConfig ?? throw new ArgumentNullException(nameof(deviceConfig));
            AppDirs        = commContext.AppDirs;
            Log            = lineContext.LineConfig.LineOptions.DetailedLog ? lineContext.Log : new LogStub();
            LastRequestOK  = false;
            IsBound        = lineContext.LineConfig.IsBound && deviceConfig.IsBound;
            DeviceNum      = deviceConfig.DeviceNum;
            Title          = CommUtils.GetDeviceTitle(DeviceNum, deviceConfig.Name);
            NumAddress     = deviceConfig.NumAddress;
            StrAddress     = deviceConfig.StrAddress;
            PollingOptions = deviceConfig.PollingOptions;
            ReqRetries     = lineContext.LineConfig.LineOptions.ReqRetries;

            CanSendCommands    = false;
            ConnectionRequired = false;
            LastSessionTime    = DateTime.MinValue;
            LastCommandTime    = DateTime.MinValue;
            DeviceTags         = new DeviceTags();
            DeviceData         = new DeviceData();

            terminated = false;
            connection = null;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Construct a <see cref="SessionController"/>
        /// </summary>
        /// <param name="reattachInformation">The value of <see cref="reattachInformation"/></param>
        /// <param name="process">The value of <see cref="process"/></param>
        /// <param name="byondLock">The value of <see cref="byondLock"/></param>
        /// <param name="byondTopicSender">The value of <see cref="byondTopicSender"/></param>
        /// <param name="interopContext">The value of <see cref="interopContext"/></param>
        /// <param name="chat">The value of <see cref="chat"/></param>
        /// <param name="chatJsonTrackingContext">The value of <see cref="chatJsonTrackingContext"/></param>
        /// <param name="logger">The value of <see cref="logger"/></param>
        /// <param name="launchSecurityLevel">The value of <see cref="launchSecurityLevel"/></param>
        /// <param name="startupTimeout">The optional time to wait before failing the <see cref="LaunchResult"/></param>
        public SessionController(
            ReattachInformation reattachInformation,
            IProcess process,
            IByondExecutableLock byondLock,
            IByondTopicSender byondTopicSender,
            IJsonTrackingContext chatJsonTrackingContext,
            ICommContext interopContext,
            IChat chat,
            ILogger <SessionController> logger,
            DreamDaemonSecurity?launchSecurityLevel,
            uint?startupTimeout)
        {
            this.chatJsonTrackingContext = chatJsonTrackingContext;             // null valid
            this.reattachInformation     = reattachInformation ?? throw new ArgumentNullException(nameof(reattachInformation));
            this.byondTopicSender        = byondTopicSender ?? throw new ArgumentNullException(nameof(byondTopicSender));
            this.process        = process ?? throw new ArgumentNullException(nameof(process));
            this.byondLock      = byondLock ?? throw new ArgumentNullException(nameof(byondLock));
            this.interopContext = interopContext ?? throw new ArgumentNullException(nameof(interopContext));
            this.chat           = chat ?? throw new ArgumentNullException(nameof(chat));
            this.logger         = logger ?? throw new ArgumentNullException(nameof(logger));

            this.launchSecurityLevel = launchSecurityLevel;

            interopContext.RegisterHandler(this);

            portClosedForReboot = false;
            disposed            = false;
            apiValidationStatus = ApiValidationStatus.NeverValidated;
            released            = false;

            rebootTcs = new TaskCompletionSource <object>();

            process.Lifetime.ContinueWith(x => chatJsonTrackingContext.Active = false, TaskScheduler.Current);

            async Task <LaunchResult> GetLaunchResult()
            {
                var  startTime = DateTimeOffset.Now;
                Task toAwait   = process.Startup;

                if (startupTimeout.HasValue)
                {
                    toAwait = Task.WhenAny(process.Startup, Task.Delay(startTime.AddSeconds(startupTimeout.Value) - startTime));
                }

                await toAwait.ConfigureAwait(false);

                var result = new LaunchResult
                {
                    ExitCode    = process.Lifetime.IsCompleted ? (int?)await process.Lifetime.ConfigureAwait(false) : null,
                    StartupTime = process.Startup.IsCompleted ? (TimeSpan?)(DateTimeOffset.Now - startTime) : null
                };

                return(result);
            }

            LaunchResult = GetLaunchResult();

            logger.LogDebug("Created session controller. Primary: {0}, CommsKey: {1}, Port: {2}", IsPrimary, reattachInformation.AccessIdentifier, Port);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DataSourceLogic(ICommContext commContext, DataSourceConfig dataSourceConfig)
 {
     CommContext      = commContext ?? throw new ArgumentNullException(nameof(commContext));
     DataSourceConfig = dataSourceConfig ?? throw new ArgumentNullException(nameof(dataSourceConfig));
     Code             = dataSourceConfig.Code;
     Title            = CommUtils.GetDataSourceTitle(Code, dataSourceConfig.Name);
     IsReady          = false;
 }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public CustomServer(ICommContext commContext, OpcUaServerDSO options, ILog log)
        {
            this.commContext = commContext ?? throw new ArgumentNullException(nameof(commContext));
            this.options     = options ?? throw new ArgumentNullException(nameof(options));
            this.log         = log ?? throw new ArgumentNullException(nameof(log));

            certificateValidator = null;
            NodeManager          = null;
        }
Exemplo n.º 8
0
        private readonly TextStopCondition textStopCond; // the stop condition for reading text


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public DevTesterLogic(ICommContext commContext, ILineContext lineContext, DeviceConfig deviceConfig)
            : base(commContext, lineContext, deviceConfig)
        {
            options      = new TesterOptions(deviceConfig.PollingOptions.CustomOptions);
            inBuf        = new byte[options.BufferLength];
            binStopCond  = options.BinStopCode > 0 ? new BinStopCondition(options.BinStopCode) : null;
            textStopCond = string.IsNullOrEmpty(options.StopEnding) ?
                           TextStopCondition.OneLine : new TextStopCondition(options.StopEnding);

            CanSendCommands = true;
        }
Exemplo n.º 9
0
        private bool loggingFlag;                  // indicates that a ready message should be logged


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public DevEmailLogic(ICommContext commContext, ILineContext lineContext, DeviceConfig deviceConfig)
            : base(commContext, lineContext, deviceConfig)
        {
            CanSendCommands    = true;
            ConnectionRequired = false;

            config      = new EmailDeviceConfig();
            smtpClient  = new SmtpClient();
            addressBook = null;
            isReady     = false;
            loggingFlag = false;
        }
Exemplo n.º 10
0
        private bool loggingFlag;                  // indicates that a ready message should be logged


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public DevHttpNotifLogic(ICommContext commContext, ILineContext lineContext, DeviceConfig deviceConfig)
            : base(commContext, lineContext, deviceConfig)
        {
            CanSendCommands    = true;
            ConnectionRequired = false;

            stopwatch    = new Stopwatch();
            config       = new NotifDeviceConfig();
            addressBook  = null;
            paramUri     = null;
            paramContent = null;
            httpClient   = null;
            isReady      = false;
            loggingFlag  = false;
        }
Exemplo n.º 11
0
        private Dictionary <string, CommandConfig> cmdByCode;  // the commands accessed by code


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public DevOpcUaLogic(ICommContext commContext, ILineContext lineContext, DeviceConfig deviceConfig)
            : base(commContext, lineContext, deviceConfig)
        {
            opcLock          = new object();
            opcDeviceConfig  = null;
            connected        = false;
            connAttemptDT    = DateTime.MinValue;
            opcSession       = null;
            reconnectHandler = null;
            subscrByID       = null;
            cmdByNum         = null;
            cmdByCode        = null;

            CanSendCommands    = true;
            ConnectionRequired = false;
        }
Exemplo n.º 12
0
        private Dictionary <string, VarItem> varByPath;   // variables accessed by path


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public NodeManager(IServerInternal server, ApplicationConfiguration configuration,
                           ICommContext commContext, OpcUaServerDSO options, ILog log)
            : base(server, configuration, NamespaceUri)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            this.commContext = commContext ?? throw new ArgumentNullException(nameof(commContext));
            deviceFilter     = options.DeviceFilter.Count > 0 ? new HashSet <int>(options.DeviceFilter) : null;
            this.log         = log ?? throw new ArgumentNullException(nameof(log));
            dataLock         = new object();

            varByDevice = null;
            varByPath   = null;
        }
Exemplo n.º 13
0
        private int curDataSkipped;                                     // the number of skipped slices of current data


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public MqttDSL(ICommContext commContext, DataSourceConfig dataSourceConfig)
            : base(commContext, dataSourceConfig)
        {
            dsOptions        = new MqttDSO(dataSourceConfig.CustomOptions);
            dsLog            = CreateLog(DriverUtils.DriverCode);
            mqttClientHelper = new MqttClientHelper(dsOptions.ConnectionOptions, dsLog);
            commandTopic     = dsOptions.PublishOptions.RootTopic + CommandTopic;
            dataLifetime     = TimeSpan.FromSeconds(dsOptions.PublishOptions.DataLifetime);
            deviceFilter     = dsOptions.PublishOptions.DeviceFilter.Count > 0 ?
                               new HashSet <int>(dsOptions.PublishOptions.DeviceFilter) : null;
            maxQueueSize  = Math.Max(dsOptions.PublishOptions.MaxQueueSize, MinQueueSize);
            curDataQueue  = new Queue <QueueItem <DeviceSlice> >(maxQueueSize);
            topicByDevice = new Dictionary <int, DeviceTopics>();

            thread         = null;
            terminated     = false;
            curDataSkipped = 0;
        }
Exemplo n.º 14
0
        /// <summary>
        /// Construct a <see cref="SessionController"/>
        /// </summary>
        /// <param name="reattachInformation">The value of <see cref="reattachInformation"/></param>
        /// <param name="process">The value of <see cref="process"/></param>
        /// <param name="byondLock">The value of <see cref="byondLock"/></param>
        /// <param name="byondTopicSender">The value of <see cref="byondTopicSender"/></param>
        /// <param name="interopContext">The value of <see cref="interopContext"/></param>
        /// <param name="chat">The value of <see cref="chat"/></param>
        /// <param name="chatJsonTrackingContext">The value of <see cref="chatJsonTrackingContext"/></param>
        /// <param name="logger">The value of <see cref="logger"/></param>
        /// <param name="startupTimeout">The optional time to wait before failing the <see cref="LaunchResult"/></param>
        public SessionController(ReattachInformation reattachInformation, IProcess process, IByondExecutableLock byondLock, IByondTopicSender byondTopicSender, IJsonTrackingContext chatJsonTrackingContext, ICommContext interopContext, IChat chat, ILogger <SessionController> logger, uint?startupTimeout)
        {
            this.chatJsonTrackingContext = chatJsonTrackingContext;             //null valid
            this.reattachInformation     = reattachInformation ?? throw new ArgumentNullException(nameof(reattachInformation));
            this.byondTopicSender        = byondTopicSender ?? throw new ArgumentNullException(nameof(byondTopicSender));
            this.process        = process ?? throw new ArgumentNullException(nameof(process));
            this.byondLock      = byondLock ?? throw new ArgumentNullException(nameof(byondLock));
            this.interopContext = interopContext ?? throw new ArgumentNullException(nameof(interopContext));
            this.chat           = chat ?? throw new ArgumentNullException(nameof(chat));
            this.logger         = logger ?? throw new ArgumentNullException(nameof(logger));

            interopContext.RegisterHandler(this);

            portClosedForReboot = false;
            disposed            = false;
            apiValidated        = false;
            released            = false;

            rebootTcs = new TaskCompletionSource <object>();

            async Task <LaunchResult> GetLaunchResult()
            {
                var  startTime = DateTimeOffset.Now;
                Task toAwait   = process.Startup;

                if (startupTimeout.HasValue)
                {
                    toAwait = Task.WhenAny(process.Startup, Task.Delay(startTime.AddSeconds(startupTimeout.Value) - startTime));
                }

                await toAwait.ConfigureAwait(false);

                var result = new LaunchResult
                {
                    ExitCode    = process.Lifetime.IsCompleted ? (int?)await process.Lifetime.ConfigureAwait(false) : null,
                    StartupTime = process.Startup.IsCompleted ? (TimeSpan?)(DateTimeOffset.Now - startTime) : null
                };

                return(result);
            };
            LaunchResult = GetLaunchResult();
        }
Exemplo n.º 15
0
        private int eventSkipped;                                        // the number of skipped events


        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        public ScadaServerDSL(ICommContext commContext, DataSourceConfig dataSourceConfig)
            : base(commContext, dataSourceConfig)
        {
            options       = new ScadaServerDSO(dataSourceConfig.CustomOptions);
            maxCurDataAge = TimeSpan.FromSeconds(options.MaxCurDataAge);
            dataLifetime  = TimeSpan.FromSeconds(options.DataLifetime);
            deviceFilter  = options.DeviceFilter.Count > 0 ? new HashSet <int>(options.DeviceFilter) : null;
            log           = commContext.Log;

            maxQueueSize  = Math.Max(options.MaxQueueSize, MinQueueSize);
            curDataQueue  = new Queue <QueueItem <DeviceSlice> >(maxQueueSize);
            histDataQueue = new Queue <QueueItem <DeviceSlice> >(maxQueueSize);
            eventQueue    = new Queue <QueueItem <DeviceEvent> >(maxQueueSize);

            connOptions     = null;
            scadaClient     = null;
            thread          = null;
            terminated      = false;
            curDataSkipped  = 0;
            histDataSkipped = 0;
            eventSkipped    = 0;
        }
Exemplo n.º 16
0
        /// <summary>
        /// Gets a new instance of the driver logic.
        /// </summary>
        public static bool GetDriverLogic(string directory, string driverCode, ICommContext commContext,
                                          out DriverLogic driverLogic, out string message)
        {
            string fileName = Path.Combine(directory, driverCode + ".Logic.dll");
            string typeName = string.Format("Scada.Comm.Drivers.{0}.Logic.{0}Logic", driverCode);

            try
            {
                if (File.Exists(fileName))
                {
                    Assembly assembly = Assembly.LoadFile(fileName);
                    Type     type     = assembly.GetType(typeName, true);
                    driverLogic = (DriverLogic)Activator.CreateInstance(type, commContext);

                    message = string.Format(Locale.IsRussian ?
                                            "Загружен драйвер {0} {1} из файла {2}" :
                                            "Loaded driver {0} {1} from file {2}",
                                            driverCode, assembly.GetName().Version, fileName);
                    return(true);
                }
                else
                {
                    driverLogic = null;
                    message     = string.Format(Locale.IsRussian ?
                                                "Невозможно создать логику драйвера {0}. Файл {1} не найден" :
                                                "Unable to create driver logic {0}. File {1} not found", driverCode, fileName);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                driverLogic = null;
                message     = string.Format(Locale.IsRussian ?
                                            "Ошибка при создании логики драйвера {0} типа {1} из файла {2}: {3}" :
                                            "Error creating driver logic {0} of type {1} from file {2}: {3}",
                                            driverCode, typeName, fileName, ex.Message);
                return(false);
            }
        }
Exemplo n.º 17
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DrvOpcUaLogic(ICommContext commContext)
     : base(commContext)
 {
 }
Exemplo n.º 18
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DrvCnlMqttLogic(ICommContext commContext)
     : base(commContext)
 {
 }
Exemplo n.º 19
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DrvTesterLogic(ICommContext commContext)
     : base(commContext)
 {
 }
Exemplo n.º 20
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DrvModbusLogic(ICommContext commContext)
     : base(commContext)
 {
 }
Exemplo n.º 21
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DrvSimulatorLogic(ICommContext commContext)
     : base(commContext)
 {
 }
Exemplo n.º 22
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DrvDsOpcUaServerLogic(ICommContext commContext)
     : base(commContext)
 {
 }
Exemplo n.º 23
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DrvMqttPublisherLogic(ICommContext commContext)
     : base(commContext)
 {
 }
Exemplo n.º 24
0
 /// <summary>
 /// Creates a new data source.
 /// </summary>
 public virtual DataSourceLogic CreateDataSource(ICommContext commContext, DataSourceConfig dataSourceConfig)
 {
     return(null);
 }
Exemplo n.º 25
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DrvEmailLogic(ICommContext commContext)
     : base(commContext)
 {
 }
Exemplo n.º 26
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DrvCnlBasicLogic(ICommContext commContext)
     : base(commContext)
 {
 }
Exemplo n.º 27
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DrvDsScadaServerLogic(ICommContext commContext)
     : base(commContext)
 {
 }
Exemplo n.º 28
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public DevSimulatorLogic(ICommContext commContext, ILineContext lineContext, DeviceConfig deviceConfig)
     : base(commContext, lineContext, deviceConfig)
 {
     CanSendCommands = true;
 }
Exemplo n.º 29
0
 /// <summary>
 /// Initializes a new instance of the class.
 /// </summary>
 public OpcUaServerDSL(ICommContext commContext, DataSourceConfig dataSourceConfig)
     : base(commContext, dataSourceConfig)
 {
     options = new OpcUaServerDSO(dataSourceConfig.CustomOptions);
     dsLog   = CreateLog(DriverUtils.DriverCode);
 }
Exemplo n.º 30
0
 /// <summary>
 /// Creates a new data source.
 /// </summary>
 public override DataSourceLogic CreateDataSource(ICommContext commContext, DataSourceConfig dataSourceConfig)
 {
     return(new ScadaServerDSL(commContext, dataSourceConfig));
 }