Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(command): publish device service response to external MQTT #4166

Merged
merged 3 commits into from
Oct 3, 2022
Merged

feat(command): publish device service response to external MQTT #4166

merged 3 commits into from
Oct 3, 2022

Conversation

chr1shung
Copy link

fix #4076

If your build fails due to your commit message not passing the build checks, please review the guidelines here: https://github.com/edgexfoundry/edgex-go/blob/main/.github/Contributing.md

PR Checklist

Please check if your PR fulfills the following requirements:

  • I am not introducing a breaking change (if you are, flag in conventional commit message with BREAKING CHANGE: describing the break)
  • I am not introducing a new dependency (add notes below if you are)
  • I have added unit tests for the new feature or bug fix (if not, why?)
  • I have fully tested (add details below) this the new feature or bug fix (if not, why?)
  • I have opened a PR for the related docs change (if not, why?)

Testing Instructions

  1. Run core-command from this branch with external MQTT broker and MESSAGEQUEUE_REQUIRED=true environment variable
  2. Run device-simple from device-sdk-go
  3. Send command request to edgex/command/request/<device-name>/<command-name>/<method> topic. For example send following request to edgex/command/Simple-Device01/Xrotation/get:
{
  "CorrelationID": "14a42ea6-c394-41c3-8bcd-a29b9f5e6835",
  "ApiVersion": "v2",
  "RequestId": "e6e8a2f4-eb14-4649-9e2b-175247911369",
  "ContentType": "application/json",
  "QueryParams": {
    "ds-pushevent": "no",
    "ds-returnevent": "yes"
  },
}
  1. check the response is received on edgex/command/response/<device-name>/<command-name>/<method> topic:
{"ReceivedTopic":"edgex/command/response/device-simple/Simple-Device01/Xrotation/get","CorrelationID":"14a42ea6-c394-41c3-8bcd-a29b9f5e6835","ApiVersion":"v2","RequestID":"e6e8a2f4-eb14-4649-9e2b-175247911369","ErrorCode":0,"Payload":"eyJhcGlWZXJzaW9uIjoidjIiLCJzdGF0dXNDb2RlIjoyMDAsImV2ZW50Ijp7ImFwaVZlcnNpb24iOiJ2MiIsImlkIjoiZDVhMDA2YjYtYmY4OS00ZDQ4LWEyOWUtMTFhYzRlMzJjZWY3IiwiZGV2aWNlTmFtZSI6IlNpbXBsZS1EZXZpY2UwMSIsInByb2ZpbGVOYW1lIjoiU2ltcGxlLURldmljZSIsInNvdXJjZU5hbWUiOiJYcm90YXRpb24iLCJvcmlnaW4iOjE2NjM5MDgzMDQ1Mjk4OTEwMDAsInJlYWRpbmdzIjpbeyJpZCI6IjgwMTExNWI3LWEyZTEtNDE2Ny1iNzVmLWRkMjg1ODY2YjZlNiIsIm9yaWdpbiI6MTY2MzkwODMwNDUyOTg5MTAwMCwiZGV2aWNlTmFtZSI6IlNpbXBsZS1EZXZpY2UwMSIsInJlc291cmNlTmFtZSI6Ilhyb3RhdGlvbiIsInByb2ZpbGVOYW1lIjoiU2ltcGxlLURldmljZSIsInZhbHVlVHlwZSI6IkludDMyIiwidW5pdHMiOiJycG0iLCJ2YWx1ZSI6IjIwIn1dfX0=","ContentType":"application/json","QueryParams":{}}

New Dependency Instructions (If applicable)

Copy link
Member

@lenny-goodell lenny-goodell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just small name tweak for clarification

internal/core/command/controller/messaging/internal.go Outdated Show resolved Hide resolved
subscribe to device command response from device service via internal
message bus and publish response back to 3rd-party requester

Signed-off-by: Chris Hung <[email protected]>
@chr1shung chr1shung requested review from cloudxxx8 and lenny-goodell and removed request for lenny-goodell and cloudxxx8 September 28, 2022 05:12
internal/core/command/controller/messaging/external.go Outdated Show resolved Hide resolved
internal/core/command/controller/messaging/router.go Outdated Show resolved Hide resolved
Set(string, string)
}

func NewMessagingRouter() MessagingRouter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this Router determining which bus the response message goes to?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad I forgot the most cirtical problem ...

A quick thought came to my mind is to maintain 2 maps internally :

func (r *MessagingRouter) Get(id string) (string, bool) {
    topic, ok := r.ExternalMap[id]
    if ok {
        return topic, true
    }

    topic, _ = r.InternalMap[id]
    return topic, false
}

func (r *MessagingRouter) Set(id string, topic string, source string) {
    switch source {
    case "internal":
        r.InternalMap[id] = topic
    case "external":
        r.ExternalMap[id] = topic
    }
}

Will try to improve it or find if there's better solution tomorrow ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was what I had in mind.

internal/core/command/main.go Outdated Show resolved Hide resolved
internal/core/command/controller/messaging/internal.go Outdated Show resolved Hide resolved
lenny-goodell
lenny-goodell previously approved these changes Sep 29, 2022
Copy link
Member

@lenny-goodell lenny-goodell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Save active requests in two maps using requestId as keys.
This allows command service to know where to route the response
(to external MQTT or internal MessageBus)

Signed-off-by: Chris Hung <[email protected]>
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link
Member

@cloudxxx8 cloudxxx8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cloudxxx8 cloudxxx8 merged commit 1845739 into edgexfoundry:main Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants