sub.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // +build edgex
  15. package main
  16. import (
  17. "fmt"
  18. "github.com/edgexfoundry/go-mod-messaging/v2/messaging"
  19. "github.com/edgexfoundry/go-mod-messaging/v2/pkg/types"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "os"
  22. )
  23. func subEventsFromZMQ() {
  24. var msgConfig1 = types.MessageBusConfig{
  25. SubscribeHost: types.HostInfo{
  26. Host: "localhost",
  27. Port: 5571,
  28. Protocol: "tcp",
  29. },
  30. Type: messaging.ZeroMQ,
  31. }
  32. if msgClient, err := messaging.NewMessageClient(msgConfig1); err != nil {
  33. conf.Log.Fatal(err)
  34. } else {
  35. if ec := msgClient.Connect(); ec != nil {
  36. conf.Log.Fatal(ec)
  37. } else {
  38. //log.Infof("The connection to edgex messagebus is established successfully.")
  39. messages := make(chan types.MessageEnvelope)
  40. topics := []types.TopicChannel{{Topic: "", Messages: messages}}
  41. err := make(chan error)
  42. if e := msgClient.Subscribe(topics, err); e != nil {
  43. //log.Errorf("Failed to subscribe to edgex messagebus topic %s.\n", e)
  44. conf.Log.Fatal(e)
  45. } else {
  46. var count = 0
  47. for {
  48. select {
  49. case e1 := <-err:
  50. conf.Log.Errorf("%s\n", e1)
  51. return
  52. case env := <-messages:
  53. count++
  54. fmt.Printf("%s\n", env.Payload)
  55. if count == 1 {
  56. return
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. func subEventsFromMQTT(host string) {
  65. var msgConfig1 = types.MessageBusConfig{
  66. SubscribeHost: types.HostInfo{
  67. Host: host,
  68. Port: 1883,
  69. Protocol: "tcp",
  70. },
  71. Type: messaging.MQTT,
  72. }
  73. if msgClient, err := messaging.NewMessageClient(msgConfig1); err != nil {
  74. conf.Log.Fatal(err)
  75. } else {
  76. if ec := msgClient.Connect(); ec != nil {
  77. conf.Log.Fatal(ec)
  78. } else {
  79. //log.Infof("The connection to edgex messagebus is established successfully.")
  80. messages := make(chan types.MessageEnvelope)
  81. topics := []types.TopicChannel{{Topic: "result", Messages: messages}}
  82. err := make(chan error)
  83. if e := msgClient.Subscribe(topics, err); e != nil {
  84. //log.Errorf("Failed to subscribe to edgex messagebus topic %s.\n", e)
  85. conf.Log.Fatal(e)
  86. } else {
  87. var count int = 0
  88. for {
  89. select {
  90. case e1 := <-err:
  91. conf.Log.Errorf("%s\n", e1)
  92. return
  93. case env := <-messages:
  94. count++
  95. fmt.Printf("%s\n", env.Payload)
  96. if count == 1 {
  97. return
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. func subEventsFromRedis(host string) {
  106. var msgConfig1 = types.MessageBusConfig{
  107. SubscribeHost: types.HostInfo{
  108. Host: host,
  109. Port: 6379,
  110. Protocol: "redis",
  111. },
  112. Type: messaging.Redis,
  113. }
  114. if msgClient, err := messaging.NewMessageClient(msgConfig1); err != nil {
  115. conf.Log.Fatal(err)
  116. } else {
  117. if ec := msgClient.Connect(); ec != nil {
  118. conf.Log.Fatal(ec)
  119. } else {
  120. //log.Infof("The connection to edgex messagebus is established successfully.")
  121. messages := make(chan types.MessageEnvelope)
  122. topics := []types.TopicChannel{{Topic: "result", Messages: messages}}
  123. err := make(chan error)
  124. if e := msgClient.Subscribe(topics, err); e != nil {
  125. //log.Errorf("Failed to subscribe to edgex messagebus topic %s.\n", e)
  126. conf.Log.Fatal(e)
  127. } else {
  128. var count int = 0
  129. for {
  130. select {
  131. case e1 := <-err:
  132. conf.Log.Errorf("%s\n", e1)
  133. return
  134. case env := <-messages:
  135. count++
  136. fmt.Printf("%s\n", env.Payload)
  137. if count == 1 {
  138. return
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. func main() {
  147. if len(os.Args) == 1 {
  148. subEventsFromZMQ()
  149. } else if len(os.Args) == 3 {
  150. if v := os.Args[1]; v == "mqtt" {
  151. subEventsFromMQTT(os.Args[2])
  152. }
  153. if v := os.Args[1]; v == "redis" {
  154. subEventsFromRedis(os.Args[2])
  155. }
  156. }
  157. }