|
@@ -7,21 +7,22 @@ import (
|
|
|
"go.uber.org/zap"
|
|
|
"kpt-temporary-mqtt/config"
|
|
|
"kpt-temporary-mqtt/model"
|
|
|
+ "kpt-temporary-mqtt/store/kptstore"
|
|
|
"kpt-temporary-mqtt/util"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
var messagePubHandler golangMqtt.MessageHandler = func(client golangMqtt.Client, msg golangMqtt.Message) {
|
|
|
- fmt.Printf("messagePubHandler Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
|
|
|
+ zaplog.Info("messagePubHandlerReceived", zap.Any("message", string(msg.Payload())), zap.Any("topic", msg.Topic()))
|
|
|
}
|
|
|
|
|
|
var connectHandler golangMqtt.OnConnectHandler = func(client golangMqtt.Client) {
|
|
|
- fmt.Println("Connected-client", client)
|
|
|
+ zaplog.Info("connectedClient", zap.Any("client", client))
|
|
|
}
|
|
|
|
|
|
var connectLostHandler golangMqtt.ConnectionLostHandler = func(client golangMqtt.Client, err error) {
|
|
|
- fmt.Printf("Connect lost: %v\n", err.Error())
|
|
|
+ zaplog.Info("connectLost", zap.Any("err", err.Error()))
|
|
|
}
|
|
|
|
|
|
func NewMqtt(configOption *config.AppConfig) golangMqtt.Client {
|
|
@@ -64,10 +65,10 @@ func CreatMsgLog(msg []byte) {
|
|
|
zaplog.Error("CreatMsgLog", zap.Any("err", err), zap.Any("msg", string(msg)))
|
|
|
}
|
|
|
|
|
|
- //if err := kptstore.DB.Table(new(model.SubMsgLog).TableName()).Create(subMsgLog).Error; err != nil {
|
|
|
- fmt.Println("msg", string(msg))
|
|
|
- fmt.Println("subMsgLog", subMsgLog)
|
|
|
- //}
|
|
|
+ if err := &kptstore.DB.Table(new(model.SubMsgLog).TableName()).Create(subMsgLog).Error; err != nil {
|
|
|
+ fmt.Println("msg", string(msg))
|
|
|
+ fmt.Println("subMsgLog", subMsgLog)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func MsgDataFormat(msg []byte) (*model.SubMsgLog, error) {
|
|
@@ -75,13 +76,14 @@ func MsgDataFormat(msg []byte) (*model.SubMsgLog, error) {
|
|
|
pairs := strings.Split(util.MsgFormat(string(msg)), " ")
|
|
|
for _, pair := range pairs {
|
|
|
parts := strings.SplitN(pair, ":", 2)
|
|
|
- if len(parts) == 2 {
|
|
|
- key, value := parts[0], parts[1]
|
|
|
- if len(key) == 0 {
|
|
|
- continue
|
|
|
- }
|
|
|
- msgData[key] = value
|
|
|
+ if len(parts) != 2 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ key, value := parts[0], parts[1]
|
|
|
+ if len(key) == 0 {
|
|
|
+ continue
|
|
|
}
|
|
|
+ msgData[key] = value
|
|
|
}
|
|
|
|
|
|
softVer := int64(0)
|