pub.go 8.9 KB

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