-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
156 lines (135 loc) · 4.43 KB
/
root.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
package cmd
import (
"fmt"
"log"
"log/syslog"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/ftl/hamradio/cfg"
"github.com/spf13/cobra"
"github.com/ftl/hamdeck/pkg/hamdeck"
"github.com/ftl/hamdeck/pkg/hamlib"
"github.com/ftl/hamdeck/pkg/mqtt"
"github.com/ftl/hamdeck/pkg/pulse"
"github.com/ftl/hamdeck/pkg/streamdeck"
"github.com/ftl/hamdeck/pkg/tci"
)
var (
version string = "development"
gitCommit string = "unknown"
buildTime string = "unknown"
)
var rootFlags = struct {
syslog bool
serial string
brightness int
configFile string
hamlibAddress string
tciAddress string
mqttAddress string
mqttUsername string
mqttPassword string
}{}
var rootCmd = &cobra.Command{
Use: "hamdeck",
Short: "HamDeck Version " + version + " - control your ham radio station with an Elgato Stream Deck",
Version: version,
Run: run,
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
func init() {
rootCmd.PersistentFlags().BoolVar(&rootFlags.syslog, "syslog", false, "use syslog for logging")
rootCmd.PersistentFlags().StringVar(&rootFlags.serial, "serial", "", "the serial number of the Stream Deck device that should be used")
rootCmd.PersistentFlags().IntVar(&rootFlags.brightness, "brightness", 100, "the initial brightness of the Stream Deck device")
rootCmd.PersistentFlags().StringVar(&rootFlags.configFile, "config", "", "the configuration file that should be used (default: .config/hamradio/hamdeck.json)")
rootCmd.PersistentFlags().StringVar(&rootFlags.hamlibAddress, "hamlib", "", "the address of the rigctld server (if empty, hamlib buttons are not available)")
rootCmd.PersistentFlags().StringVar(&rootFlags.tciAddress, "tci", "", "the address of the TCI server (if empty, tci buttons are not available)")
rootCmd.PersistentFlags().StringVar(&rootFlags.mqttAddress, "mqtt", "", "the address of the MQTT server (if empty, atu100 buttons are not available)")
rootCmd.PersistentFlags().StringVar(&rootFlags.mqttUsername, "mqttusername", "", "the username for MQTT")
rootCmd.PersistentFlags().StringVar(&rootFlags.mqttPassword, "mqttpassword", "", "the password for MQTT")
}
func run(cmd *cobra.Command, args []string) {
log.Printf("Hamdeck Version %s", version)
shutdown := monitorShutdownSignals()
if rootFlags.syslog {
logger, err := syslog.NewLogger(syslog.LOG_INFO, 0)
if err != nil {
log.Fatal(err)
}
log.SetOutput(logger.Writer())
}
device, err := streamdeck.Open(rootFlags.serial)
if err != nil {
log.Fatalf("Cannot open Stream Deck: %v", err)
}
defer func() {
log.Print("Closing device")
err := device.Close()
if err != nil {
log.Printf("Cannot close Stream Deck: %v", err)
} else {
log.Print("Device closed")
}
}()
log.Printf("Using Stream Deck %s %dx%d %s", device.ID(), device.Columns(), device.Rows(), device.Serial())
log.Printf("Firmware Version %s", device.FirmwareVersion())
device.SetBrightness(rootFlags.brightness)
deck := hamdeck.New(device)
deck.RegisterFactory(hamdeck.NewButtonFactory(deck))
deck.RegisterFactory(pulse.NewButtonFactory())
deck.RegisterFactory(hamlib.NewButtonFactory(deck, rootFlags.hamlibAddress))
deck.RegisterFactory(tci.NewButtonFactory(deck, rootFlags.tciAddress))
deck.RegisterFactory(mqtt.NewButtonFactory(deck, rootFlags.mqttAddress, rootFlags.mqttUsername, rootFlags.mqttPassword))
err = configureHamDeck(deck, rootFlags.configFile)
if err != nil {
log.Fatal(err)
}
err = deck.Run(shutdown)
if err != nil {
log.Fatal(err)
}
}
func monitorShutdownSignals() <-chan struct{} {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
shutdown := make(chan struct{})
go func() {
rude := false
for {
<-signals
if rude {
log.Fatal("graceful shutdown failed")
}
rude = true
close(shutdown)
}
}()
return shutdown
}
func configureHamDeck(deck *hamdeck.HamDeck, config string) error {
if config == "" {
configDirectory, err := cfg.Directory("")
if err != nil {
return fmt.Errorf("cannot resolve configuration directory: %w", err)
}
config = filepath.Join(configDirectory, hamdeck.ConfigDefaultFilename)
log.Printf("Using default configuration file %s", config)
}
file, err := os.Open(config)
if err != nil {
return fmt.Errorf("cannot open configuration file: %w", err)
}
defer file.Close()
err = deck.ReadConfig(file)
if err != nil {
return err
}
deck.CloseUnusedFactories()
return nil
}