-
Notifications
You must be signed in to change notification settings - Fork 0
/
osc.go
67 lines (61 loc) · 1.4 KB
/
osc.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
package main
import (
"encoding/json"
"fmt"
"github.com/loffa/gosc"
"github.com/spf13/viper"
"log"
"reflect"
"time"
)
var (
cli *gosc.Client
)
func setupOSCClient() {
address := fmt.Sprintf("%s:%d", viper.GetString("osc.host"), viper.GetInt("osc.port"))
client, err := gosc.NewClient(address)
if err != nil {
log.Fatalln(err)
}
cli = client
log.Println("Fetching console info...")
info, err := cli.CallMessage("/info")
if err != nil {
log.Fatalln("Could not get mixer info:", err)
}
log.Println("Got info from the OSC server:", info.Arguments)
err = cli.ReceiveMessageFunc("/*", oscMessageHandler)
if err != nil {
log.Fatalln("Could not register handler:", err)
}
go xremoteSender()
}
func xremoteSender() {
ticker := time.NewTicker(time.Second * 9)
err := cli.EmitMessage("/xremote")
if err != nil {
log.Println(err)
}
for range ticker.C {
err := cli.EmitMessage("/xremote")
if err != nil {
log.Println(err)
}
}
}
func oscMessageHandler(msg *gosc.Message) {
prefix := viper.GetString("mqtt.topic_prefix")
topic := fmt.Sprintf("%s%s", prefix, msg.Address)
res := make(mqttPayload, 0, len(msg.Arguments))
for _, v := range msg.Arguments {
res = append(res, &dataField{
Type: reflect.TypeOf(v).Name(),
Value: v,
})
}
data, _ := json.Marshal(res)
err := mq.Publish(topic, 0, true, data).Error()
if err != nil {
log.Println("MQTT: Could not publish:", err)
}
}