This repository has been archived by the owner on Apr 26, 2023. It is now read-only.
/
command.go
150 lines (130 loc) · 3.97 KB
/
command.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cue
import (
"context"
"errors"
"regexp"
"strings"
"time"
"github.com/nickysemenza/hyperion/core/light"
"github.com/nickysemenza/hyperion/util/color"
"github.com/nickysemenza/hyperion/util/tracing"
opentracing "github.com/opentracing/opentracing-go"
)
var (
errorWrongPartCount = errors.New("command: wrong number of parts, should be lights:colors:timings")
errorPartSizeMismatch = errors.New("command: number of lights, colors, and timings must be the same")
errorInvalidTime = errors.New("command: invalid time")
errorMissingFunction = errors.New("command: fn(?) is missing")
errorUndefinedFunction = errors.New("command: function is not defined")
errorWrongPartLen = errors.New("command: wrong number of colon delimited groups")
)
//CommandToCue returns a cue based on a command.
func CommandToCue(ctx context.Context, m MasterManager, cmd string) (*Cue, error) {
span, ctx := opentracing.StartSpanFromContext(ctx, "command: ToCue")
defer span.Finish()
//remove spaces
cmd = strings.Replace(cmd, " ", "", -1)
//extracts: `match1(match2)`
re := regexp.MustCompile(`(?m)(.*?)\((.*?)\)`)
groups := re.FindAllStringSubmatch(cmd, -1)
span.SetTag("command", cmd)
if len(groups) != 1 {
tracing.SetError(span, errorMissingFunction)
return nil, errorMissingFunction
}
commandType := groups[0][1]
argString := groups[0][2]
span.SetTag("command-type", commandType)
args := strings.Split(argString, ",")
var cue *Cue
var err error
switch commandType {
case "set":
cue, err = processSetCommand(argString)
case "cycle":
cue, err = processCycleCommand(argString)
default:
if userCommand, ok := m.GetCommands()[commandType]; ok {
cue, err = BuildCueFromUserCommand(ctx, m, userCommand, args)
} else if systemCommand, ok := systemCommands[commandType]; ok {
cue, err = BuildCueFromUserCommand(ctx, m, systemCommand, args)
} else {
err = errorUndefinedFunction
}
}
if err != nil {
tracing.SetError(span, err)
return nil, err
}
return cue, nil
}
// e.g. cycle(c1+c2+c3+c4+c5+c6:500ms)
func processCycleCommand(cmd string) (*Cue, error) {
cue := Cue{}
parts := strings.Split(cmd, ":")
if len(parts) != 2 {
return nil, errorWrongPartLen
}
lightList := strings.Split(parts[0], "+")
duration, err := time.ParseDuration(parts[1])
if err != nil {
return nil, err
}
for x := range lightList {
frame := Frame{}
for y := 0; y < len(lightList); y++ {
action := FrameAction{}
action.LightName = lightList[y]
action.NewState = light.TargetState{
State: light.State{RGB: color.GetRGBFromString("#0000FF")},
Duration: duration,
}
if x == y {
action.NewState = light.TargetState{
State: light.State{RGB: color.GetRGBFromString("#FF0000")},
Duration: duration,
}
}
frame.Actions = append(frame.Actions, action)
}
cue.Frames = append(cue.Frames, frame)
}
return &cue, nil
}
func processSetCommand(cmd string) (*Cue, error) {
cue := Cue{}
for _, cueFrameString := range strings.Split(cmd, "|") {
frame := Frame{}
parts := strings.Split(cueFrameString, ":")
if len(parts) != 3 {
return nil, errorWrongPartCount
}
if len(parts[0]) == 0 || len(parts[1]) == 0 || len(parts[2]) == 0 {
return nil, errorWrongPartCount
}
lightList := strings.Split(parts[0], "+")
colorList := strings.Split(parts[1], "+")
timeList := strings.Split(parts[2], "+")
numTimes := len(timeList)
numColors := len(colorList)
numLights := len(lightList)
if !(numTimes == numColors && numColors == numLights) {
return nil, errorPartSizeMismatch
}
for x := 0; x < numLights; x++ {
action := FrameAction{}
action.LightName = lightList[x]
duration, err := time.ParseDuration(timeList[x])
if err != nil {
return nil, errorInvalidTime
}
action.NewState = light.TargetState{
State: light.State{RGB: color.GetRGBFromString(colorList[x])},
Duration: duration,
}
frame.Actions = append(frame.Actions, action)
}
cue.Frames = append(cue.Frames, frame)
}
return &cue, nil
}