-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: replace REST device callbacks with System Events
BREAKING CHANGE: the following device callback REST endpoints are removed: - POST /callback/device - PUT /callback/device - DELETE /callback/device/name/{name} closes #1259 Signed-off-by: Chris Hung <chris@iotechsys.com>
- Loading branch information
Chris Hung
committed
Jan 18, 2023
1 parent
8541928
commit 3f884ed
Showing
7 changed files
with
1,422 additions
and
62 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 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,108 @@ | ||
// | ||
// Copyright (C) 2023 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package messaging | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
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" | ||
"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/edgexfoundry/go-mod-messaging/v3/pkg/types" | ||
|
||
"github.com/edgexfoundry/device-sdk-go/v3/internal/application" | ||
"github.com/edgexfoundry/device-sdk-go/v3/internal/container" | ||
) | ||
|
||
const SystemEventTopic = "SystemEventTopic" | ||
|
||
func DeviceCallback(ctx context.Context, dic *di.Container) errors.EdgeX { | ||
lc := bootstrapContainer.LoggingClientFrom(dic.Get) | ||
messageBusInfo := container.ConfigurationFrom(dic.Get).MessageBus | ||
systemEventTopic := messageBusInfo.Topics[SystemEventTopic] | ||
|
||
messages := make(chan types.MessageEnvelope) | ||
messageErrors := make(chan error) | ||
topics := []types.TopicChannel{ | ||
{ | ||
Topic: systemEventTopic, | ||
Messages: messages, | ||
}, | ||
} | ||
|
||
messageBus := bootstrapContainer.MessagingClientFrom(dic.Get) | ||
err := messageBus.Subscribe(topics, messageErrors) | ||
if err != nil { | ||
return errors.NewCommonEdgeXWrapper(err) | ||
} | ||
|
||
go func() { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
lc.Infof("Exiting waiting for MessageBus '%s' topic messages", systemEventTopic) | ||
return | ||
case err = <-messageErrors: | ||
lc.Error(err.Error()) | ||
case msgEnvelope := <-messages: | ||
lc.Debugf("System event received on message queue. Topic: %s, Correlation-id: %s ", systemEventTopic, msgEnvelope.CorrelationID) | ||
|
||
var systemEvent dtos.SystemEvent | ||
err := json.Unmarshal(msgEnvelope.Payload, &systemEvent) | ||
if err != nil { | ||
lc.Errorf("failed to JSON decoding system event: %s", err.Error()) | ||
continue | ||
} | ||
|
||
serviceName := container.DeviceServiceFrom(dic.Get).Name | ||
if systemEvent.Owner != serviceName { | ||
lc.Errorf("unmatched system event owner %s with service name %s", systemEvent.Owner, serviceName) | ||
continue | ||
} | ||
|
||
if systemEvent.Type != common.DeviceSystemEventType { | ||
lc.Errorf("unknown system event type %s", systemEvent.Type) | ||
continue | ||
} | ||
|
||
var device dtos.Device | ||
err = systemEvent.DecodeDetails(&device) | ||
if err != nil { | ||
lc.Errorf("failed to decode system event details: %s", err.Error()) | ||
continue | ||
} | ||
|
||
switch systemEvent.Action { | ||
case common.DeviceSystemEventActionAdd: | ||
err = application.AddDevice(requests.NewAddDeviceRequest(device), dic) | ||
if err != nil { | ||
lc.Error(err.Error(), common.CorrelationHeader, msgEnvelope.CorrelationID) | ||
} | ||
case common.DeviceSystemEventActionUpdate: | ||
deviceModel := dtos.ToDeviceModel(device) | ||
updateDeviceDTO := dtos.FromDeviceModelToUpdateDTO(deviceModel) | ||
err = application.UpdateDevice(requests.NewUpdateDeviceRequest(updateDeviceDTO), dic) | ||
if err != nil { | ||
lc.Error(err.Error(), common.CorrelationHeader, msgEnvelope.CorrelationID) | ||
} | ||
case common.DeviceSystemEventActionDelete: | ||
err = application.DeleteDevice(device.Name, dic) | ||
if err != nil { | ||
lc.Error(err.Error(), common.CorrelationHeader, msgEnvelope.CorrelationID) | ||
} | ||
default: | ||
lc.Errorf("unknown device system event action %s", systemEvent.Action) | ||
} | ||
} | ||
} | ||
}() | ||
|
||
return nil | ||
} |
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,3 @@ | ||
API changes from v2 | ||
|
||
* Remove /callback/device, /callback/device/name/{name} endpoints |
Oops, something went wrong.