sub.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2021-2023 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build edgex
  15. // +build edgex
  16. package main
  17. import (
  18. "fmt"
  19. "os"
  20. "github.com/edgexfoundry/go-mod-messaging/v3/messaging"
  21. "github.com/edgexfoundry/go-mod-messaging/v3/pkg/types"
  22. "github.com/lf-edge/ekuiper/internal/conf"
  23. )
  24. func subEventsFromMQTT(host string) {
  25. msgConfig1 := types.MessageBusConfig{
  26. Broker: types.HostInfo{
  27. Host: host,
  28. Port: 1883,
  29. Protocol: "tcp",
  30. },
  31. Type: messaging.MQTT,
  32. }
  33. if msgClient, err := messaging.NewMessageClient(msgConfig1); err != nil {
  34. conf.Log.Fatal(err)
  35. } else {
  36. if ec := msgClient.Connect(); ec != nil {
  37. conf.Log.Fatal(ec)
  38. } else {
  39. // log.Infof("The connection to edgex messagebus is established successfully.")
  40. messages := make(chan types.MessageEnvelope)
  41. topics := []types.TopicChannel{{Topic: "result", Messages: messages}}
  42. err := make(chan error)
  43. if e := msgClient.Subscribe(topics, err); e != nil {
  44. // log.Errorf("Failed to subscribe to edgex messagebus topic %s.\n", e)
  45. conf.Log.Fatal(e)
  46. } else {
  47. count := 0
  48. for {
  49. select {
  50. case e1 := <-err:
  51. conf.Log.Errorf("%s\n", e1)
  52. return
  53. case env := <-messages:
  54. count++
  55. fmt.Printf("%s\n", env.Payload)
  56. if count == 1 {
  57. return
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. func subEventsFromRedis(host string) {
  66. msgConfig1 := types.MessageBusConfig{
  67. Broker: types.HostInfo{
  68. Host: host,
  69. Port: 6379,
  70. Protocol: "redis",
  71. },
  72. Type: messaging.Redis,
  73. }
  74. if msgClient, err := messaging.NewMessageClient(msgConfig1); err != nil {
  75. conf.Log.Fatal(err)
  76. } else {
  77. if ec := msgClient.Connect(); ec != nil {
  78. conf.Log.Fatal(ec)
  79. } else {
  80. // log.Infof("The connection to edgex messagebus is established successfully.")
  81. messages := make(chan types.MessageEnvelope)
  82. topics := []types.TopicChannel{{Topic: "result", Messages: messages}}
  83. err := make(chan error)
  84. if e := msgClient.Subscribe(topics, err); e != nil {
  85. // log.Errorf("Failed to subscribe to edgex messagebus topic %s.\n", e)
  86. conf.Log.Fatal(e)
  87. } else {
  88. count := 0
  89. for {
  90. select {
  91. case e1 := <-err:
  92. conf.Log.Errorf("%s\n", e1)
  93. return
  94. case env := <-messages:
  95. count++
  96. fmt.Printf("%s\n", env.Payload)
  97. if count == 1 {
  98. return
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. func main() {
  107. if len(os.Args) == 3 {
  108. if v := os.Args[1]; v == "mqtt" {
  109. subEventsFromMQTT(os.Args[2])
  110. }
  111. if v := os.Args[1]; v == "redis" {
  112. subEventsFromRedis(os.Args[2])
  113. }
  114. }
  115. }