From 90c2a1a80e76362a81ccd6b766aa483ef5c5450f Mon Sep 17 00:00:00 2001 From: Suvir Kumar Date: Sun, 22 Aug 2021 07:18:14 +0700 Subject: [PATCH] code cleanup --- commandkeys.go | 40 +------------- htgotts.go | 31 ++++------- mqtt.go | 6 +- onevent.go | 7 ++- scripts/update-talkkonnect.sh | 6 ++ usbkeyboard.go | 31 +++++++++++ utils.go | 8 +++ xmlparser.go | 101 +++++++++++++++++----------------- 8 files changed, 118 insertions(+), 112 deletions(-) diff --git a/commandkeys.go b/commandkeys.go index 4957586..25c7451 100644 --- a/commandkeys.go +++ b/commandkeys.go @@ -572,7 +572,7 @@ func (b *Talkkonnect) cmdClearScreen() { if OLEDEnabled { Oled.DisplayOn() LCDIsDark = false - oledDisplay(true, 0, 0, "") // clear the screen + oledDisplay(true, 0, 0, "") } } } @@ -742,12 +742,6 @@ func (b *Talkkonnect) cmdPanicSimulation() { b.SendMessage(fmt.Sprintf("My Username is %s and Ident is %s", b.Username, b.Ident), PRecursive) } - // New. Need to allow sending email / text alert on Panic Event even is the GPS is not enabled or working. - // If no GPS is included in email / Mumble text message, it means GPS in not enabled or not working. - - //if PSendGpsLocation && GpsEnabled { - //if GpsEnabled { - if PSendGpsLocation { var i int = 0 @@ -811,47 +805,17 @@ func (b *Talkkonnect) cmdPanicSimulation() { IsPlayStream = false b.IsTransmitting = false - // New. Decide should TX Led be On or Off during a panic event? I think don't turn off here, - // Want to see PTT key working when testing panic function. - // We shall use another low profile lights/LCD function to turn all the lights and display off during panic event - // They should be dim. Not attracting attention - //if TargetBoard == "rpi" { - //if !LedStripEnabled { - //b.LEDOff(b.TransmitLED) - //} else { - // - //MyLedStripTransmitLEDOff() - //} - //log.Println("--") - - //New. Low Profile Leds and LCD during Panic Event. Turn All Lights Off. - //Heartbeat will be running - disable in xml config? - //Backlight LCD led is dim with a transistor control, but it should work... - //Add another feature to enable all lights back on a PTT press after the panic event is over. To do? - if PLowProfile { b.LEDOffAll() log.Println("info: Low Profile Lights Option is Enabled. Turning All Leds Off During Panic Event") - - //b.LEDOff(b.TransmitLED) - //b.LEDOff(b.BackLightLED) - //b.LEDOff(b.OnlineLED) - //b.LEDOff(b.ParticipantsLED) - //b.LEDOff(b.VoiceActivityLED) - // - //b.LEDOff(b.HeartBeatLED) - // - if LCDEnabled { log.Println("info: Low Profile Lights is Enabled. Turning Off Display During Panic Event") LcdText = [4]string{"", "", "", ""} LcdDisplay(LcdText, LCDRSPin, LCDEPin, LCDD4Pin, LCDD5Pin, LCDD6Pin, LCDD7Pin, LCDInterfaceType, LCDI2CAddress) } if OLEDEnabled { - oledDisplay(true, 0, 0, "") // clear OLED screen. How about HD44780 and heartbit? + oledDisplay(true, 0, 0, "") } - // End of Low Profile Lights - } } } diff --git a/htgotts.go b/htgotts.go index 93615e5..7344116 100644 --- a/htgotts.go +++ b/htgotts.go @@ -111,26 +111,19 @@ func localmediaplayer(fileName string) { localplayer.Run() } -func (b *Talkkonnect) TTSPlayer(ttsMessage string, ttsMessageReadEnabled bool, ttsLocalPlay bool, ttsLocalPlayRXLed bool, ttlPlayIntoStream bool) { - - // check if tts message read is enabled - if ttsMessageReadEnabled { - //check if the user wants local play - if ttsLocalPlay { - //check if the user wants local play with rxled on - if ttsLocalPlayRXLed { - LEDOnFunc(VoiceActivityLED) - } - b.Speak(ttsMessage, "local") - //check if the user wants local play with rxled on - if ttsLocalPlayRXLed { - LEDOffFunc(VoiceActivityLED) - } - } +func (b *Talkkonnect) TTSPlayer(ttsMessage string, ttsLocalPlay bool, ttsLocalPlayRXLed bool, ttlPlayIntoStream bool) { - //check if the user wants this message played into mumble stream - if ttlPlayIntoStream { - b.Speak(ttsMessage, "intostream") + if ttsLocalPlay { + if ttsLocalPlayRXLed { + LEDOnFunc(VoiceActivityLED) + } + b.Speak(ttsMessage, "local") + if ttsLocalPlayRXLed { + LEDOffFunc(VoiceActivityLED) } } + + if ttlPlayIntoStream { + b.Speak(ttsMessage, "intostream") + } } diff --git a/mqtt.go b/mqtt.go index 8c8737c..0adadf9 100644 --- a/mqtt.go +++ b/mqtt.go @@ -253,11 +253,11 @@ func (b *Talkkonnect) onMessageReceived(client MQTT.Client, message MQTT.Message b.LEDOff(b.AttentionLED) case "attentionled:blink": log.Println("info: MQTT Blink Attention LED 20 times Successfully") - for i := 0; i < 20; i++ { + for i := 0; i < MQTTAttentionBlinkTimes; i++ { b.LEDOn(b.AttentionLED) - time.Sleep(300 * time.Millisecond) + time.Sleep(time.Duration(MQTTAttentionBlinkmsecs) * time.Millisecond) b.LEDOff(b.AttentionLED) - time.Sleep(300 * time.Millisecond) + time.Sleep(time.Duration(MQTTAttentionBlinkmsecs) * time.Millisecond) } case "relay1:on": log.Println("info: MQTT Turn On Relay 1 Successfully") diff --git a/onevent.go b/onevent.go index 4ed0782..906b3fd 100644 --- a/onevent.go +++ b/onevent.go @@ -144,9 +144,10 @@ func (b *Talkkonnect) OnTextMessage(e *gumble.TextMessageEvent) { log.Println(fmt.Sprintf("info: Message ("+strconv.Itoa(len(message))+") from %v %v\n", sender, message)) - voiceMessage := fmt.Sprintf("Message from %v %v\n", sender, cleanstring(e.Message)) - - b.TTSPlayer(voiceMessage, TTSMessageEnabled, TTSLocalPlay, TTSLocalPlayWithRXLED, TTSPlayIntoStream) + if TTSMessageEnabled { + voiceMessage := fmt.Sprintf("Message from %v %v\n", sender, cleanstring(e.Message)) + b.TTSPlayer(voiceMessage, TTSLocalPlay, TTSLocalPlayWithRXLED, TTSPlayIntoStream) + } if TargetBoard == "rpi" { if LCDEnabled { diff --git a/scripts/update-talkkonnect.sh b/scripts/update-talkkonnect.sh index 00b43cf..104c231 100755 --- a/scripts/update-talkkonnect.sh +++ b/scripts/update-talkkonnect.sh @@ -54,6 +54,11 @@ then echo "copying talkkonnect.xml for safe keeping to /root/talkkonnect.xml" cp /home/talkkonnect/gocode/src/github.com/talkkonnect/talkkonnect/talkkonnect.xml /root/ fi +if [[ -f "/home/talkkonnect/gocode/src/github.com/talkkonnect/talkkonnect/mumble.pem" ]] +then + echo "copying talkkonnect.xml for safe keeping to /root/mumble.pem" + cp /home/talkkonnect/gocode/src/github.com/talkkonnect/talkkonnect/mumble.pem /root/ +fi rm -rf /home/talkkonnect/gocode/src/github.old rm -rf /home/talkkonnect/gocode/src/google.golang.org @@ -85,6 +90,7 @@ then echo "copying original talkkonnect.xml back to /home/talkkonnect/gocode/src/github.com/talkkonnect/talkkonnect/talkkonnect.xml" rm /home/talkkonnect/gocode/src/github.com/talkkonnect/talkkonnect/talkkonnect.xml cp /home/talkkonnect/gocode/src/github.old/talkkonnect/talkkonnect/talkkonnect.xml /home/talkkonnect/gocode/src/github.com/talkkonnect/talkkonnect/talkkonnect.xml + cp /home/talkkonnect/gocode/src/github.old/talkkonnect/talkkonnect/mumble.pem /home/talkkonnect/gocode/src/github.com/talkkonnect/talkkonnect/mumble.pem fi diff --git a/usbkeyboard.go b/usbkeyboard.go index 01b2a99..b0c2d94 100644 --- a/usbkeyboard.go +++ b/usbkeyboard.go @@ -1,3 +1,33 @@ +/* + * talkkonnect headless mumble client/gateway with lcd screen and channel control + * Copyright (C) 2018-2019, Suvir Kumar + * + * 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 + * Portions created by the Initial Developer are Copyright (C) Suvir Kumar. All Rights Reserved. + * + * Contributor(s): + * + * Suvir Kumar + * Library golang-evdev Copyright (c) 2016 Georgi Valkov. All rights reserved. See Copyright Message in Library source code. + * + * My Blog is at www.talkkonnect.com + * The source code is hosted at github.com/talkkonnect + * + * usbkeyboard.go -> function in talkkonnect for reading from external usb keyboard + */ + package talkkonnect import ( @@ -56,6 +86,7 @@ func (b *Talkkonnect) USBKeyboard() { case "setcomment": CommentMessageOff = USBKeyMap[rune(ke.Scancode)].ParamName CommentMessageOn = USBKeyMap[rune(ke.Scancode)].ParamName + log.Println("info: User Status Set to Message ", USBKeyMap[rune(ke.Scancode)].ParamName) case "transmitstart": b.cmdStartTransmitting() case "transmitstop": diff --git a/utils.go b/utils.go index c207de4..d495cf1 100644 --- a/utils.go +++ b/utils.go @@ -359,3 +359,11 @@ func FileExists(filepath string) bool { return !fileinfo.IsDir() } + +func restart() { + time.Sleep(2 * time.Second) + c := exec.Command("reset") + c.Stdout = os.Stdout + c.Run() + os.Exit(0) +} diff --git a/xmlparser.go b/xmlparser.go index a325c5f..c4a35d1 100644 --- a/xmlparser.go +++ b/xmlparser.go @@ -50,8 +50,8 @@ import ( //version and release date const ( - talkkonnectVersion string = "1.67.11" - talkkonnectReleased string = "Aug 21 2021" + talkkonnectVersion string = "1.67.12" + talkkonnectReleased string = "Aug 22 2021" ) // Generic Global Variables @@ -271,23 +271,25 @@ var ( // mqtt settings var ( - MQTTEnabled bool = false - Iotuuid string - RelayAllState bool = false - RelayPulseMills time.Duration - TotalRelays uint - RelayPins = [9]uint{} - MQTTTopic string - MQTTBroker string - MQTTPassword string - MQTTUser string - MQTTId string - MQTTCleansess bool - MQTTQos int - MQTTNum int - MQTTPayload string - MQTTAction string - MQTTStore string + MQTTEnabled bool = false + Iotuuid string + RelayAllState bool = false + RelayPulseMills time.Duration + TotalRelays uint + RelayPins = [9]uint{} + MQTTTopic string + MQTTBroker string + MQTTPassword string + MQTTUser string + MQTTId string + MQTTCleansess bool + MQTTQos int + MQTTNum int + MQTTPayload string + MQTTAction string + MQTTStore string + MQTTAttentionBlinkTimes int + MQTTAttentionBlinkmsecs int ) // ttsmessages settings @@ -693,18 +695,20 @@ type DocumentStruct struct { PrintUSBKeyboard bool `xml:"printusbkeyboard"` } `xml:"printvariables"` MQTT struct { - MQTTEnabled bool `xml:"enabled,attr"` - MQTTTopic string `xml:"mqtttopic"` - MQTTBroker string `xml:"mqttbroker"` - MQTTPassword string `xml:"mqttpassword"` - MQTTUser string `xml:"mqttuser"` - MQTTId string `xml:"mqttid"` - MQTTCleansess bool `xml:"cleansess"` - MQTTQos int `xml:"qos"` - MQTTNum int `xml:"num"` - MQTTPayload string `xml:"payload"` - MQTTAction string `xml:"action"` - MQTTStore string `xml:"store"` + MQTTEnabled bool `xml:"enabled,attr"` + MQTTTopic string `xml:"mqtttopic"` + MQTTBroker string `xml:"mqttbroker"` + MQTTPassword string `xml:"mqttpassword"` + MQTTUser string `xml:"mqttuser"` + MQTTId string `xml:"mqttid"` + MQTTCleansess bool `xml:"cleansess"` + MQTTQos int `xml:"qos"` + MQTTNum int `xml:"num"` + MQTTPayload string `xml:"payload"` + MQTTAction string `xml:"action"` + MQTTStore string `xml:"store"` + MQTTAttentionBlinkTimes int `xml:"attentionblinktimes"` + MQTTAttentionBlinkmsecs int `xml:"attentionblinkmsecs"` } `xml:"mqtt"` TTSMessages struct { TTSMessageEnabled bool `xml:"enabled,attr"` @@ -942,8 +946,6 @@ func readxmlconfig(file string) error { } } - // insert the voice target back here - exec, err := os.Executable() if err != nil { @@ -1415,6 +1417,8 @@ func readxmlconfig(file string) error { MQTTPayload = Document.Global.Software.MQTT.MQTTPayload MQTTAction = Document.Global.Software.MQTT.MQTTAction MQTTStore = Document.Global.Software.MQTT.MQTTStore + MQTTAttentionBlinkTimes = Document.Global.Software.MQTT.MQTTAttentionBlinkTimes + MQTTAttentionBlinkmsecs = Document.Global.Software.MQTT.MQTTAttentionBlinkmsecs TTSMessageEnabled = Document.Global.Software.TTSMessages.TTSMessageEnabled TTSLocalPlay = Document.Global.Software.TTSMessages.TTSLocalPlay @@ -1944,17 +1948,19 @@ func printxmlconfig() { } if PrintMQTT { log.Println("info: ------------ MQTT Function -------------- ") - log.Println("info: Enabled " + fmt.Sprintf("%v", MQTTEnabled)) - log.Println("info: Topic " + fmt.Sprintf("%v", MQTTTopic)) - log.Println("info: Broker " + fmt.Sprintf("%v", MQTTBroker)) - log.Println("info: Password " + fmt.Sprintf("%v", MQTTPassword)) - log.Println("info: Id " + fmt.Sprintf("%v", MQTTId)) - log.Println("info: Cleansess " + fmt.Sprintf("%v", MQTTCleansess)) - log.Println("info: Qos " + fmt.Sprintf("%v", MQTTQos)) - log.Println("info: Num " + fmt.Sprintf("%v", MQTTNum)) - log.Println("info: Payload " + fmt.Sprintf("%v", MQTTPayload)) - log.Println("info: Action " + fmt.Sprintf("%v", MQTTAction)) - log.Println("info: Store " + fmt.Sprintf("%v", MQTTStore)) + log.Println("info: Enabled " + fmt.Sprintf("%v", MQTTEnabled)) + log.Println("info: Topic " + fmt.Sprintf("%v", MQTTTopic)) + log.Println("info: Broker " + fmt.Sprintf("%v", MQTTBroker)) + log.Println("info: Password " + fmt.Sprintf("%v", MQTTPassword)) + log.Println("info: Id " + fmt.Sprintf("%v", MQTTId)) + log.Println("info: Cleansess " + fmt.Sprintf("%v", MQTTCleansess)) + log.Println("info: Qos " + fmt.Sprintf("%v", MQTTQos)) + log.Println("info: Num " + fmt.Sprintf("%v", MQTTNum)) + log.Println("info: Payload " + fmt.Sprintf("%v", MQTTPayload)) + log.Println("info: Action " + fmt.Sprintf("%v", MQTTAction)) + log.Println("info: Store " + fmt.Sprintf("%v", MQTTStore)) + log.Println("info: AttentionBlinkTimes " + fmt.Sprintf("%v", MQTTAttentionBlinkTimes)) + log.Println("info: AttentionBlinkmsecs " + fmt.Sprintf("%v", MQTTAttentionBlinkmsecs)) } else { log.Println("info: ------------ MQTT Function ------- SKIPPED ") } @@ -2006,11 +2012,8 @@ func modifyXMLTagServerHopping(inputXMLFile string, newserverindex int) { err := cmd.Run() if err != nil { log.Println("error: Failed to Set Next Server XML Tag with Error ", err) + return } - time.Sleep(2 * time.Second) - c := exec.Command("reset") - c.Stdout = os.Stdout - c.Run() - os.Exit(0) + restart() }