/
response.go
50 lines (46 loc) · 1.01 KB
/
response.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
package main
import (
"github.com/dustin/go-coap"
"github.com/qualiapps/observer/db"
"github.com/qualiapps/observer/utils"
"log"
"net"
)
type Response struct {
Data []byte
FromAddr *net.UDPAddr
}
/**
* Processing response
* @param *net.UDPConn l - connection instance
* @param dbClient - db instance
* @param Response response - res data
*/
func ProcessResponse(l *net.UDPConn, dbClient db.DBClient, response Response) {
// parse to CoAP struct
rv := coap.Message{}
err := rv.UnmarshalBinary(response.Data)
if err == nil {
if IsValidToken(rv.Token) {
if rv.IsObservable() {
// Send ACK
if rv.IsConfirmable() {
err := SendAck(l, response.FromAddr, rv.MessageID)
if err != nil {
log.Printf("Send ACK ERROR: %#v\n", err)
}
}
// processing payload
data := string(rv.Payload)
if !utils.IsEmpty(data) {
// Send to DB
if dbClient != nil {
dbClient.Processing(data)
}
}
} else {
RemoveUnObservable(&rv, response.FromAddr)
}
}
}
}