forked from lornajane/streamdeck-tricks
/
mqtt_thing.go
134 lines (113 loc) · 3.94 KB
/
mqtt_thing.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
package addons
import (
"encoding/json"
"image/color"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/rf152/go-streamdeck"
"github.com/rf152/go-streamdeck/buttons"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
type MqttThing struct {
SD *streamdeck.StreamDeck
mqtt_client mqtt.Client
}
func (p *MqttThing) Init() {
// Initialise MQTT to use the shelf light features
log.Debug().Msg("Connecting to MQTT...")
opts := mqtt.NewClientOptions().AddBroker("tcp://10.1.0.1:1883").SetClientID("go-streamdeck")
p.mqtt_client = mqtt.NewClient(opts)
if conn_token := p.mqtt_client.Connect(); conn_token.Wait() && conn_token.Error() != nil {
log.Warn().Err(conn_token.Error()).Msg("Cannot connect to MQTT")
}
}
type LEDWallBg struct {
Action string `json:"action"`
Red uint8 `json:"r"`
Green uint8 `json:"g"`
Blue uint8 `json:"b"`
}
type LEDWallFirework struct {
Action string `json:"action"`
Hue uint8 `json:"b,omitempty"`
}
type LEDWallSnake struct {
Action string `json:"action"`
Enabled bool `json:"enabled"`
}
// Set up buttons
func (p *MqttThing) Buttons() {
button_index := 18
bgoptions3 := LEDWallBg{"background", 1, 3, 3}
lbutton3 := buttons.NewColourButton(color.RGBA{155, 255, 255, 255})
lbutton3.SetActionHandler(&MQTTBgAction{Options: bgoptions3, Client: p.mqtt_client})
p.SD.AddButton(button_index, lbutton3)
button_index = button_index + 1
bgoptions4 := LEDWallBg{"background", 6, 1, 4}
lbutton4 := buttons.NewColourButton(color.RGBA{255, 155, 155, 255})
lbutton4.SetActionHandler(&MQTTBgAction{Options: bgoptions4, Client: p.mqtt_client})
p.SD.AddButton(button_index, lbutton4)
button_index = button_index + 1
bgoptions2 := LEDWallBg{"background", 6, 5, 1}
lbutton2 := buttons.NewColourButton(color.RGBA{255, 255, 155, 255})
lbutton2.SetActionHandler(&MQTTBgAction{Options: bgoptions2, Client: p.mqtt_client})
p.SD.AddButton(button_index, lbutton2)
button_index = button_index + 1
bgoptions5 := LEDWallBg{"background", 4, 2, 4}
lbutton5 := buttons.NewColourButton(color.RGBA{255, 200, 255, 255})
lbutton5.SetActionHandler(&MQTTBgAction{Options: bgoptions5, Client: p.mqtt_client})
p.SD.AddButton(button_index, lbutton5)
fireworkoptions := LEDWallFirework{"firework", 0}
fbutton, ferr := buttons.NewImageFileButton(viper.GetString("buttons.images") + "/firework-sparkler.png")
if ferr != nil {
panic(ferr)
}
fbutton.SetActionHandler(&MQTTFireworkAction{Options: fireworkoptions, Client: p.mqtt_client})
p.SD.AddButton(16, fbutton)
snakeoptions := LEDWallSnake{"snake", false}
sbutton := buttons.NewTextButton("Snake")
sbutton.SetActionHandler(&MQTTSnakeAction{Options: snakeoptions, Client: p.mqtt_client})
p.SD.AddButton(17, sbutton)
}
type MQTTBgAction struct {
Client mqtt.Client
Options LEDWallBg
btn streamdeck.Button
}
func (action *MQTTBgAction) Pressed(btn streamdeck.Button) {
payload, _ := json.Marshal(action.Options)
log.Debug().Msg(string(payload))
token := action.Client.Publish("/ledwall/1/request", 0, false, payload)
token.Wait()
}
type MQTTFireworkAction struct {
Client mqtt.Client
Options LEDWallFirework
btn streamdeck.Button
}
func (action *MQTTFireworkAction) Pressed(btn streamdeck.Button) {
payload, _ := json.Marshal(action.Options)
log.Debug().Msg(string(payload))
token := action.Client.Publish("/ledwall/1/request", 0, false, payload)
token.Wait()
}
type MQTTSnakeAction struct {
Client mqtt.Client
Options LEDWallSnake
btn streamdeck.Button
snake_state bool
}
func (action *MQTTSnakeAction) Pressed(btn streamdeck.Button) {
// use current snake state before sending, then switch state after
action.Options.Enabled = action.snake_state
payload, _ := json.Marshal(action.Options)
log.Debug().Msg(string(payload))
token := action.Client.Publish("/ledwall/1/request", 0, false, payload)
token.Wait()
// now toggle state ready for next press
if action.snake_state {
action.snake_state = false
} else {
action.snake_state = true
}
}