-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
51 lines (43 loc) · 1.47 KB
/
config.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
package nuki
import (
"context"
"fmt"
"github.com/tarent/go-nuki/communication/command"
"time"
)
// UpdateTime set the given time on the connected device.
func (c *Client) UpdateTime(ctx context.Context, pin string, t time.Time) error {
parsedPin, err := c.checkPreconditionAndParsePin(pin)
if err != nil {
return err
}
return c.PerformAction(ctx, func(nonce []byte) command.Command {
return command.NewUpdateTime(t, parsedPin, nonce)
})
}
// ReadConfig will request and return the applied config of the connected device.
func (c *Client) ReadConfig(ctx context.Context) (command.ConfigCommand, error) {
if c.client == nil {
return nil, ConnectionNotEstablishedError
}
if c.udioCom == nil {
return nil, UnauthenticatedError
}
err := c.udioCom.Send(command.NewRequest(command.IdChallenge))
if err != nil {
return nil, fmt.Errorf("unable to send request for challenge: %w", err)
}
challenge, err := c.udioCom.WaitForSpecificResponse(ctx, command.IdChallenge, c.responseTimeout)
if err != nil {
return nil, fmt.Errorf("error while waiting for challenge: %w", err)
}
err = c.udioCom.Send(command.NewRequestConfig(challenge.AsChallengeCommand().Nonce()))
if err != nil {
return nil, fmt.Errorf("unable to send request for config: %w", err)
}
config, err := c.udioCom.WaitForSpecificResponse(ctx, command.IdConfig, c.responseTimeout)
if err != nil {
return nil, fmt.Errorf("error while waiting for config: %w", err)
}
return config.AsConfigCommand(), nil
}