44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var MqttConnect MQTT.OnConnectHandler = func(client MQTT.Client) {
|
|
token := client.Subscribe("access/ble", 0, serviceHandler)
|
|
if token.Wait() && token.Error() != nil {
|
|
fmt.Println(token.Error())
|
|
}
|
|
}
|
|
|
|
var MqttConnLost MQTT.ConnectionLostHandler = func(client MQTT.Client, err error) {
|
|
fmt.Println(err.Error())
|
|
}
|
|
|
|
type Pack struct {
|
|
V int `json:"v"`
|
|
Mid int `json:"mid"`
|
|
Time int `json:"time"`
|
|
IP string `json:"ip"`
|
|
Mac string `json:"mac"`
|
|
Devices [][]interface{} `json:"devices"`
|
|
}
|
|
|
|
// 具体业务订阅的处理,此处为示例
|
|
var serviceHandler MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
|
|
body := &Pack{}
|
|
json.Unmarshal([]byte(msg.Payload()), body)
|
|
for _, o := range body.Devices {
|
|
if o[1] != "D02EAB2D6D49" {
|
|
continue
|
|
}
|
|
payload := fmt.Sprintf("%s,%s,%s,%f,%s", time.Unix(int64(body.Time), 0), body.Mac, o[1], o[2].(float64), o[3])
|
|
logrus.Println(payload)
|
|
}
|
|
}
|