forked from talkkonnect/talkkonnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
2409 lines (2042 loc) · 63 KB
/
client.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.
*
* Contributor(s):
*
* Suvir Kumar <[email protected]>
*
* My Blog is at www.talkkonnect.com
* The source code is hosted at github.com/talkkonnect
*
*
*/
package talkkonnect
import (
"crypto/rand"
"crypto/tls"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/comail/colog"
htgotts "github.com/hegedustibor/htgo-tts"
"github.com/kennygrant/sanitize"
hd44780 "github.com/talkkonnect/go-hd44780"
"github.com/talkkonnect/gpio"
"github.com/talkkonnect/gumble/gumble"
"github.com/talkkonnect/gumble/gumbleutil"
_ "github.com/talkkonnect/gumble/opus"
term "github.com/talkkonnect/termbox-go"
"github.com/talkkonnect/volume-go"
)
var (
LcdText = [4]string{"nil", "nil", "nil", "nil"}
currentChannelID uint32
prevChannelID uint32
prevParticipantCount int = 0
prevButtonPress string = "none"
maxchannelid uint32
origVolume int
tempVolume int
ConfigXMLFile string
GPSTime string
GPSDate string
GPSLatitude float64
GPSLongitude float64
Streaming bool
AccountIndex int
ServerHop bool
httpServRunning bool
message string
isrepeattx bool = true
NowStreaming bool
)
type Talkkonnect struct {
Config *gumble.Config
Client *gumble.Client
Name string
Address string
Username string
Ident string
TLSConfig tls.Config
ConnectAttempts uint
Stream *Stream
ChannelName string
Logging string
Daemonize bool
IsConnected bool
IsTransmitting bool
IsPlayStream bool
GPIOEnabled bool
OnlineLED gpio.Pin
ParticipantsLED gpio.Pin
TransmitLED gpio.Pin
HeartBeatLED gpio.Pin
BackLightLED gpio.Pin
VoiceActivityLED gpio.Pin
TxButton gpio.Pin
TxButtonState uint
TxToggle gpio.Pin
TxToggleState uint
UpButton gpio.Pin
UpButtonState uint
DownButton gpio.Pin
DownButtonState uint
PanicButton gpio.Pin
PanicButtonState uint
CommentButton gpio.Pin
CommentButtonState uint
ChimesButton gpio.Pin
ChimesButtonState uint
}
// new configurable functionality not yet moved to XML
type ChannelsListStruct struct {
chanID uint32
chanName string
chanParent *gumble.Channel
chanUsers int
}
func reset() {
term.Sync()
}
func PreInit0(file string) {
ConfigXMLFile = file
err := readxmlconfig(ConfigXMLFile)
if err != nil {
log.Println("XML Parser Module Returned Error: ", err)
log.Fatal("Please Make Sure the XML Configuration File is In the Correct Path with the Correct Format, Exiting talkkonnect! ...... bye\n")
}
if APEnabled {
log.Println("info: Contacting http Provisioning Server Pls Wait")
err := autoProvision()
time.Sleep(5 * time.Second)
if err != nil {
log.Println("alert: Error from AutoProvisioning Module: ", err)
log.Println("Please Fix Problem with Provisioning Configuration or use Static File By Disabling AutoProvisioning ")
log.Fatal("Exiting talkkonnect! ...... bye\n")
} else {
log.Println("info: Got New Configuration Reloading XML Config")
ConfigXMLFile = file
readxmlconfig(ConfigXMLFile)
}
}
b := Talkkonnect{
Config: gumble.NewConfig(),
Name: Name[AccountIndex],
Address: Server[AccountIndex],
Username: Username[AccountIndex],
Ident: Ident[AccountIndex],
ChannelName: Channel[AccountIndex],
Logging: Logging,
Daemonize: Daemonize,
}
b.PreInit1(false)
}
func (b *Talkkonnect) PreInit1(httpServRunning bool) {
if len(b.Username) == 0 {
buf := make([]byte, 6)
_, err := rand.Read(buf)
if err != nil {
log.Println("alert: Cannot Generate Random Name Error: ", err)
log.Fatal("Exiting talkkonnect! ...... bye!\n")
}
buf[0] |= 2
b.Config.Username = fmt.Sprintf("talkkonnect-%02x%02x%02x%02x%02x%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])
} else {
b.Config.Username = Username[AccountIndex]
}
b.Config.Password = Password[AccountIndex]
if Insecure[AccountIndex] {
b.TLSConfig.InsecureSkipVerify = true
}
if Certificate[AccountIndex] != "" {
cert, err := tls.LoadX509KeyPair(Certificate[AccountIndex], Certificate[AccountIndex])
if err != nil {
log.Println("alert: Certificate Error: ", err)
log.Fatal("Exiting talkkonnect! ...... bye!\n")
}
b.TLSConfig.Certificates = append(b.TLSConfig.Certificates, cert)
}
if APIEnabled && !httpServRunning {
go func() {
http.HandleFunc("/", b.httpHandler)
if err := http.ListenAndServe(":"+APIListenPort, nil); err != nil {
log.Println("alert: Problem With Starting HTTP API Server Error: ", err)
log.Fatal("Please Fix Problem or Disable API in XML Config, Exiting talkkonnect! ...... bye!\n")
}
}()
}
b.Init()
b.IsConnected = false
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
exitStatus := 0
<-sigs
b.CleanUp()
os.Exit(exitStatus)
}
func (b *Talkkonnect) Init() {
f, err := os.OpenFile(LogFilenameAndPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
log.Println("alert: Trying to Open File ", LogFilenameAndPath)
if err != nil {
log.Println("alert: Problem opening talkkonnect.log file Error: ", err)
log.Fatal("Exiting talkkonnect! ...... bye!\n")
}
if TargetBoard == "rpi" {
b.LEDOffAll()
}
if b.Logging == "screen" {
colog.Register()
colog.SetOutput(os.Stdout)
} else {
wrt := io.MultiWriter(os.Stdout, f)
log.SetOutput(wrt)
}
err = term.Init()
if err != nil {
log.Println("alert: Cannot Initalize Terminal Error: ", err)
log.Fatal("Exiting talkkonnect! ...... bye!\n")
}
b.Config.Attach(gumbleutil.AutoBitrate)
b.Config.Attach(b)
if TargetBoard == "rpi" {
log.Println("info: Target Board Set as RPI (gpio enabled) ")
b.initGPIO()
} else {
log.Println("info: Target Board Set as PC (gpio disabled) ")
}
talkkonnectBanner()
err = volume.Unmute(OutputDevice)
if err != nil {
log.Println("warn: Unable to Unmute ", err)
} else {
log.Println("info: Speaker UnMuted Before Connect to Server")
}
if TTSEnabled && TTSTalkkonnectLoaded {
err := PlayWavLocal(TTSTalkkonnectLoadedFilenameAndPath, TTSVolumeLevel)
if err != nil {
log.Println("PlayWavLocal(TTSTalkkonnectLoadedFilenameAndPath) Returned Error: ", err)
}
}
b.Connect()
if HeartBeatEnabled && TargetBoard == "rpi" {
HeartBeat := time.NewTicker(time.Duration(PeriodmSecs) * time.Millisecond)
go func() {
for _ = range HeartBeat.C {
timer1 := time.NewTimer(time.Duration(LEDOnmSecs) * time.Millisecond)
timer2 := time.NewTimer(time.Duration(LEDOffmSecs) * time.Millisecond)
<-timer1.C
b.LEDOn(b.HeartBeatLED)
<-timer2.C
b.LEDOff(b.HeartBeatLED)
}
}()
}
if BeaconEnabled {
BeaconTicker := time.NewTicker(time.Duration(BeaconTimerSecs) * time.Second)
go func() {
for _ = range BeaconTicker.C {
b.IsPlayStream = true
b.playIntoStream(BeaconFilenameAndPath, BVolume)
b.IsPlayStream = false
log.Println("warn: Beacon Enabled and Timed Out Auto Played File ", BeaconFilenameAndPath, " Into Stream")
}
}()
}
b.BackLightTimer()
if AudioRecordEnabled == true {
if AudioRecordOnStart == true {
if AudioRecordMode != "" {
if AudioRecordMode == "traffic" {
log.Println("info: Incoming Traffic will be Recorded with sox")
AudioRecordTraffic()
if TargetBoard == "rpi" {
if LCDEnabled == true {
LcdText = [4]string{"nil", "nil", "nil", "Traffic Recording ->"} // 4
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(false, 6, 1, "Traffic Recording") // 6
}
}
}
if AudioRecordMode == "ambient" {
log.Println("info: Ambient Audio from Mic will be Recorded with sox")
AudioRecordAmbient()
if TargetBoard == "rpi" {
if LCDEnabled == true {
LcdText = [4]string{"nil", "nil", "nil", "Mic Recording ->"} // 4
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(false, 6, 1, "Mic Recording") // 6
}
}
}
if AudioRecordMode == "combo" {
log.Println("info: Both Incoming Traffic and Ambient Audio from Mic will be Recorded with sox")
AudioRecordCombo()
if TargetBoard == "rpi" {
if LCDEnabled == true {
LcdText = [4]string{"nil", "nil", "nil", "Combo Recording ->"} // 4
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(false, 6, 1, "Combo Recording") //6
}
}
}
}
}
}
keyPressListenerLoop:
for {
switch ev := term.PollEvent(); ev.Type {
case term.EventKey:
switch ev.Key {
case term.KeyEsc:
log.Println("--")
log.Println("warn: ESC Key is Invalid")
reset()
break keyPressListenerLoop
log.Println("--")
case term.KeyDelete:
b.commandKeyDel()
case term.KeyF1:
b.commandKeyF1()
case term.KeyF2:
b.commandKeyF2()
case term.KeyF3:
b.commandKeyF3()
case term.KeyF4:
b.commandKeyF4()
case term.KeyF5:
b.commandKeyF5()
case term.KeyF6:
b.commandKeyF6()
case term.KeyF7:
b.commandKeyF7()
case term.KeyF8:
b.commandKeyF8()
case term.KeyF9:
b.commandKeyF9()
case term.KeyF10:
b.commandKeyF10()
case term.KeyF11:
b.commandKeyF11()
case term.KeyF12:
b.commandKeyF12()
case term.KeyCtrlC:
talkkonnectAcknowledgements()
b.commandKeyCtrlC()
case term.KeyCtrlE:
b.commandKeyCtrlE()
case term.KeyCtrlF:
b.commandKeyCtrlF()
case term.KeyCtrlI: // New. Audio Recording. Traffic
b.commandKeyCtrlI()
case term.KeyCtrlJ: // New. Audio Recording. Mic
b.commandKeyCtrlJ()
case term.KeyCtrlK: // New/ Audio Recording. Combo
b.commandKeyCtrlK()
case term.KeyCtrlL:
b.commandKeyCtrlL()
case term.KeyCtrlO:
b.commandKeyCtrlO()
case term.KeyCtrlN:
b.commandKeyCtrlN()
case term.KeyCtrlP:
b.commandKeyCtrlP()
case term.KeyCtrlR:
b.commandKeyCtrlR()
case term.KeyCtrlS:
b.commandKeyCtrlS()
case term.KeyCtrlT:
b.commandKeyCtrlT()
case term.KeyCtrlV:
b.commandKeyCtrlV()
case term.KeyCtrlX:
b.commandKeyCtrlX()
default:
log.Println("--")
if ev.Ch != 0 {
log.Println("warn: Invalid Keypress ASCII", ev.Ch)
} else {
log.Println("warn: Key Not Mapped")
}
log.Println("--")
}
case term.EventError:
log.Println("alert: Terminal Error: ", ev.Err)
log.Fatal("Exiting talkkonnect! ...... bye!\n")
}
}
}
func (b *Talkkonnect) CleanUp() {
log.Println("warn: SIGHUP Termination of Program Requested...shutting down...bye!")
if TargetBoard == "rpi" {
t := time.Now()
if LCDEnabled == true {
LcdText = [4]string{"talkkonnect stopped", t.Format("02-01-2006 15:04:05"), "Please Visit", "www.talkkonnect.com"}
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(true, 0, 1, "talkkonnect stopped")
oledDisplay(false, 1, 1, t.Format("02-01-2006 15:04:05"))
oledDisplay(false, 6, 1, "Please Visit")
oledDisplay(false, 7, 1, "www.talkkonnect.com")
}
b.LEDOffAll()
}
b.Client.Disconnect()
c := exec.Command("reset")
c.Stdout = os.Stdout
c.Run()
os.Exit(0)
}
func (b *Talkkonnect) Connect() {
b.IsConnected = false
b.IsPlayStream = false
NowStreaming = false
time.Sleep(2 * time.Second)
var err error
b.ConnectAttempts++
_, err = gumble.DialWithDialer(new(net.Dialer), b.Address, b.Config, &b.TLSConfig)
if err != nil {
log.Println("warn: Connection Error ", err, " connecting to ", b.Address, " failed (%s), attempting again in 10 seconds...")
if !ServerHop {
log.Println("warn: In the Connect Function & Trying With Username ", Username)
b.ReConnect()
}
} else {
b.OpenStream()
}
}
func (b *Talkkonnect) ReConnect() {
b.IsConnected = false
b.IsPlayStream = false
NowStreaming = false
if b.Client != nil {
log.Println("warn: Attempting Reconnection With Server")
b.Client.Disconnect()
}
time.Sleep(10 * time.Second)
if b.ConnectAttempts < 10 {
//go func() {
if !ServerHop {
b.Connect()
time.Sleep(3 * time.Second)
ServerHop = false
}
//}()
return
} else {
log.Println("warn: Unable to connect, giving up")
if TargetBoard == "rpi" {
if LCDEnabled == true {
LcdText = [4]string{"Failed to Connect!", "nil", "nil", "nil"}
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(false, 2, 1, "Failed to Connect!")
}
}
log.Fatal("Exiting talkkonnect! ...... bye!\n")
}
}
func (b *Talkkonnect) OpenStream() {
if os.Getenv("ALSOFT_LOGLEVEL") == "" {
os.Setenv("ALSOFT_LOGLEVEL", "0")
}
if stream, err := New(b.Client); err != nil {
log.Println("warn: Stream open error ", err)
if TargetBoard == "pi" {
if LCDEnabled == true {
LcdText = [4]string{"Stream Error!", "nil", "nil", "nil"}
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(false, 2, 1, "Stream Error!!")
}
}
log.Fatal("Exiting talkkonnect! ...... bye!\n")
} else {
b.Stream = stream
}
}
func (b *Talkkonnect) ResetStream() {
b.Stream.Destroy()
time.Sleep(50 * time.Millisecond)
b.OpenStream()
}
func (b *Talkkonnect) TransmitStart() {
if !(b.IsConnected) {
return
}
b.BackLightTimer()
t := time.Now()
if SimplexWithMute {
err := volume.Mute(OutputDevice)
if err != nil {
log.Println("warn: Unable to Mute ", err)
} else {
log.Println("info: Speaker Muted ")
}
}
if b.IsPlayStream {
b.IsPlayStream = false
NowStreaming = false
b.playIntoStream(ChimesSoundFilenameAndPath, ChimesSoundVolume)
time.Sleep(100 * time.Millisecond)
}
if TargetBoard == "rpi" {
b.LEDOn(b.TransmitLED)
if LCDEnabled == true {
LcdText[0] = "Online/TX"
LcdText[3] = "TX at " + t.Format("15:04:05")
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
//oledDisplay(true, 0, 0, "") // clear the screen
oledDisplay(false, 0, 1, "Online/TX")
oledDisplay(false, 3, 1, "TX at "+t.Format("15:04:05"))
oledDisplay(false, 6, 1, "Please Visit ")
oledDisplay(false, 7, 1, "www.talkkonnect.com")
}
}
b.IsTransmitting = true
b.Stream.StartSource()
}
func (b *Talkkonnect) TransmitStop(withBeep bool) {
if !(b.IsConnected) {
return
}
b.BackLightTimer()
if TargetBoard == "rpi" {
b.LEDOff(b.TransmitLED)
if LCDEnabled == true {
LcdText[0] = b.Address
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(false, 0, 1, b.Address)
}
}
b.IsTransmitting = false
b.Stream.StopSource()
if RogerBeepSoundEnabled {
if withBeep {
err := b.RogerBeep(RogerBeepSoundFilenameAndPath, RogerBeepSoundVolume)
if err != nil {
log.Println("alert: Roger Beep Module Returned Error: ", err)
}
}
}
if SimplexWithMute {
err := volume.Unmute(OutputDevice)
if err != nil {
log.Println("warn: Unable to Unmute ", err)
} else {
log.Println("info: Speaker UnMuted ")
}
}
}
func (b *Talkkonnect) OnConnect(e *gumble.ConnectEvent) {
b.IsConnected = true
b.BackLightTimer()
b.Client = e.Client
b.ConnectAttempts = 0
log.Println("info: Connected to ", b.Name, " ", b.Client.Conn.RemoteAddr(), " on attempt", b.ConnectAttempts)
if e.WelcomeMessage != nil {
log.Print(fmt.Sprintf("info: Welcome message: %s\n", esc(*e.WelcomeMessage)))
}
if TargetBoard == "rpi" {
b.LEDOn(b.OnlineLED)
if LCDEnabled == true {
LcdText = [4]string{"nil", "nil", "nil", "nil"}
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(true, 0, 0, "") // clear the screen
}
b.ParticipantLEDUpdate(true)
}
if b.ChannelName != "" {
b.ChangeChannel(b.ChannelName)
prevChannelID = b.Client.Self.Channel.ID
}
}
func (b *Talkkonnect) OnDisconnect(e *gumble.DisconnectEvent) {
if !ServerHop {
b.BackLightTimer()
}
var reason string
switch e.Type {
case gumble.DisconnectError:
reason = "connection error"
}
b.IsConnected = false
if TargetBoard == "rpi" {
b.LEDOff(b.OnlineLED)
b.LEDOff(b.ParticipantsLED)
b.LEDOff(b.TransmitLED)
}
if reason == "" {
log.Println("warn: Connection to ", b.Address, "disconnected")
if !ServerHop {
log.Println("warn: Attempting Reconnect in 10 seconds...")
}
} else {
log.Println("warn: Connection to ", b.Address, " disconnected ", reason)
if !ServerHop {
log.Println("warn: Aattempting Reconnect in 10 seconds...\n")
}
}
if !ServerHop {
b.ReConnect()
}
}
func (b *Talkkonnect) ChangeChannel(ChannelName string) {
if !(b.IsConnected) {
return
}
b.BackLightTimer()
channel := b.Client.Channels.Find(ChannelName)
if channel != nil {
b.Client.Self.Move(channel)
if TargetBoard == "rpi" {
if LCDEnabled == true {
LcdText[1] = "Joined " + ChannelName
LcdText[2] = Username[AccountIndex]
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(false, 0, 1, "Joined "+ChannelName)
oledDisplay(false, 1, 1, Username[AccountIndex])
}
}
log.Println("info: Joined Channel Name: ", channel.Name, " ID ", channel.ID)
prevChannelID = b.Client.Self.Channel.ID
} else {
log.Println("warn: Unable to Find Channel Name: ", ChannelName)
prevChannelID = 0
}
}
func (b *Talkkonnect) ParticipantLEDUpdate(verbose bool) {
if !(b.IsConnected) {
return
}
b.BackLightTimer()
time.Sleep(100 * time.Millisecond)
var participantCount = len(b.Client.Self.Channel.Users)
if participantCount > 1 && participantCount != prevParticipantCount {
if TTSEnabled && TTSParticipants {
speech := htgotts.Speech{Folder: "audio", Language: "en"}
speech.Speak("There Are Currently " + strconv.Itoa(participantCount) + " Users in The Channel " + b.Client.Self.Channel.Name)
}
if EventSoundEnabled {
err := PlayWavLocal(EventSoundFilenameAndPath, 100)
if err != nil {
log.Println("PlayWavLocal(EventSoundFilenameAndPath) Returned Error: ", err)
}
}
prevParticipantCount = participantCount
if verbose {
log.Println("info: Current Channel ", b.Client.Self.Channel.Name, " has (", participantCount, ") participants")
b.ListUsers()
if TargetBoard == "rpi" {
if LCDEnabled == true {
LcdText[0] = b.Address
LcdText[1] = b.Client.Self.Channel.Name + " (" + strconv.Itoa(participantCount) + " Users)"
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(false, 0, 1, b.Address)
oledDisplay(false, 1, 1, b.Client.Self.Channel.Name+" ("+strconv.Itoa(participantCount)+" Users)")
oledDisplay(false, 6, 1, "Please Visit")
oledDisplay(false, 7, 1, "www.talkkonnect.com")
}
}
}
}
if participantCount > 1 {
if TargetBoard == "rpi" {
b.LEDOn(b.ParticipantsLED)
b.LEDOn(b.OnlineLED)
}
} else {
if verbose {
if TTSEnabled && TTSParticipants {
speech := htgotts.Speech{Folder: "audio", Language: "en"}
speech.Speak("You are Currently Alone in The Channel " + b.Client.Self.Channel.Name)
}
log.Println("info: Channel ", b.Client.Self.Channel.Name, " has no other participants")
prevParticipantCount = 0
if TargetBoard == "rpi" {
b.LEDOff(b.ParticipantsLED)
if LCDEnabled == true {
LcdText = [4]string{b.Address, "Alone in " + b.Client.Self.Channel.Name, "", "nil"}
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(false, 0, 1, b.Address)
oledDisplay(false, 1, 1, "Alone in "+b.Client.Self.Channel.Name)
}
}
}
}
}
func (b *Talkkonnect) OnTextMessage(e *gumble.TextMessageEvent) {
b.BackLightTimer()
if len(cleanstring(e.Message)) > 105 {
log.Println(fmt.Sprintf("alert: Message Too Long to Be Displayed on Screen\n"))
message = strings.TrimSpace(cleanstring(e.Message)[:105])
} else {
message = strings.TrimSpace(cleanstring(e.Message))
}
var sender string
if e.Sender != nil {
sender = strings.TrimSpace(cleanstring(e.Sender.Name))
log.Println("alert: Sender Name is ", sender)
} else {
sender = ""
}
log.Println(fmt.Sprintf("alert: Message ("+strconv.Itoa(len(message))+") from %v %v\n", sender, message))
if TargetBoard == "rpi" {
if LCDEnabled == true {
LcdText[0] = "Msg From " + sender
LcdText[1] = message
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(false, 2, 1, "Msg From "+sender)
if len(message) <= 21 {
oledDisplay(false, 3, 1, message)
oledDisplay(false, 4, 1, "")
oledDisplay(false, 5, 1, "")
oledDisplay(false, 6, 1, "")
oledDisplay(false, 7, 1, "")
} else if len(message) <= 42 {
oledDisplay(false, 3, 1, message[0:21])
oledDisplay(false, 4, 1, message[21:len(message)])
oledDisplay(false, 5, 1, "")
oledDisplay(false, 6, 1, "")
oledDisplay(false, 7, 1, "")
} else if len(message) <= 63 {
oledDisplay(false, 3, 1, message[0:21])
oledDisplay(false, 4, 1, message[21:42])
oledDisplay(false, 5, 1, message[42:len(message)])
oledDisplay(false, 6, 1, "")
oledDisplay(false, 7, 1, "")
} else if len(message) <= 84 {
oledDisplay(false, 3, 1, message[0:21])
oledDisplay(false, 4, 1, message[21:42])
oledDisplay(false, 5, 1, message[42:63])
oledDisplay(false, 6, 1, message[63:len(message)])
oledDisplay(false, 7, 1, "")
} else if len(message) <= 105 {
oledDisplay(false, 3, 1, message[0:20])
oledDisplay(false, 4, 1, message[21:44])
oledDisplay(false, 5, 1, message[42:63])
oledDisplay(false, 6, 1, message[63:84])
oledDisplay(false, 7, 1, message[84:105])
}
}
}
if EventSoundEnabled {
err := PlayWavLocal(EventSoundFilenameAndPath, 100)
if err != nil {
log.Println("PlayWavLocal(EventSoundFilenameAndPath) Returned Error: ", err)
}
}
}
func (b *Talkkonnect) OnUserChange(e *gumble.UserChangeEvent) {
b.BackLightTimer()
var info string
switch e.Type {
case gumble.UserChangeConnected:
info = "conn"
case gumble.UserChangeDisconnected:
info = "disconnected!"
case gumble.UserChangeKicked:
info = "kicked"
case gumble.UserChangeBanned:
info = "banned"
case gumble.UserChangeRegistered:
info = "registered"
case gumble.UserChangeUnregistered:
info = "unregistered"
case gumble.UserChangeName:
info = "chg name"
case gumble.UserChangeChannel:
info = "chg channel"
log.Println("info:", cleanstring(e.User.Name), " Changed Channel to ", e.User.Channel.Name)
LcdText[2] = cleanstring(e.User.Name) + "->" + e.User.Channel.Name
LcdText[3] = ""
case gumble.UserChangeComment:
info = "chg comment"
case gumble.UserChangeAudio:
info = "chg audio"
case gumble.UserChangePrioritySpeaker:
info = "is priority"
case gumble.UserChangeRecording:
info = "chg rec status"
case gumble.UserChangeStats:
info = "chg stats"
if info != "chg channel" {
if info != "" {
log.Println("info: User ", cleanstring(e.User.Name), " ", info, "Event type=", e.Type, " channel=", e.User.Channel.Name)
if TTSEnabled && TTSParticipants {
speech := htgotts.Speech{Folder: "audio", Language: "en"}
speech.Speak("User ")
}
}
} else {
log.Println("info: User ", cleanstring(e.User.Name), " Event type=", e.Type, " channel=", e.User.Channel.Name)
}
LcdText[2] = cleanstring(e.User.Name) + " " + info //+strconv.Atoi(string(e.Type))
}
b.ParticipantLEDUpdate(true)
}
func (b *Talkkonnect) OnPermissionDenied(e *gumble.PermissionDeniedEvent) {
var info string
switch e.Type {
case gumble.PermissionDeniedOther:
info = e.String
case gumble.PermissionDeniedPermission:
info = "insufficient permissions"
LcdText[2] = "insufficient perms"
// Set Upper Boundary
if prevButtonPress == "ChannelUp" && b.Client.Self.Channel.ID == maxchannelid {
log.Println("info: Can't Increment Channel Maximum Channel Reached")
}
// Set Lower Boundary
if prevButtonPress == "ChannelDown" && currentChannelID == 0 {
log.Println("info: Can't Increment Channel Minumum Channel Reached")
}
// Implement Seek Up Until Permissions are Sufficient for User to Join Channel whilst avoiding all null channels
if prevButtonPress == "ChannelUp" && b.Client.Self.Channel.ID+1 < maxchannelid {
prevChannelID++
b.ChannelUp()
LcdText[1] = b.Client.Self.Channel.Name + " (" + strconv.Itoa(len(b.Client.Self.Channel.Users)) + " Users)"
}
// Implement Seek Dwn Until Permissions are Sufficient for User to Join Channel whilst avoiding all null channels
if prevButtonPress == "ChannelDown" && int(b.Client.Self.Channel.ID) > 0 {
prevChannelID--
b.ChannelDown()
LcdText[1] = b.Client.Self.Channel.Name + " (" + strconv.Itoa(len(b.Client.Self.Channel.Users)) + " Users)"
}
if TargetBoard == "rpi" {
if LCDEnabled == true {
go hd44780.LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress)
}
if OLEDEnabled == true {
oledDisplay(false, 1, 1, LcdText[1])
oledDisplay(false, 2, 1, LcdText[2])
}
}