/
index.go
150 lines (121 loc) · 3.28 KB
/
index.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 engine
import (
"strconv"
"sync"
"github.com/amimof/huego"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
// Index provides cached data contained in a hue bridge
type Index struct {
Groups []huego.Group
Lights []huego.Light
Scenes []huego.Scene
}
var ErrIndexNilBridge = errors.New("NewIndex: bridge is nil")
// NewIndex creates an index of a given bridge
func NewIndex(bridge *huego.Bridge) (*Index, error) {
if bridge == nil {
return nil, ErrIndexNilBridge
}
eg := &errgroup.Group{}
index := &Index{}
eg.Go(func() (err error) {
index.Groups, err = bridge.GetGroups()
return
})
eg.Go(func() (err error) {
index.Lights, err = bridge.GetLights()
return
})
eg.Go(func() (err error) {
index.Scenes, err = bridge.GetScenes()
return
})
return index, eg.Wait()
}
// QueryString sends a string query to this index
func (index Index) QueryString(input string) ([]Action, []MatchScore, []Score) {
return index.Query(ParseQuery(input))
}
var scoringPool = &sync.Pool{
New: func() interface{} {
return new(ScoreQueries)
},
}
var resultsPool = &sync.Pool{
New: func() interface{} {
return new(Results)
},
}
// Query queries an index for a set of queries
// It returns a list of matching actions
func (index Index) Query(queries []Query) (actions []Action, matchScores []MatchScore, scores []Score) {
scoring := scoringPool.Get().(*ScoreQueries)
defer scoringPool.Put(scoring)
results := resultsPool.Get().(*Results)
results.Reset(len(index.Groups) + len(index.Lights)) // todo: do we want to cache this?
defer resultsPool.Put(results)
for _, g := range index.Groups {
scoring.Use(queries)
theGroup := NewHueGroup(g)
if !scoring.Score(func(q Query) float64 { return q.MatchGroup(theGroup) }) {
groupPool.Put(theGroup)
continue
}
// match the word "on"
if scores, _ := scoring.ScoreFinal(func(q Query) float64 { return q.MatchOnOff(BoolOn) }); len(scores) > 0 {
results.Add(Action{
Group: theGroup,
OnOff: BoolOn,
}, scores)
}
// match the word "off"
if scores, _ := scoring.ScoreFinal(func(q Query) float64 { return q.MatchOnOff(BoolOff) }); len(scores) > 0 {
results.Add(Action{
Group: theGroup,
OnOff: BoolOff,
}, scores)
}
// iterate over scenes in this group!
gID := strconv.Itoa(g.ID)
for _, s := range index.Scenes {
if s.Group != gID {
continue
}
theScene := NewHueScene(s)
scores, _ := scoring.ScoreFinal(func(q Query) float64 { return q.MatchScene(theScene) })
if len(scores) == 0 {
scenePool.Put(theScene)
continue
}
results.Add(Action{
Group: theGroup,
Scene: theScene,
}, scores)
}
}
for _, l := range index.Lights {
scoring.Use(queries)
theLight := NewHueLight(l)
if !scoring.Score(func(q Query) float64 { return q.MatchLight(theLight) }) {
lightPool.Put(theLight)
continue
}
// match the word "on"
if scores, _ := scoring.ScoreFinal(func(q Query) float64 { return q.MatchOnOff(BoolOn) }); len(scores) > 0 {
results.Add(Action{
Light: theLight,
OnOff: BoolOn,
}, scores)
}
// match the word "off"
if scores, _ := scoring.ScoreFinal(func(q Query) float64 { return q.MatchOnOff(BoolOff) }); len(scores) > 0 {
results.Add(Action{
Light: theLight,
OnOff: BoolOff,
}, scores)
}
}
return results.Results()
}