forked from Elara6331/itd
		
	Create new config format
This commit is contained in:
		
							
								
								
									
										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] | ||||
|     reconnect = true | ||||
|  | ||||
| [notify] | ||||
|     onConnect = true | ||||
|     onReconnect = true | ||||
| [on.connect] | ||||
|     notify = true | ||||
|  | ||||
| [notifications.ignore] | ||||
| [on.reconnect] | ||||
|     notify = true | ||||
|     setTime = true | ||||
|  | ||||
| [notifs.ignore] | ||||
|     sender = [] | ||||
|     summary = ["InfiniTime"] | ||||
|     body = [] | ||||
|  | ||||
| [music] | ||||
|     volInterval = 5 | ||||
|     vol.interval = 5 | ||||
|  | ||||
|   | ||||
							
								
								
									
										18
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								main.go
									
									
									
									
									
								
							| @@ -50,6 +50,10 @@ func init() { | ||||
| } | ||||
|  | ||||
| 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 | ||||
| 	defer infinitime.Exit() | ||||
|  | ||||
| @@ -63,14 +67,16 @@ func main() { | ||||
|  | ||||
| 	// When InfiniTime reconnects | ||||
| 	dev.OnReconnect(func() { | ||||
| 		// Set time to current time | ||||
| 		err = dev.SetTime(time.Now()) | ||||
| 		if err != nil { | ||||
| 			log.Error().Err(err).Msg("Error setting current time on connected InfiniTime") | ||||
| 		if viper.GetBool("on.reconnect.setTime") { | ||||
| 			// Set time to current time | ||||
| 			err = dev.SetTime(time.Now()) | ||||
| 			if err != nil { | ||||
| 				log.Error().Err(err).Msg("Error setting current time on connected InfiniTime") | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		// If config specifies to notify on reconnect | ||||
| 		if viper.GetBool("notify.onReconnect") { | ||||
| 		if viper.GetBool("on.reconnect.notify") { | ||||
| 			// Send notification to InfiniTime | ||||
| 			err = dev.Notify("itd", "Successfully reconnected") | ||||
| 			if err != nil { | ||||
| @@ -89,7 +95,7 @@ func main() { | ||||
| 	log.Info().Str("version", ver).Msg("Connected to InfiniTime") | ||||
|  | ||||
| 	// If config specifies to notify on connect | ||||
| 	if viper.GetBool("notify.onConnect") { | ||||
| 	if viper.GetBool("on.connect.notify") { | ||||
| 		// Send notification to InfiniTime | ||||
| 		err = dev.Notify("itd", "Successfully connected") | ||||
| 		if err != nil { | ||||
|   | ||||
							
								
								
									
										4
									
								
								music.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								music.go
									
									
									
									
									
								
							| @@ -85,9 +85,9 @@ func initMusicCtrl(dev *infinitime.Device) error { | ||||
| 			case infinitime.MusicEventPrev: | ||||
| 				player.Prev() | ||||
| 			case infinitime.MusicEventVolUp: | ||||
| 				player.VolUp(viper.GetUint("music.volInterval")) | ||||
| 				player.VolUp(viper.GetUint("music.vol.interval")) | ||||
| 			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 | ||||
| func ignored(sender, summary, body string) bool { | ||||
| 	ignoreSender := viper.GetStringSlice("notifications.ignore.sender") | ||||
| 	ignoreSummary := viper.GetStringSlice("notifications.ignore.summary") | ||||
| 	ignoreBody := viper.GetStringSlice("notifications.ignore.body") | ||||
| 	ignoreSender := viper.GetStringSlice("notifs.ignore.sender") | ||||
| 	ignoreSummary := viper.GetStringSlice("notifs.ignore.summary") | ||||
| 	ignoreBody := viper.GetStringSlice("notifs.ignore.body") | ||||
| 	return strSlcContains(ignoreSender, sender) || | ||||
| 		strSlcContains(ignoreSummary, summary) || | ||||
| 		strSlcContains(ignoreBody, body) | ||||
|   | ||||
							
								
								
									
										11
									
								
								socket.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								socket.go
									
									
									
									
									
								
							| @@ -29,12 +29,11 @@ import ( | ||||
|  | ||||
| 	"github.com/mitchellh/mapstructure" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| 	"github.com/spf13/viper" | ||||
| 	"go.arsenm.dev/infinitime" | ||||
| 	"go.arsenm.dev/itd/internal/types" | ||||
| ) | ||||
|  | ||||
| const SockPath = "/tmp/itd/socket" | ||||
|  | ||||
| const ( | ||||
| 	ReqTypeHeartRate = "hrt" | ||||
| 	ReqTypeBattLevel = "battlvl" | ||||
| @@ -52,19 +51,19 @@ const ( | ||||
|  | ||||
| func startSocket(dev *infinitime.Device) error { | ||||
| 	// 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 { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	// Remove old socket if it exists | ||||
| 	err = os.RemoveAll(SockPath) | ||||
| 	err = os.RemoveAll(viper.GetString("socket.path")) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	// Listen on socket path | ||||
| 	ln, err := net.Listen("unix", SockPath) | ||||
| 	ln, err := net.Listen("unix", viper.GetString("socket.path")) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| @@ -83,7 +82,7 @@ func startSocket(dev *infinitime.Device) error { | ||||
| 	}() | ||||
|  | ||||
| 	// 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 | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user