forked from talkkonnect/talkkonnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.go
279 lines (261 loc) · 10.6 KB
/
mqtt.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* talkkonnect headless mumble client/gateway with lcd screen and channel control
* Copyright (C) 2018-2019, Suvir Kumar <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* talkkonnect is the based on talkiepi and barnard by Daniel Chote and Tim Cooper
*
* The Initial Developer of the Original Code is
* Suvir Kumar <[email protected]>
* Portions created by the Initial Developer are Copyright (C) Suvir Kumar. All Rights Reserved.
*
* talKKonnectContributor(s):
*
* Suvir Kumar <[email protected]>
*
* My Blog is at www.talkkonnect.com
* The source code is hosted at github.com/talkkonnect
*
* MQTT License Details Copyright (c) 2013 IBM Corp.
*
* This project is dual licensed under the Eclipse Public License 1.0 and the
* Eclipse Distribution License 1.0 as described in the epl-v10 and edl-v10 files.
* The EDL is copied below in order to pass the pkg.go.dev license check (https://pkg.go.dev/license-policy).
* Eclipse Distribution License - v 1.0
* Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the Eclipse Foundation, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
*
*/
package talkkonnect
import (
"crypto/tls"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/talkkonnect/gpio"
)
func relayCommand(relayNo int, command string) {
// all relays (0)
if relayNo == 0 {
for i := 1; i <= int(TotalRelays); i++ {
if command == "on" {
log.Println("info: Relay ", i, "On")
gpio.NewOutput(RelayPins[i], false)
}
if command == "off" {
log.Println("info: Relay ", i, "Off")
gpio.NewOutput(RelayPins[i], true)
}
if command == "pulse" {
log.Println("info: Relay ", i, "Pulse")
gpio.NewOutput(RelayPins[i], false)
time.Sleep(RelayPulseMills * time.Millisecond)
gpio.NewOutput(RelayPins[i], true)
}
}
return
}
//specific relay (Number Between 1 and TotalRelays)
if relayNo >= 0 && relayNo <= int(TotalRelays) {
if command == "on" {
log.Println("info: Relay ", relayNo, "On")
gpio.NewOutput(RelayPins[relayNo], false)
}
if command == "off" {
log.Println("info: Relay ", relayNo, "Off")
gpio.NewOutput(RelayPins[relayNo], true)
}
if command == "pulse" {
log.Println("info: Relay ", relayNo, "Pulse")
gpio.NewOutput(RelayPins[relayNo], false)
time.Sleep(RelayPulseMills * time.Millisecond)
gpio.NewOutput(RelayPins[relayNo], true)
}
}
}
func (b *Talkkonnect) mqttsubscribe() {
log.Printf("info: MQTT Subscription Information")
log.Printf("info: MQTT Broker : %s\n", MQTTBroker)
log.Printf("debug: MQTT clientid : %s\n", MQTTId)
log.Printf("debug: MQTT user : %s\n", MQTTUser)
log.Printf("debug: MQTT password : %s\n", MQTTPassword)
log.Printf("info: Subscribed topic : %s\n", MQTTTopic)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
connOpts := MQTT.NewClientOptions().AddBroker(MQTTBroker).SetClientID(MQTTId).SetCleanSession(true)
if MQTTUser != "" {
connOpts.SetUsername(MQTTUser)
if MQTTPassword != "" {
connOpts.SetPassword(MQTTPassword)
}
}
tlsConfig := &tls.Config{InsecureSkipVerify: true, ClientAuth: tls.NoClientCert}
connOpts.SetTLSConfig(tlsConfig)
connOpts.OnConnect = func(c MQTT.Client) {
if token := c.Subscribe(MQTTTopic, byte(MQTTQos), b.onMessageReceived); token.Wait() && token.Error() != nil {
panic(token.Error())
}
}
client := MQTT.NewClient(connOpts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
} else {
log.Printf("info: Connected to : %s\n", MQTTBroker)
}
<-c
}
func (b *Talkkonnect) onMessageReceived(client MQTT.Client, message MQTT.Message) {
log.Printf("info: Received MQTT message on topic: %s Payload: %s\n", message.Topic(), message.Payload())
PayLoad := strings.ToLower(string(message.Payload()))
var ID int
var err error
if strings.Contains(PayLoad, "voicetargetset") {
if strings.Contains(PayLoad, ":") {
IDString := fmt.Sprintf("%v", PayLoad[15:])
ID, err = strconv.Atoi(IDString)
if err != nil {
log.Println("error: Target is ID not a number")
return
}
if ID >= 0 && ID <= 31 {
b.cmdSendVoiceTargets(uint32(ID))
log.Printf("info: MQTT SetTargetID %v Request Processed Successfully\n", IDString)
return
} else {
log.Println("error: Target ID NOT in Valid Range")
return
}
}
}
switch PayLoad {
case "displaymenu":
log.Println("info: MQTT Display Menu Request Processed Successfully")
b.cmdDisplayMenu()
case "channelup":
log.Println("info: MQTT Channel Up Request Processed Successfully")
b.cmdChannelUp()
case "channeldown":
log.Println("info: MQTT Channel Down Request Processed Successfully")
b.cmdChannelDown()
case "mute-toggle":
log.Println("info: MQTT Mute/UnMute Speaker Request Processed Successfully")
b.cmdMuteUnmute("toggle")
case "mute":
log.Println("info: MQTT Mute/UnMute Speaker Request Processed Successfully")
b.cmdMuteUnmute("mute")
case "unmute":
log.Println("info: MQTT Mute/UnMute Speaker Request Processed Successfully")
b.cmdMuteUnmute("unmute")
case "currentvolume":
log.Println("info: MQTT Current Volume Level Request Processed Successfully")
b.cmdCurrentVolume()
case "volumeup":
log.Println("info: MQTT Digital Volume Up Request Processed Successfully")
b.cmdVolumeUp()
case "volumedown":
log.Println("info: MQTT Digital Volume Down Request Processed Successfully")
b.cmdVolumeDown()
case "listchannels":
log.Println("info: MQTT List Server Channels Request Processed Successfully")
b.cmdListServerChannels()
case "starttransmitting":
log.Println("info: MQTT Start Transmitting Request Processed Successfully")
b.cmdStartTransmitting()
case "stoptransmitting":
log.Println("info: MQTT Stop Transmitting Request Processed Successfully")
b.cmdStopTransmitting()
case "listonlineusers":
log.Println("info: MQTT List Online Users Request Processed Successfully")
b.cmdListOnlineUsers()
case "stream-toggle":
log.Println("info: MQTT Play/Stop Stream Request Processed Successfully")
b.cmdPlayback()
case "gpsposition":
log.Println("info: MQTT Request GPS Position Processed Successfully")
b.cmdGPSPosition()
case "sendemail":
log.Println("info: MQTT Send Email Processed Successfully")
b.cmdSendEmail()
case "connpreviousserver":
log.Println("info: MQTT Previous Server Processed Successfully")
b.cmdConnPreviousServer()
case "connnextserver":
log.Println("info: MQTT Next Server Processed Successfully")
b.cmdConnNextServer()
case "clearscreen":
log.Println("info: MQTT Clear Screen Processed Successfully")
b.cmdClearScreen()
case "pingservers":
log.Println("info: MQTT Ping Servers Processed Successfully")
b.cmdPingServers()
case "panicsimulation":
log.Println("info: MQTT Request Panic Simulation Processed Successfully")
b.cmdPanicSimulation()
case "repeattxLoop":
log.Println("info: MQTT Request Repeat Tx Loop Test Processed Successfully")
b.cmdRepeatTxLoop()
case "scanchannels":
log.Println("info: MQTT Request Scan Processed Successfully")
b.cmdScanChannels()
case "thanks":
log.Println("info: MQTT Request Show Acknowledgements Processed Successfully")
b.cmdThanks()
case "showuptime":
log.Println("info: MQTT Request Current Version Successfully")
b.cmdShowUptime()
case "dumpxmlconfig":
log.Println("info: MQTT Print XML Config Processed Successfully")
b.cmdDumpXMLConfig()
case "attentionled:on":
log.Println("info: MQTT Turn On Attention LED Successfully")
b.LEDOn(b.AttentionLED)
case "attentionled:off":
log.Println("info: MQTT Turn Off Attention LED Successfully")
b.LEDOff(b.AttentionLED)
case "attentionled:blink":
log.Println("info: MQTT Blink Attention LED 20 times Successfully")
for i := 0; i < MQTTAttentionBlinkTimes; i++ {
b.LEDOn(b.AttentionLED)
time.Sleep(time.Duration(MQTTAttentionBlinkmsecs) * time.Millisecond)
b.LEDOff(b.AttentionLED)
time.Sleep(time.Duration(MQTTAttentionBlinkmsecs) * time.Millisecond)
}
case "relay1:on":
log.Println("info: MQTT Turn On Relay 1 Successfully")
relayCommand(1, "on")
case "relay1:off":
log.Println("info: MQTT Turn Off Relay 1 Successfully")
relayCommand(1, "off")
case "relay1:pulse":
log.Println("info: MQTT Pulse Relay 1 Successfully")
relayCommand(1, "pulse")
case "playrepeatertone":
log.Println("info: MQTT Play Repeater Tone Processed Successfully")
b.cmdPlayRepeaterTone()
// todo add other automation control for buttons, relays and leds here as needed in the future
default:
log.Printf("error: Undefined Command Received MQTT message on topic: %s Payload: %s\n", message.Topic(), message.Payload())
}
}