config for kiosk handler

This commit is contained in:
Mike Bloy 2024-09-29 12:10:36 -05:00
parent f085312e13
commit 85abccf14a
4 changed files with 29 additions and 7 deletions

View File

@ -104,10 +104,16 @@ func initConfig() {
"image_dir",
"version",
"webserver_port",
"mqtt_broker_url",
"mqtt_broker_user",
"mqtt_broker_password",
"mqtt_presence_topic",
"kiosk_cmd_screen_on",
"kiosk_cmd_screen_off",
}
for _, key := range expected_config {
if !viper.IsSet(key) {
fmt.Fprintf(os.Stderr, "Missing configuration value: %s\n", key)
log.Fatalf("Missing configuration value: %s\n", key)
os.Exit(1)
}
}

View File

@ -14,6 +14,12 @@ func TestInitConfig(t *testing.T) {
viper.Set("deployment", "testing")
viper.Set("image_dir", "/tmp")
viper.Set("webserver_port", "8080")
viper.Set("mqtt_broker_url", "mqtt://example.com:1883")
viper.Set("mqtt_broker_user", "testuser")
viper.Set("mqtt_broker_password", "testpass")
viper.Set("mqtt_presence_topic", "home/foo/test")
viper.Set("kiosk_cmd_screen_on", "echo screen on")
viper.Set("kiosk_cmd_screen_off", "echo screen off")
initConfig()
assert.Equal(t, gitVersion, viper.GetString("version"), "config version mismatch")
@ -25,6 +31,12 @@ func TestInitConfigMissingImageDir(t *testing.T) {
viper.Set("deployment", "testing")
viper.Set("version", "vTest")
viper.Set("webserver_port", "8080")
viper.Set("mqtt_broker_url", "mqtt://example.com:1883")
viper.Set("mqtt_broker_user", "testuser")
viper.Set("mqtt_broker_password", "testpass")
viper.Set("mqtt_presence_topic", "home/foo/test")
viper.Set("kiosk_cmd_screen_on", "echo screen on")
viper.Set("kiosk_cmd_screen_off", "echo screen off")
initConfig()
return

View File

@ -10,11 +10,13 @@ import (
type handler func(area string, present bool)
type configObj struct {
logger *slog.Logger
broker string
username string
password string
presenceTopic string
logger *slog.Logger
broker string
username string
password string
presenceTopic string
screen_on_cmd string
screen_off_cmd string
}
var config = configObj{}
@ -25,6 +27,8 @@ func Run(rootLogger *slog.Logger, exitch chan bool, ctx context.Context) {
config.username = viper.GetString("mqtt_broker_user")
config.password = viper.GetString("mqtt_broker_password")
config.presenceTopic = viper.GetString("mqtt_presence_topic")
config.screen_on_cmd = viper.GetString("kiosk_cmd_screen_on")
config.screen_off_cmd = viper.GetString("kiosk_cmd_screen_off")
config.logger.Info("starting MQTT broker client")
brokerConsume(ctx)
exitch <- true

View File

@ -9,5 +9,5 @@
**/*.go .env {
prep: go mod tidy
prep: go test @dirmods
daemon +sigterm: go run ./main.go
daemon +sigterm: go run ./main.go
}