Exemplo n.º 1
0
        internal static void HandleInitialData(string url, OpcInitialData initialData)
        {
            log.Debug("HandleInitialData entered");
            OPCServerFolderBehaviorData folderExt = EntityCache <OPCServerFolderBehaviorData> .GetCache().AllEntities.FirstOrDefault(s => s.Url == url);

            Folder f = folderExt?.GetEntity() as Folder;

            if (f == null)
            {
                throw new Exception("No OPC server found with this URL");
            }

            if (initialData.Nodes != null)
            {
                UpdateTagsInFolder(f.FolderID + ".tagdata", f.FolderID, url, initialData.Nodes);
            }

            string[] eventIds = FolderService.GetFolderEntities <OPCEvent>(f.FolderID).Select(e => e.Id).ToArray();

            foreach (BaseTagValue value in initialData.Values)
            {
                // Record the value separately for each event, because they might update at different rates:
                foreach (string eventId in eventIds)
                {
                    string key = eventId + "|" + value.Path;
                    OPCEngine.mostRecentValues[key] = value;
                }
            }

            log.Debug("HandleInitialData complete");
        }
Exemplo n.º 2
0
        public override void AfterSave()
        {
            base.AfterSave();

            if (string.IsNullOrEmpty(EntityFolderID))
            {
                throw new Exception("EntityFolderID not found");
            }

            Folder folder = new ORM <Folder>().Fetch(EntityFolderID);
            OPCServerFolderBehaviorData ext = folder?.GetExtensionData <OPCServerFolderBehaviorData>();

            if (ext != null)
            {
                if (Disabled)
                {
                    OPCEngine.RemoveEventGroup(ext.Url, ext.AgentId, Id);
                }
                else
                {
                    OpcEventGroup eventGroup = new OpcEventGroup
                    {
                        Paths      = EventValues?.Select(y => y.PathToValue).ToArray() ?? new string[0],
                        EventId    = Id,
                        Deadband   = Deadband,
                        UpdateRate = UpdateRate
                    };
                    OPCEngine.AddOrUpdateEventGroup(ext.Url, ext.AgentId, eventGroup);
                }
            }
        }
        public ResultData Run(StepStartData data)
        {
            object value = data.Data[VALUE_INPUT];

            OPCTag tag;
            string serverUrl;

            if (chooseTagAtRuntime)
            {
                string tagPath = data.Data[TAG_INPUT] as string;
                serverUrl = data.Data[SERVER_URL_INPUT] as string;
                if (string.IsNullOrEmpty(tagPath))
                {
                    throw new Exception("No tag specified");
                }
                if (string.IsNullOrEmpty(serverUrl))
                {
                    throw new Exception("No server URL specified");
                }
                tag = EntityCache <OPCTag> .GetCache().AllEntities.FirstOrDefault(x => x.Path == tagPath && x.ServerUrl == serverUrl);

                if (tag == null)
                {
                    throw new Exception($"No tag found at '{tagPath}' on server '{serverUrl}'");
                }
            }
            else
            {
                tag = EntityCache <OPCTag> .GetCache().GetById(tagId);

                if (tag == null)
                {
                    throw new Exception($"Tag '{tagId}' not found");
                }
                serverUrl = tag.ServerUrl;
            }

            BaseTagValue tagValue = TagValueUtils.GetTagWithValue(TypeUtilities.FindTypeByFullName(tag.TypeName), value);

            tagValue.Path = tag.Path;

            OPCServerFolderBehaviorData folderExt = EntityCache <OPCServerFolderBehaviorData> .GetCache().AllEntities.FirstOrDefault(s => s.Url == serverUrl);

            Folder folder = folderExt?.GetEntity() as Folder;

            if (folder == null)
            {
                throw new Exception($"No server folder configured for URL '{serverUrl}'.");
            }

            OPCEngine.SetTagValues(serverUrl, folderExt.AgentId, new BaseTagValue[] { tagValue });

            return(new ResultData(PATH_DONE, new KeyValuePair <string, object>[] { }));
        }
