Extract data from mqtt json payload
This commit is contained in:
parent
985b4e8fde
commit
8f7a285289
17
models/mo.go
Normal file
17
models/mo.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package models
|
||||||
|
|
||||||
|
type Mo struct {
|
||||||
|
ID MoID `json:"id"`
|
||||||
|
MO MoMO `json:"mo"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type MoID struct {
|
||||||
|
PTU4_MAC string
|
||||||
|
PTU4_SN string
|
||||||
|
}
|
||||||
|
|
||||||
|
type MoMO struct {
|
||||||
|
Action string `json:"action"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Number string `json:"number"`
|
||||||
|
}
|
25
mqtt/mqtt.go
25
mqtt/mqtt.go
|
@ -2,19 +2,20 @@ package mqtt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
//"crypto/x509"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
MQTT "github.com/eclipse/paho.mqtt.golang"
|
|
||||||
//"os"
|
|
||||||
"mqttListener/Config"
|
"mqttListener/Config"
|
||||||
"mqttListener/models"
|
"mqttListener/models"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
|
||||||
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -126,6 +127,8 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
|
||||||
fmt.Printf("MSG: \n%s\n", msg.Payload())
|
fmt.Printf("MSG: \n%s\n", msg.Payload())
|
||||||
//fmt.Printf("* [%s] %s\n", msg.Topic(), string(msg.Payload()))
|
//fmt.Printf("* [%s] %s\n", msg.Topic(), string(msg.Payload()))
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------
|
||||||
|
// create ID from topic
|
||||||
regexpWithType := "\\/ATB\\/[A-Z]+\\/[0-9]+\\/[0-9]+\\/mo"
|
regexpWithType := "\\/ATB\\/[A-Z]+\\/[0-9]+\\/[0-9]+\\/mo"
|
||||||
regexpDefault := "\\/ATB\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/mo"
|
regexpDefault := "\\/ATB\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/[0-9]+\\/mo"
|
||||||
|
|
||||||
|
@ -160,5 +163,21 @@ var subscriptionHandler = func(client MQTT.Client, msg MQTT.Message) {
|
||||||
device.SN = "t.b.d."
|
device.SN = "t.b.d."
|
||||||
device.LastMsg = "t.b.d"
|
device.LastMsg = "t.b.d"
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------
|
||||||
|
// extract data from payload
|
||||||
|
bytes := []byte(msg.Payload())
|
||||||
|
mo := models.Mo{}
|
||||||
|
err = json.Unmarshal(bytes, &mo)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
device.MAC = mo.ID.PTU4_MAC
|
||||||
|
device.SN = mo.ID.PTU4_SN
|
||||||
|
|
||||||
|
fmt.Printf("Device MAC = %s\n", device.MAC)
|
||||||
|
fmt.Printf("Device SN = %s\n", device.SN)
|
||||||
|
|
||||||
// TODO: store this device in database
|
// TODO: store this device in database
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user