-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.go
156 lines (125 loc) · 3.44 KB
/
profile.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
151
152
153
154
155
156
package eltee
import (
"fmt"
"github.com/eyethereal/go-config"
"github.com/tomseago/go-eltee/api"
"io/ioutil"
"path/filepath"
"sort"
"strings"
)
/////////
// A Profile is basically a class of fixture. That is, it represents a particular model of
// light from a particular manufacturer and holds all the information about controls that
// can be manipulated for that light. It is represented by a single .acl file in the
// profiles directory. There may be multiple instances of a profile - each instance being
// known as a fixture - within a particular setup.
type Profile struct {
Id string
Name string
ChannelCount int
DefaultData []byte
Controls *GroupProfileControl
}
func NewProfile(id string, rootNode *config.AclNode) (*Profile, error) {
p := &Profile{
Id: id,
}
p.Name = rootNode.DefChildAsString(id, "name")
p.ChannelCount = rootNode.DefChildAsInt(0, "channel_count")
var err error
p.Controls, err = NewGroupProfileControl("_root", rootNode.Child("controls"))
if err != nil {
return nil, err
}
defData := rootNode.ChildAsByteList("default_values")
if len(defData) > 0 {
p.DefaultData = defData
}
return p, nil
}
func (p *Profile) String() string {
return fmt.Sprintf("%v: '%v' %v\n%v\n", p.Id, p.Name, p.ChannelCount, p.Controls)
}
/////////
// A ProfileLibrary holds all profiles known to the system. It is loaded from a
// set of .acl files.
type ProfileLibrary struct {
Profiles map[string]*Profile
}
func NewProfileLibrary() *ProfileLibrary {
lib := &ProfileLibrary{
Profiles: make(map[string]*Profile),
}
return lib
}
// Loads a single file using the given id. When used by LoadDirectory(string) the
// id will be the base name of the .acl file
func (lib *ProfileLibrary) LoadFile(id string, filename string) error {
node := config.NewAclNode()
err := node.ParseFile(filename)
if err != nil {
return fmt.Errorf("While reading '%v' : %v", filename, err)
}
profile, err := NewProfile(id, node)
if err != nil {
return fmt.Errorf("While creating profile %v : %v", id, err)
}
lib.Profiles[id] = profile
return nil
}
// Loads the library from all .acl files in a given directory
func (lib *ProfileLibrary) LoadDirectory(dirname string) error {
files, err := ioutil.ReadDir(dirname)
if err != nil {
return err
}
for i := 0; i < len(files); i++ {
file := files[i]
if file.IsDir() {
continue
}
ext := filepath.Ext(file.Name())
log.Debugf("name=%v ext=%v", file.Name(), ext)
if ext != ".acl" {
continue
}
base := filepath.Base(file.Name())
base = base[:len(base)-4]
full := filepath.Join(dirname, file.Name())
log.Infof("Loading '%v' from '%v'", base, full)
err = lib.LoadFile(base, full)
if err != nil {
log.Errorf("%v", err)
// But try to load other things
}
}
return nil
}
// Dumps the entire library to a reasonable string representation, mostly for debugging
func (lib *ProfileLibrary) String() string {
// Output in sorted order
ids := make([]string, 0, len(lib.Profiles))
for id, _ := range lib.Profiles {
ids = append(ids, id)
}
sort.Strings(ids)
var b strings.Builder
b.WriteString("\n")
for ix := 0; ix < len(ids); ix++ {
id := ids[ix]
profile := lib.Profiles[id]
b.WriteString(profile.String())
b.WriteString("\n")
}
return b.String()
}
func (p *Profile) ToAPI() *api.Profile {
apiP := &api.Profile{
Id: p.Id,
Name: p.Name,
ChannelCount: int32(p.ChannelCount),
Controls: p.Controls.ToAPI(),
}
return apiP
}