Skip to content

Commit

Permalink
Refactor to use go-mod-bootstrap package (#432)
Browse files Browse the repository at this point in the history
* Refactor to use common go-mod-bootstrap package

leverage bootstrap config structs and update configuration file
to make it consistent with other services.

Signed-off-by: Chris Hung <chris@iotechsys.com>
  • Loading branch information
Chris Hung authored Apr 6, 2020
1 parent 0605716 commit 79a40a3
Show file tree
Hide file tree
Showing 32 changed files with 637 additions and 1,171 deletions.
78 changes: 0 additions & 78 deletions async.go

This file was deleted.

14 changes: 13 additions & 1 deletion example/cmd/device-simple/Attribution.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,16 @@ ugorji/go (MIT) https://github.com/ugorji/go
https://github.com/ugorji/go/blob/master/LICENSE

kr/logfmt (MIT) https://github.com/kr/logfmt
https://github.com/kr/logfmt/blob/master/Readme
https://github.com/kr/logfmt/blob/master/Readme

hashicorp/consul/api (Mozilla Public License 2.0) https://github.com/hashicorp/consul/api
https://github.com/hashicorp/consul/blob/master/LICENSE

edgexfoundry/go-mod-bootstrap (Apache 2.0) https://github.com/edgexfoundry/go-mod-bootstrap
https://github.com/edgexfoundry/go-mod-bootstrap/blob/master/LICENSE

edgexfoundry/go-mod-secrets (Apache 2.0) https://github.com/edgexfoundry/go-mod-secrets
https://github.com/edgexfoundry/go-mod-secrets/blob/master/LICENSE

edgexfoundry/go-mod-configuration (Apache 2.0) https://github.com/edgexfoundry/go-mod-configuration
https://github.com/edgexfoundry/go-mod-configuration/blob/master/LICENSE
66 changes: 31 additions & 35 deletions example/cmd/device-simple/res/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,70 @@
LogLevel = 'INFO'

[Service]
Host = "localhost"
BootTimeout = 30000
CheckInterval = '10s'
ClientMonitor = 15000
Host = 'localhost'
Port = 49990
Protocol = 'http'
StartupMsg = 'device simple started'
Timeout = 5000
ConnectRetries = 20
Labels = []
OpenMsg = "device simple started"
Timeout = 5000
EnableAsyncReadings = true
AsyncBufferSize = 16

[Registry]
Host = "localhost"
Host = 'localhost'
Port = 8500
Type = "consul"
CheckInterval = "10s"
FailLimit = 3
FailWaitTime = 10
Type = 'consul'

[Clients]
[Clients.Data]
Name = "edgex-core-data"
Protocol = "http"
Host = "localhost"
Protocol = 'http'
Host = 'localhost'
Port = 48080
Timeout = 5000

[Clients.Metadata]
Name = "edgex-core-metadata"
Protocol = "http"
Host = "localhost"
Protocol = 'http'
Host = 'localhost'
Port = 48081
Timeout = 5000

[Clients.Logging]
Name = "edgex-support-logging"
Protocol = "http"
Host = "localhost"
Protocol = 'http'
Host = 'localhost'
Port = 48061

[Device]
DataTransform = true
InitCmd = ""
InitCmdArgs = ""
InitCmd = ''
InitCmdArgs = ''
MaxCmdOps = 128
MaxCmdValueLen = 256
RemoveCmd = ""
RemoveCmdArgs = ""
ProfilesDir = "./res"
RemoveCmd = ''
RemoveCmdArgs = ''
ProfilesDir = './res'
UpdateLastConnected = false

[Logging]
EnableRemote = false
File = "./device-simple.log"
File = './device-simple.log'

# Pre-define Devices
[[DeviceList]]
Name = "Simple-Device01"
Profile = "Simple-Device"
Description = "Example of Simple Device"
Labels = [ "industrial" ]
Name = 'Simple-Device01'
Profile = 'Simple-Device'
Description = 'Example of Simple Device'
Labels = [ 'industrial' ]
[DeviceList.Protocols]
[DeviceList.Protocols.other]
Address = "simple01"
Port = "300"
Address = 'simple01'
Port = '300'
[[DeviceList.AutoEvents]]
Frequency = "10s"
Frequency = '10s'
OnChange = false
Resource = "Switch"
Resource = 'Switch'
[[DeviceList.AutoEvents]]
Frequency = "30s"
Frequency = '30s'
OnChange = false
Resource = "Image"
Resource = 'Image'
12 changes: 5 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ module github.com/edgexfoundry/device-sdk-go

require (
github.com/OneOfOne/xxhash v1.2.6
github.com/edgexfoundry/go-mod-bootstrap v0.0.26
github.com/edgexfoundry/go-mod-core-contracts v0.1.36
github.com/edgexfoundry/go-mod-registry v0.1.0
github.com/edgexfoundry/go-mod-registry v0.1.17
github.com/google/uuid v1.1.0
github.com/gorilla/context v0.0.0-20181012153548-51ce91d2eadd // indirect
github.com/gorilla/mux v1.6.2
github.com/jessevdk/go-flags v1.4.0
github.com/pelletier/go-toml v1.2.0
github.com/gorilla/mux v1.7.1
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.5.1
github.com/ugorji/go v1.1.4
gopkg.in/yaml.v2 v2.2.2
gopkg.in/yaml.v2 v2.2.8
)

go 1.13
65 changes: 35 additions & 30 deletions internal/autoevent/executor.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// -*- Mode: Go; indent-tabs-mode: t -*-
//
// Copyright (C) 2019 IOTech Ltd
// Copyright (C) 2019-2020 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

package autoevent

import (
"context"
"fmt"
"sync"
"time"
Expand All @@ -19,7 +20,7 @@ import (
)

type Executor interface {
Run()
Run(ctx context.Context, wg *sync.WaitGroup)
Stop()
}

Expand All @@ -33,39 +34,43 @@ type executor struct {
}

// Run triggers this Executor executes the handler for the resource periodically
func (e *executor) Run() {
func (e *executor) Run(ctx context.Context, wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
for {
if e.stop {
break
}
time.Sleep(e.duration)
select {
case <-ctx.Done():
return
case <-time.After(e.duration):
if e.stop {
return
}

common.LoggingClient.Debug(fmt.Sprintf("AutoEvent - executing %v", e.autoEvent))
evt, appErr := readResource(e)
if appErr != nil {
common.LoggingClient.Error(fmt.Sprintf("AutoEvent - error occurs when reading resource %s",
e.autoEvent.Resource))
continue
}
common.LoggingClient.Debug(fmt.Sprintf("AutoEvent - executing %v", e.autoEvent))
evt, appErr := readResource(e)
if appErr != nil {
common.LoggingClient.Error(fmt.Sprintf("AutoEvent - error occurs when reading resource %s",
e.autoEvent.Resource))
continue
}

if evt != nil {
if e.autoEvent.OnChange {
if compareReadings(e, evt.Readings, evt.HasBinaryValue()) {
common.LoggingClient.Debug(fmt.Sprintf("AutoEvent - readings are the same as previous one %v", e.lastReadings))
continue
if evt != nil {
if e.autoEvent.OnChange {
if compareReadings(e, evt.Readings, evt.HasBinaryValue()) {
common.LoggingClient.Debug(fmt.Sprintf("AutoEvent - readings are the same as previous one %v", e.lastReadings))
continue
}
}
common.LoggingClient.Debug(fmt.Sprintf("AutoEvent - pushing event %s", evt.String()))
event := &dsModels.Event{Event: evt.Event}
// Attach origin timestamp for events if none yet specified
if event.Origin == 0 {
event.Origin = common.GetUniqueOrigin()
}
go common.SendEvent(event)
} else {
common.LoggingClient.Info(fmt.Sprintf("AutoEvent - no event generated when reading resource %s", e.autoEvent.Resource))
}
common.LoggingClient.Debug(fmt.Sprintf("AutoEvent - pushing event %s", evt.String()))
event := &dsModels.Event{Event: evt.Event}
// Attach origin timestamp for events if none yet specified
if event.Origin == 0 {
event.Origin = common.GetUniqueOrigin()
}
go common.SendEvent(event)
} else {
common.LoggingClient.Info(fmt.Sprintf("AutoEvent - no event generated when reading resource %s",
e.autoEvent.Resource))
continue
}
}
}
Expand Down
Loading

0 comments on commit 79a40a3

Please sign in to comment.