Make connections auto-retry on failure for connections to MQTT/MIDI devices. This should improve stability with physical devices when ran as a service.

This commit is contained in:
James Coleman 2024-09-08 16:10:19 -05:00
parent 24ecc9870e
commit 1eb7440496
3 changed files with 109 additions and 80 deletions

View File

@ -110,7 +110,7 @@ On MacOS, there is an IAC Driver that can be enabled in Audio MIDI Setup.
midi_routers:
- name: service_notifications
device: IAC Driver Bus 1
log_level: 1
log_level: 2
```
### Example note trigger configuration
@ -120,7 +120,7 @@ midi_routers:
midi_routers:
- name: service_notifications
device: IAC Driver Bus 1
log_level: 1
log_level: 2
note_triggers:
- channel: 0
note: 0
@ -136,7 +136,7 @@ midi_routers:
midi_routers:
- name: service_notifications
device: IAC Driver Bus 1
log_level: 1
log_level: 2
request_triggers:
- channel: 0
note: 0
@ -152,7 +152,7 @@ midi_routers:
midi_routers:
- name: service_notifications
device: IAC Driver Bus 1
log_level: 2
log_level: 3
note_triggers:
- channel: 0
note: 0
@ -190,7 +190,7 @@ midi_routers:
user: mqtt
password: password
topic: midi/behringer_wing
log_level: 3
log_level: 4
note_triggers:
- channel: 0
note: 1

View File

@ -15,7 +15,7 @@ import (
const (
serviceName = "midi-request-trigger"
serviceDescription = "Takes trigger MIDI messages by HTTP or MQTT requests and trigger HTTP or MQTT requests by MIDI messages"
serviceVersion = "0.2.1"
serviceVersion = "0.2.2"
)
// App is the global application structure for communicating between servers and storing information.

View File

@ -11,6 +11,7 @@ import (
"regexp"
"strconv"
"strings"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"gitlab.com/gomidi/midi/v2"
@ -21,8 +22,10 @@ import (
type LogLevel int
const (
// Logs only errors.
ErrorLog LogLevel = iota
// Logs info messages.
InfoLog LogLevel = iota
// Log only errors.
ErrorLog
// MQTT, HTTP, and MIDI receive logging.
ReceiveLog
// MQTT, HTTP, and MIDI send logging.
@ -33,7 +36,7 @@ const (
// Provides a string value for a log level.
func (l LogLevel) String() string {
return [...]string{"Error", "Receive", "Send", "Debug"}[l]
return [...]string{"Info", "Error", "Receive", "Send", "Debug"}[l]
}
// Configurations relating to MQTT connection.
@ -134,10 +137,11 @@ type MidiRouter struct {
RequestTriggers []RequestTrigger `fig:"request_triggers"`
// How much logging.
// 0 - Errors
// 1 - MQTT, HTTP, and MIDI receive logging.
// 2 - MQTT, HTTP, and MIDI send logging.
// 3 - Debug
// 0 - Info
// 1 - Errors
// 2 - MQTT, HTTP, and MIDI receive logging.
// 3 - MQTT, HTTP, and MIDI send logging.
// 4 - Debug
LogLevel LogLevel `fig:"log_level"`
// Connection to MIDI device.
@ -462,24 +466,34 @@ func (r *MidiRouter) MqttSubscribe(topic string) {
func (r *MidiRouter) Connect() {
// If request triggers defined, find the out port.
if len(r.RequestTriggers) != 0 {
go func() {
for {
out, err := midi.FindOutPort(r.Device)
if err != nil {
log.Println("Can't find output device:", r.Device)
r.Log(ErrorLog, "Can't find output device: %s", r.Device)
} else {
r.MidiOut = out
}
}
// If listener is disabled, stop here.
if r.DisableListener {
return
break
}
r.Log(ErrorLog, "Retrying in 1 minute.")
time.Sleep(time.Minute)
}
}()
}
// If listener is disabled, stop here.
if !r.DisableListener {
go func() {
for {
// Try finding input port.
log.Println("Connecting to device:", r.Device)
r.Log(InfoLog, "Connecting to input device: %s", r.Device)
in, err := midi.FindInPort(r.Device)
if err != nil {
log.Println("Can't find device:", r.Device)
return
r.Log(ErrorLog, "Can't find input device: %s", r.Device)
r.Log(ErrorLog, "Retrying in 1 minute.")
time.Sleep(time.Minute)
continue
}
// Start listening to MIDI messages.
@ -503,13 +517,22 @@ func (r *MidiRouter) Connect() {
})
if err != nil {
r.Log(ErrorLog, "Error listening to device: %s", err)
return
r.Log(ErrorLog, "Retrying in 1 minute.")
time.Sleep(time.Minute)
continue
}
r.Log(InfoLog, "Connected to input device: %s", r.Device)
// Update stop function for disconnects.
r.ListenerStop = stop
break
}
}()
}
if r.MQTT.Host != "" && r.MQTT.Port != 0 {
go func() {
for {
// Connect to MQTT.
mqtt_opts := mqtt.NewClientOptions()
mqtt_opts.AddBroker(fmt.Sprintf("tcp://%s:%d", r.MQTT.Host, r.MQTT.Port))
@ -522,7 +545,9 @@ func (r *MidiRouter) Connect() {
r.Log(DebugLog, "Connecting to MQTT")
if t := r.MqttClient.Connect(); t.Wait() && t.Error() != nil {
log.Fatalf("MQTT error: %s", t.Error())
return
r.Log(ErrorLog, "Retrying in 1 minute.")
time.Sleep(time.Minute)
continue
}
// Subscribe to MQTT topics.
@ -537,6 +562,9 @@ func (r *MidiRouter) Connect() {
r.MqttSubscribe(r.MQTT.Topic + "/" + trig.MqttSubTopic)
}
}
break
}
}()
}
}
@ -546,4 +574,5 @@ func (r *MidiRouter) Disconnect() {
if r.ListenerStop != nil {
r.ListenerStop()
}
r.MqttClient.Disconnect(0)
}