NatsMqtt.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package NatsServer
  2. import (
  3. "Cold_Api/controllers/lib"
  4. "github.com/beego/beego/v2/core/logs"
  5. "Cold_Api/models/Device"
  6. "github.com/vmihailenco/msgpack/v5"
  7. )
  8. func Read_DeviceParameter(T_SN, T_mqttid string) {
  9. logs.Info("Nats =>", lib.FuncName(), T_SN)
  10. err := lib.Nats.Publish("Read_DeviceParameter"+T_mqttid, []byte(T_SN))
  11. if err != nil {
  12. logs.Info("Nats =>", lib.FuncName(), err)
  13. }
  14. }
  15. func Read_DeviceSensorParameter(T_SN, T_mqttid string) {
  16. logs.Info("Nats =>", lib.FuncName(), T_SN)
  17. err := lib.Nats.Publish("Read_DeviceSensorParameter"+T_mqttid, []byte(T_SN))
  18. if err != nil {
  19. logs.Info("Nats =>", lib.FuncName(), err)
  20. }
  21. }
  22. func Pu_DeviceParameter(v Device.DeviceParameter, T_mqttid string) {
  23. logs.Info("Nats =>", lib.FuncName(), v)
  24. b, err := msgpack.Marshal(&v)
  25. if err != nil {
  26. panic(err)
  27. }
  28. err = lib.Nats.Publish("Pu_DeviceParameter"+T_mqttid, b)
  29. if err != nil {
  30. logs.Info("Nats =>", lib.FuncName(), err)
  31. }
  32. }
  33. func Pu_DeviceParameter_Sensor(v Device.DeviceSensorParameter, T_mqttid string) {
  34. logs.Info("Nats =>", lib.FuncName(), v)
  35. b, err := msgpack.Marshal(&v)
  36. if err != nil {
  37. panic(err)
  38. }
  39. err = lib.Nats.Publish("Pu_DeviceParameter_Sensor"+T_mqttid, b)
  40. if err != nil {
  41. logs.Info("Nats =>", lib.FuncName(), err)
  42. }
  43. }
  44. func Set_DeviceTask(v Device.Device_task, T_mqttid string) {
  45. logs.Info("Nats =>", lib.FuncName(), v)
  46. b, err := msgpack.Marshal(&v)
  47. if err != nil {
  48. panic(err)
  49. }
  50. err = lib.Nats.Publish("Set_DeviceTask"+T_mqttid, b)
  51. if err != nil {
  52. logs.Info("Nats =>", lib.FuncName(), err)
  53. }
  54. }
  55. func Set_RestartShutdown(v Device.Device_task, T_mqttid string) {
  56. logs.Info("Nats =>", lib.FuncName(), v)
  57. b, err := msgpack.Marshal(&v)
  58. if err != nil {
  59. panic(err)
  60. }
  61. err = lib.Nats.Publish("Set_RestartShutdown"+T_mqttid, b)
  62. if err != nil {
  63. logs.Info("Nats =>", lib.FuncName(), err)
  64. }
  65. }
  66. func Del_DeviceSensor(v Device.DeviceSensor_Del, T_mqttid string) {
  67. logs.Info("Nats =>", lib.FuncName(), v)
  68. b, err := msgpack.Marshal(&v)
  69. if err != nil {
  70. panic(err)
  71. }
  72. err = lib.Nats.Publish("Del_DeviceSensor"+T_mqttid, b)
  73. if err != nil {
  74. logs.Info("Nats =>", lib.FuncName(), err)
  75. }
  76. }
  77. func Get_Device_Realtime(v string, T_mqttid string) {
  78. //logs.Info("Nats =>", lib.FuncName(),T_mqttid, v)
  79. err := lib.Nats.Publish("Get_Device_Realtime"+T_mqttid, []byte(v))
  80. if err != nil {
  81. logs.Info("Nats =>", lib.FuncName(), err)
  82. }
  83. }