sub.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package mqtt
  2. import (
  3. "fmt"
  4. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  5. golangMqtt "github.com/eclipse/paho.mqtt.golang"
  6. "go.uber.org/zap"
  7. "kpt-temporary-mqtt/config"
  8. "kpt-temporary-mqtt/model"
  9. "kpt-temporary-mqtt/store/kptstore"
  10. "kpt-temporary-mqtt/util"
  11. "strconv"
  12. "strings"
  13. )
  14. var messagePubHandler golangMqtt.MessageHandler = func(client golangMqtt.Client, msg golangMqtt.Message) {
  15. zaplog.Info("messagePubHandlerReceived", zap.Any("message", string(msg.Payload())), zap.Any("topic", msg.Topic()))
  16. }
  17. var connectHandler golangMqtt.OnConnectHandler = func(client golangMqtt.Client) {
  18. zaplog.Info("connectedClient", zap.Any("client", client))
  19. }
  20. var connectLostHandler golangMqtt.ConnectionLostHandler = func(client golangMqtt.Client, err error) {
  21. zaplog.Info("connectLost", zap.Any("err", err.Error()))
  22. }
  23. func NewMqtt(configOption *config.AppConfig) golangMqtt.Client {
  24. opts := golangMqtt.NewClientOptions()
  25. opts.AddBroker(fmt.Sprintf("tcp://%s:%d", configOption.Broker, configOption.Port))
  26. opts.SetClientID(util.RandString(6))
  27. opts.SetUsername(configOption.UserName)
  28. opts.SetPassword(configOption.Password)
  29. opts.SetDefaultPublishHandler(messagePubHandler)
  30. opts.OnConnect = connectHandler
  31. opts.OnConnectionLost = connectLostHandler
  32. client := golangMqtt.NewClient(opts)
  33. if token := client.Connect(); token.Wait() && token.Error() != nil {
  34. panic(token.Error())
  35. }
  36. return client
  37. }
  38. func SubMsg(configOption *config.AppConfig, client golangMqtt.Client) {
  39. var subMsgChan = make(chan []byte, configOption.WorkNumber)
  40. client.Subscribe(configOption.SubTopName, 1, func(client golangMqtt.Client, msg golangMqtt.Message) {
  41. subMsgChan <- msg.Payload()
  42. })
  43. //for i := 0; i < int(configOption.WorkNumber); i++ {
  44. //go func() {
  45. for {
  46. select {
  47. case msg := <-subMsgChan:
  48. CreatMsgLog(msg)
  49. }
  50. }
  51. //}()
  52. //}
  53. }
  54. func CreatMsgLog(msg []byte) {
  55. subMsgLog, err := MsgDataFormat(msg)
  56. if err != nil {
  57. zaplog.Error("CreatMsgLog", zap.Any("err", err), zap.Any("msg", string(msg)))
  58. }
  59. if err := &kptstore.DB.Table(new(model.SubMsgLog).TableName()).Create(subMsgLog).Error; err != nil {
  60. fmt.Println("msg", string(msg))
  61. fmt.Println("subMsgLog", subMsgLog)
  62. }
  63. }
  64. func MsgDataFormat(msg []byte) (*model.SubMsgLog, error) {
  65. msgData := make(map[string]interface{})
  66. pairs := strings.Split(util.MsgFormat(string(msg)), " ")
  67. for _, pair := range pairs {
  68. parts := strings.SplitN(pair, ":", 2)
  69. if len(parts) != 2 {
  70. continue
  71. }
  72. key, value := parts[0], parts[1]
  73. if len(key) == 0 {
  74. continue
  75. }
  76. msgData[key] = value
  77. }
  78. softVer := int64(0)
  79. if softVerInter, ok := msgData["SOFT_VER"]; ok {
  80. if softVerstr, ok := softVerInter.(string); ok {
  81. softVer, _ = strconv.ParseInt(softVerstr, 10, 64)
  82. }
  83. }
  84. uuid := ""
  85. if uuidInter, ok := msgData["uuid"]; ok {
  86. if uuidStr, ok := uuidInter.(string); ok {
  87. uuid = uuidStr
  88. }
  89. }
  90. frameId := int64(0)
  91. if frameIdInter, ok := msgData["frameid"]; ok {
  92. if frameId64, ok := frameIdInter.(string); ok {
  93. frameId, _ = strconv.ParseInt(frameId64, 10, 64)
  94. }
  95. }
  96. cowId := ""
  97. if cowIdInter, ok := msgData["cowid"]; ok {
  98. if cowIdStr, ok := cowIdInter.(string); ok {
  99. cowId = cowIdStr
  100. }
  101. }
  102. csq := int64(0)
  103. if csqInter, ok := msgData["csq"]; ok {
  104. if csq32, ok := csqInter.(string); ok {
  105. csq, _ = strconv.ParseInt(csq32, 10, 64)
  106. }
  107. }
  108. temp := float64(0)
  109. if tempInter, ok := msgData["Temp"]; ok {
  110. if tempFloat, ok := tempInter.(string); ok {
  111. temp, _ = strconv.ParseFloat(tempFloat, 64)
  112. }
  113. }
  114. imei := ""
  115. if imeiInter, ok := msgData["imei"]; ok {
  116. if imeiStr, ok := imeiInter.(string); ok {
  117. imei = imeiStr
  118. }
  119. }
  120. active := int64(0)
  121. if activeInter, ok := msgData["active"]; ok {
  122. if active32, ok := activeInter.(string); ok {
  123. active, _ = strconv.ParseInt(active32, 10, 64)
  124. }
  125. }
  126. inAction := int64(0)
  127. if inActionInter, ok := msgData["inactive"]; ok {
  128. if inAction32, ok := inActionInter.(string); ok {
  129. inAction, _ = strconv.ParseInt(inAction32, 10, 64)
  130. }
  131. }
  132. ruMina := int64(0)
  133. if ruMinaInter, ok := msgData["Rumina"]; ok {
  134. if ruMina32, ok := ruMinaInter.(string); ok {
  135. ruMina, _ = strconv.ParseInt(ruMina32, 10, 64)
  136. }
  137. }
  138. intake := int64(0)
  139. if intakeInter, ok := msgData["Intake"]; ok {
  140. if intake32, ok := intakeInter.(string); ok {
  141. intake, _ = strconv.ParseInt(intake32, 10, 64)
  142. }
  143. }
  144. gasp := int64(0)
  145. if gaspInter, ok := msgData["gasp"]; ok {
  146. if gasp32, ok := gaspInter.(string); ok {
  147. gasp, _ = strconv.ParseInt(gasp32, 10, 64)
  148. }
  149. }
  150. other := int64(0)
  151. if otherInter, ok := msgData["other"]; ok {
  152. if other32, ok := otherInter.(string); ok {
  153. other, _ = strconv.ParseInt(other32, 10, 64)
  154. }
  155. }
  156. reMain := int64(0)
  157. if reMainInter, ok := msgData["Remain"]; ok {
  158. if reMain32, ok := reMainInter.(string); ok {
  159. reMain, _ = strconv.ParseInt(reMain32, 10, 64)
  160. }
  161. }
  162. return &model.SubMsgLog{
  163. SoftVer: softVer,
  164. Uuid: uuid,
  165. FrameId: frameId,
  166. CowId: cowId,
  167. Csq: csq,
  168. Temp: int64(temp * 100),
  169. Imei: imei,
  170. Active: int32(active),
  171. InActive: int32(inAction),
  172. RuMina: int32(ruMina),
  173. Intake: int32(intake),
  174. Gasp: int32(gasp),
  175. Other: int32(other),
  176. ReMain: int32(reMain),
  177. }, nil
  178. }