forked from Elara6331/itd
Create new config format
This commit is contained in:
parent
7b5c228591
commit
59ecd11340
21
itd.toml
21
itd.toml
@ -1,14 +1,25 @@
|
|||||||
|
# This is temporary, it is to show a notice
|
||||||
|
# to people still using the old config
|
||||||
|
cfg.version = 2
|
||||||
|
|
||||||
|
[socket]
|
||||||
|
path = "/tmp/itd/socket"
|
||||||
|
|
||||||
[conn]
|
[conn]
|
||||||
reconnect = true
|
reconnect = true
|
||||||
|
|
||||||
[notify]
|
[on.connect]
|
||||||
onConnect = true
|
notify = true
|
||||||
onReconnect = true
|
|
||||||
|
|
||||||
[notifications.ignore]
|
[on.reconnect]
|
||||||
|
notify = true
|
||||||
|
setTime = true
|
||||||
|
|
||||||
|
[notifs.ignore]
|
||||||
sender = []
|
sender = []
|
||||||
summary = ["InfiniTime"]
|
summary = ["InfiniTime"]
|
||||||
body = []
|
body = []
|
||||||
|
|
||||||
[music]
|
[music]
|
||||||
volInterval = 5
|
vol.interval = 5
|
||||||
|
|
||||||
|
18
main.go
18
main.go
@ -50,6 +50,10 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if viper.GetInt("cfg.version") != 2 {
|
||||||
|
log.Fatal().Msg("Please update your config to the newest format, only v2 configs supported.")
|
||||||
|
}
|
||||||
|
|
||||||
// Cleanly exit after function
|
// Cleanly exit after function
|
||||||
defer infinitime.Exit()
|
defer infinitime.Exit()
|
||||||
|
|
||||||
@ -63,14 +67,16 @@ func main() {
|
|||||||
|
|
||||||
// When InfiniTime reconnects
|
// When InfiniTime reconnects
|
||||||
dev.OnReconnect(func() {
|
dev.OnReconnect(func() {
|
||||||
// Set time to current time
|
if viper.GetBool("on.reconnect.setTime") {
|
||||||
err = dev.SetTime(time.Now())
|
// Set time to current time
|
||||||
if err != nil {
|
err = dev.SetTime(time.Now())
|
||||||
log.Error().Err(err).Msg("Error setting current time on connected InfiniTime")
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Error setting current time on connected InfiniTime")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If config specifies to notify on reconnect
|
// If config specifies to notify on reconnect
|
||||||
if viper.GetBool("notify.onReconnect") {
|
if viper.GetBool("on.reconnect.notify") {
|
||||||
// Send notification to InfiniTime
|
// Send notification to InfiniTime
|
||||||
err = dev.Notify("itd", "Successfully reconnected")
|
err = dev.Notify("itd", "Successfully reconnected")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -89,7 +95,7 @@ func main() {
|
|||||||
log.Info().Str("version", ver).Msg("Connected to InfiniTime")
|
log.Info().Str("version", ver).Msg("Connected to InfiniTime")
|
||||||
|
|
||||||
// If config specifies to notify on connect
|
// If config specifies to notify on connect
|
||||||
if viper.GetBool("notify.onConnect") {
|
if viper.GetBool("on.connect.notify") {
|
||||||
// Send notification to InfiniTime
|
// Send notification to InfiniTime
|
||||||
err = dev.Notify("itd", "Successfully connected")
|
err = dev.Notify("itd", "Successfully connected")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
4
music.go
4
music.go
@ -85,9 +85,9 @@ func initMusicCtrl(dev *infinitime.Device) error {
|
|||||||
case infinitime.MusicEventPrev:
|
case infinitime.MusicEventPrev:
|
||||||
player.Prev()
|
player.Prev()
|
||||||
case infinitime.MusicEventVolUp:
|
case infinitime.MusicEventVolUp:
|
||||||
player.VolUp(viper.GetUint("music.volInterval"))
|
player.VolUp(viper.GetUint("music.vol.interval"))
|
||||||
case infinitime.MusicEventVolDown:
|
case infinitime.MusicEventVolDown:
|
||||||
player.VolDown(viper.GetUint("music.volInterval"))
|
player.VolDown(viper.GetUint("music.vol.interval"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -90,9 +90,9 @@ func initNotifRelay(dev *infinitime.Device) error {
|
|||||||
|
|
||||||
// ignored checks whether any fields were ignored in the config
|
// ignored checks whether any fields were ignored in the config
|
||||||
func ignored(sender, summary, body string) bool {
|
func ignored(sender, summary, body string) bool {
|
||||||
ignoreSender := viper.GetStringSlice("notifications.ignore.sender")
|
ignoreSender := viper.GetStringSlice("notifs.ignore.sender")
|
||||||
ignoreSummary := viper.GetStringSlice("notifications.ignore.summary")
|
ignoreSummary := viper.GetStringSlice("notifs.ignore.summary")
|
||||||
ignoreBody := viper.GetStringSlice("notifications.ignore.body")
|
ignoreBody := viper.GetStringSlice("notifs.ignore.body")
|
||||||
return strSlcContains(ignoreSender, sender) ||
|
return strSlcContains(ignoreSender, sender) ||
|
||||||
strSlcContains(ignoreSummary, summary) ||
|
strSlcContains(ignoreSummary, summary) ||
|
||||||
strSlcContains(ignoreBody, body)
|
strSlcContains(ignoreBody, body)
|
||||||
|
11
socket.go
11
socket.go
@ -29,12 +29,11 @@ import (
|
|||||||
|
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/spf13/viper"
|
||||||
"go.arsenm.dev/infinitime"
|
"go.arsenm.dev/infinitime"
|
||||||
"go.arsenm.dev/itd/internal/types"
|
"go.arsenm.dev/itd/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
const SockPath = "/tmp/itd/socket"
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ReqTypeHeartRate = "hrt"
|
ReqTypeHeartRate = "hrt"
|
||||||
ReqTypeBattLevel = "battlvl"
|
ReqTypeBattLevel = "battlvl"
|
||||||
@ -52,19 +51,19 @@ const (
|
|||||||
|
|
||||||
func startSocket(dev *infinitime.Device) error {
|
func startSocket(dev *infinitime.Device) error {
|
||||||
// Make socket directory if non-existent
|
// Make socket directory if non-existent
|
||||||
err := os.MkdirAll(filepath.Dir(SockPath), 0755)
|
err := os.MkdirAll(filepath.Dir(viper.GetString("socket.path")), 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove old socket if it exists
|
// Remove old socket if it exists
|
||||||
err = os.RemoveAll(SockPath)
|
err = os.RemoveAll(viper.GetString("socket.path"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen on socket path
|
// Listen on socket path
|
||||||
ln, err := net.Listen("unix", SockPath)
|
ln, err := net.Listen("unix", viper.GetString("socket.path"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -83,7 +82,7 @@ func startSocket(dev *infinitime.Device) error {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Log socket start
|
// Log socket start
|
||||||
log.Info().Str("path", SockPath).Msg("Started control socket")
|
log.Info().Str("path", viper.GetString("socket.path")).Msg("Started control socket")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user