pub.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. "context"
  19. "encoding/json"
  20. "fmt"
  21. "log"
  22. "os"
  23. "time"
  24. "github.com/edgexfoundry/go-mod-core-contracts/v3/common"
  25. "github.com/edgexfoundry/go-mod-core-contracts/v3/dtos"
  26. "github.com/edgexfoundry/go-mod-messaging/v3/messaging"
  27. "github.com/edgexfoundry/go-mod-messaging/v3/pkg/types"
  28. )
  29. var msgConfig1 = types.MessageBusConfig{
  30. Broker: types.HostInfo{
  31. Host: "127.0.0.1",
  32. Port: 6379,
  33. Protocol: "redis",
  34. },
  35. Type: messaging.Redis,
  36. }
  37. func pubEventClientRedis() {
  38. if msgClient, err := messaging.NewMessageClient(msgConfig1); err != nil {
  39. log.Fatal(err)
  40. } else {
  41. if ec := msgClient.Connect(); ec != nil {
  42. log.Fatal(ec)
  43. } else {
  44. // r := rand.New(rand.NewSource(time.Now().UnixNano()))
  45. for i := 0; i < 10; i++ {
  46. // temp := r.Intn(100)
  47. // humd := r.Intn(100)
  48. testEvent := dtos.NewEvent("demoProfile", "demo", "demoSource")
  49. testEvent.Origin = 123
  50. err := testEvent.AddSimpleReading("Temperature", common.ValueTypeInt64, int64(i*8))
  51. if err != nil {
  52. fmt.Printf("Add reading error for %d.Temperature: %v\n", i, i*8)
  53. }
  54. err = testEvent.AddSimpleReading("Humidity", common.ValueTypeInt64, int64(i*9))
  55. if err != nil {
  56. fmt.Printf("Add reading error for %d.Humidity: %v\n", i, i*9)
  57. }
  58. err = testEvent.AddSimpleReading("b1", common.ValueTypeBool, i%2 == 0)
  59. if err != nil {
  60. fmt.Printf("Add reading error for %d.b1: %v\n", i, i%2 == 0)
  61. }
  62. err = testEvent.AddSimpleReading("i1", common.ValueTypeInt64, int64(i))
  63. if err != nil {
  64. fmt.Printf("Add reading error for %d.i1: %v\n", i, i)
  65. }
  66. err = testEvent.AddSimpleReading("f1", common.ValueTypeFloat64, float64(i)/2.0)
  67. if err != nil {
  68. fmt.Printf("Add reading error for %d.f1: %v\n", i, float64(i)/2.0)
  69. }
  70. err = testEvent.AddSimpleReading("ui64", common.ValueTypeUint64, uint64(10796529505058023104))
  71. if err != nil {
  72. fmt.Printf("Add reading error for %d.ui64: %v\n", i, uint64(10796529505058023104))
  73. }
  74. fmt.Printf("readings: %v\n", testEvent.Readings)
  75. data, err := json.Marshal(testEvent)
  76. if err != nil {
  77. fmt.Printf("unexpected error MarshalEvent %v", err)
  78. } else {
  79. fmt.Println(string(data))
  80. }
  81. env := types.NewMessageEnvelope(data, context.Background())
  82. env.ContentType = "application/json"
  83. if e := msgClient.Publish(env, "events"); e != nil {
  84. log.Fatal(e)
  85. } else {
  86. fmt.Printf("Pub successful: %s\n", data)
  87. }
  88. time.Sleep(1500 * time.Millisecond)
  89. }
  90. }
  91. }
  92. }
  93. func pubArrayMessage() {
  94. if msgClient, err := messaging.NewMessageClient(msgConfig1); err != nil {
  95. log.Fatal(err)
  96. } else {
  97. if ec := msgClient.Connect(); ec != nil {
  98. log.Fatal(ec)
  99. }
  100. testEvent := dtos.NewEvent("demo1Profile", "demo1", "demo1Source")
  101. testEvent.Origin = 123
  102. err := testEvent.AddSimpleReading("ba", common.ValueTypeBoolArray, []bool{true, true, false})
  103. if err != nil {
  104. fmt.Printf("Add reading error for ba: %v\n", []bool{true, true, false})
  105. }
  106. err = testEvent.AddSimpleReading("ia", common.ValueTypeInt32Array, []int32{30, 40, 50})
  107. if err != nil {
  108. fmt.Printf("Add reading error for ia: %v\n", []int32{30, 40, 50})
  109. }
  110. err = testEvent.AddSimpleReading("fa", common.ValueTypeFloat64Array, []float64{3.14, 3.1415, 3.1415926})
  111. if err != nil {
  112. fmt.Printf("Add reading error for fa: %v\n", []float64{3.14, 3.1415, 3.1415926})
  113. }
  114. testEvent.Readings[len(testEvent.Readings)-1].Value = "[3.14, 3.1415, 3.1415926]"
  115. data, err := json.Marshal(testEvent)
  116. if err != nil {
  117. fmt.Printf("unexpected error MarshalEvent %v", err)
  118. } else {
  119. fmt.Println(string(data))
  120. }
  121. env := types.NewMessageEnvelope(data, context.Background())
  122. env.ContentType = "application/json"
  123. if e := msgClient.Publish(env, "events"); e != nil {
  124. log.Fatal(e)
  125. }
  126. time.Sleep(1500 * time.Millisecond)
  127. }
  128. }
  129. func pubToMQTT(host string) {
  130. msgConfig2 := types.MessageBusConfig{
  131. Broker: types.HostInfo{
  132. Host: host,
  133. Port: 1883,
  134. Protocol: "tcp",
  135. },
  136. Optional: map[string]string{
  137. "ClientId": "0001_client_id",
  138. },
  139. Type: messaging.MQTT,
  140. }
  141. if msgClient, err := messaging.NewMessageClient(msgConfig2); err != nil {
  142. log.Fatal(err)
  143. } else {
  144. if ec := msgClient.Connect(); ec != nil {
  145. log.Fatal(ec)
  146. }
  147. testEvent := dtos.NewEvent("demo1Profile", "demo1", "demo1Source")
  148. testEvent.Origin = 123
  149. err := testEvent.AddSimpleReading("Temperature", common.ValueTypeInt64, int64(20))
  150. if err != nil {
  151. fmt.Printf("Add reading error for Temperature: %v\n", 20)
  152. }
  153. err = testEvent.AddSimpleReading("Humidity", common.ValueTypeInt64, int64(30))
  154. if err != nil {
  155. fmt.Printf("Add reading error for Humidity: %v\n", 20)
  156. }
  157. data, err := json.Marshal(testEvent)
  158. if err != nil {
  159. fmt.Printf("unexpected error MarshalEvent %v", err)
  160. } else {
  161. fmt.Println(string(data))
  162. }
  163. env := types.NewMessageEnvelope(data, context.Background())
  164. env.ContentType = "application/json"
  165. if e := msgClient.Publish(env, "events"); e != nil {
  166. log.Fatal(e)
  167. } else {
  168. fmt.Printf("pubToAnother successful: %s\n", data)
  169. }
  170. time.Sleep(1500 * time.Millisecond)
  171. }
  172. }
  173. func pubMetaSource() {
  174. if msgClient, err := messaging.NewMessageClient(msgConfig1); err != nil {
  175. log.Fatal(err)
  176. } else {
  177. if ec := msgClient.Connect(); ec != nil {
  178. log.Fatal(ec)
  179. } else {
  180. evtDevice := []string{"demo1", "demo2"}
  181. for i, device := range evtDevice {
  182. j := int64(i) + 1
  183. testEvent := dtos.NewEvent("demo1Profile", device, "demo1Source")
  184. testEvent.Origin = 13 * j
  185. err := testEvent.AddSimpleReading("Temperature", common.ValueTypeInt64, j*8)
  186. if err != nil {
  187. fmt.Printf("Add reading error for %d.Temperature: %v\n", i, j*8)
  188. }
  189. testEvent.Readings[0].Origin = 24 * j
  190. testEvent.Readings[0].DeviceName = "Temperature sensor"
  191. err = testEvent.AddSimpleReading("Humidity", common.ValueTypeInt64, j*8)
  192. if err != nil {
  193. fmt.Printf("Add reading error for %d.Humidity: %v\n", i, j*8)
  194. }
  195. testEvent.Readings[1].Origin = 34 * j
  196. testEvent.Readings[1].DeviceName = "Humidity sensor"
  197. testEvent.AddBinaryReading("raw", []byte("Hello World"), "application/text")
  198. data, err := json.Marshal(testEvent)
  199. if err != nil {
  200. fmt.Printf("unexpected error MarshalEvent %v", err)
  201. } else {
  202. fmt.Println(string(data))
  203. }
  204. env := types.NewMessageEnvelope(data, context.Background())
  205. env.ContentType = "application/json"
  206. if e := msgClient.Publish(env, "events"); e != nil {
  207. log.Fatal(e)
  208. } else {
  209. fmt.Printf("Pub successful: %s\n", data)
  210. }
  211. time.Sleep(1500 * time.Millisecond)
  212. }
  213. }
  214. }
  215. }
  216. func main() {
  217. if len(os.Args) == 1 {
  218. pubEventClientRedis()
  219. } else if len(os.Args) == 2 {
  220. if v := os.Args[1]; v == "meta" {
  221. pubMetaSource()
  222. } else if v == "array" {
  223. pubArrayMessage()
  224. }
  225. } else if len(os.Args) == 3 {
  226. if v := os.Args[1]; v == "mqtt" {
  227. // The 2nd parameter is MQTT broker server address
  228. pubToMQTT(os.Args[2])
  229. }
  230. }
  231. }