opcua

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 29, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

README

OPC-UA Adapter

Adapter between Magistrala IoT system and an OPC-UA Server.

This adapter sits between Magistrala and an OPC-UA server and just forwards the messages from one system to another.

OPC-UA Server is used for connectivity layer and the data is pushed via this adapter service to Magistrala, where it is persisted and routed to other protocols via Magistrala multi-protocol message broker. Magistrala adds user accounts, application management and security in order to obtain the overall end-to-end OPC-UA solution.

Configuration

The service is configured using the environment variables presented in the following table. Note that any unset variables will be replaced with their default values.

Variable Description Default
MG_OPCUA_ADAPTER_LOG_LEVEL Log level for the WS Adapter (debug, info, warn, error) info
MG_OPCUA_ADAPTER_HTTP_HOST Service OPC-UA host ""
MG_OPCUA_ADAPTER_HTTP_PORT Service WOPC-UAS port 8180
MG_OPCUA_ADAPTER_HTTP_SERVER_CERT Path to the PEM encoded server certificate file ""
MG_OPCUA_ADAPTER_HTTP_SERVER_KEY Path to the PEM encoded server key file ""
MG_OPCUA_ADAPTER_ROUTE_MAP_URL Route-map database URL redis://localhost:6379/0
MG_ES_URL Event source URL nats://localhost:4222
MG_OPCUA_ADAPTER_EVENT_CONSUMER Service event consumer name opcua-adapter
MG_MESSAGE_BROKER_URL Message broker instance URL nats://localhost:4222
MG_JAEGER_URL Jaeger server URL http://localhost:14268/api/traces
MG_JAEGER_TRACE_RATIO Jaeger sampling ratio 1.0
MG_SEND_TELEMETRY Send telemetry to magistrala call home server true
MG_OPCUA_ADAPTER_INSTANCE_ID Service instance ID ""

Deployment

The service itself is distributed as Docker container. Check the opcua-adapter service section in docker-compose to see how service is deployed.

Running this service outside of container requires working instance of the message broker service, redis routemap server and Jaeger server. To start the service outside of the container, execute the following shell script:

# download the latest version of the service
git clone https://github.com/absmach/magistrala

cd magistrala

# compile the opcua-adapter
make opcua

# copy binary to bin
make install

# set the environment variables and run the service
MG_OPCUA_ADAPTER_LOG_LEVEL=info \
MG_OPCUA_ADAPTER_HTTP_HOST=localhost \
MG_OPCUA_ADAPTER_HTTP_PORT=8180 \
MG_OPCUA_ADAPTER_HTTP_SERVER_CERT="" \
MG_OPCUA_ADAPTER_HTTP_SERVER_KEY="" \
MG_OPCUA_ADAPTER_ROUTE_MAP_URL=redis://localhost:6379/0 \
MG_ES_URL=nats://localhost:4222 \
MG_OPCUA_ADAPTER_EVENT_CONSUMER=opcua-adapter \
MG_MESSAGE_BROKER_URL=nats://localhost:4222 \
MG_JAEGER_URL=http://localhost:14268/api/traces \
MG_JAEGER_TRACE_RATIO=1.0 \
MG_SEND_TELEMETRY=true \
MG_OPCUA_ADAPTER_INSTANCE_ID="" \
$GOBIN/magistrala-opcua

Setting MG_LORA_ADAPTER_HTTP_SERVER_CERT and MG_LORA_ADAPTER_HTTP_SERVER_KEY will enable TLS against the service. The service expects a file in PEM format for both the certificate and the key.

Using docker-compose

This service can be deployed using docker containers. Docker compose file is available in <project_root>/docker/addons/opcua-adapter/docker-compose.yml. In order to run Magistrala opcua-adapter, execute the following command:

docker-compose -f docker/addons/opcua-adapter/docker-compose.yml up -d

Usage

For more information about service capabilities and its usage, please check out the Magistrala documentation.

