1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-02 22:17:10 +08:00
Manuel Imperiale e16a025fba
MF-886 - Add OPC-UA adapter (#878)
* NOISSUE- Add OPC-UA adapter

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* NOISSUE - Add opc-adapter PoC, docker and vendor

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Convert OPC messages to SenML

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add gopcua package

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* lora-adapter typo

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add OPC Reader

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Typo fix

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Typo fix

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Update copyright headers

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix reviews

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix reviews

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add opc config

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add all opc envars in the config

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Config typo

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add route map

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use opcua package instead of opc

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix OPCUA typo

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Rm MQTT sub

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Move interefaces to root

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix revieews and typo

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Update Gopkg.toml

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Add all envars into .env

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
2019-10-22 17:44:19 +02:00

139 lines
3.8 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package opcua
import (
"context"
"errors"
"fmt"
"github.com/mainflux/mainflux"
)
const (
protocol = "opcua"
thingSuffix = "thing"
channelSuffix = "channel"
)
var (
// ErrMalformedIdentity indicates malformed identity received (e.g.
// invalid namespace or ID).
ErrMalformedIdentity = errors.New("malformed identity received")
// ErrMalformedMessage indicates malformed OPC-UA message.
ErrMalformedMessage = errors.New("malformed message received")
// ErrNotFoundIdentifier indicates a non-existent route map for a Node Identifier.
ErrNotFoundIdentifier = errors.New("route map not found for this node identifier")
// ErrNotFoundNamespace indicates a non-existent route map for an Node Namespace.
ErrNotFoundNamespace = errors.New("route map not found for this node namespace")
)
// Service specifies an API that must be fullfiled by the domain service
// implementation, and all of its decorators (e.g. logging & metrics).
type Service interface {
// CreateThing creates thing mfx:opc & opc:mfx route-map
CreateThing(string, string) error
// UpdateThing updates thing mfx:opc & opc:mfx route-map
UpdateThing(string, string) error
// RemoveThing removes thing mfx:opc & opc:mfx route-map
RemoveThing(string) error
// CreateChannel creates channel mfx:opc & opc:mfx route-map
CreateChannel(string, string) error
// UpdateChannel updates mfx:opc & opc:mfx route-map
UpdateChannel(string, string) error
// RemoveChannel removes channel mfx:opc & opc:mfx route-map
RemoveChannel(string) error
// Publish forwards messages from the OPC-UA MQTT broker to Mainflux NATS broker
Publish(context.Context, string, Message) error
}
// Config OPC-UA Server
type Config struct {
ServerURI string
NodeNamespace string
NodeIdintifier string
Policy string
Mode string
CertFile string
KeyFile string
}
var _ Service = (*adapterService)(nil)
type adapterService struct {
publisher mainflux.MessagePublisher
thingsRM RouteMapRepository
channelsRM RouteMapRepository
}
// New instantiates the OPC-UA adapter implementation.
func New(pub mainflux.MessagePublisher, thingsRM, channelsRM RouteMapRepository) Service {
return &adapterService{
publisher: pub,
thingsRM: thingsRM,
channelsRM: channelsRM,
}
}
// Publish forwards messages from OPC-UA MQTT broker to Mainflux NATS broker
func (as *adapterService) Publish(ctx context.Context, token string, m Message) error {
// Get route map of OPC-UA Node Namespace
channelID, err := as.channelsRM.Get(m.Namespace)
if err != nil {
return ErrNotFoundNamespace
}
// Get route map of OPC-UA Node Identifier
thingID, err := as.thingsRM.Get(m.ID)
if err != nil {
return ErrNotFoundIdentifier
}
// Publish on Mainflux NATS broker
SenML := fmt.Sprintf(`[{"n":"opcua","v":%f}]`, m.Data)
payload := []byte(SenML)
msg := mainflux.RawMessage{
Publisher: thingID,
Protocol: protocol,
ContentType: "Content-Type",
Channel: channelID,
Payload: payload,
}
return as.publisher.Publish(ctx, token, msg)
}
func (as *adapterService) CreateThing(mfxDevID string, opcID string) error {
return as.thingsRM.Save(mfxDevID, opcID)
}
func (as *adapterService) UpdateThing(mfxDevID string, opcID string) error {
return as.thingsRM.Save(mfxDevID, opcID)
}
func (as *adapterService) RemoveThing(mfxDevID string) error {
return as.thingsRM.Remove(mfxDevID)
}
func (as *adapterService) CreateChannel(mfxChanID string, opcNamespace string) error {
return as.channelsRM.Save(mfxChanID, opcNamespace)
}
func (as *adapterService) UpdateChannel(mfxChanID string, opcNamespace string) error {
return as.channelsRM.Save(mfxChanID, opcNamespace)
}
func (as *adapterService) RemoveChannel(mfxChanID string) error {
return as.channelsRM.Remove(mfxChanID)
}