Skip to content

Commit

Permalink
feat!: change device definition file to YAML format
Browse files Browse the repository at this point in the history
BREAKING CHANGE: stop supporting TOML format and support YAML format
for Device definition files

closes #1380

Signed-off-by: Chris Hung <chris@iotechsys.com>
  • Loading branch information
Chris Hung committed Mar 15, 2023
1 parent 6f965a0 commit 9bade9f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 44 deletions.
17 changes: 0 additions & 17 deletions example/cmd/device-simple/res/devices/simple-device.toml

This file was deleted.

17 changes: 17 additions & 0 deletions example/cmd/device-simple/res/devices/simple-device.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
deviceList:
- name: Simple-Device01
profileName: Simple-Device
description: Example of Simple Device
labels:
- industrial
protocols:
other:
Address: simple01
Port: 300
autoEvents:
- interval: 10s
onChange: false
sourceName: Switch
- interval: 30s
onChange: false
sourceName: Image
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ go 1.20
require (
github.com/OneOfOne/xxhash v1.2.8
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.47
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.28
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.30
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.18
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/pelletier/go-toml v1.9.5
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
github.com/stretchr/testify v1.8.2
gopkg.in/yaml.v3 v3.0.1
Expand Down Expand Up @@ -54,6 +53,7 @@ require (
github.com/nats-io/nats.go v1.24.0 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spiffe/go-spiffe/v2 v2.1.2 // indirect
github.com/stretchr/objx v0.5.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.47 h1:KG2CAxu1BKwvT8vMONZ
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.0.0-dev.47/go.mod h1:W26ejUzaSRzq/b/8oIdB8hKve4xW55XbCkfj8pvQNfE=
github.com/edgexfoundry/go-mod-configuration/v3 v3.0.0-dev.7 h1:pOH2GLDg1KB4EmAzo6IEvl4NEVFAw3ywxPUMa5Wi1Vw=
github.com/edgexfoundry/go-mod-configuration/v3 v3.0.0-dev.7/go.mod h1:ZZbOu7K0/P8B1VKhZygVujLQyhvWuPe0E2vC/k2yscw=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.28 h1:aNHlmF2uPcwUr7VKvNVJXdxMDaI41j2laoGxZy5H95g=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.28/go.mod h1:SYoD+tmUP/zwWuuIySmsQSjPx6MHP+2w9FsLgm0IR7A=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.30 h1:diarfNbfUeSYD7Hq8pOqHzxMtIbaPO6W8nWq34W8X6g=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.0.0-dev.30/go.mod h1:SYoD+tmUP/zwWuuIySmsQSjPx6MHP+2w9FsLgm0IR7A=
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.18 h1:8empqTOIVhKzebC3gnP9C0LGQvwXtHLJF8x8AqlTGLM=
github.com/edgexfoundry/go-mod-messaging/v3 v3.0.0-dev.18/go.mod h1:Sf7lvKtICWRy0Tmvw+bV6L1wdXKRaTSmKmJKw4Caxe4=
github.com/edgexfoundry/go-mod-registry/v3 v3.0.0-dev.5 h1:FUUfsQUKHJqSwOXEj0j/qAn+uoWzM8nV7IOapGf7D80=
Expand Down
33 changes: 19 additions & 14 deletions internal/provision/devices.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-
//
// Copyright (C) 2017-2018 Canonical Ltd
// Copyright (C) 2018-2021 IOTech Ltd
// Copyright (C) 2018-2023 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -14,9 +14,6 @@ import (
"path/filepath"
"strings"

"github.com/edgexfoundry/device-sdk-go/v3/internal/cache"
"github.com/edgexfoundry/device-sdk-go/v3/internal/container"

bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v3/di"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
Expand All @@ -25,43 +22,51 @@ import (
"github.com/edgexfoundry/go-mod-core-contracts/v3/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v3/models"
"github.com/google/uuid"
"github.com/pelletier/go-toml"
"gopkg.in/yaml.v3"

"github.com/edgexfoundry/device-sdk-go/v3/internal/cache"
"github.com/edgexfoundry/device-sdk-go/v3/internal/container"
)

func LoadDevices(path string, dic *di.Container) errors.EdgeX {
if path == "" {
return nil
}
lc := bootstrapContainer.LoggingClientFrom(dic.Get)

absPath, err := filepath.Abs(path)
if err != nil {
return errors.NewCommonEdgeX(errors.KindServerError, "failed to create absolute path", err)
}

fileInfo, err := os.ReadDir(absPath)
files, err := os.ReadDir(absPath)
if err != nil {
return errors.NewCommonEdgeX(errors.KindServerError, "failed to read directory", err)
}

if len(files) == 0 {
return nil
}

lc := bootstrapContainer.LoggingClientFrom(dic.Get)
lc.Infof("Loading pre-defined devices from %s(%d files found)", absPath, len(files))

var addDevicesReq []requests.AddDeviceRequest
serviceName := container.DeviceServiceFrom(dic.Get).Name
lc.Infof("Loading pre-defined devices from %s", absPath)
for _, file := range fileInfo {
for _, file := range files {
var devices []dtos.Device
fullPath := filepath.Join(absPath, file.Name())
if strings.HasSuffix(fullPath, ".toml") {
if strings.HasSuffix(fullPath, yamlExt) || strings.HasSuffix(fullPath, ymlExt) {
content, err := os.ReadFile(fullPath)
if err != nil {
lc.Errorf("Failed to read %s: %v", fullPath, err)
continue
}
d := struct {
DeviceList []dtos.Device
DeviceList []dtos.Device `yaml:"deviceList"`
}{}
err = toml.Unmarshal(content, &d)
err = yaml.Unmarshal(content, &d)
if err != nil {
lc.Errorf("Failed to decode %s: %v", fullPath, err)
lc.Errorf("Failed to YAML decode %s: %v", fullPath, err)
continue
}
devices = d.DeviceList
Expand All @@ -73,7 +78,7 @@ func LoadDevices(path string, dic *di.Container) errors.EdgeX {
}
err = json.Unmarshal(content, &devices)
if err != nil {
lc.Errorf("Failed to decode %s: %v", fullPath, err)
lc.Errorf("Failed to JSON decode %s: %v", fullPath, err)
continue
}
} else {
Expand Down
23 changes: 14 additions & 9 deletions internal/provision/profiles.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: Go; indent-tabs-mode: t -*-
//
// Copyright (C) 2017-2018 Canonical Ltd
// Copyright (C) 2018-2021 IOTech Ltd
// Copyright (C) 2018-2023 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -15,8 +15,6 @@ import (
"path/filepath"
"strings"

"github.com/edgexfoundry/device-sdk-go/v3/internal/cache"

bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v3/di"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
Expand All @@ -25,6 +23,8 @@ import (
"github.com/edgexfoundry/go-mod-core-contracts/v3/errors"
"github.com/google/uuid"
"gopkg.in/yaml.v3"

"github.com/edgexfoundry/device-sdk-go/v3/internal/cache"
)

const (
Expand All @@ -37,22 +37,27 @@ func LoadProfiles(path string, dic *di.Container) errors.EdgeX {
if path == "" {
return nil
}
lc := bootstrapContainer.LoggingClientFrom(dic.Get)

absPath, err := filepath.Abs(path)
if err != nil {
return errors.NewCommonEdgeX(errors.KindServerError, "failed to create absolute path", err)
}

fileInfo, err := os.ReadDir(absPath)
files, err := os.ReadDir(absPath)
if err != nil {
return errors.NewCommonEdgeX(errors.KindServerError, "failed to read directory", err)
}

if len(files) == 0 {
return nil
}

lc := bootstrapContainer.LoggingClientFrom(dic.Get)
lc.Infof("Loading pre-defined profiles from %s(%d files found)", absPath, len(files))

var addProfilesReq []requests.DeviceProfileRequest
dpc := bootstrapContainer.DeviceProfileClientFrom(dic.Get)
lc.Infof("Loading pre-defined profiles from %s", absPath)
for _, file := range fileInfo {
for _, file := range files {
var profile dtos.DeviceProfile
fullPath := filepath.Join(absPath, file.Name())
if strings.HasSuffix(fullPath, yamlExt) || strings.HasSuffix(fullPath, ymlExt) {
Expand All @@ -64,7 +69,7 @@ func LoadProfiles(path string, dic *di.Container) errors.EdgeX {

err = yaml.Unmarshal(content, &profile)
if err != nil {
lc.Errorf("Failed to decode profile %s: %v", file.Name(), err)
lc.Errorf("Failed to YAML decode profile %s: %v", file.Name(), err)
continue
}
} else if strings.HasSuffix(fullPath, jsonExt) {
Expand All @@ -76,7 +81,7 @@ func LoadProfiles(path string, dic *di.Container) errors.EdgeX {

err = json.Unmarshal(content, &profile)
if err != nil {
lc.Errorf("Failed to decode profile %s: %v", file.Name(), err)
lc.Errorf("Failed to JSON decode profile %s: %v", file.Name(), err)
continue
}
} else {
Expand Down

0 comments on commit 9bade9f

Please sign in to comment.