コード例 #1
0
        public object GetValue(string name)
        {
            Log.Debug($"GetValue called on path {Path}");

            if (Path == null || !Path.Contains("."))
            {
                // If at the top, or at a server, the next part can't be a tag:
                string nextPath = (Path == null) ? name : $"{Path}.{name}";
                return(new OPCDataProvider {
                    Path = nextPath, Snapshot = this.Snapshot
                });
            }
            string[] pathSplit = Path.Split(new char[] { '.' }, 2); // {Server Name}.{remaining.tag.path}

            Folder folder = opcServerOrm.Fetch().Select(ext => ext.GetEntity()).FirstOrDefault(f => f.EntityName == pathSplit[0]) as Folder;

            if (folder == null)
            {
                throw new Exception($"Could not find server named {pathSplit[0]}");
            }

            string[]     eventIds      = FolderService.GetFolderEntities <OPCEvent>(folder.FolderID).Select(e => e.Id).ToArray();
            BaseTagValue mostRecentTag = null;

            IDictionary <string, BaseTagValue> sourceDict = ((Snapshot as IDictionary <string, BaseTagValue>) ?? OPCEngine.mostRecentValues);

            foreach (string eventId in eventIds)
            {
                string       key = $"{eventId}|{pathSplit[1]}.{name}";
                BaseTagValue tag;
                if (sourceDict.TryGetValue(key, out tag))
                {
                    if (mostRecentTag == null || mostRecentTag.TimeStamp < tag.TimeStamp)
                    {
                        mostRecentTag = tag;
                    }
                }
            }

            if (mostRecentTag != null)
            {
                return(TagValueUtils.GetObjectValueFromTag(mostRecentTag));
            }

            // No current value found, so check folders to see whether this is a folder or a tag:

            Folder tagDataFolder = folder.GetSubFolder().FirstOrDefault(x => x.EntityName == "Tag Data");

            if (tagDataFolder == null)
            {
                throw new Exception("Tag data folder not found");
            }

            string[] parts         = pathSplit[1].Split('.');
            Folder   currentFolder = tagDataFolder;

            for (int i = 0; i < parts.Length; ++i)
            { // descend for each part, arriving at the folder which should contain the named entity
                Folder nextFolder = currentFolder.GetSubFolder().FirstOrDefault(x => x.EntityName == parts[i]);
                if (nextFolder == null)
                {
                    throw new Exception($"Folder '{parts[i]}' not found in folder '{currentFolder?.EntityName}'.");
                }
                currentFolder = nextFolder;
            }

            Folder namedFolder = FolderService.GetFolderEntities <Folder>(currentFolder.FolderID).FirstOrDefault(x => x.EntityName == name);

            if (namedFolder != null)
            {
                return new OPCDataProvider {
                           Path = $"{this.Path}.{name}", Snapshot = this.Snapshot
                }
            }
            ;

            OPCTag namedTag = FolderService.GetFolderEntities <OPCTag>(currentFolder.FolderID).FirstOrDefault(x => x.EntityName == name);

            if (namedTag != null)
            {
                throw new Exception($"Tag '{name}' has no known value");
            }
            else
            {
                throw new Exception($"No tag or folder '{name}' found in '{currentFolder.EntityName}'");
            }
        }
コード例 #2
0
        private void SetValues(string opcServerUrl, string eventId, BaseTagValueWrapper valuesWrapper)
        {
            BaseTagValue[] values = valuesWrapper.Values;
            using (UserContextHolder.Register(new SystemUserContext()))
            {
                Folder configFolder = EntityCache <OPCServerFolderBehaviorData> .GetCache().AllEntities.FirstOrDefault(s => s.Url == opcServerUrl)?.GetEntity() as Folder;

                if (configFolder == null)
                {
                    return;
                }

                Array.ForEach(values, tagValue => { LOG.Debug($"Value Change: {tagValue.Path} - {TagValueUtils.GetObjectValueFromTag(tagValue)}"); });

                // put values in last cache
                foreach (var v in values)
                {
                    string       key = eventId + "|" + v.Path;
                    BaseTagValue priorValue;
                    OPCEngine.mostRecentValues.TryGetValue(key, out priorValue);

                    OPCEngine.mostRecentValues[key] = v;

                    if (priorValue == null)
                    {
                        OPCEngine.priorValues[key] = v;
                    }
                    else
                    {
                        OPCEngine.priorValues[key] = priorValue;
                    }
                }

                OPCEvent opcEvent = opcEventOrm.Fetch(eventId);
                if (opcEvent == null || opcEvent.Disabled)
                {
                    return;
                }

                bool runIt = false;
                // see if this event is interested in this change
                foreach (var v in opcEvent.EventValues)
                {
                    if (values.FirstOrDefault(changedValue => changedValue.Path == v.PathToValue) != null)
                    {
                        runIt = true;
                        break;
                    }
                }

                if (runIt)
                {
                    try
                    {
                        List <DataPair> inputs = new List <DataPair>();

                        foreach (var v in opcEvent.EventValues)
                        {
                            string       key   = eventId + "|" + v.PathToValue;
                            BaseTagValue value = null;

                            OPCEngine.mostRecentValues.TryGetValue(key, out value);

                            inputs.Add(new DataPair(v.Name, value));

                            BaseTagValue priorvalue = null;

                            OPCEngine.priorValues.TryGetValue(key, out priorvalue);

                            inputs.Add(new DataPair("Last " + v.Name, priorvalue));
                        }

                        inputs.Add(new DataPair("LastWorkflowRun", opcEvent.LastRun));

                        // check rule to see if it matches
                        var ruleResult = RuleEngine.RunRule(opcEvent.Rule, inputs.ToArray());

                        if (ruleResult != null && ruleResult is bool)
                        {
                            if (((bool)ruleResult) == true)
                            {
                                new Log("OPC").Error("Value Changed And Rule Returned True - running flow");
                                FlowEngine.Start(FlowEngine.LoadFlowByID(opcEvent.Flow, false, true),
                                                 new FlowStateData(inputs.ToArray()));
                            }
                            else
                            {
                                new Log("OPC").Error("Value Changed But Rule Returned False");
                            }
                        }
                        else
                        {
                            new Log("OPC").Error("Value Changed But Rule Returned False");
                        }
                    }
                    catch (Exception except)
                    {
                        new Log("OPC").Error(except, "Error running flow from event");
                    }
                }
            }
        }