-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathbytes.go
45 lines (37 loc) · 819 Bytes
/
bytes.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
package characteristic
import (
"encoding/base64"
"net"
)
type Bytes struct {
*String
}
func NewBytes(typ string) *Bytes {
s := NewString(typ)
s.Format = FormatTLV8
return &Bytes{s}
}
func (bs *Bytes) SetValue(b []byte) {
bs.String.SetValue(base64FromBytes(b))
}
func (bs *Bytes) GetValue() []byte {
str := bs.String.GetValue()
if b, err := base64.StdEncoding.DecodeString(str); err != nil {
return []byte{}
} else {
return b
}
}
func (bs *Bytes) OnValueRemoteUpdate(fn func([]byte)) {
bs.OnValueUpdateFromConn(func(conn net.Conn, c *Characteristic, new, old interface{}) {
str := new.(string)
if b, err := base64.StdEncoding.DecodeString(str); err != nil {
fn([]byte{})
} else {
fn(b)
}
})
}
func base64FromBytes(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}