pub.go 10 KB

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