Documentation

Overview

Package opcua contains OPC-UA server implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BrowsedNode

type BrowsedNode struct {
	NodeID      string
	DataType    string
	Description string
	Unit        string
	Scale       string
	BrowseName  string
}

BrowsedNode represents the details of a browsed OPC-UA node.

type Browser

type Browser interface {
	// Browse availlable Nodes for a given URI.
	Browse(string, string) ([]BrowsedNode, error)
}

Browser represents the OPC-UA Server Nodes browser.

type Config

type Config struct {
	ServerURI string
	NodeID    string
	Interval  string `env:"MG_OPCUA_ADAPTER_INTERVAL_MS"   envDefault:"1000"`
	Policy    string `env:"MG_OPCUA_ADAPTER_POLICY"        envDefault:""`
	Mode      string `env:"MG_OPCUA_ADAPTER_MODE"          envDefault:""`
	CertFile  string `env:"MG_OPCUA_ADAPTER_CERT_FILE"     envDefault:""`
	KeyFile   string `env:"MG_OPCUA_ADAPTER_KEY_FILE"      envDefault:""`
}

Config OPC-UA Server.

type RouteMapRepository

type RouteMapRepository interface {
	// Save stores/routes pair OPC-UA Server & Magistrala.
	Save(context.Context, string, string) error

	// Get returns the stored Magistrala route-map for a given OPC-UA pair.
	Get(context.Context, string) (string, error)

	// Remove route-map from cache.
	Remove(context.Context, string) error
}

RouteMapRepository store route-map between the OPC-UA Server and Magistrala.

type Service

type Service interface {
	// CreateThing creates thingID:OPC-UA-nodeID route-map
	CreateThing(ctx context.Context, thingID, nodeID string) error

	// UpdateThing updates thingID:OPC-UA-nodeID route-map
	UpdateThing(ctx context.Context, thingID, nodeID string) error

	// RemoveThing removes thingID:OPC-UA-nodeID route-map
	RemoveThing(ctx context.Context, thingID string) error

	// CreateChannel creates channelID:OPC-UA-serverURI route-map
	CreateChannel(ctx context.Context, chanID, serverURI string) error

	// UpdateChannel updates channelID:OPC-UA-serverURI route-map
	UpdateChannel(ctx context.Context, chanID, serverURI string) error

	// RemoveChannel removes channelID:OPC-UA-serverURI route-map
	RemoveChannel(ctx context.Context, chanID string) error

	// ConnectThing creates thingID:channelID route-map
	ConnectThing(ctx context.Context, chanID, thingID string) error

	// DisconnectThing removes thingID:channelID route-map
	DisconnectThing(ctx context.Context, chanID, thingID string) error

	// Browse browses available nodes for a given OPC-UA Server URI and NodeID
	Browse(ctx context.Context, serverURI, namespace, identifier string) ([]BrowsedNode, error)
}

Service specifies an API that must be fullfiled by the domain service implementation, and all of its decorators (e.g. logging & metrics).

func New

func New(sub Subscriber, brow Browser, thingsRM, channelsRM, connectRM RouteMapRepository, cfg Config, log *slog.Logger) Service

New instantiates the OPC-UA adapter implementation.

type Subscriber

type Subscriber interface {
	// Subscribes to given NodeID and receives events.
	Subscribe(context.Context, Config) error
}

Subscriber represents the OPC-UA Server client.

Directories

Path Synopsis
Package api contains API-related concerns: endpoint definitions, middlewares and all resource representations.
Package api contains API-related concerns: endpoint definitions, middlewares and all resource representations.
Package db contains the database implementation of opcua repository layer.
Package db contains the database implementation of opcua repository layer.
Package events provides the domain concept definitions needed to support opcua events functionality.
Package events provides the domain concept definitions needed to support opcua events functionality.
Package gopcua contains the OPC-UA client implementation.
Package gopcua contains the OPC-UA client implementation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL