sub.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2021 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. "github.com/edgexfoundry/go-mod-messaging/v2/messaging"
  20. "github.com/edgexfoundry/go-mod-messaging/v2/pkg/types"
  21. "github.com/lf-edge/ekuiper/internal/conf"
  22. "os"
  23. )
  24. func subEventsFromZMQ() {
  25. var msgConfig1 = types.MessageBusConfig{
  26. SubscribeHost: types.HostInfo{
  27. Host: "localhost",
  28. Port: 5571,
  29. Protocol: "tcp",
  30. },
  31. Type: messaging.ZeroMQ,
  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: "", 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. var 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 subEventsFromMQTT(host string) {
  66. var msgConfig1 = types.MessageBusConfig{
  67. SubscribeHost: types.HostInfo{
  68. Host: host,
  69. Port: 1883,
  70. Protocol: "tcp",
  71. },
  72. Type: messaging.MQTT,
  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. var 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 subEventsFromRedis(host string) {
  107. var msgConfig1 = types.MessageBusConfig{
  108. SubscribeHost: types.HostInfo{
  109. Host: host,
  110. Port: 6379,
  111. Protocol: "redis",
  112. },
  113. Type: messaging.Redis,
  114. }
  115. if msgClient, err := messaging.NewMessageClient(msgConfig1); err != nil {
  116. conf.Log.Fatal(err)
  117. } else {
  118. if ec := msgClient.Connect(); ec != nil {
  119. conf.Log.Fatal(ec)
  120. } else {
  121. //log.Infof("The connection to edgex messagebus is established successfully.")
  122. messages := make(chan types.MessageEnvelope)
  123. topics := []types.TopicChannel{{Topic: "result", Messages: messages}}
  124. err := make(chan error)
  125. if e := msgClient.Subscribe(topics, err); e != nil {
  126. //log.Errorf("Failed to subscribe to edgex messagebus topic %s.\n", e)
  127. conf.Log.Fatal(e)
  128. } else {
  129. var count = 0
  130. for {
  131. select {
  132. case e1 := <-err:
  133. conf.Log.Errorf("%s\n", e1)
  134. return
  135. case env := <-messages:
  136. count++
  137. fmt.Printf("%s\n", env.Payload)
  138. if count == 1 {
  139. return
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. func main() {
  148. if len(os.Args) == 1 {
  149. subEventsFromZMQ()
  150. } else if len(os.Args) == 3 {
  151. if v := os.Args[1]; v == "mqtt" {
  152. subEventsFromMQTT(os.Args[2])
  153. }
  154. if v := os.Args[1]; v == "redis" {
  155. subEventsFromRedis(os.Args[2])
  156. }
  157. }
  158. }