示例#1
0
        public static void SetTagValues(string opcServerUrl, BaseTagValueWrapper valuesWrapper)
        {
            lock (GetLockObject(opcServerUrl))
            {
                OpcDaServer server = GetOrCreateServer(opcServerUrl);
                OpcDaGroup  group  = server.Groups.FirstOrDefault(x => x.Name == "DecisionsWriteGroup");
                if (group == null)
                {
                    group = server.AddGroup("DecisionsWriteGroup");
                }

                BaseTagValue[] values = valuesWrapper.Values;
                if (values == null || values.Length == 0)
                {
                    return;
                }

                List <OpcDaItemDefinition> missing = new List <OpcDaItemDefinition>();
                foreach (BaseTagValue value in values)
                {
                    OpcDaItem item = group.Items.FirstOrDefault(x => x.ItemId == value.Path);
                    if (item == null)
                    {
                        missing.Add(new OpcDaItemDefinition {
                            ItemId = value.Path
                        });
                    }
                } // ensure that tags are in group

                if (missing.Count > 0)
                {
                    OpcDaItemResult[] addResults = group.AddItems(missing);
                    foreach (OpcDaItemResult result in addResults)
                    {
                        if (result.Error.Failed)
                        {
                            throw new Exception($"Set tag value failed: could not add tag to group");
                        }
                    }
                }

                List <OpcDaItem> items      = new List <OpcDaItem>();
                List <object>    itemValues = new List <object>();
                foreach (BaseTagValue value in values)
                {
                    OpcDaItem item = group.Items.First(x => x.ItemId == value.Path); //throw if missing
                    items.Add(item);
                    itemValues.Add(OPCAgentUtils.GetObjectValueFromTag(value));
                }
                HRESULT[] writeResults = group.Write(items, itemValues.ToArray());
                foreach (HRESULT writeResult in writeResults)
                {
                    if (writeResult.Failed)
                    {
                        throw new Exception($"Set tag value failed: Error while writing values: {writeResult.ToString()}");
                    }
                }
            }
        }
示例#2
0
        private static BaseTagValue[] ConvertToBaseTagValues(OpcDaItemValue[] opcDaValues)
        {
            List <BaseTagValue> values = new List <BaseTagValue>();

            foreach (OpcDaItemValue valueChange in opcDaValues)
            {
                BaseTagValue value = OPCAgentUtils.GetTagWithValue(valueChange.Item.CanonicalDataType, valueChange.Value);
                value.Path    = valueChange.Item.ItemId;
                value.Quality = new OpcQuality
                {
                    Status    = (OpcQualityStatus)valueChange.Quality.Master,
                    SubStatus = (OpcQualitySubStatus)valueChange.Quality.Status,
                    Limit     = (OpcQualityLimit)valueChange.Quality.Limit
                };
                value.TimeStamp = valueChange.Timestamp.DateTime; //todo, make sure this is utc
                values.Add(value);
            }
            return(values.ToArray());
        }