-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflow.go
68 lines (54 loc) · 2.64 KB
/
flow.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
package yeelight
import (
"context"
"fmt"
"time"
)
// FlowExpression is the expression of the state changing series.
type FlowExpression struct {
// Duration Gradual change time or sleep time. Minimum value 50 milliseconds.
Duration time.Duration
// Mode can be FlowModeColor, FlowColorTemperature, FlowSleep
Mode FlowMode
// Value is RGB value when mode is FlowModeColor, CT value when mode is FlowColorTemperature, Ignored when mode is FlowSleep.
Value int
// Brightness value, -1 or 1 ~ 100. Ignored when mode is FlowSleep.
// When this value is -1, brightness in this tuple is ignored (only color or CT change takes effect).
Brightness int
}
// StartColorFlow method is used to start a color flow. Color flow is a series of smart LED visible state changing.
// It can be brightness changing, color changing or color temperature changing.
// `count` is the total number of visible state changing before color flow stopped. 0 means infinite loop on the state changing.
func (c Client) StartColorFlow(ctx context.Context, count int, action FlowAction, expressions []FlowExpression) error {
return c.startColorFlow(ctx, MethodStartCF, count, action, expressions)
}
// StartBackgroundColorFlow method is used to start a color flow. Color flow is a series of smart LED visible state changing.
// It can be brightness changing, color changing or color temperature changing.
// `count` is the total number of visible state changing before color flow stopped. 0 means infinite loop on the state changing.
func (c Client) StartBackgroundColorFlow(ctx context.Context, count int, action FlowAction, expressions []FlowExpression) error {
return c.startColorFlow(ctx, MethodBgStartCF, count, action, expressions)
}
// StopColorFlow method is used to stop a running color flow.
func (c Client) StopColorFlow(ctx context.Context) error {
return c.stopColorFlow(ctx, MethodStopCF)
}
// StopBackgroundColorFlow method is used to stop a running color flow.
func (c Client) StopBackgroundColorFlow(ctx context.Context) error {
return c.stopColorFlow(ctx, MethodBgStopCF)
}
func (c Client) startColorFlow(ctx context.Context, method string, count int, action FlowAction, expressions []FlowExpression) error {
if len(expressions) == 0 {
return ErrRequiredMinimumOneExpression
}
var expressionStr string
for _, exp := range expressions {
if len(expressionStr) > 0 {
expressionStr += ","
}
expressionStr += fmt.Sprintf("%d,%d,%d,%d", exp.Duration.Milliseconds(), exp.Mode, exp.Value, exp.Brightness)
}
return c.rawWithOk(ctx, method, count, action, expressionStr)
}
func (c Client) stopColorFlow(ctx context.Context, method string) error {
return c.rawWithOk(ctx, method)
}