Skip to content

Commit

Permalink
Merge pull request #1 from boschrexroth/feature/ver116
Browse files Browse the repository at this point in the history
Feature/ver116
  • Loading branch information
Johannes Albrecht committed Jul 28, 2022
2 parents 951f36a + 958c4d2 commit a4ce95a
Show file tree
Hide file tree
Showing 492 changed files with 32,460 additions and 123 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Expand Up @@ -24,8 +24,8 @@ jobs:
sudo apt-get update
sudo apt-get install -y libsystemd-dev libzmq3-dev
sudo apt install pkg-config
wget https://github.com/boschrexroth/ctrlx-automation-sdk/releases/download/1.12.1/ctrlx-datalayer-1.7.5.deb
sudo apt-get install -y -f ./ctrlx-datalayer-1.7.5.deb
wget https://github.com/boschrexroth/ctrlx-automation-sdk/releases/download/1.16.0/ctrlx-datalayer-1.8.6.deb
sudo apt-get install -y -f ./ctrlx-datalayer-1.8.6.deb
- name: Go
uses: actions/setup-go@v2
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -7,8 +7,8 @@ GOOS := $(shell go env GOOS)
GOARCH := $(shell go env GOARCH)
BUILD := build/public/$(GOOS)_$(GOARCH)

DATALAYER_DEB_VERSION := 1.12.1
DATALAYER_DEB_FILE_VERSION := 1.7.5
DATALAYER_DEB_VERSION := 1.16.0
DATALAYER_DEB_FILE_VERSION := 1.8.6

.PHONY: all go-dep apt-dep lint vet test test-coverage build clean

Expand Down
7 changes: 4 additions & 3 deletions README.md
Expand Up @@ -3,10 +3,11 @@
This project provides ctrlX Data Layer access via Golang.
It wraps the original ctrlX Data Layer written in C++.

