-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmxconn.go
150 lines (117 loc) · 2.7 KB
/
dmxconn.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 eltee
import (
"fmt"
"github.com/eyethereal/go-config"
// "github.com/nickysemenza/gola"
"net/http"
"strconv"
"strings"
)
type DMXConn interface {
SendDMX(universe int, data []byte)
}
type OLADConn struct {
hostaddr string
// client *gola.OlaClient
//connected bool
url string
}
func NewOLADConn(cfg *config.AclNode) (DMXConn, error) {
hostaddr := cfg.DefChildAsString("localhost:9090", "hostaddr")
conn := &OLADConn{
hostaddr: hostaddr,
}
conn.url = fmt.Sprintf("http://%v/set_dmx", hostaddr)
return conn, nil
// return conn, conn.AttemptOpen()
}
// func (c *OLADConn) AttemptOpen() error {
// if c == nil {
// return fmt.Errorf("Can not open a nil OLADConn")
// }
// log.Infof("Opening connection to %v", c.hostaddr)
// c.client = gola.New(c.hostaddr)
// c.connected = true
// return nil
// }
// func (c *OLADConn) SendDMX(universe int, data []byte) {
// if c == nil || c.connected == false {
// return
// }
// status, err := c.client.SendDmx(universe, data)
// _ = status
// if err != nil {
// log.Errorf("Error sending DMX. Considering this dead: %v", err)
// c.connected = false
// return
// }
// }
func (c *OLADConn) SendDMX(universe int, data []byte) {
var b strings.Builder
b.WriteString("u=")
b.WriteString(strconv.Itoa(universe))
b.WriteString("&d=")
for i := 0; i < len(data); i++ {
b.WriteString(strconv.Itoa(int(data[i])))
if i < len(data)-1 {
b.WriteString(",")
}
}
dataStr := b.String()
// fmt.Printf("data = %v\n", dataStr)
reader := strings.NewReader(dataStr)
// log.Infof("url = %v", c.url)
resp, err := http.Post(c.url, "application/x-www-form-urlencoded", reader)
_ = resp
_ = err
// fmt.Printf("resp=%v\nerr=%v", resp, err)
}
///////////
type LogConn struct {
start int
length int
}
func NewLogConn(cfg *config.AclNode) (DMXConn, error) {
conn := &LogConn{
start: cfg.DefChildAsInt(0, "start"),
length: cfg.DefChildAsInt(30, "length"),
}
return conn, nil
}
func (c *LogConn) SendDMX(Universe int, data []byte) {
if c == nil {
return
}
var b strings.Builder
b.WriteString(fmt.Sprintf("SendDMX %d [", c.start))
for i := 0; i < c.length; i++ {
if c.start+i >= len(data) {
break
}
b.WriteString(fmt.Sprintf("%d ", data[c.start+i]))
}
b.WriteString("]")
log.Debugf(b.String())
}
///////////
type FtdiConn struct {
ctx *FtdiContext
}
func NewFtdiConn(cfg *config.AclNode) (DMXConn, error) {
conn := &FtdiConn{
ctx: NewFtdiContext(),
}
// No reason to start it straight away
err := conn.ctx.Start()
if err != nil {
return nil, err
}
return conn, nil
}
func (c *FtdiConn) SendDMX(Universe int, data []byte) {
if c == nil {
return
}
// TODO: Honor universe mappings here???
c.ctx.WriteDmx(data)
}