public void PostResult(AgentInstructions instructions, AgentInstructionsResult r)
        {
            DataPair[] results = r.Data;
            if (results == null)
            {
                throw new Exception("Get Initial Data received no response data");
            }
            DataPair result = results.FirstOrDefault(x => x.Name == "initialData");

            if (result == null || !(result.OutputValue is OpcInitialData))
            {
                throw new Exception("Get Initial Data did not get the expected data");
            }
            OpcInitialData initialData = result.OutputValue as OpcInitialData;

            string url = instructions.Data.FirstOrDefault(d => d.Name == "opcServerUrl")?.OutputValue as string;

            if (string.IsNullOrEmpty(url))
            {
                throw new Exception("No URL found in agent instructions");
            }
            bool?valuesOnly = instructions.Data.FirstOrDefault(d => d.Name == "valuesOnly")?.OutputValue as bool?;

            if (valuesOnly == false && initialData.Values == null)
            {
                throw new Exception("No values found in agent instructions");
            }

            OPCEngine.HandleInitialData(url, initialData);
        }
        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>[] { }));
        }
        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());
        }
        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);
            }
        }
        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);
        }