Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exporter add influxdb, opc now can auto obtain child node of root node[option] #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"env": {
"OPCUA_PASSWORD":"appleapple2023!",
"OPCUA_USERNAME":"Administrator"
},
}

]
}
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
FROM golang:1.19-alpine3.16 as build
WORKDIR /app
COPY . .
ENV GOPROXY https://goproxy.cn/
RUN go build -o /gopclogs

#Copy files from build, to slim down the overall image size
Expand Down
45 changes: 45 additions & 0 deletions controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package controller

import (
"log"

"github.com/doteich/OPC-UA-Logger/global"
"github.com/gopcua/opcua/ua"
)

type Controller struct {
Rest bool
}

var EnabledControllers Controller

func InitControllers() {
EnabledControllers.Rest = true
}

func WriteNode(nodeId string, value any) (*ua.WriteResponse, error) {
id, err := ua.ParseNodeID(nodeId)
if err != nil {
return nil, err
}

v, err := ua.NewVariant(value)
if err != nil {
log.Fatalf("invalid value: %v", err)
}

req := &ua.WriteRequest{
NodesToWrite: []*ua.WriteValue{
{
NodeID: id,
AttributeID: ua.AttributeIDValue,
Value: &ua.DataValue{
EncodingMask: ua.DataValueValue,
Value: v,
},
},
},
}

return global.HttpOpcClient.Write(req)
}
22 changes: 22 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

var (
ErrUnauthorized = newError("Unauthorized")
ErrBadRequest = newError("Body invalid")
ErrForbidden = newError("Forbidden")
ErrNotFound = newError("Resource not found")
ErrRequestTimeout = newError("Timeout")
)

// HTTPError is custom HTTP error for API
type HTTPError struct {
Message string `json:"message"`
}

func (e *HTTPError) Error() string {
return e.Message
}

func newError(msg string) *HTTPError {
return &HTTPError{Message: msg}
}
39 changes: 39 additions & 0 deletions exporters/influxdb/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package influxdb

import (
"context"
"time"

"github.com/doteich/OPC-UA-Logger/exporters/logging"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/api"
"github.com/influxdata/influxdb-client-go/v2/api/write"
)

var ctx context.Context
var client influxdb2.Client
var writeApi api.WriteAPIBlocking

func CreateConnection(namespace, org, bucket, connectionString, token string) {
ctx = context.Background()

client = influxdb2.NewClient(connectionString, token)
writeApi = client.WriteAPIBlocking(org, bucket)
}

func WriteData(nodeId string, nodeName string, value interface{}, timestamp time.Time, logName string, server string, datatype string, namespace string) {
tags := map[string]string{
"nodeId": nodeId,
"nodeName": nodeName,
"server": server,
"datatype": datatype,
"namespace": namespace,
}
fields := map[string]interface{}{
nodeName: value,
}
point := write.NewPoint(logName, tags, fields, timestamp)
if err := writeApi.WritePoint(context.Background(), point); err != nil {
logging.LogError(err, "Failed to write point", "influxdb")
}
}
24 changes: 24 additions & 0 deletions exporters/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/doteich/OPC-UA-Logger/exporters/http_exporter"
"github.com/doteich/OPC-UA-Logger/exporters/influxdb"
"github.com/doteich/OPC-UA-Logger/exporters/logging"
"github.com/doteich/OPC-UA-Logger/exporters/metrics_exporter"
"github.com/doteich/OPC-UA-Logger/exporters/mongodb"
Expand All @@ -18,6 +19,7 @@ type Exporters struct {
Prometheus bool
Websockets bool
MongoDB bool
InfluxDB bool
}

var EnabledExporters Exporters
Expand Down Expand Up @@ -53,6 +55,16 @@ func InitExporters(config *setup.Config) {
EnabledExporters.MongoDB = true
}

if config.ExporterConfig.InfluxDB.Enabled {
influxdb.CreateConnection(
namespace,
config.ExporterConfig.InfluxDB.Org,
config.ExporterConfig.InfluxDB.Bucket,
config.ExporterConfig.InfluxDB.ConnectionString,
config.ExporterConfig.InfluxDB.Token)
EnabledExporters.InfluxDB = true
}

go InitHTTPServer()

}
Expand Down Expand Up @@ -85,6 +97,18 @@ func PublishData(nodeId string, iface interface{}, timestamp time.Time) {
mongodb.WriteData(node.NodeId, node.NodeName, iface, timestamp, setup.PubConfig.LoggerConfig.Name, setup.PubConfig.ClientConfig.Url, dataType, namespace)
}

if EnabledExporters.InfluxDB {
influxdb.WriteData(
node.NodeId,
node.NodeName,
iface,
timestamp,
setup.PubConfig.LoggerConfig.Name,
setup.PubConfig.ClientConfig.Url,
dataType,
namespace,
)
}
}

func findNodeDetails(nodeId string) (setup.NodeObject, error) {
Expand Down
9 changes: 9 additions & 0 deletions global/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package global

import "github.com/gopcua/opcua"

var HttpOpcClient *opcua.Client

func SetOPCUAClient(mainClient *opcua.Client) {
HttpOpcClient = mainClient
}
29 changes: 14 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,36 @@ module github.com/doteich/OPC-UA-Logger
go 1.18

require (
github.com/go-chi/chi/v5 v5.0.0
github.com/go-chi/cors v1.2.1
github.com/go-chi/render v1.0.3
github.com/gopcua/opcua v0.3.13
github.com/gorilla/websocket v1.5.0
github.com/influxdata/influxdb-client-go/v2 v2.12.3
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.13.0
github.com/spf13/viper v1.13.0
go.mongodb.org/mongo-driver v1.11.2
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2
)

require (
github.com/Luzifer/go-openssl/v4 v4.1.0 // indirect
github.com/Masterminds/squirrel v1.5.3 // indirect
github.com/ajg/form v1.5.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/deepmap/oapi-codegen v1.8.2 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgx/v5 v5.2.0 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
Expand All @@ -43,16 +45,13 @@ require (
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.mongodb.org/mongo-driver v1.11.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.23.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.0.0-20220923202941-7f9b1623fab7 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading