This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
94 lines (81 loc) · 2.28 KB
/
node.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
85
86
87
88
89
90
91
92
93
94
package actions
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/I1820/lanserver/models"
jwt "github.com/dgrijalva/jwt-go"
paho "github.com/eclipse/paho.mqtt.golang"
"github.com/gobuffalo/envy"
"github.com/mongodb/mongo-go-driver/bson"
)
// Log provides data gathering endpoint for devices.
// we call these data that are came from devices log
// from kaa old days.
// log/{deveui}/send
func Log(client paho.Client, message paho.Message) {
deveui := strings.Split(message.Topic(), "/")[1]
logger.Infof("Device: %s /log/send", deveui)
result := db.Collection("devices").FindOne(context.Background(), bson.NewDocument(
bson.EC.String("deveui", deveui),
))
var d models.Device
if err := result.Decode(&d); err != nil {
logger.Error(err)
return
}
var log models.LogMessage
if err := json.Unmarshal(message.Payload(), &log); err != nil {
logger.Error(err)
return
}
if log.Token != d.Token {
logger.Error(fmt.Errorf("Mismatched token"))
return
}
var key []byte
copy(key[:], envy.Get("NODE_SECRET", ""))
if _, err := jwt.ParseWithClaims(d.Token, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
c := token.Claims.(*jwt.StandardClaims)
if !c.VerifyIssuer("lanserver.sh", true) {
return nil, fmt.Errorf("Unexpected issuer %v", c.Issuer)
}
if c.Id != deveui {
return nil, fmt.Errorf("Mismatched identifier %s != %s", c.Id, deveui)
}
return key, nil
}); err != nil {
logger.Error(err)
return
}
b, err := json.Marshal(models.RxMessage{
DevEUI: deveui,
Data: log.Data,
})
if err != nil {
logger.Error(err)
return
}
mqttApplication.Publish(fmt.Sprintf("device/%s/rx", deveui), 0, true, b)
}
// Notification provides data sending endpoint for devices.
// we call these data that are sent to devices notification
// from kaa old days.
// device/{deveui}/tx
func Notification(client paho.Client, message paho.Message) {
deveui := strings.Split(message.Topic(), "/")[1]
var notification models.TxMessage
if err := json.Unmarshal(message.Payload(), ¬ification); err != nil {
logger.Error(err)
return
}
b, err := json.Marshal(models.NotificationMessage{
Data: notification.Data,
})
if err != nil {
logger.Error(err)
return
}
mqttNode.Publish(fmt.Sprintf("notification/%s/request", deveui), 0, true, b)
}