godsharp - GodSharp.Extensions.Opc.Ua.Generator 2022.205.10.1

Opc.Ua produced by GodSharp

PM> Install-Package GodSharp.Extensions.Opc.Ua.Generator -Version 2022.205.10.1 -Source https://www.myget.org/F/godsharp/api/v3/index.json

Copy to clipboard

> nuget.exe install GodSharp.Extensions.Opc.Ua.Generator -Version 2022.205.10.1 -Source https://www.myget.org/F/godsharp/api/v3/index.json

Copy to clipboard

> dotnet add package GodSharp.Extensions.Opc.Ua.Generator --version 2022.205.10.1 --source https://www.myget.org/F/godsharp/api/v3/index.json

Copy to clipboard
<PackageReference Include="GodSharp.Extensions.Opc.Ua.Generator" Version="2022.205.10.1" />
Copy to clipboard
source https://www.myget.org/F/godsharp/api/v3/index.json

nuget GodSharp.Extensions.Opc.Ua.Generator  ~> 2022.205.10.1
Copy to clipboard

> choco install GodSharp.Extensions.Opc.Ua.Generator --version 2022.205.10.1 --source https://www.myget.org/F/godsharp/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "godsharp" -SourceLocation "https://www.myget.org/F/godsharp/api/v2"
Install-Module -Name "GodSharp.Extensions.Opc.Ua.Generator" -RequiredVersion "2022.205.10.1" -Repository "godsharp" 
Copy to clipboard

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

  • QQ Qun: 467764581
  • QQ: 2267930027
  • Email: seayxu@163.com

License

Free!

  • .NETFramework 4.6
  • .NETFramework 4.7.2
  • .NETStandard 2.0
  • .NETStandard 2.1
  • .NETFramework 4.6: 4.6.0.0
  • .NETFramework 4.7.2: 4.7.2.0
  • .NETStandard 2.0: 2.0.0.0
  • .NETStandard 2.1: 2.1.0.0

Owners

Seay

Authors

seayxu

Project URL

https://github.com/godsharp/opcua.extensions

License

Unknown

Tags

OpcUa Opc Ua GodSharp

Info

352 total downloads
64 downloads for version 2022.205.10.1
Download (135.78 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
2022.205.10.1 135.78 KB Thu, 12 May 2022 06:31:05 GMT 64
2022.205.10 135.79 KB Thu, 12 May 2022 03:56:45 GMT 62
2021.411.18 135.78 KB Thu, 18 Nov 2021 10:48:35 GMT 88
2021.309.1 135.78 KB Wed, 22 Sep 2021 16:46:22 GMT 70
2021.308.1 132.97 KB Tue, 31 Aug 2021 06:13:56 GMT 68