コード例 #1
0
 /// <summary>
 /// Convert back to service model
 /// </summary>
 /// <returns></returns>
 public static NodeModel ToServiceModel(
     this NodeApiModel model)
 {
     if (model == null)
     {
         return(null);
     }
     return(new NodeModel {
         NodeId = model.NodeId,
         Children = model.Children,
         BrowseName = model.BrowseName,
         DisplayName = model.DisplayName,
         Description = model.Description,
         NodeClass = (IIoT.OpcUa.Core.Models.NodeClass?)model.NodeClass,
         IsAbstract = model.IsAbstract,
         AccessLevel = (IIoT.OpcUa.Core.Models.NodeAccessLevel?)model.AccessLevel,
         EventNotifier = (IIoT.OpcUa.Core.Models.NodeEventNotifier?)model.EventNotifier,
         Executable = model.Executable,
         DataType = model.DataType,
         ValueRank = (IIoT.OpcUa.Core.Models.NodeValueRank?)model.ValueRank,
         AccessRestrictions = (IIoT.OpcUa.Core.Models.NodeAccessRestrictions?)model.AccessRestrictions,
         ArrayDimensions = model.ArrayDimensions,
         ContainsNoLoops = model.ContainsNoLoops,
         DataTypeDefinition = model.DataTypeDefinition,
         Value = model.Value,
         Historizing = model.Historizing,
         InverseName = model.InverseName,
         MinimumSamplingInterval = model.MinimumSamplingInterval,
         Symmetric = model.Symmetric,
         UserAccessLevel = (IIoT.OpcUa.Core.Models.NodeAccessLevel?)model.UserAccessLevel,
         UserExecutable = model.UserExecutable,
         UserWriteMask = model.UserWriteMask,
         WriteMask = model.WriteMask,
         RolePermissions = model.RolePermissions?
                           .Select(p => p.ToServiceModel())
                           .ToList(),
         UserRolePermissions = model.UserRolePermissions?
                               .Select(p => p.ToServiceModel())
                               .ToList(),
         TypeDefinitionId = model.TypeDefinitionId
     });
 }
