-
Notifications
You must be signed in to change notification settings - Fork 0
/
motu.go
144 lines (120 loc) · 2.93 KB
/
motu.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
package motu
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
type motu struct {
Addr string
Datastore Datastore
client *http.Client
}
func NewMotu(addr string) *motu {
m := new(motu)
m.Addr = addr
m.Datastore = make(Datastore, 500)
return m
}
func (m *motu) StartListener(ch chan *Event) {
l := new(Listener)
l.Motu = m
l.Ch = ch
l.Start()
}
type singleFloatValue struct {
Value float64 `json:"value"`
}
type singleIntValue struct {
Value int64 `json:"value"`
}
func (m *motu) post(path string, data []byte) error {
if m.client == nil {
tr := &http.Transport{
MaxIdleConns: 1,
IdleConnTimeout: 30 * time.Second,
}
m.client = &http.Client{Transport: tr}
}
payload := url.Values{"json": {string(data)}}
req, err := http.NewRequest("PATCH", "http://"+m.Addr+"/datastore/"+path, strings.NewReader(payload.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := m.client.Do(req)
if err != nil {
// Timeout o.i.d.
return err
}
defer resp.Body.Close()
if resp.StatusCode == 204 {
return nil
}
if resp.StatusCode >= 400 {
return fmt.Errorf("got HTTP %d response", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if len(body) != 0 {
return fmt.Errorf("Expected empty response: %v", body)
}
return nil
}
func (m *motu) setSingleFloatValue(path string, value float64) error {
floatValue := new(singleFloatValue)
floatValue.Value = value
data, err := json.Marshal(floatValue)
if err != nil {
return fmt.Errorf("failed to create JSON: %v", err)
}
return m.post(path, data)
}
func (m *motu) setSingleIntValue(path string, value int64) error {
intValue := new(singleIntValue)
intValue.Value = value
data, err := json.Marshal(intValue)
if err != nil {
return fmt.Errorf("failed to create JSON: %v", err)
}
return m.post(path, data)
}
func (m *motu) setSingleBoolValue(path string, value bool) error {
if value {
return m.setSingleIntValue(path, 1)
} else {
return m.setSingleIntValue(path, 0)
}
}
func (m *motu) GetFaderPosition(id string) (float64, error) {
value, ok := m.Datastore[id+"/fader"].(float64)
if !ok {
return 0, fmt.Errorf("Cannot convert %v to float", m.Datastore[id+"/fader"])
}
return value, nil
}
func (m *motu) SetFaderPosition(id string, value float64) error {
return m.setSingleFloatValue(id+"/fader", value)
}
func (m *motu) GetFaderMute(id string) (bool, error) {
value, ok := m.Datastore[id+"/mute"].(float64)
if !ok {
return false, fmt.Errorf("Cannot convert %v to int", m.Datastore[id+"/mute"])
}
return value == 1, nil
}
func (m *motu) SetFaderMute(id string, value bool) error {
return m.setSingleBoolValue(id+"/mute", value)
}
func (m *motu) ToggleFaderMute(id string, value bool) error {
value, err := m.GetFaderMute(id)
if err != nil {
return err
}
return m.setSingleBoolValue(id+"/mute", !value)
}