Switch to iota for request types and move to types package
This commit is contained in:
parent
0fe83fccc1
commit
36b683204d
@ -26,6 +26,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@ -36,7 +37,7 @@ var addressCmd = &cobra.Command{
|
||||
Short: "Get InfiniTime's bluetooth address",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@ -44,7 +45,7 @@ var addressCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeBtAddress,
|
||||
Type: types.ReqTypeBtAddress,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error making request")
|
||||
|
@ -26,6 +26,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@ -36,7 +37,7 @@ var batteryCmd = &cobra.Command{
|
||||
Short: "Get battery level from InfiniTime",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@ -44,7 +45,7 @@ var batteryCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeBattLevel,
|
||||
Type: types.ReqTypeBattLevel,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error making request")
|
||||
|
@ -18,23 +18,6 @@
|
||||
|
||||
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 {
|
||||
Received int64 `mapstructure:"recvd"`
|
||||
Total int64 `mapstructure:"total"`
|
||||
|
@ -26,6 +26,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@ -35,7 +36,7 @@ var heartCmd = &cobra.Command{
|
||||
Short: "Get heart rate from InfiniTime",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@ -43,7 +44,7 @@ var heartCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeHeartRate,
|
||||
Type: types.ReqTypeHeartRate,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error making request")
|
||||
|
@ -25,6 +25,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@ -40,7 +41,7 @@ var notifyCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@ -48,7 +49,7 @@ var notifyCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeNotify,
|
||||
Type: types.ReqTypeNotify,
|
||||
Data: types.ReqDataNotify{
|
||||
Title: args[0],
|
||||
Body: args[1],
|
||||
|
@ -21,6 +21,7 @@ package cmd
|
||||
import (
|
||||
"github.com/abiosoft/ishell"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
@ -63,3 +64,15 @@ func Execute() {
|
||||
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||
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")
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@ -41,7 +42,7 @@ var timeCmd = &cobra.Command{
|
||||
}
|
||||
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@ -49,7 +50,7 @@ var timeCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeSetTime,
|
||||
Type: types.ReqTypeSetTime,
|
||||
Data: args[0],
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -38,7 +38,7 @@ var upgradeCmd = &cobra.Command{
|
||||
Aliases: []string{"upg"},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@ -49,13 +49,13 @@ var upgradeCmd = &cobra.Command{
|
||||
if viper.GetString("archive") != "" {
|
||||
// Get archive data struct
|
||||
data = types.ReqDataFwUpgrade{
|
||||
Type: UpgradeTypeArchive,
|
||||
Type: types.UpgradeTypeArchive,
|
||||
Files: []string{viper.GetString("archive")},
|
||||
}
|
||||
} else if viper.GetString("initPkt") != "" && viper.GetString("firmware") != "" {
|
||||
// Get files data struct
|
||||
data = types.ReqDataFwUpgrade{
|
||||
Type: UpgradeTypeFiles,
|
||||
Type: types.UpgradeTypeFiles,
|
||||
Files: []string{viper.GetString("initPkt"), viper.GetString("firmware")},
|
||||
}
|
||||
} else {
|
||||
@ -66,7 +66,7 @@ var upgradeCmd = &cobra.Command{
|
||||
|
||||
// Encode response into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeFwUpgrade,
|
||||
Type: types.ReqTypeFwUpgrade,
|
||||
Data: data,
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -26,6 +26,7 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"go.arsenm.dev/itd/internal/types"
|
||||
)
|
||||
|
||||
@ -36,7 +37,7 @@ var versionCmd = &cobra.Command{
|
||||
Short: "Get firmware version of InfiniTime",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Connect to itd UNIX socket
|
||||
conn, err := net.Dial("unix", SockPath)
|
||||
conn, err := net.Dial("unix", viper.GetString("sockPath"))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error dialing socket. Is itd running?")
|
||||
}
|
||||
@ -44,7 +45,7 @@ var versionCmd = &cobra.Command{
|
||||
|
||||
// Encode request into connection
|
||||
err = json.NewEncoder(conn).Encode(types.Request{
|
||||
Type: ReqTypeFwVersion,
|
||||
Type: types.ReqTypeFwVersion,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Error making request")
|
||||
|
@ -1,5 +1,20 @@
|
||||
package types
|
||||
|
||||
const (
|
||||
ReqTypeHeartRate = iota
|
||||
ReqTypeBattLevel
|
||||
ReqTypeFwVersion
|
||||
ReqTypeFwUpgrade
|
||||
ReqTypeBtAddress
|
||||
ReqTypeNotify
|
||||
ReqTypeSetTime
|
||||
)
|
||||
|
||||
const (
|
||||
UpgradeTypeArchive = iota
|
||||
UpgradeTypeFiles
|
||||
)
|
||||
|
||||
type ReqDataFwUpgrade struct {
|
||||
Type int
|
||||
Files []string
|
||||
@ -12,7 +27,7 @@ type Response struct {
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
Type string `json:"type"`
|
||||
Type int `json:"type"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
|
33
socket.go
33
socket.go
@ -34,21 +34,6 @@ import (
|
||||
"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 {
|
||||
// Make socket directory if non-existent
|
||||
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 {
|
||||
case ReqTypeHeartRate:
|
||||
case types.ReqTypeHeartRate:
|
||||
// Get heart rate from watch
|
||||
heartRate, err := dev.HeartRate()
|
||||
if err != nil {
|
||||
@ -118,7 +103,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
|
||||
json.NewEncoder(conn).Encode(types.Response{
|
||||
Value: heartRate,
|
||||
})
|
||||
case ReqTypeBattLevel:
|
||||
case types.ReqTypeBattLevel:
|
||||
// Get battery level from watch
|
||||
battLevel, err := dev.BatteryLevel()
|
||||
if err != nil {
|
||||
@ -129,7 +114,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
|
||||
json.NewEncoder(conn).Encode(types.Response{
|
||||
Value: battLevel,
|
||||
})
|
||||
case ReqTypeFwVersion:
|
||||
case types.ReqTypeFwVersion:
|
||||
// Get firmware version from watch
|
||||
version, err := dev.Version()
|
||||
if err != nil {
|
||||
@ -140,12 +125,12 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
|
||||
json.NewEncoder(conn).Encode(types.Response{
|
||||
Value: version,
|
||||
})
|
||||
case ReqTypeBtAddress:
|
||||
case types.ReqTypeBtAddress:
|
||||
// Encode bluetooth address to connection
|
||||
json.NewEncoder(conn).Encode(types.Response{
|
||||
Value: dev.Address(),
|
||||
})
|
||||
case ReqTypeNotify:
|
||||
case types.ReqTypeNotify:
|
||||
// If no data, return error
|
||||
if req.Data == nil {
|
||||
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
|
||||
json.NewEncoder(conn).Encode(types.Response{})
|
||||
case ReqTypeSetTime:
|
||||
case types.ReqTypeSetTime:
|
||||
// If no data, return error
|
||||
if req.Data == nil {
|
||||
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
|
||||
json.NewEncoder(conn).Encode(types.Response{})
|
||||
case ReqTypeFwUpgrade:
|
||||
case types.ReqTypeFwUpgrade:
|
||||
// If no data, return error
|
||||
if req.Data == nil {
|
||||
connErr(conn, nil, "Data required for firmware upgrade request")
|
||||
@ -212,7 +197,7 @@ func handleConnection(conn net.Conn, dev *infinitime.Device) {
|
||||
break
|
||||
}
|
||||
switch reqData.Type {
|
||||
case UpgradeTypeArchive:
|
||||
case types.UpgradeTypeArchive:
|
||||
// If less than one file, return error
|
||||
if len(reqData.Files) < 1 {
|
||||
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")
|
||||
break
|
||||
}
|
||||
case UpgradeTypeFiles:
|
||||
case types.UpgradeTypeFiles:
|
||||
// If less than two files, return error
|
||||
if len(reqData.Files) < 2 {
|
||||
connErr(conn, nil, "Files upgrade requires two files. First with .dat and second with .bin extension.")
|
||||
|
Loading…
Reference in New Issue
Block a user