-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1270 from hahattan/issue-1100
feat: implement support for ProvisionWatchersDir
- Loading branch information
Showing
7 changed files
with
131 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
example/cmd/device-simple/res/provisionwatchers/Simple-Provision-Watcher.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "Simple-Provision-Watcher", | ||
"labels": [ | ||
"simple" | ||
], | ||
"identifiers": { | ||
"Address":"simple[0-9]+", | ||
"Port":"3[0-9]{2}" | ||
}, | ||
"blockingIdentifiers": { | ||
"Port": [ | ||
"397", | ||
"398", | ||
"399" | ||
] | ||
}, | ||
"profileName": "Simple-Device", | ||
"serviceName": "device-simple", | ||
"adminState": "UNLOCKED", | ||
"autoEvents": [ | ||
{ | ||
"interval": "15s", | ||
"sourceName": "SwitchButton" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// Copyright (C) 2023 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package provision | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"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" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/requests" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/errors" | ||
"github.com/google/uuid" | ||
|
||
"github.com/edgexfoundry/device-sdk-go/v3/internal/cache" | ||
) | ||
|
||
func LoadProvisionWatchers(path string, dic *di.Container) errors.EdgeX { | ||
if path == "" { | ||
return nil | ||
} | ||
|
||
absPath, err := filepath.Abs(path) | ||
if err != nil { | ||
return errors.NewCommonEdgeX(errors.KindServerError, "failed to create absolute path", err) | ||
} | ||
|
||
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 := container.LoggingClientFrom(dic.Get) | ||
lc.Infof("Loading pre-defined provision watchers from %s(%d files found)", absPath, len(files)) | ||
|
||
var addProvisionWatchersReq []requests.AddProvisionWatcherRequest | ||
for _, file := range files { | ||
var watcher dtos.ProvisionWatcher | ||
filename := filepath.Join(absPath, file.Name()) | ||
if !strings.HasSuffix(filename, jsonExt) { | ||
continue | ||
} | ||
|
||
data, err := os.ReadFile(filename) | ||
if err != nil { | ||
lc.Errorf("failed to read %s: %v", filename, err) | ||
continue | ||
} | ||
|
||
if err := json.Unmarshal(data, &watcher); err != nil { | ||
lc.Errorf("failed to JSON decode %s: %v", filename, err) | ||
continue | ||
} | ||
|
||
err = common.Validate(watcher) | ||
if err != nil { | ||
lc.Errorf("ProvisionWatcher %s validation failed: %v", watcher.Name, err) | ||
continue | ||
} | ||
|
||
if _, ok := cache.ProvisionWatchers().ForName(watcher.Name); ok { | ||
lc.Infof("ProvisionWatcher %s exists, using the existing one", watcher.Name) | ||
} else { | ||
lc.Infof("ProvisionWatcher %s not found in Metadata, adding it...", watcher.Name) | ||
req := requests.NewAddProvisionWatcherRequest(watcher) | ||
addProvisionWatchersReq = append(addProvisionWatchersReq, req) | ||
} | ||
} | ||
|
||
if len(addProvisionWatchersReq) == 0 { | ||
return nil | ||
} | ||
|
||
pwc := container.ProvisionWatcherClientFrom(dic.Get) | ||
ctx := context.WithValue(context.Background(), common.CorrelationHeader, uuid.NewString()) //nolint: staticcheck | ||
_, edgexErr := pwc.Add(ctx, addProvisionWatchersReq) | ||
return edgexErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters