Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Latest commit

 

History

History
344 lines (276 loc) · 11.3 KB

README.md

File metadata and controls

344 lines (276 loc) · 11.3 KB

GodSharp.Extensions.Opc.Ua

continuous.publish continuous.complie

Package Version

Name NuGet MyGet
GodSharp.Extensions.Opc.Ua NuGet MyGet
GodSharp.Extensions.Opc.Ua.Client NuGet MyGet
GodSharp.Extensions.Opc.Ua.Generator NuGet MyGet
GodSharp.Extensions.Opc.Ua.CodeGenerator NuGet MyGet
GodSharp.Extensions.Opc.Ua.MsBuild NuGet MyGet
GodSharp.Opc.Ua.Client NuGet MyGet

Introduction

Package Uasge
GodSharp.Extensions.Opc.Ua extensions library for opc ua
GodSharp.Extensions.Opc.Ua.Client extensions library for opc ua client
GodSharp.Extensions.Opc.Ua.Generator attributes for CodeGenerator
GodSharp.Extensions.Opc.Ua.CodeGenerator generate encode/decode method for EncodeableObject class by DIY
GodSharp.Extensions.Opc.Ua.MsBuild generate code for which class with ComplexObjectGenerator attribute
GodSharp.Opc.Ua.Client a Sample opc ua client

Getting Started

PM> Install-Package GodSharp.Opc.Ua.Client

Discovery Server

Sample code:

var url = "opc.tcp://127.0.0.1:4840";
OpcUaServerDiscovery discovery = new OpcUaServerDiscovery();

Console.WriteLine("discovery Discovery");
var servers = discovery.Discovery(url);
foreach (var item in servers)
{
    foreach (var durl in item.DiscoveryUrls)
    {
        Console.WriteLine($"{durl}");
        var endpoints = discovery.GetEndpoints(durl);
        if (endpoints == null) continue;
        foreach (var endpoint in endpoints)
        {
            Console.WriteLine($"\t- {endpoint.EndpointUrl}/ {endpoint.SecurityMode}/  {endpoint.SecurityPolicyUri}");
        }
    }
}

Console.WriteLine("discovery DiscoveryUrls");
var discoverys = discovery.DiscoveryUrls(url);
if (discoverys != null)
{
    foreach (var discoveryUrl in discoverys)
    {
        Console.WriteLine($"{discoveryUrl}");
        var endpoints = discovery.GetEndpoints(discoveryUrl);
        if (endpoints == null) continue;
        foreach (var endpoint in endpoints)
        {
            Console.WriteLine($"\t- {endpoint.EndpointUrl}/ {endpoint.SecurityMode}/  {endpoint.SecurityPolicyUri}");
        }
    }
}

Console.WriteLine("discovery finished");

Browse / BrowseTree

  • Browse: To browse node list with specialized node or default.

  • BrowseTree: To browse node tree with specialized node or default.

    Sample code:

    // To browse node list with specialized node or default 
    var all = client.Session.Browse();
    //var browse = client.Session.Browse(new NodeId  ("ns=4;s=Demo.Static"));
    Console.WriteLine(" DisplayName, BrowseName, NodeClass");
    foreach (var obj in all)
    {
        Console.WriteLine(" {0}, {1}, {2}", obj.DisplayName,   obj.BrowseName, obj.NodeClass);
    
        var browse2 = client.Session.Browse((NodeId)obj.NodeId);
        foreach (var refd in browse2)
        {
            Console.WriteLine("   + {0}, {1}, {2}",   refd.DisplayName, refd.BrowseName, refd.NodeClass);
        }
    }
    
    // To browse node tree with specialized node or default
    var tree = client.Session.BrowseTree();
    //var tree = client.Session.BrowseTree(new NodeId  ("ns=4;s=Demo.Static"));
    Browse(tree);
    
    static void Browse(IEnumerable<ReferenceBrowseDescription>   refs, int level = -1)
    {
        level++;
        foreach (var description in refs)
        {
            Console.WriteLine("{0}{4}+{1}, {2},{3}",
                new string('\t', level),
                //Formatter.FormatAttributeValue  (attribute.ValueId.AttributeId,   attribute.Value)}
                //description.Node.BrowseName, 
                description.GetFormatText(),
                description.Node.NodeClass,
                description.Node.NodeId,
      		level
            );
            if (description.Children != null)
            {
                Browse(description.Children, level);
            }
        }
    }

GetAttributes / GetProperties

Sample code:

var node = new NodeId("ns=0;i=2258"); // 2258   Server.ServerStatus.CurrentTime
var attributes = client.Session.GetAttributes(node);
foreach (var attribute in attributes)
{
    Console.WriteLine($"{attribute.Name}:  {attribute.ValueText}");
}

