Exemplo n.º 1
0
        /// <summary>
        /// Adds the device to the communication line, eliminating duplication of devices.
        /// </summary>
        private void AddDevice(DeviceLogic deviceLogic)
        {
            if (!deviceMap.ContainsKey(deviceLogic.DeviceNum))
            {
                DeviceWrapper deviceWrapper = new DeviceWrapper(deviceLogic, Log)
                {
                    InfoFileName = Path.Combine(coreLogic.AppDirs.LogDir,
                                                CommUtils.GetDeviceLogFileName(deviceLogic.DeviceNum, ".txt"))
                };

                devices.Add(deviceWrapper);
                deviceMap.Add(deviceLogic.DeviceNum, deviceWrapper);

                if (deviceLogic.NumAddress > 0)
                {
                    deviceByNumAddr[deviceLogic.NumAddress] = deviceLogic;
                }

                if (!string.IsNullOrEmpty(deviceLogic.StrAddress))
                {
                    deviceByStrAddr[deviceLogic.StrAddress] = deviceLogic;
                }

                if (maxDeviceTitleLength < deviceLogic.Title.Length)
                {
                    maxDeviceTitleLength = deviceLogic.Title.Length;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Removes, validates and returns the command at the beginning of the queue.
        /// </summary>
        private bool DequeueCommand(DateTime utcNow, out TeleCommand cmd, out DeviceWrapper deviceWrapper)
        {
            bool queueIsNotEmpty = true;
            bool commandIsValid  = false;

            cmd           = null;
            deviceWrapper = null;

            while (queueIsNotEmpty && !commandIsValid)
            {
                lock (commands)
                {
                    queueIsNotEmpty = commands.Count > 0;
                    cmd             = queueIsNotEmpty ? commands.Dequeue() : null;
                }

                if (cmd != null)
                {
                    if (cmd.CmdTypeID != CmdTypeID.Standard)
                    {
                        Log.WriteError(Locale.IsRussian ?
                                       "Команда с недопустимым типом {0}, отклонена" :
                                       "Command with invalid type {0} is rejected", cmd.CmdTypeID);
                    }
                    else if (utcNow - cmd.CreationTime > ScadaUtils.CommandLifetime)
                    {
                        Log.WriteError(Locale.IsRussian ?
                                       "Устаревшая команда для КП {0} отклонена" :
                                       "Outdated command to the device {0} is rejected", cmd.DeviceNum);
                    }
                    else if (!deviceMap.TryGetValue(cmd.DeviceNum, out deviceWrapper))
                    {
                        Log.WriteError(Locale.IsRussian ?
                                       "Команда с недопустимым КП {0}, отклонена" :
                                       "Command with invalid device {0} is rejected", cmd.DeviceNum);
                    }
                    else if (!deviceWrapper.DeviceLogic.CanSendCommands)
                    {
                        Log.WriteError(Locale.IsRussian ?
                                       "КП {0} не поддерживает отправку команд" :
                                       "The device {0} does not support sending commands");
                    }
                    else
                    {
                        commandIsValid = true;
                    }
                }
            }

            if (commandIsValid)
            {
                return(true);
            }
            else
            {
                cmd           = null;
                deviceWrapper = null;
                return(false);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds the device to the communication line.
        /// </summary>
        private void AddDevice(DeviceLogic deviceLogic)
        {
            if (!deviceMap.ContainsKey(deviceLogic.DeviceNum))
            {
                DeviceWrapper deviceWrapper = new DeviceWrapper(deviceLogic, Log)
                {
                    InfoFileName = Path.Combine(coreLogic.AppDirs.LogDir,
                                                CommUtils.GetDeviceLogFileName(deviceLogic.DeviceNum, ".txt"))
                };

                devices.Add(deviceWrapper);
                deviceMap.Add(deviceLogic.DeviceNum, deviceWrapper);

                if (maxDeviceTitleLength < deviceLogic.Title.Length)
                {
                    maxDeviceTitleLength = deviceLogic.Title.Length;
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Schedules a priority poll the device.
        /// </summary>
        private void PollWithPriority(DeviceWrapper deviceWrapper)
        {
            lock (priorityPoll)
            {
                // check if the device poll is already enqueued
                int  deviceNum   = deviceWrapper.DeviceLogic.DeviceNum;
                bool deviceFound = false;

                foreach (DeviceWrapper d in priorityPoll)
                {
                    if (d.DeviceLogic.DeviceNum == deviceNum)
                    {
                        deviceFound = true;
                        break;
                    }
                }

                // enqueue a poll
                if (!deviceFound)
                {
                    priorityPoll.Enqueue(deviceWrapper);
                }
            }
        }