Example #1
0
        /// <summary>
        /// Gets the channel prototypes for the device.
        /// </summary>
        public override ICollection <CnlPrototype> GetCnlPrototypes()
        {
            OpcDeviceConfig opcDeviceConfig = new();

            if (!opcDeviceConfig.Load(Path.Combine(AppDirs.ConfigDir, OpcDeviceConfig.GetFileName(DeviceNum)),
                                      out string errMsg))
            {
                throw new ScadaException(errMsg);
            }

            // create channels for subscriptions
            List <CnlPrototype> cnlPrototypes = new();
            int tagNum = 1;
            int eventMask = new EventMask {
                Enabled = true, StatusChange = true, Command = true
            }.Value;
            int cmdEventMask = new EventMask {
                Enabled = true, Command = true
            }.Value;

            foreach (SubscriptionConfig subscriptionConfig in opcDeviceConfig.Subscriptions)
            {
                foreach (ItemConfig itemConfig in subscriptionConfig.Items)
                {
                    CnlPrototype cnl = new()
                    {
                        Active    = itemConfig.Active,
                        Name      = itemConfig.DisplayName,
                        CnlTypeID = CnlTypeID.InputOutput,
                        TagNum    = string.IsNullOrEmpty(itemConfig.TagCode) ? tagNum : null,
                        TagCode   = itemConfig.TagCode,
                        EventMask = eventMask
                    };

                    if (itemConfig.IsString)
                    {
                        cnl.DataTypeID = DataTypeID.Unicode;
                        cnl.DataLen    = DriverUtils.GetTagDataLength(itemConfig.DataLength);
                        cnl.FormatCode = FormatCode.String;
                    }
                    else if (itemConfig.IsArray)
                    {
                        cnl.DataLen = itemConfig.DataLength;
                    }

                    if (DriverUtils.DataTypeEquals(itemConfig.DataTypeName, typeof(DateTime)))
                    {
                        cnl.FormatCode = FormatCode.DateTime;
                    }

                    cnlPrototypes.Add(cnl);
                    tagNum++;
                }
            }

            // create channels for commands
            foreach (CommandConfig commandConfig in opcDeviceConfig.Commands)
            {
                CnlPrototype cnl = new()
                {
                    Name      = commandConfig.DisplayName,
                    CnlTypeID = CnlTypeID.Output,
                    TagNum    = string.IsNullOrEmpty(commandConfig.CmdCode) ? commandConfig.CmdNum : null,
                    TagCode   = commandConfig.CmdCode,
                    EventMask = cmdEventMask
                };

                if (commandConfig.IsMethod)
                {
                    cnl.FormatCode = FormatCode.Execute;
                }
                else if (DriverUtils.DataTypeEquals(commandConfig.DataTypeName, typeof(string)))
                {
                    cnl.FormatCode = FormatCode.String;
                }
                else if (DriverUtils.DataTypeEquals(commandConfig.DataTypeName, typeof(DateTime)))
                {
                    cnl.FormatCode = FormatCode.DateTime;
                }

                cnlPrototypes.Add(cnl);
            }

            return(cnlPrototypes);
        }
    }
}