void LoadDataSourceLocks(ISystemContext context, DsatsDemo.DataSource.DataSource datasource)
        {
            if (datasource == null || datasource.Lock == null)
            {
                return;
            }

            foreach (DsatsDemo.DataSource.LockType source in datasource.Lock)
            {
                DsatsDemo.LockConditionState node = datasource.ReadLock(context, source);

                node.Request.OnCallMethod2          = OnRequestLock;
                node.Release.OnCallMethod2          = OnReleaseLock;
                node.Approve.OnCallMethod2          = OnApproveLock;
                node.LockStateAsString.OnWriteValue = OnChangeLockByWrite;

                if (source.Permission != null)
                {
                    foreach (DsatsDemo.DataSource.CertificatePermissionType permission in source.Permission)
                    {
                        node.SetPermission(permission.Thumbprint);
                    }
                }

                node.AddNotifier(context, Opc.Ua.ReferenceTypeIds.HasEventSource, true, m_rig.Locks);
                m_rig.Locks.AddNotifier(context, Opc.Ua.ReferenceTypeIds.HasEventSource, false, node);

                AddPredefinedNode(context, node);
            }
        }
        /// <summary>
        /// Loads the datasource file.
        /// </summary>
        void LoadDataSource(ISystemContext context)
        {
            DsatsDemo.DataSource.DataSource datasource = null;

            string filePath = Utils.GetAbsoluteFilePath(m_configuration.DataSourceLocation, true, false, false);

            if (filePath != null)
            {
                try
                {
                    using (Stream istrm = File.OpenRead(filePath))
                    {
                        datasource = DsatsDemo.DataSource.DataSource.Read(istrm);
                    }
                }
                catch (Exception e)
                {
                    Utils.Trace(e, "Could not load the data source file. {0}", filePath);
                }
            }

            if (datasource == null)
            {
                return;
            }

            LoadDataSourcePhases(context, datasource);
            LoadDataSourceLocks(context, datasource);
            LoadDataSourceDeclarations(context, datasource);
        }
        void LoadDataSourcePhases(ISystemContext context, DsatsDemo.DataSource.DataSource datasource)
        {
            if (datasource == null || datasource.Phase == null)
            {
                return;
            }

            foreach (DsatsDemo.DataSource.PhaseType phase in datasource.Phase)
            {
                BaseObjectState node = datasource.ReadPhase(context, phase);
                node.AddReference(Opc.Ua.ReferenceTypeIds.Organizes, true, m_rig.Phases.NodeId);
                m_rig.Phases.AddReference(Opc.Ua.ReferenceTypeIds.Organizes, false, node.NodeId);
                AddPredefinedNode(context, node);

                m_rig.SetPhase(node.NodeId);

                if (m_currentPhase == null)
                {
                    m_currentPhase = node.NodeId;
                }
            }
        }
        void LoadDataSourceSources(
            ISystemContext context,
            ToolState tool,
            DsatsDemo.DataSource.DataSource datasource,
            DsatsDemo.DataSource.DeclarationType declaration)
        {
            if (declaration == null || declaration.Sources == null)
            {
                return;
            }

            // need to ensure the server's tables are not updated when loading this file.
            ServerSystemContext context2 = new ServerSystemContext(this.Server);

            context2.NamespaceUris = new NamespaceTable();
            context2.ServerUris    = new StringTable();
            context2.NodeIdFactory = this;

            context2.NamespaceUris.Append(context.ServerUris.GetString(0));
            context2.ServerUris.Append(context.ServerUris.GetString(0));

            foreach (DsatsDemo.DataSource.SourceType source in declaration.Sources)
            {
                BaseInstanceState child = tool.FindChildBySymbolicName(context, source.Path);

                if (child == null)
                {
                    continue;
                }

                if (source.DefaultValue != null)
                {
                    BaseVariableState variable = child as BaseVariableState;

                    if (variable != null)
                    {
                        try
                        {
                            Variant value = datasource.Read(context, source.DefaultValue);
                            variable.WrappedValue = value;
                        }
                        catch (Exception)
                        {
                            Utils.Trace("Could not read Variant in file. {0}", source.DefaultValue.InnerXml);
                        }
                    }
                }

                if (source.RemoteId != null)
                {
                    ExpandedNodeId remoteId = datasource.ReadExpandedNodeId(context2, source.RemoteId);

                    if (m_remoteNodes == null)
                    {
                        m_remoteNodes = new NodeIdDictionary <RemoteNode>();
                    }

                    RemoteNode remoteNode = new RemoteNode();

                    remoteNode.Tool      = tool;
                    remoteNode.ServerUrl = context2.ServerUris.GetString(remoteId.ServerIndex);
                    remoteNode.LocalNode = child;
                    remoteNode.RemoteId  = remoteId;

                    m_remoteNodes.Add(child.NodeId, remoteNode);
                }
            }
        }
        void LoadDataSourceDeclarations(ISystemContext context, DsatsDemo.DataSource.DataSource datasource)
        {
            if (datasource == null || datasource.Declaration == null)
            {
                return;
            }

            m_tools = new List <ToolState>();

            foreach (DsatsDemo.DataSource.DeclarationType declaration in datasource.Declaration)
            {
                BaseInstanceState parent = m_rig;

                if (!String.IsNullOrEmpty(declaration.Path))
                {
                    parent = m_rig.FindChildBySymbolicName(context, declaration.Path);
                }

                if (parent == null)
                {
                    Utils.Trace("Skipping declaration with unknown parent. {0}", declaration.Path);
                    continue;
                }

                NodeId            typeDefinitionId = datasource.ReadNodeId(context, declaration.TypeDefinitionId);
                BaseInstanceState child            = parent.FindChildBySymbolicName(context, declaration.BrowseName);

                if (child == null)
                {
                    uint?id = typeDefinitionId.Identifier as uint?;

                    if (id != null)
                    {
                        switch (id.Value)
                        {
                        case DsatsDemo.ObjectTypes.MudPumpType:
                        {
                            child = new MudPumpState(null);
                            break;
                        }
                        }

                        child.Create(
                            context,
                            new NodeId(declaration.Path + "/" + declaration.BrowseName, NamespaceIndex),
                            new QualifiedName(declaration.BrowseName, NamespaceIndex),
                            null,
                            true);

                        child.AddReference(Opc.Ua.ReferenceTypeIds.Organizes, true, parent.NodeId);
                        parent.AddReference(Opc.Ua.ReferenceTypeIds.Organizes, false, child.NodeId);

                        AddPredefinedNode(context, child);
                    }
                }

                ToolState tool = child as ToolState;

                if (tool != null)
                {
                    LoadDataSourceAccessRules(context, tool, declaration);
                    LoadDataSourceSources(context, tool, datasource, declaration);
                }

                m_tools.Add(tool);
            }
        }