Skip to content

Commit

Permalink
Added opc ua server sample
Browse files Browse the repository at this point in the history
  • Loading branch information
seayxu committed Mar 8, 2023
1 parent 9e41790 commit b6d0cb7
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
|Name|NuGet|MyGet|
|---|---|---|
| GodSharp.Opc.Da | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Da?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da) |
| GodSharp.Opc.Da.OpcAutomation | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da.OpcAutomation?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da.OpcAutomation) | [![MyGet](https://img.shields.io/myget/godsharplab/v/GodSharp.Opc.Da.OpcAutomation?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da.OpcAutomation) |
| GodSharp.Opc.Da.OpcAutomation | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da.OpcAutomation?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da.OpcAutomation) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Da.OpcAutomation?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da.OpcAutomation) |
| GodSharp.Opc.Da.OpcAutomation.Kepware | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da.OpcAutomation.Kepware?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da.OpcAutomation.Kepware) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Da.OpcAutomation.Kepware?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da.OpcAutomation.Kepware) |
| GodSharp.Opc.Da.OpcAutomation.Graybox | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da.OpcAutomation.Graybox?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da.OpcAutomation.Graybox) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Da.OpcAutomation.Graybox?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da.OpcAutomation.Graybox) |
| GodSharp.Opc.Da.OpcNetApi | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da.OpcNetApi?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da.OpcNetApi) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Da.OpcNetApi?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da.OpcNetApi) |
Expand All @@ -27,6 +27,8 @@

### [GodSharp.Opc.Da](./src/GodSharpOpcDaSample/README.md)
### [GodSharp.Opc.Ua.Client](./src/GodSharpOpcUaClientSample/README.md)
### [GodSharp.Opc.Ua.Server](./src/GodSharpOpcUaServer/README.md)
### [GodSharp.Opc.Ua.DiscoveryServer](./src/GodSharpOpcUaDiscoveryServer/README.md)

## Contact

Expand Down
2 changes: 1 addition & 1 deletion src/GodSharpOpcDaSample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
|Name|NuGet|MyGet|
|---|---|---|
| GodSharp.Opc.Da | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Da?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da) |
| GodSharp.Opc.Da.OpcAutomation | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da.OpcAutomation?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da.OpcAutomation) | [![MyGet](https://img.shields.io/myget/godsharplab/v/GodSharp.Opc.Da.OpcAutomation?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da.OpcAutomation) |
| GodSharp.Opc.Da.OpcAutomation | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da.OpcAutomation?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da.OpcAutomation) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Da.OpcAutomation?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da.OpcAutomation) |
| GodSharp.Opc.Da.OpcAutomation.Kepware | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da.OpcAutomation.Kepware?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da.OpcAutomation.Kepware) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Da.OpcAutomation.Kepware?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da.OpcAutomation.Kepware) |
| GodSharp.Opc.Da.OpcAutomation.Graybox | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da.OpcAutomation.Graybox?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da.OpcAutomation.Graybox) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Da.OpcAutomation.Graybox?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da.OpcAutomation.Graybox) |
| GodSharp.Opc.Da.OpcNetApi | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Da.OpcNetApi?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Da.OpcNetApi) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Da.OpcNetApi?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Da.OpcNetApi) |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\GodSharp.Opc.Ua.Common\GodSharp.Opc.Ua.Common.csproj" />
</ItemGroup>

</Project>
54 changes: 54 additions & 0 deletions src/GodSharpOpcUaDiscoveryServer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// See https://aka.ms/new-console-template for more information
using GodSharp.Opc.Ua;

using Opc.Ua;

Console.WriteLine("Hello, GodSharpOpcUaDiscoveryServer!");
// Auto Generate ApplicationConfigurationFile
// GodSharp.Opc.Ua.OpcUaHelper.GenerateApplicationConfigurationFile = true;

// Configuration for UA Discovery Server
ApplicationConfiguration? configuration = null;
configuration = await GodSharp.Opc.Ua.OpcUaHelper.DiscoveryServerConfigurationAsync(
"GodSharpOpcUaDiscoveryServer",
"GodSharp Opc Ua Discovery Server",
new string[] { Utils.Format(@"opc.tcp://{0}:4840", "localhost") }
//,(s, e) => CertificateValidationHandler(configuration, s, e)
, CertificateValidationHandler
);

// Instance UA Server
var ds = new OpcUaDiscoveryServer(TimeSpan.FromSeconds(15));

// Start UA Discovery Server
ds.Start(configuration);
Console.WriteLine("GodSharpOpcUaDiscoveryServer Started!");
Console.ReadLine();

// Start UA Discovery Server
ds.Stop();

