Switch to iota for request types and move to types package

This commit is contained in:
Elara 2021-08-24 20:32:17 -07:00
parent 0fe83fccc1
commit 36b683204d
11 changed files with 62 additions and 60 deletions

View File

@ -26,6 +26,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"go.arsenm.dev/itd/internal/types" "go.arsenm.dev/itd/internal/types"
) )
@ -36,7 +37,7 @@ var addressCmd = &cobra.Command{
Short: "Get InfiniTime's bluetooth address", Short: "Get InfiniTime's bluetooth address",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Connect to itd UNIX socket // Connect to itd UNIX socket
conn, err := net.Dial("unix", SockPath) conn, err := net.Dial("unix", viper.GetString("sockPath"))
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?") log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
} }
@ -44,7 +45,7 @@ var addressCmd = &cobra.Command{
// Encode request into connection // Encode request into connection
err = json.NewEncoder(conn).Encode(types.Request{ err = json.NewEncoder(conn).Encode(types.Request{
Type: ReqTypeBtAddress, Type: types.ReqTypeBtAddress,
}) })
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error making request") log.Fatal().Err(err).Msg("Error making request")

View File

@ -26,6 +26,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"go.arsenm.dev/itd/internal/types" "go.arsenm.dev/itd/internal/types"
) )
@ -36,7 +37,7 @@ var batteryCmd = &cobra.Command{
Short: "Get battery level from InfiniTime", Short: "Get battery level from InfiniTime",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Connect to itd UNIX socket // Connect to itd UNIX socket
conn, err := net.Dial("unix", SockPath) conn, err := net.Dial("unix", viper.GetString("sockPath"))
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?") log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
} }
@ -44,7 +45,7 @@ var batteryCmd = &cobra.Command{
// Encode request into connection // Encode request into connection
err = json.NewEncoder(conn).Encode(types.Request{ err = json.NewEncoder(conn).Encode(types.Request{
Type: ReqTypeBattLevel, Type: types.ReqTypeBattLevel,
}) })
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error making request") log.Fatal().Err(err).Msg("Error making request")

View File

@ -18,23 +18,6 @@
package cmd package cmd
const SockPath = "/tmp/itd/socket"
const (
ReqTypeHeartRate = "hrt"
ReqTypeBattLevel = "battlvl"
ReqTypeFwVersion = "fwver"
ReqTypeFwUpgrade = "fwupg"
ReqTypeBtAddress = "btaddr"
ReqTypeNotify = "notify"
ReqTypeSetTime = "settime"
)
const (
UpgradeTypeArchive = iota
UpgradeTypeFiles
)
type DFUProgress struct { type DFUProgress struct {
Received int64 `mapstructure:"recvd"` Received int64 `mapstructure:"recvd"`
Total int64 `mapstructure:"total"` Total int64 `mapstructure:"total"`

View File

@ -26,6 +26,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"go.arsenm.dev/itd/internal/types" "go.arsenm.dev/itd/internal/types"
) )
@ -35,7 +36,7 @@ var heartCmd = &cobra.Command{
Short: "Get heart rate from InfiniTime", Short: "Get heart rate from InfiniTime",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Connect to itd UNIX socket // Connect to itd UNIX socket
conn, err := net.Dial("unix", SockPath) conn, err := net.Dial("unix", viper.GetString("sockPath"))
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?") log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
} }
@ -43,7 +44,7 @@ var heartCmd = &cobra.Command{
// Encode request into connection // Encode request into connection
err = json.NewEncoder(conn).Encode(types.Request{ err = json.NewEncoder(conn).Encode(types.Request{
Type: ReqTypeHeartRate, Type: types.ReqTypeHeartRate,
}) })
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error making request") log.Fatal().Err(err).Msg("Error making request")

View File

@ -25,6 +25,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"go.arsenm.dev/itd/internal/types" "go.arsenm.dev/itd/internal/types"
) )
@ -40,7 +41,7 @@ var notifyCmd = &cobra.Command{
} }
// Connect to itd UNIX socket // Connect to itd UNIX socket
conn, err := net.Dial("unix", SockPath) conn, err := net.Dial("unix", viper.GetString("sockPath"))
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?") log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
} }
@ -48,7 +49,7 @@ var notifyCmd = &cobra.Command{
// Encode request into connection // Encode request into connection
err = json.NewEncoder(conn).Encode(types.Request{ err = json.NewEncoder(conn).Encode(types.Request{
Type: ReqTypeNotify, Type: types.ReqTypeNotify,
Data: types.ReqDataNotify{ Data: types.ReqDataNotify{
Title: args[0], Title: args[0],
Body: args[1], Body: args[1],

View File

@ -21,6 +21,7 @@ package cmd
import ( import (
"github.com/abiosoft/ishell" "github.com/abiosoft/ishell"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
@ -63,3 +64,15 @@ func Execute() {
rootCmd.CompletionOptions.DisableDefaultCmd = true rootCmd.CompletionOptions.DisableDefaultCmd = true
cobra.CheckErr(rootCmd.Execute()) cobra.CheckErr(rootCmd.Execute())
} }
func init() {
// Register flag for socket path
rootCmd.Flags().StringP("socket-path", "s", "", "Path to itd socket")
// Bind flag and environment variable to viper key
viper.BindPFlag("sockPath", rootCmd.Flags().Lookup("socket-path"))
viper.BindEnv("sockPath", "ITCTL_SOCKET_PATH")
// Set default value for socket path
viper.SetDefault("sockPath", "/tmp/itd/socket")
}

View File

@ -25,6 +25,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"go.arsenm.dev/itd/internal/types" "go.arsenm.dev/itd/internal/types"
) )
@ -39,9 +40,9 @@ var timeCmd = &cobra.Command{
log.Warn().Msg("Command time requires one argument") log.Warn().Msg("Command time requires one argument")
return return
} }
// Connect to itd UNIX socket // Connect to itd UNIX socket
conn, err := net.Dial("unix", SockPath) conn, err := net.Dial("unix", viper.GetString("sockPath"))
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?") log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
} }
@ -49,7 +50,7 @@ var timeCmd = &cobra.Command{
// Encode request into connection // Encode request into connection
err = json.NewEncoder(conn).Encode(types.Request{ err = json.NewEncoder(conn).Encode(types.Request{
Type: ReqTypeSetTime, Type: types.ReqTypeSetTime,
Data: args[0], Data: args[0],
}) })
if err != nil { if err != nil {

View File

@ -38,7 +38,7 @@ var upgradeCmd = &cobra.Command{
Aliases: []string{"upg"}, Aliases: []string{"upg"},
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Connect to itd UNIX socket // Connect to itd UNIX socket
conn, err := net.Dial("unix", SockPath) conn, err := net.Dial("unix", viper.GetString("sockPath"))
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?") log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
} }
@ -49,13 +49,13 @@ var upgradeCmd = &cobra.Command{
if viper.GetString("archive") != "" { if viper.GetString("archive") != "" {
// Get archive data struct // Get archive data struct
data = types.ReqDataFwUpgrade{ data = types.ReqDataFwUpgrade{
Type: UpgradeTypeArchive, Type: types.UpgradeTypeArchive,
Files: []string{viper.GetString("archive")}, Files: []string{viper.GetString("archive")},
} }
} else if viper.GetString("initPkt") != "" && viper.GetString("firmware") != "" { } else if viper.GetString("initPkt") != "" && viper.GetString("firmware") != "" {
// Get files data struct // Get files data struct
data = types.ReqDataFwUpgrade{ data = types.ReqDataFwUpgrade{
Type: UpgradeTypeFiles, Type: types.UpgradeTypeFiles,
Files: []string{viper.GetString("initPkt"), viper.GetString("firmware")}, Files: []string{viper.GetString("initPkt"), viper.GetString("firmware")},
} }
} else { } else {
@ -66,7 +66,7 @@ var upgradeCmd = &cobra.Command{
// Encode response into connection // Encode response into connection
err = json.NewEncoder(conn).Encode(types.Request{ err = json.NewEncoder(conn).Encode(types.Request{
Type: ReqTypeFwUpgrade, Type: types.ReqTypeFwUpgrade,
Data: data, Data: data,
}) })
if err != nil { if err != nil {

View File

@ -26,6 +26,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"go.arsenm.dev/itd/internal/types" "go.arsenm.dev/itd/internal/types"
) )
@ -36,7 +37,7 @@ var versionCmd = &cobra.Command{
Short: "Get firmware version of InfiniTime", Short: "Get firmware version of InfiniTime",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// Connect to itd UNIX socket // Connect to itd UNIX socket
conn, err := net.Dial("unix", SockPath) conn, err := net.Dial("unix", viper.GetString("sockPath"))
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?") log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
} }
@ -44,7 +45,7 @@ var versionCmd = &cobra.Command{
// Encode request into connection // Encode request into connection
err = json.NewEncoder(conn).Encode(types.Request{ err = json.NewEncoder(conn).Encode(types.Request{
Type: ReqTypeFwVersion, Type: types.ReqTypeFwVersion,
}) })
if err != nil { if err != nil {
log.Fatal().Err(err).Msg("Error making request") log.Fatal().Err(err).Msg("Error making request")

View File

@ -1,5 +1,20 @@
package types package types
const (
ReqTypeHeartRate = iota
ReqTypeBattLevel
ReqTypeFwVersion
ReqTypeFwUpgrade
ReqTypeBtAddress
ReqTypeNotify
ReqTypeSetTime
)
const (
UpgradeTypeArchive = iota
UpgradeTypeFiles
)
type ReqDataFwUpgrade struct { type ReqDataFwUpgrade struct {
Type int Type int
Files []string Files []string
@ -12,11 +27,11 @@ type Response struct {
} }
type Request struct { type Request struct {
Type string `json:"type"` Type int `json:"type"`
Data interface{} `json:"data,omitempty"` Data interface{} `json:"data,omitempty"`
} }
type ReqDataNotify struct { type ReqDataNotify struct {
Title string Title string
Body string Body string
} }

View File

@ -34,21 +34,6 @@ import (
"go.arsenm.dev/itd/internal/types" "go.arsenm.dev/itd/internal/types"
) )
const (
ReqTypeHeartRate = "hrt"
ReqTypeBattLevel = "battlvl"
ReqTypeFwVersion = "fwver"
ReqTypeFwUpgrade = "fwupg"
ReqTypeBtAddress = "btaddr"
ReqTypeNotify = "notify"
ReqTypeSetTime = "settime"
)
const (
UpgradeTypeArchive = iota
UpgradeTypeFiles
)
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(viper.GetString("socket.path")), 0755)
@ -107,7 +92,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
} }
switch req.Type { switch req.Type {
case ReqTypeHeartRate: case types.ReqTypeHeartRate:
// Get heart rate from watch // Get heart rate from watch
heartRate, err := dev.HeartRate() heartRate, err := dev.HeartRate()
if err != nil { if err != nil {
@ -118,7 +103,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
json.NewEncoder(conn).Encode(types.Response{ json.NewEncoder(conn).Encode(types.Response{
Value: heartRate, Value: heartRate,
}) })
case ReqTypeBattLevel: case types.ReqTypeBattLevel:
// Get battery level from watch // Get battery level from watch
battLevel, err := dev.BatteryLevel() battLevel, err := dev.BatteryLevel()
if err != nil { if err != nil {
@ -129,7 +114,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
json.NewEncoder(conn).Encode(types.Response{ json.NewEncoder(conn).Encode(types.Response{
Value: battLevel, Value: battLevel,
}) })
case ReqTypeFwVersion: case types.ReqTypeFwVersion:
// Get firmware version from watch // Get firmware version from watch
version, err := dev.Version() version, err := dev.Version()
if err != nil { if err != nil {
@ -140,12 +125,12 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
json.NewEncoder(conn).Encode(types.Response{ json.NewEncoder(conn).Encode(types.Response{
Value: version, Value: version,
}) })
case ReqTypeBtAddress: case types.ReqTypeBtAddress:
// Encode bluetooth address to connection // Encode bluetooth address to connection
json.NewEncoder(conn).Encode(types.Response{ json.NewEncoder(conn).Encode(types.Response{
Value: dev.Address(), Value: dev.Address(),
}) })
case ReqTypeNotify: case types.ReqTypeNotify:
// If no data, return error // If no data, return error
if req.Data == nil { if req.Data == nil {
connErr(conn, nil, "Data required for notify request") connErr(conn, nil, "Data required for notify request")
@ -166,7 +151,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
} }
// Encode empty types.Response to connection // Encode empty types.Response to connection
json.NewEncoder(conn).Encode(types.Response{}) json.NewEncoder(conn).Encode(types.Response{})
case ReqTypeSetTime: case types.ReqTypeSetTime:
// If no data, return error // If no data, return error
if req.Data == nil { if req.Data == nil {
connErr(conn, nil, "Data required for settime request") connErr(conn, nil, "Data required for settime request")
@ -198,7 +183,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
} }
// Encode empty types.Response to connection // Encode empty types.Response to connection
json.NewEncoder(conn).Encode(types.Response{}) json.NewEncoder(conn).Encode(types.Response{})
case ReqTypeFwUpgrade: case types.ReqTypeFwUpgrade:
// If no data, return error // If no data, return error
if req.Data == nil { if req.Data == nil {
connErr(conn, nil, "Data required for firmware upgrade request") connErr(conn, nil, "Data required for firmware upgrade request")
@ -212,7 +197,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
break break
} }
switch reqData.Type { switch reqData.Type {
case UpgradeTypeArchive: case types.UpgradeTypeArchive:
// If less than one file, return error // If less than one file, return error
if len(reqData.Files) < 1 { if len(reqData.Files) < 1 {
connErr(conn, nil, "Archive upgrade requires one file with .zip extension") connErr(conn, nil, "Archive upgrade requires one file with .zip extension")
@ -229,7 +214,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
connErr(conn, err, "Error loading archive file") connErr(conn, err, "Error loading archive file")
break break
} }
case UpgradeTypeFiles: case types.UpgradeTypeFiles:
// If less than two files, return error // If less than two files, return error
if len(reqData.Files) < 2 { if len(reqData.Files) < 2 {
connErr(conn, nil, "Files upgrade requires two files. First with .dat and second with .bin extension.") connErr(conn, nil, "Files upgrade requires two files. First with .dat and second with .bin extension.")