-
Notifications
You must be signed in to change notification settings - Fork 17
/
registeredclient.go
125 lines (97 loc) · 2.87 KB
/
registeredclient.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package betwixt
import (
"fmt"
"log"
"net"
"time"
. "github.com/zubairhamed/canopus"
)
// Returns a new instance of DefaultRegisteredClient implementing RegisteredClient
func NewRegisteredClient(ep string, id string, addr string, coapServer CoapServer) RegisteredClient {
return &DefaultRegisteredClient{
name: ep,
id: id,
addr: addr,
regDate: time.Now(),
updateDate: time.Now(),
coapServer: coapServer,
}
}
type DefaultRegisteredClient struct {
id string
name string
lifetime int
version string
bindingMode BindingMode
smsNumber string
addr string
regDate time.Time
updateDate time.Time
coapServer CoapServer
enabledObjects map[LWM2MObjectType]Object
}
func (c *DefaultRegisteredClient) GetAddress() string {
return c.addr
}
func (c *DefaultRegisteredClient) GetId() string {
return c.id
}
func (c *DefaultRegisteredClient) GetName() string {
return c.name
}
func (c *DefaultRegisteredClient) GetLifetime() int {
return c.lifetime
}
func (c *DefaultRegisteredClient) GetVersion() string {
return c.version
}
func (c *DefaultRegisteredClient) GetBindingMode() BindingMode {
return c.bindingMode
}
func (c *DefaultRegisteredClient) GetSmsNumber() string {
return c.smsNumber
}
func (c *DefaultRegisteredClient) GetRegistrationDate() time.Time {
return c.regDate
}
func (c *DefaultRegisteredClient) Update() {
c.updateDate = time.Now()
}
func (c *DefaultRegisteredClient) LastUpdate() time.Time {
return c.updateDate
}
func (c *DefaultRegisteredClient) SetObjects(objects map[LWM2MObjectType]Object) {
c.enabledObjects = objects
}
func (c *DefaultRegisteredClient) GetObjects() map[LWM2MObjectType]Object {
return c.enabledObjects
}
func (c *DefaultRegisteredClient) GetObject(t LWM2MObjectType) Object {
return c.enabledObjects[t]
}
func (c *DefaultRegisteredClient) ReadObject(obj uint16, inst uint16) (Value, error) {
return nil, nil
}
func (c *DefaultRegisteredClient) ReadResource(obj uint16, inst uint16, rsrc uint16) (Value, error) {
rAddr, _ := net.ResolveUDPAddr("udp", c.addr)
uri := fmt.Sprintf("/%d/%d/%d", obj, inst, rsrc)
req := NewRequest(MessageConfirmable, Get, GenerateMessageID())
req.SetRequestURI(uri)
resourceDefinition := c.GetObject(LWM2MObjectType(obj)).GetDefinition().GetResource(LWM2MResourceType(rsrc))
if resourceDefinition.MultipleValuesAllowed() {
req.SetMediaType(MediaTypeTlvVndOmaLwm2m)
} else {
req.SetMediaType(MediaTypeTextPlainVndOmaLwm2m)
}
response, err := c.coapServer.SendTo(req, rAddr)
if err != nil {
log.Println(err)
return nil, err
}
responseValue, _ := DecodeResourceValue(LWM2MResourceType(rsrc), response.GetMessage().Payload.GetBytes(), resourceDefinition)
return responseValue, nil
}
func (c *DefaultRegisteredClient) Delete(int, int) {
}
func (c *DefaultRegisteredClient) Execute(int, int, int) {
}