static void CertificateValidationHandler(
//ApplicationConfiguration configuration,
CertificateValidator sender, CertificateValidationEventArgs e)
{
if (e.Error.StatusCode.Code == StatusCodes.BadCertificateUntrusted)
{
e.Accept = true;
//return;
//if (!Directory.Exists(configuration.SecurityConfiguration.RejectedCertificateStore.StorePath))
//{
// Directory.CreateDirectory(configuration.SecurityConfiguration.RejectedCertificateStore.StorePath);
//}

//if (configuration.SecurityConfiguration.AutoAcceptUntrustedCertificates)
//{
// using (var store = configuration.SecurityConfiguration.TrustedPeerCertificates.OpenStore())
// {
// store.Add(e.Certificate);
// }
// e.Accept = true;
// return;
//}
}
}
65 changes: 65 additions & 0 deletions src/GodSharpOpcUaDiscoveryServer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# GodSharp.Opc.Ua.DiscoveryServer

![Azure DevOps builds (main)](https://img.shields.io/azure-devops/build/godsharp/public/4/main?label=azure%20pipelines&style=flat-square)

## Package Version

|Name|NuGet|MyGet|
|---|---|---|
| GodSharp.Opc.Ua.Common | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.Common?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.Common) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.Common?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.Common) |
| GodSharp.Opc.Ua.Client | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.Client?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.Client) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.Client?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.Client) |
| GodSharp.Opc.Ua.ProxyServer | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.ProxyServer?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.ProxyServer) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.ProxyServer?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.ProxyServer) |
| GodSharp.Opc.Ua.Server | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.Server?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.Server) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.Server?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.Server) |
| GodSharp.Opc.Ua.Server.AspNetCore | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.Server.AspNetCore?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.Server.AspNetCore) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.Server.AspNetCore?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.Server.AspNetCore) |
| GodSharp.Opc.Ua.ComplexTypes | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.ComplexTypes?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.ComplexTypes) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.ComplexTypes?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.ComplexTypes) |

## Introduction

|Package|Uasge|
|---:|:---|
| GodSharp.Opc.Ua.Common | a opc ua common lib |
| GodSharp.Opc.Ua.Client | a opc ua client |
| GodSharp.Opc.Ua.ProxyServer | a proxy for opc ua server |
| GodSharp.Opc.Ua.Server | a opc ua server |
| GodSharp.Opc.Ua.Server.AspNetCore | opc ua server for asp.net core |
| GodSharp.Opc.Ua.ComplexTypes | custom generate encode/decode method for `EncodeableObject` class |

## Getting Started

```ps
PM> Install-Package GodSharp.Opc.Ua.Common
```

### Configuration

```csharp
// Auto Generate ApplicationConfigurationFile
// GodSharp.Opc.Ua.OpcUaHelper.GenerateApplicationConfigurationFile = true;
// Configuration for UA Discovery Server
ApplicationConfiguration? configuration = null;
configuration = await GodSharp.Opc.Ua.OpcUaHelper.DiscoveryServerConfigurationAsync(
"GodSharpOpcUaDiscoveryServer",
"GodSharp Opc Ua Discovery Server",
new string[] { Utils.Format(@"opc.tcp://{0}:4840", "localhost") }
//,(s, e) => CertificateValidationHandler(configuration, s, e)
, CertificateValidationHandler
);
```

### Instance

```csharp
// Instance UA Discovery Server
var ds = new OpcUaDiscoveryServer(TimeSpan.FromSeconds(15));

// Start UA Discovery Server
ds.Start(configuration);

// Start UA Discovery Server
ds.Stop();
```

## License

**Free!**
14 changes: 14 additions & 0 deletions src/GodSharpOpcUaServer/GodSharpOpcUaServer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\GodSharp.Opc.Ua.Server\GodSharp.Opc.Ua.Server.csproj" />
</ItemGroup>

</Project>
63 changes: 63 additions & 0 deletions src/GodSharpOpcUaServer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// See https://aka.ms/new-console-template for more information
using GodSharp.Opc.Ua.Server;

using Opc.Ua;

Console.WriteLine("Hello, GodSharpOpcUaServer!");

// Auto Generate ApplicationConfigurationFile
GodSharp.Opc.Ua.OpcUaHelper.GenerateApplicationConfigurationFile = true;

// Configuration for UA Server
ApplicationConfiguration? configuration = null;
configuration = await GodSharp.Opc.Ua.OpcUaHelper.ServerConfigurationAsync(
"GodSharpOpcUaServer",
"GodSharp Opc Ua Server",
new string[] { Utils.Format(@"opc.tcp://{0}:4840", "localhost") }
//,(s, e) => CertificateValidationHandler(configuration, s, e)
, CertificateValidationHandler
);

// Instance UA Server
var server = new OpcUaStandardServer(new SampleServerAuthorizationFilter());

// Start server
server.Start(configuration);

Console.WriteLine("GodSharpOpcUaServer Started!");
string? input = null;

