/
controller-pair-remove.go
70 lines (58 loc) · 1.25 KB
/
controller-pair-remove.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
package hkontroller
import (
"bytes"
"errors"
"fmt"
"github.com/hkontrol/hkontroller/log"
"github.com/hkontrol/hkontroller/tlv8"
"io"
"net/http"
"strconv"
)
type pairRemoveReqPayload struct {
State byte `tlv8:"6"`
Method byte `tlv8:"0"`
Identifier string `tlv8:"1"`
}
type pairRemoveResPayload struct {
State byte `tlv8:"6"`
Error byte `tlv8:"7"`
}
func (d *Device) PairRemove(controllerId string) error {
pl := pairRemoveReqPayload{
State: M1,
Method: MethodDeletePairing,
Identifier: controllerId,
}
b, err := tlv8.Marshal(pl)
if err != nil {
return err
}
resp, err := d.doPost("/pairings", HTTPContentTypePairingTLV8, bytes.NewReader(b))
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("invalid status code %v", resp.StatusCode)
}
res := resp.Body
defer res.Close()
all, err := io.ReadAll(res)
if err != nil {
return err
}
m2 := pairRemoveResPayload{}
err = tlv8.Unmarshal(all, &m2)
if m2.Error != 0x00 {
return errors.New("res.err = " + strconv.FormatInt(int64(m2.Error), 10))
}
if err != nil {
return err
}
return nil
}
func (d *Device) Unpair() error {
log.Debug.Println("d.Unpair()")
d.emit("unpaired")
return d.PairRemove(d.controllerId)
}