Exemplo n.º 4
0
        public override BaseActionType[] GetFolderActions(Folder folder, BaseActionType[] proposedActions, EntityActionType[] types)
        {
            BaseActionType action = null;

            OPCServerFolderBehaviorData ext = folder.GetExtensionData <OPCServerFolderBehaviorData>();

            if (!OPCEngine.IsListening(ext.Url, ext.AgentId))
            {
                action = new ConfirmAction("Start Listening", null, "Start Listening", "Are You Sure?", new SimpleDelegate(() =>
                {
                    OPCServerFolderBehaviorData folderExt = folder.GetExtensionData <OPCServerFolderBehaviorData>();

                    var allEvents = Folder.GetEntitiesInFolder <OPCEvent>(folder.FolderID);

                    OpcEventGroup[] eventGroups = allEvents.Where(x => !x.Disabled).Select(x => new OpcEventGroup
                    {
                        Paths      = x.EventValues?.Select(y => y.PathToValue).ToArray() ?? new string[0],
                        EventId    = x.Id,
                        Deadband   = x.Deadband,
                        UpdateRate = x.UpdateRate
                    }).ToArray();

                    OPCEngine.StartListening(folderExt.Url, folderExt.AgentId, eventGroups);
                    OPCEngine.GetInitialData(folderExt.Url, folderExt.AgentId, true);
                }));
            }
            else
            {
                action = new ConfirmAction("Stop Listening", null, "Stop Listening", "Are You Sure?", new SimpleDelegate(() =>
                {
                    OPCServerFolderBehaviorData folderExt = folder.GetExtensionData <OPCServerFolderBehaviorData>();
                    OPCEngine.StopListening(folderExt.Url, folderExt.AgentId);
                }));
            }


            return(new BaseActionType[]
            {
                new AddEntityAction(typeof(OPCEvent), "Add Event", null, "Add OPC Event"),
                new SimpleAction("Rebuild Tag Cache", null, new SimpleDelegate(() =>
                {
                    RebuildTagCache(folder);
                }))
                {
                    DisplayType = ActionDisplayType.Secondary
                },
                action
            }.Concat(proposedActions.Where(a => a is NavigateToFolderAction || a.Name == "Delete Folder")).ToArray());
        }
Exemplo n.º 5
0
        public override void BeforeDelete()
        {
            base.BeforeDelete();

            if (string.IsNullOrEmpty(EntityFolderID))
            {
                return;
            }

            Folder folder = new ORM <Folder>().Fetch(EntityFolderID);
            OPCServerFolderBehaviorData ext = folder?.GetExtensionData <OPCServerFolderBehaviorData>();

            if (ext != null)
            {
                OPCEngine.RemoveEventGroup(ext.Url, ext.AgentId, Id);
            }
        }
Exemplo n.º 6
0
        private void RebuildTagCache(Folder f)
        {
            ORM <Folder> orm = new ORM <Folder>();

            if (orm.Fetch(f.FolderID + ".tagdata") == null)
            {
                Folder tagf = new Folder();
                tagf.EntityFolderID     = f.FolderID;
                tagf.EntityName         = "Tag Data";
                tagf.FolderBehaviorType = typeof(DefaultFolderBehavior).FullName;
                tagf.FolderID           = f.FolderID + ".tagdata";
                tagf.Store();
            }

            OPCServerFolderBehaviorData folderExt = f.GetExtensionData <OPCServerFolderBehaviorData>();
            var url = folderExt.Url;

            OPCEngine.GetInitialData(url, folderExt.AgentId, false);
        }
Exemplo n.º 7
0
        private void AddServer(AbstractUserContext usercontext, object addServerObject)
        {
            AddOPCServer addServer = (AddOPCServer)addServerObject;

            if (string.IsNullOrEmpty(addServer.Name))
            {
                throw new Exception("Name is required");
            }
            if (string.IsNullOrEmpty(addServer.Url))
            {
                throw new Exception("URL is required");
            }
            if (string.IsNullOrEmpty(addServer.AgentId))
            {
                throw new Exception("Agent is required");
            }

            Folder[] serverFolders = FolderService.Instance.GetSubFolders(usercontext, addServer.FolderId);
            foreach (Folder serverFolder in serverFolders)
            {
                OPCServerFolderBehaviorData extData = serverFolder.ExtensionData as OPCServerFolderBehaviorData;
                if (extData?.Url == addServer.Url)
                {
                    throw new Exception($"A server ({serverFolder.EntityName}) already exists with this URL ({addServer.Url}).");
                }
            }

            Folder f = new Folder(addServer.Name, addServer.FolderId);

            f.FolderBehaviorType = typeof(OPCServerFolderBehavior).FullName;
            f.ExtensionData      = new OPCServerFolderBehaviorData {
                Url = addServer.Url, AgentId = addServer.AgentId
            };
            f.Store();
            f.ExtensionData.Store();
        }