コード例 #2
0
        public async Task <ActionResult> GetChildren(string jstreeNode)
        {
            // This delimiter is used to allow the storing of the OPC UA parent node ID together with the OPC UA child node ID in jstree data structures and provide it as parameter to
            // Ajax calls.
            string[] delimiter       = { "__$__" };
            string[] jstreeNodeSplit = jstreeNode.Split(delimiter, 3, StringSplitOptions.None);
            string   node;

            if (jstreeNodeSplit.Length == 1)
            {
                node = jstreeNodeSplit[0];
            }
            else
            {
                node = jstreeNodeSplit[1];
            }

            List <object> jsonTree    = new List <object>();
            string        endpointId  = Session["EndpointId"].ToString();
            string        endpointUrl = Session["EndpointUrl"].ToString();
            string        ProductUri  = Session["ProductUri"].ToString();

            // read the currently published nodes
            PublishedItemListResponseApiModel publishedNodes = new PublishedItemListResponseApiModel();

            try
            {
                PublishedItemListRequestApiModel publishModel = new PublishedItemListRequestApiModel();
                publishedNodes = await TwinService.GetPublishedNodesAsync(endpointId, publishModel);
            }
            catch (Exception e)
            {
                // do nothing, since we still want to show the tree
                Trace.TraceWarning("Can not read published nodes for endpoint '{0}'.", endpointUrl);
                string errorMessage = string.Format(Strings.BrowserOpcException, e.Message,
                                                    e.InnerException?.Message ?? "--", e?.StackTrace ?? "--");
                Trace.TraceWarning(errorMessage);
            }

            BrowseResponseApiModel browseData = new BrowseResponseApiModel();

            try
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                try
                {
                    BrowseRequestApiModel model = new BrowseRequestApiModel();
                    model.NodeId          = node;
                    model.TargetNodesOnly = false;

                    browseData = TwinService.NodeBrowse(endpointId, model);
                }
                catch (Exception e)
                {
                    // skip this node
                    Trace.TraceError("Can not browse node '{0}'", node);
                    string errorMessage = string.Format(Strings.BrowserOpcException, e.Message,
                                                        e.InnerException?.Message ?? "--", e?.StackTrace ?? "--");
                    Trace.TraceError(errorMessage);
                }

                Trace.TraceInformation("Browsing node '{0}' data took {0} ms", node.ToString(), stopwatch.ElapsedMilliseconds);

                if (browseData.References != null)
                {
                    var idList = new List <string>();
                    foreach (var nodeReference in browseData.References)
                    {
                        bool idFound = false;
                        foreach (var id in idList)
                        {
                            if (id == nodeReference.Target.NodeId.ToString())
                            {
                                idFound = true;
                            }
                        }
                        if (idFound == true)
                        {
                            continue;
                        }

                        Trace.TraceInformation("Browse '{0}' count: {1}", nodeReference.Target.NodeId, jsonTree.Count);

                        NodeApiModel currentNode = nodeReference.Target;

                        currentNode = nodeReference.Target;

                        byte currentNodeAccessLevel   = 0;
                        byte currentNodeEventNotifier = 0;
                        bool currentNodeExecutable    = false;

                        switch (currentNode.NodeClass)
                        {
                        case NodeClass.Variable:
                            currentNodeAccessLevel = (byte)currentNode.UserAccessLevel;
                            if (!PermsChecker.HasPermission(Permission.ControlOpcServer))
                            {
                                currentNodeAccessLevel = (byte)((uint)currentNodeAccessLevel & ~0x2);
                            }
                            break;

                        case NodeClass.Object:
                            currentNodeEventNotifier = (byte)currentNode.EventNotifier;
                            break;

                        case NodeClass.View:
                            currentNodeEventNotifier = (byte)currentNode.EventNotifier;
                            break;

                        case NodeClass.Method:
                            if (PermsChecker.HasPermission(Permission.ControlOpcServer))
                            {
                                currentNodeExecutable = (bool)currentNode.UserExecutable;
                            }
                            break;

                        default:
                            break;
                        }

                        var isPublished = false;
                        var isRelevant  = false;
                        if (publishedNodes.Items != null)
                        {
                            foreach (var item in publishedNodes.Items)
                            {
                                if (item.NodeId == nodeReference.Target.NodeId.ToString())
                                {
                                    isPublished = true;
                                    ContosoOpcUaNode contosoOpcUaNode = Startup.Topology.GetOpcUaNode(ProductUri, item.NodeId);
                                    if (contosoOpcUaNode?.Relevance != null)
                                    {
                                        isRelevant = true;
                                    }
                                }
                            }
                        }

                        jsonTree.Add(new
                        {
                            id            = ("__" + node + delimiter[0] + nodeReference.Target.NodeId.ToString()),
                            text          = nodeReference.Target.DisplayName.ToString(),
                            nodeClass     = nodeReference.Target.NodeClass.ToString(),
                            accessLevel   = currentNodeAccessLevel.ToString(),
                            eventNotifier = currentNodeEventNotifier.ToString(),
                            executable    = currentNodeExecutable.ToString(),
                            children      = nodeReference.Target.HasChildren,
                            publishedNode = isPublished,
                            relevantNode  = isRelevant
                        });
                        idList.Add(nodeReference.Target.NodeId.ToString());
                    }
                }

                stopwatch.Stop();
                Trace.TraceInformation("Browing all childeren info of node '{0}' took {0} ms", node, stopwatch.ElapsedMilliseconds);

                return(Json(jsonTree, JsonRequestBehavior.AllowGet));
            }
            catch (Exception exception)
            {
                return(Content(CreateOpcExceptionActionString(exception)));
            }
        }