Пример #1
0
        private static UInt32 GetHistorianTagKey(string topic)
        {
            UInt32  returnValue;
            Boolean result;
            HistorianAccessError error;
            HistorianTag         settingsServerTag;
            HistorianTag         serverTag;
            UInt32 tagKey;

            returnValue = 0;

            try
            {
                try
                {
                    // Get the server tag from the in memory object
                    settingsServerTag = _HistorianTags.subscribetags.First(x => x.topic.Equals(topic)).historianTag;
                }
                catch
                {
                    // explicity set to null b/c we didn't find it
                    settingsServerTag = null;
                }

                // If we can't locate then try to process via regex
                if (settingsServerTag == null)
                {
                    // Get all entries where useregex is True
                    foreach (subscription localSubscription in _HistorianTags.subscribetags.Where(u => u.useregex).ToList())
                    {
                        //Attempt to match the tagName using the regex
                        Regex testReg = new Regex(localSubscription.regexmatch);
                        if (testReg.IsMatch(topic))
                        {
                            string newTagName = testReg.Replace(topic, localSubscription.historianTag.TagName);

                            // Iterate through the topic name replacements to make sure we don't pass through any bad characters
                            foreach (topicnamereplacement tnr in _HistorianTags.topicnamereplacements)
                            {
                                newTagName = newTagName.Replace(tnr.find, tnr.replace);
                            }

                            // Push this tag back into the tags list so we can cache this information for future processing
                            subscription newSubscription = new subscription();

                            // First copy the existing local subscription
                            newSubscription = ObjectExtension.CopyObject <subscription>(localSubscription);

                            // Replace key values with this specific tag's information
                            newSubscription.topic = topic;
                            newSubscription.historianTag.TagName = newTagName;
                            newSubscription.regexmatch           = "";
                            newSubscription.useregex             = false;

                            //Add the subscription back into the in-memory object
                            _HistorianTags.subscribetags.Add(newSubscription);

                            //And set the local settingsServerTag to the tag we just constructed so it can be used later in the process
                            settingsServerTag = newSubscription.historianTag;

                            // Add to the in-memory list so we don't have to repeat this
                            //_HistorianTags.subscribetags.Add(newSubscription);

                            // Now get the tag key back
                            //tagKey = GetHistorianTagKey(topic);

                            // exit the foreach
                            break;
                        }
                    }
                }


                // If we already have a key then use that
                if (settingsServerTag.TagKey > 0)
                {
                    return(settingsServerTag.TagKey);
                }

                // If we don't have a key then check to see if the tag already exists on the historian
                result = _historian.GetTagInfoByName(settingsServerTag.TagName, true, out serverTag, out error);

                if (!result)
                {
                    // Couldn't find tag so just keep going
                }

                // If the tag already exists, stick it back into the in memory object
                if (serverTag.TagKey > 0)
                {
                    try
                    {
                        // Set the data back into the in memory object
                        _HistorianTags.subscribetags.First(x => x.topic.Equals(topic)).historianTag = serverTag;
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex);
                    }
                }
                else
                {
                    // The tag didn't exists so load it up with the configuration data we specified externally
                    serverTag = settingsServerTag;
                }

                // Now "Add" to the historian which really just gives us a handle back
                log.DebugFormat("Adding {0} to historian.", serverTag.TagName);
                result = _historian.AddTag(serverTag, out tagKey, out error);

                // The actual status of the tag addition will be checked later when the application attempts to write a tag

                if (!result)
                {
                    throw new Exception(error.ErrorDescription);
                }

                try
                {
                    // Finally set the data on the in-memory object with the tag key
                    _HistorianTags.subscribetags.First(x => x.topic.Equals(topic)).historianTag.TagKey = tagKey;
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }

                //And set the return value
                returnValue = tagKey;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                returnValue = 0;
            }

            return(returnValue);
        }