-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (72 loc) · 2.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"strings"
"github.com/team4yf/fpm-go-pkg/utils"
"github.com/team4yf/fpm-iot-go-middleware/external/device/env"
_ "github.com/team4yf/fpm-go-plugin-cache-redis/plugin"
_ "github.com/team4yf/fpm-go-plugin-mqtt-client/plugin"
_ "github.com/team4yf/fpm-go-plugin-orm/plugins/pg"
_ "github.com/team4yf/fpm-go-plugin-tcp/plugin"
"github.com/team4yf/fpm-iot-go-middleware/consumer"
"github.com/team4yf/fpm-iot-go-middleware/handler/device"
"github.com/team4yf/fpm-iot-go-middleware/handler/mqttuser"
"github.com/team4yf/fpm-iot-go-middleware/handler/project"
"github.com/team4yf/fpm-iot-go-middleware/internal/model"
"github.com/team4yf/fpm-iot-go-middleware/router"
"github.com/team4yf/yf-fpm-server-go/fpm"
)
func main() {
app := fpm.New()
app.AddHook("BEFORE_INIT", func(f *fpm.Fpm) {
// Init the model
dbclient, _ := app.GetDatabase("pg")
migrator := &model.Migration{
DS: dbclient,
}
migrator.Install()
}, 10)
app.AddHook("AFTER_INIT", func(f *fpm.Fpm) {
router.LoadPushAPI(app)
}, 10)
app.Init()
//执行订阅的函数
app.Execute("mqttclient.subscribe", &fpm.BizParam{
"topics": []string{"$s2d/+/+/send", "$d2s/+/mcu20/push"},
})
mqttHandler := consumer.DefaultMqttConsumer(app)
mcuHandler := consumer.DevicePushConsumer(app)
app.Subscribe("#mqtt/receive", func(_ string, data interface{}) {
//data 通常是 byte[] 类型,可以转成 string 或者 map
body := data.(map[string]interface{})
topic := body["topic"].(string)
switch {
case strings.HasSuffix(topic, "send"):
mqttHandler(topic, body["payload"])
case strings.HasSuffix(topic, "mcu20/push"):
mcuHandler(topic, body["payload"])
}
})
// try in terminal: echo -n -e "\xfe\xdc\x01\x17\x1c\xb7\x40\xe3\x0f\x00\x00\x01\xde\x03\x00\x1c\x00\x00\x01\x0f\x00\x00\x01\xcb\x00\x00\x00\x34\x00\x00\x00\x4b\x00\x00\x00\x87\x00\x00\x00\x7a\x00\x00\x00\x14\x00" | nc localhost 5001
// and sub the topic: mosquitto_sub -h open.yunplus.io -t '$d2s/ceaa191a/partner/push' -u "fpmuser" -P
envDevice := env.NewEnvDevice("jingxun")
app.Subscribe("#tcp/receive", func(_ string, data interface{}) {
body := data.(map[string]interface{})
app.Logger.Debugf("receive tcp data: %X", body["data"])
buf := body["data"].([]byte)
envData, err := envDevice.Parse(&buf)
if err != nil {
app.Logger.Errorf("parse env tcp data error: %v", err)
return
}
// body["data"] = fmt.Sprintf("%x", body["data"])
app.Execute("mqttclient.publish", &fpm.BizParam{
"topic": fmt.Sprintf(`$d2s/%s/partner/push`, envData.Header.AppID),
"payload": ([]byte)(utils.JSON2String(envData)),
})
})
mqttuser.InitBiz(app)
device.InitBiz(app)
project.InitBiz(app)
app.Run()
}