forked from mraerino/atem-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
48 lines (40 loc) · 1.02 KB
/
time.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
package atem
import (
"context"
"time"
"github.com/mraerino/atem-go/models"
"github.com/mraerino/atem-go/packet"
"github.com/mraerino/atem-go/packet/cmds"
"github.com/pkg/errors"
)
var (
ErrChannelFull = errors.New("Channel for time requests is full")
ErrChannelClosed = errors.New("Channel was closed")
)
// Timecode requests the current system timecode from the switcher
//
// The context can be used to provide a timeout
func (c *Client) Timecode(ctx context.Context) (*models.Timecode, error) {
msg := c.makeHeader(packet.FlagACK)
msg.Commands = append(msg.Commands, cmds.TiRqCmd{})
if err := c.send(msg); err != nil {
return nil, errors.Wrap(err, "failed to send request")
}
ch := make(chan models.Timecode)
select {
case c.timeRequests <- ch:
default:
return nil, ErrChannelFull
}
ctx, cancel := context.WithTimeout(ctx, time.Second*15)
defer cancel()
select {
case time, open := <-ch:
if !open {
return nil, ErrChannelClosed
}
return &time, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}