var properties = client.Session.GetProperties(node);
if (properties != null)
{
    foreach (var attribute in properties)
    {
        Console.WriteLine($"{attribute.Name}:  {Formatter.FormatAttributeValue  (attribute.ValueId.AttributeId, attribute.Value,   client.Session)}");
    }
}

Subscribe / Unsubscribe

Sample code:

var sub_name = "andon";
var subscribes = new string[] { "ns=0;i=2258",   "ns=0;i=2259" };
// 2258 Server.ServerStatus.CurrentTime
// 2259 Server.ServerStatus.State

client.Subscribe(sub_name, subscribes);

Console.WriteLine("Press any key to Unsubscribe");
Console.ReadLine();

client.Unsubscribe(sub_name);

Read Node

Sample code:

T val = client.Session.Read<T>(node);
// or
DataValue val = client.Session.Read(node);

Write Node

Sample code:

var ret = client.Session.Write(node, value);

MsBuild

Generate code for which class is customized with ComplexObjectGenerator attribute, the class generated is implement ComplexObject.

PM> Install-Package GodSharp.Extensions.Opc.Ua.Generator
PM> Install-Package GodSharp.Extensions.Opc.Ua.MsBuild

ComplexObject class types:

[DefaultValue(ComplexObjectType.EncodeableObject)]
public enum ComplexObjectType
{
    EncodeableObject,
    SwitchField,
    OptionalField
}

Sample code:

[ComplexObjectGenerator(ComplexObjectType.SwitchField,   EncodingMethodType.Factory)]
public partial class UaAnsiUnion
{
    [SwitchField(1)]
    public int Int32;
    [SwitchField(2, 4)]
    public string String;

    public UaAnsiUnion()
    {
        TypeIdNamespace = "nsu=http://www.unifiedautomation.com/DemoServer/;i=3006";
        BinaryEncodingIdNamespace = "nsu=http://www.unifiedautomation.com/DemoServer/;i=5003";
    }
}

Just build project, code will be generated by msbuild task in the file with extension {filename}.uamgen.cs by default.

like this:

//----------------------------------------------------------- - ------------------
// <auto-generated>
//     Generated by MSBuild generator.
//     Source: UaAnsiUnion.cs
// </auto-generated>
//----------------------------------------------------------- - ------------------

using GodSharp.Extensions.Opc.Ua.Types;
using Opc.Ua;
using static   GodSharp.Extensions.Opc.Ua.Types.Encodings.EncodingFactory;

namespace GodSharpOpcUaClientSample.Types
{
	public partial class UaAnsiUnion : ComplexObject 
	{
		public uint SwitchField;

		public override void Encode(IEncoder encoder)
		{
			base.Encode(encoder);
			encoder.WriteUInt32("SwitchField",SwitchField);
			switch (SwitchField)
			{
				case 1:
					Encoding.Write(encoder, Int32, nameof  (Int32));
					break;
				case 2:
				case 4:
					Encoding.Write(encoder, String, nameof  (String));
					break;
				default:
					break;
			}
		}

		public override void Decode(IDecoder decoder)
		{
			base.Decode(decoder);
			SwitchField = decoder.ReadUInt32("SwitchField");
			switch (SwitchField)
			{
				case 1:
					Encoding.Read(decoder,ref Int32, nameof  (Int32));
					break;
				case 2:
				case 4:
					Encoding.Read(decoder,ref String, nameof  (String));
					break;
				default:
					break;
			}
		}
	}
}

Register Custom Types

Register Custom Type Namespace

You can register custom type namespace by hard code in constructor.

Sample code:

[ComplexObjectGenerator(ComplexObjectType.SwitchField,   EncodingMethodType.Factory)]
public partial class UaAnsiUnion
{
    [SwitchField(1)]
    public int Int32;
    [SwitchField(2, 4)]
    public string String;

    public UaAnsiUnion()
    {
        TypeIdNamespace = "nsu=http://  www.unifiedautomation.com/DemoServer/;i=3006";
        BinaryEncodingIdNamespace = "nsu=http://  www.unifiedautomation.com/DemoServer/;i=5003";
    }
}

Also can support by configuration from file or db,and others.

Sample code:

EncodingFactory.Instance.RegisterTypeNamespace(
new TypeNamespace()
{
    Type = typeof(UaAnsiVector).AssemblyQualifiedName,
    TypeId = "nsu=http://www.unifiedautomation.com/  DemoServer/;i=3002",
    BinaryEncodingId = "nsu=http://www.unifiedautomation.com/  DemoServer/;i=5054"
}
);

Register Custom Type to System

Sample code:

EncodingFactory.Instance.RegisterEncodeableTypes(typeof(UaAnsiVector), typeof(UaAnsiVector));
EncodingFactory.Instance.RegisterEncodeableTypes(Assembly.GetEntryAssembly(),Assembly.GetExecutingAssembly());

Contact

License

Free!