pub.go 9.0 KB

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