Compare commits
No commits in common. "6e16aa7a7a5bde21cc85152941ff99c8b56f8987" and "b186f77bea367f3968b2bca724bea2ea8c3a310c" have entirely different histories.
6e16aa7a7a
...
b186f77bea
47
config.go
47
config.go
@ -1,47 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
// Set up logger
|
|
||||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
||||||
|
|
||||||
// Set config settings
|
|
||||||
setCfgDefaults()
|
|
||||||
viper.AddConfigPath("$HOME/.cmonfig")
|
|
||||||
viper.AddConfigPath("/etoc")
|
|
||||||
viper.SetConfigName("itd")
|
|
||||||
viper.SetConfigType("toml")
|
|
||||||
viper.WatchConfig()
|
|
||||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
||||||
viper.SetEnvPrefix("itd")
|
|
||||||
// Ignore error because defaults set
|
|
||||||
viper.ReadInConfig()
|
|
||||||
viper.AutomaticEnv()
|
|
||||||
}
|
|
||||||
|
|
||||||
func setCfgDefaults() {
|
|
||||||
viper.SetDefault("cfg.version", 2)
|
|
||||||
|
|
||||||
viper.SetDefault("socket.path", "/tmp/itd/socket")
|
|
||||||
|
|
||||||
viper.SetDefault("conn.reconnect", true)
|
|
||||||
|
|
||||||
viper.SetDefault("on.connect.notify", true)
|
|
||||||
|
|
||||||
viper.SetDefault("on.reconnect.notify", true)
|
|
||||||
viper.SetDefault("on.reconnect.setTime", true)
|
|
||||||
|
|
||||||
viper.SetDefault("notifs.ignore.sender", []string{})
|
|
||||||
viper.SetDefault("notifs.ignore.summary", []string{"InfiniTime"})
|
|
||||||
viper.SetDefault("notifs.ignore.body", []string{})
|
|
||||||
|
|
||||||
viper.SetDefault("music.vol.interval", 5)
|
|
||||||
}
|
|
4
go.mod
4
go.mod
@ -19,7 +19,7 @@ require (
|
|||||||
github.com/spf13/cast v1.4.1 // indirect
|
github.com/spf13/cast v1.4.1 // indirect
|
||||||
github.com/spf13/cobra v1.2.1
|
github.com/spf13/cobra v1.2.1
|
||||||
github.com/spf13/viper v1.8.1
|
github.com/spf13/viper v1.8.1
|
||||||
go.arsenm.dev/infinitime v0.0.0-20210823171603-8648afeebf08
|
go.arsenm.dev/infinitime v0.0.0-20210822201216-955384489609
|
||||||
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
|
golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55 // indirect
|
||||||
golang.org/x/text v0.3.7 // indirect
|
golang.org/x/text v0.3.7 // indirect
|
||||||
)
|
)
|
||||||
|
4
go.sum
4
go.sum
@ -289,8 +289,6 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
|
|||||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||||
go.arsenm.dev/infinitime v0.0.0-20210822201216-955384489609 h1:QH7hsVjulEs1OP8lcQ7EfVy2UO/rtwRsxUo3ylde83E=
|
go.arsenm.dev/infinitime v0.0.0-20210822201216-955384489609 h1:QH7hsVjulEs1OP8lcQ7EfVy2UO/rtwRsxUo3ylde83E=
|
||||||
go.arsenm.dev/infinitime v0.0.0-20210822201216-955384489609/go.mod h1:gaepaueUz4J5FfxuV19B4w5pi+V3mD0LTef50ryxr/Q=
|
go.arsenm.dev/infinitime v0.0.0-20210822201216-955384489609/go.mod h1:gaepaueUz4J5FfxuV19B4w5pi+V3mD0LTef50ryxr/Q=
|
||||||
go.arsenm.dev/infinitime v0.0.0-20210823171603-8648afeebf08 h1:eh/ZfShWAYhi3UR6nrX+5mORDvN58A1T+NHtYoQeFC4=
|
|
||||||
go.arsenm.dev/infinitime v0.0.0-20210823171603-8648afeebf08/go.mod h1:gaepaueUz4J5FfxuV19B4w5pi+V3mD0LTef50ryxr/Q=
|
|
||||||
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
|
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
|
||||||
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
|
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
|
||||||
go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ=
|
go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ=
|
||||||
@ -453,8 +451,6 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55 h1:rw6UNGRMfarCepjI8qOepea/SXwIBVfTKjztZ5gBbq4=
|
golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55 h1:rw6UNGRMfarCepjI8qOepea/SXwIBVfTKjztZ5gBbq4=
|
||||||
golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210820121016-41cdb8703e55/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k=
|
|
||||||
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
21
itd.toml
21
itd.toml
@ -1,25 +1,14 @@
|
|||||||
# 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
|
||||||
|
|
||||||
[on.connect]
|
[notify]
|
||||||
notify = true
|
onConnect = true
|
||||||
|
onReconnect = true
|
||||||
|
|
||||||
[on.reconnect]
|
[notifications.ignore]
|
||||||
notify = true
|
|
||||||
setTime = true
|
|
||||||
|
|
||||||
[notifs.ignore]
|
|
||||||
sender = []
|
sender = []
|
||||||
summary = ["InfiniTime"]
|
summary = ["InfiniTime"]
|
||||||
body = []
|
body = []
|
||||||
|
|
||||||
[music]
|
[music]
|
||||||
vol.interval = 5
|
volInterval = 5
|
||||||
|
|
31
main.go
31
main.go
@ -19,8 +19,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"go.arsenm.dev/infinitime"
|
"go.arsenm.dev/infinitime"
|
||||||
@ -28,11 +31,25 @@ import (
|
|||||||
|
|
||||||
var firmwareUpdating = false
|
var firmwareUpdating = false
|
||||||
|
|
||||||
func main() {
|
func init() {
|
||||||
if viper.GetInt("cfg.version") != 2 {
|
// Set up logger
|
||||||
log.Fatal().Msg("Please update your config to the newest format, only v2 configs supported.")
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||||
}
|
|
||||||
|
|
||||||
|
// Set config settings
|
||||||
|
viper.AddConfigPath("$HOME/.config")
|
||||||
|
viper.AddConfigPath("/etc")
|
||||||
|
viper.SetConfigName("itd")
|
||||||
|
viper.SetConfigType("toml")
|
||||||
|
viper.WatchConfig()
|
||||||
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
|
viper.SetEnvPrefix("itd")
|
||||||
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
|
log.Warn().Err(err).Msg("Could not read in config")
|
||||||
|
}
|
||||||
|
viper.AutomaticEnv()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
// Cleanly exit after function
|
// Cleanly exit after function
|
||||||
defer infinitime.Exit()
|
defer infinitime.Exit()
|
||||||
|
|
||||||
@ -46,16 +63,14 @@ func main() {
|
|||||||
|
|
||||||
// When InfiniTime reconnects
|
// When InfiniTime reconnects
|
||||||
dev.OnReconnect(func() {
|
dev.OnReconnect(func() {
|
||||||
if viper.GetBool("on.reconnect.setTime") {
|
|
||||||
// Set time to current time
|
// Set time to current time
|
||||||
err = dev.SetTime(time.Now())
|
err = dev.SetTime(time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Error setting current time on connected InfiniTime")
|
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("on.reconnect.notify") {
|
if viper.GetBool("notify.onReconnect") {
|
||||||
// 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 {
|
||||||
@ -74,7 +89,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("on.connect.notify") {
|
if viper.GetBool("notify.onConnect") {
|
||||||
// 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 {
|
||||||
|
8
music.go
8
music.go
@ -19,10 +19,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
"go.arsenm.dev/infinitime"
|
"go.arsenm.dev/infinitime"
|
||||||
"go.arsenm.dev/infinitime/pkg/player"
|
"go.arsenm.dev/infinitime/pkg/player"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initMusicCtrl(dev *infinitime.Device) error {
|
func initMusicCtrl(dev *infinitime.Device) error {
|
||||||
@ -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.vol.interval"))
|
player.VolUp(viper.GetUint("music.volInterval"))
|
||||||
case infinitime.MusicEventVolDown:
|
case infinitime.MusicEventVolDown:
|
||||||
player.VolDown(viper.GetUint("music.vol.interval"))
|
player.VolDown(viper.GetUint("music.volInterval"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -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("notifs.ignore.sender")
|
ignoreSender := viper.GetStringSlice("notifications.ignore.sender")
|
||||||
ignoreSummary := viper.GetStringSlice("notifs.ignore.summary")
|
ignoreSummary := viper.GetStringSlice("notifications.ignore.summary")
|
||||||
ignoreBody := viper.GetStringSlice("notifs.ignore.body")
|
ignoreBody := viper.GetStringSlice("notifications.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,11 +29,12 @@ 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"
|
||||||
@ -51,19 +52,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(viper.GetString("socket.path")), 0755)
|
err := os.MkdirAll(filepath.Dir(SockPath), 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(viper.GetString("socket.path"))
|
err = os.RemoveAll(SockPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen on socket path
|
// Listen on socket path
|
||||||
ln, err := net.Listen("unix", viper.GetString("socket.path"))
|
ln, err := net.Listen("unix", SockPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -82,7 +83,7 @@ func startSocket(dev *infinitime.Device) error {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Log socket start
|
// Log socket start
|
||||||
log.Info().Str("path", viper.GetString("socket.path")).Msg("Started control socket")
|
log.Info().Str("path", SockPath).Msg("Started control socket")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user