do
{
input = Console.ReadLine();

if (input == null) continue;
} while (input != "q");

// Stop server
server.Stop();

static void CertificateValidationHandler(
//ApplicationConfiguration configuration,
CertificateValidator sender, CertificateValidationEventArgs e)
{
if (e.Error.StatusCode.Code == StatusCodes.BadCertificateUntrusted)
{
e.Accept = true;
//return;
//if (!Directory.Exists(configuration.SecurityConfiguration.RejectedCertificateStore.StorePath))
//{
// Directory.CreateDirectory(configuration.SecurityConfiguration.RejectedCertificateStore.StorePath);
//}

//if (configuration.SecurityConfiguration.AutoAcceptUntrustedCertificates)
//{
// using (var store = configuration.SecurityConfiguration.TrustedPeerCertificates.OpenStore())
// {
// store.Add(e.Certificate);
// }
// e.Accept = true;
// return;
//}
}
}
97 changes: 97 additions & 0 deletions src/GodSharpOpcUaServer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# GodSharp.Opc.Ua.Server

![Azure DevOps builds (main)](https://img.shields.io/azure-devops/build/godsharp/public/4/main?label=azure%20pipelines&style=flat-square)

## Package Version

|Name|NuGet|MyGet|
|---|---|---|
| GodSharp.Opc.Ua.Common | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.Common?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.Common) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.Common?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.Common) |
| GodSharp.Opc.Ua.Client | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.Client?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.Client) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.Client?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.Client) |
| GodSharp.Opc.Ua.ProxyServer | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.ProxyServer?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.ProxyServer) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.ProxyServer?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.ProxyServer) |
| GodSharp.Opc.Ua.Server | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.Server?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.Server) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.Server?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.Server) |
| GodSharp.Opc.Ua.Server.AspNetCore | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.Server.AspNetCore?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.Server.AspNetCore) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.Server.AspNetCore?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.Server.AspNetCore) |
| GodSharp.Opc.Ua.ComplexTypes | [![NuGet](https://img.shields.io/nuget/v/GodSharp.Opc.Ua.ComplexTypes?style=flat-square)](https://www.nuget.org/packages/GodSharp.Opc.Ua.ComplexTypes) | [![MyGet](https://img.shields.io/myget/godsharp/v/GodSharp.Opc.Ua.ComplexTypes?style=flat-square&label=myget)](https://www.myget.org/feed/godsharp/package/nuget/GodSharp.Opc.Ua.ComplexTypes) |

## Introduction

|Package|Uasge|
|---:|:---|
| GodSharp.Opc.Ua.Common | a opc ua common lib |
| GodSharp.Opc.Ua.Client | a opc ua client |
| GodSharp.Opc.Ua.ProxyServer | a proxy for opc ua server |
| GodSharp.Opc.Ua.Server | a opc ua server |
| GodSharp.Opc.Ua.Server.AspNetCore | opc ua server for asp.net core |
| GodSharp.Opc.Ua.ComplexTypes | custom generate encode/decode method for `EncodeableObject` class |

## Getting Started

```ps
PM> Install-Package GodSharp.Opc.Ua.Server
```

### Configuration

```csharp
// Auto Generate ApplicationConfigurationFile
GodSharp.Opc.Ua.OpcUaHelper.GenerateApplicationConfigurationFile = true;

// Configuration for UA Server
ApplicationConfiguration? configuration = null;
configuration = await GodSharp.Opc.Ua.OpcUaHelper.ServerConfigurationAsync(
"GodSharpOpcUaServer",
"GodSharp Opc Ua Server",
new string[] { Utils.Format(@"opc.tcp://{0}:4840", "localhost") }
//,(s, e) => CertificateValidationHandler(configuration, s, e)
, CertificateValidationHandler
);

// Instance UA Server
var server = new OpcUaStandardServer(new SampleServerAuthorizationFilter ());

// Start server
server.Start(configuration);
```

### Instance

```csharp
// Instance UA Server
var server = new OpcUaStandardServer(new SampleServerAuthorizationFilter ());

// Start server
server.Start(configuration);

// Stop server
server.Stop();
```

### ServerAuthorizationFilter

Used to authorized user token.

```csharp
public class SampleServerAuthorizationFilter : IServerAuthorizationFilter
{
public ServerAuthorizationResult Verify(UserNameIdentityToken token)
{
string userName = token.UserName;
string decryptedPassword = token.DecryptedPassword;
if (userName == "admin" && decryptedPassword == "admin@password")
{
return new ServerAuthorizationResult(Authorized: true, Administrator: true);
}

if (userName == "user" && decryptedPassword == "user@password")
{
return new ServerAuthorizationResult(Authorized: true);
}

return new ServerAuthorizationResult(Authorized: false);
}
}
```

## License

**Free!**

0 comments on commit b6d0cb7

Please sign in to comment.