Documentation and examples you find here [ctrlX Software Development Kit](https://boschrexroth.github.io/ctrlx-automation-sdk/).
Documentation and examples you can find here [ctrlX Software Development Kit](https://boschrexroth.github.io/ctrlx-automation-sdk/) and [GoDoc ](https://pkg.go.dev/github.com/boschrexroth/ctrlx-datalayer-golang/pkg/datalayer).

## Status


![Make Test](https://github.com/boschrexroth/ctrlx-datalayer-golang/actions/workflows/main.yml/badge.svg)

## Usage
Expand All @@ -22,8 +23,8 @@ sudo apt-get install libsystemd-dev libsystemd-dev:arm64 libzmq3-dev libzmq3-dev
Check the current [ctrlx Data Layer](https://github.com/boschrexroth/ctrlx-automation-sdk/releases) debian package, download and install this, see example.

```bash
wget https://github.com/boschrexroth/ctrlx-automation-sdk/releases/download/1.12.1/ctrlx-datalayer-1.7.5.deb
sudo dpkg -i ctrlx-datalayer-1.7.5.deb
wget https://github.com/boschrexroth/ctrlx-automation-sdk/releases/download/1.16.0/ctrlx-datalayer-1.8.6.deb
sudo dpkg -i ctrlx-datalayer-1.8.6.deb
```

### Integrate in you project
Expand Down
2 changes: 1 addition & 1 deletion pkg/datalayer/client.go
Expand Up @@ -23,7 +23,7 @@
package datalayer

/*
#cgo linux pkg-config: libsystemd libzmq
#cgo linux pkg-config: libsystemd libzmq ctrlx-datalayer
#cgo CFLAGS: -I./headers -I/usr/include/comm/datalayer/c
#cgo linux LDFLAGS: -lcomm_datalayer -lzmq
#cgo windows,amd64 LDFLAGS: -lcomm_datalayer
Expand Down
12 changes: 6 additions & 6 deletions pkg/datalayer/converter.go
Expand Up @@ -72,10 +72,10 @@ func (c *Converter) generateJSONComplex(data Variant, ty Variant, indentStep int
}

// GenerateJsonComplex generates a JSON string out of a Variant with a complex type (flatbuffers) and the metadata of this data.
// Parameter data is a Varaint that contains data of complex data type (flatbuffers). If data is empty (VariantType::UNKNOWN) type is converted to json schema.
// Parameter type is a Variant that contains type of data (Variant with flatbuffers BFBS)
// Parameter data is an array of bytes that contains data of complex data type (flatbuffers). If data is empty (VariantType::UNKNOWN) type is converted to json schema.
// Parameter type is an array of bytes that contains type of data (Variant with flatbuffers BFBS)
// Parameter indentStep is a indentation length for json string.
// It returns generated JSON as a Variant (string).
// It returns generated JSON as a string.
func (c *Converter) GenerateJsonComplex(data []byte, ty []byte, indentStep int) (Result, []byte) {
d := NewVariant()
defer DeleteVariant(d)
Expand All @@ -92,7 +92,7 @@ func (c *Converter) GenerateJsonComplex(data []byte, ty []byte, indentStep int)
}

// ParseJsonSimple generates a Variant out of a JSON string containing the (simple) data.
// Parameter json is a data of the Variant as a json string.
// Parameter json is an array of bytes as a json string.
// It returns a result status of the function, data string which contains the data, error Error object.
func (c *Converter) ParseJsonSimple(json []byte) (Result, *Variant, error) {
cjson := C.CString(string(json))
Expand All @@ -118,8 +118,8 @@ func (c *Converter) parseJSONComplex(json []byte, ty Variant) (Result, *Variant,
}

// ParseJsonComplex generates a Variant out of a JSON string containing the (complex) data.
// Parameter json is a data of the Variant as a json string.
// Parameter type (ty) is a Variant that contains type of data (Variant with bfbs flatbuffer content).
// Parameter json is an array of bytes containing a json string.
// Parameter type (ty) is an array of bytes that contains type of data (Variant with bfbs flatbuffer content).
// It returns a result status of the function, data (array of byte) which contains the data, error Error as error object.
func (c *Converter) ParseJsonComplex(json []byte, ty []byte) (Result, []byte, error) {
t := NewVariant()
Expand Down
42 changes: 42 additions & 0 deletions pkg/datalayer/doc_test.go
@@ -0,0 +1,42 @@
/**
* MIT License
*
* Copyright (c) 2021-2022 Bosch Rexroth AG
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package datalayer_test

import (
"github.com/boschrexroth/ctrlx-datalayer-golang/pkg/datalayer"
fbs "github.com/boschrexroth/ctrlx-datalayer-golang/pkg/fbs/comm/datalayer"
)

func ExampleMetaDataBuilder() {
m := datalayer.NewMetaDataBuilder(datalayer.AllowedOperationRead|datalayer.AllowedOperationWrite, "simple description", "")
m.DisplayName("myexample").NodeClass(fbs.NodeClassVariable)

m.AddReference(datalayer.ReferenceTypeWrite, "type/myexample/write")
m.AddReference(datalayer.ReferenceTypeRead, "type/myexample/read")

m.AddExtension("key", "value")

v := m.Build()
defer datalayer.DeleteVariant(v)
}
239 changes: 239 additions & 0 deletions pkg/datalayer/metadatabuilder.go
@@ -0,0 +1,239 @@
/**
* MIT License
*
* Copyright (c) 2021-2022 Bosch Rexroth AG
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package datalayer

import (
"sort"

fbs "github.com/boschrexroth/ctrlx-datalayer-golang/pkg/fbs/comm/datalayer"
flatbuffers "github.com/google/flatbuffers/go"
)

// AllowedOperation enum
type AllowedOperation uint

// AllowedOperation enum defintion
const (
AllowedOperationNone AllowedOperation = 0x00000
AllowedOperationRead AllowedOperation = 0x00001
AllowedOperationWrite AllowedOperation = 0x00010
AllowedOperationCreate AllowedOperation = 0x00100
AllowedOperationDelete AllowedOperation = 0x01000
AllowedOperationBrowse AllowedOperation = 0x10000
AllowedOperationAll AllowedOperation = AllowedOperationRead | AllowedOperationWrite | AllowedOperationCreate | AllowedOperationDelete | AllowedOperationBrowse
)

// ReferenceType enum
type ReferenceType uint8

// ReferenceType enum defintion
const (
ReferenceTypeRead ReferenceType = 0
ReferenceTypeReadIn ReferenceType = 1
ReferenceTypeReadOut ReferenceType = 2
ReferenceTypeWrite ReferenceType = 3
ReferenceTypeWriteIn ReferenceType = 4
ReferenceTypeWriteOut ReferenceType = 5
ReferenceTypeCreate ReferenceType = 6
ReferenceTypeUses ReferenceType = 7
ReferenceTypeHasSave ReferenceType = 8
)

// EnumNamesReferenceType enum defintion
var EnumNamesReferenceType = map[ReferenceType]string{
ReferenceTypeRead: "readType",
ReferenceTypeReadIn: "readInType",
ReferenceTypeReadOut: "readOutType",
ReferenceTypeWrite: "writeType",
ReferenceTypeWriteIn: "writeInType",
ReferenceTypeWriteOut: "writeOutType",
ReferenceTypeCreate: "createType",
ReferenceTypeUses: "uses",
ReferenceTypeHasSave: "hasSave",
}

// MetaDataBuilder struct
type MetaDataBuilder struct {
name string
description string
descriptionurl string
unit string
allowed AllowedOperation
nodeclass fbs.NodeClass
displayformat fbs.DisplayFormat
refers map[string]string
extensions map[string]string
}

// NewMetaDataBuilder generates MetaDataBuilder instance
func NewMetaDataBuilder(a AllowedOperation, desc string, descurl string) *MetaDataBuilder {
m := &MetaDataBuilder{description: desc, descriptionurl: descurl}
m.name = ""
m.unit = ""
m.nodeclass = fbs.NodeClassNode
m.displayformat = fbs.DisplayFormatAuto
m.refers = make(map[string]string)
m.extensions = make(map[string]string)
m.Operations(a)
return m
}

// Build this instance
func (m *MetaDataBuilder) Build() *Variant {
builder := flatbuffers.NewBuilder(1024)

//Serialize References data
references := m.buildreferences()

//Serialize Extensions data
extensions := m.buildextensions()

//Serialize AllowedOperations data
operations := m.operations()

meta := fbs.MetadataT{}
meta.Description = m.description
meta.DescriptionUrl = m.descriptionurl
meta.DisplayName = m.name
meta.Unit = m.unit
meta.DisplayFormat = m.displayformat

meta.NodeClass = m.nodeclass
meta.Operations = operations
meta.References = references
meta.Extensions = extensions
mi := meta.Pack(builder)
builder.Finish(mi)
v := NewVariant()
v.SetFlatbuffers(builder.FinishedBytes())
return v
}

func (m *MetaDataBuilder) buildextensions() []*fbs.ExtensionT {

extensions := []*fbs.ExtensionT{}
keys := sortkeys(m.extensions)

for _, k := range keys {
re := m.addExt(k, m.extensions[k])
extensions = append(extensions, re)
}

return extensions
}

func (m *MetaDataBuilder) addExt(key, val string) *fbs.ExtensionT {
ext := &fbs.ExtensionT{Key: key, Value: val}
return ext
}

func sortkeys(m map[string]string) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}

func (m *MetaDataBuilder) buildreferences() []*fbs.ReferenceT {

references := []*fbs.ReferenceT{}
keys := sortkeys(m.refers)
for _, k := range keys {
//fmt.Println("refs: ", k, m.refers[k])
re := m.addRef(k, m.refers[k])
references = append(references, re)
}

return references
}

func (m *MetaDataBuilder) addRef(ty, addr string) *fbs.ReferenceT {
ref := &fbs.ReferenceT{Type: ty, TargetAddress: addr}
return ref
}

func (m *MetaDataBuilder) operations() *fbs.AllowedOperationsT {
ops := &fbs.AllowedOperationsT{}
ops.Read = m.isallowed(AllowedOperationRead)
ops.Browse = m.isallowed(AllowedOperationBrowse)
ops.Create = m.isallowed(AllowedOperationCreate)
ops.Delete = m.isallowed(AllowedOperationDelete)
ops.Write = m.isallowed(AllowedOperationWrite)
return ops
}

// Operations set the allowed operations
func (m *MetaDataBuilder) Operations(a AllowedOperation) *MetaDataBuilder {
m.allowed = a
return m
}

func (m *MetaDataBuilder) isallowed(a AllowedOperation) bool {
return m.allowed&a == a
}

// Unit sets the unit
func (m *MetaDataBuilder) Unit(u string) *MetaDataBuilder {
m.unit = u
return m
}

// DisplayName sets the display name
func (m *MetaDataBuilder) DisplayName(n string) *MetaDataBuilder {
m.name = n
return m
}

// NodeClass sets the node class
func (m *MetaDataBuilder) NodeClass(nc fbs.NodeClass) *MetaDataBuilder {
m.nodeclass = nc
return m
}

// DisplayFormat sets the display format
func (m *MetaDataBuilder) DisplayFormat(df fbs.DisplayFormat) *MetaDataBuilder {
m.displayformat = df
return m
}

// AddReference adds the reference
func (m *MetaDataBuilder) AddReference(r ReferenceType, t string) *MetaDataBuilder {
m.refers[ref2name(r)] = t
return m
}

// AddExtension adds the extension
func (m *MetaDataBuilder) AddExtension(key string, val string) *MetaDataBuilder {
m.extensions[key] = val
return m
}

func ref2name(r ReferenceType) string {
if s, ok := EnumNamesReferenceType[r]; ok {
return s
}
return ""
}
1 change: 0 additions & 1 deletion pkg/datalayer/system.go
Expand Up @@ -64,7 +64,6 @@ func (d *System) Start(boStartBroker bool) {

// Stop breaks a ctrlX Data Layer system.
// Parameter boForceProviderStop forces stop off all created providers for this ctrlX Data Layer system.
// It returns false if there is a client or provider active.
func (d *System) Stop(boForceProviderStop bool) {
C.DLR_systemStop(d.this, C.bool(boForceProviderStop))
}
Expand Down

0 comments on commit a4ce95a

Please sign in to comment.