From 07e2e85c152c9fd5264876f29f151ed90258c66c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Ny=C3=ADri?= Date: Sat, 16 Dec 2023 21:00:21 +0100 Subject: [PATCH] CLI app skeleton + ping & get name & discover subcommands --- cli/cmd/discover.go | 62 +++++++++++++++++++++++++++++++++++++++++++++ cli/cmd/getName.go | 58 ++++++++++++++++++++++++++++++++++++++++++ cli/cmd/ping.go | 60 +++++++++++++++++++++++++++++++++++++++++++ cli/cmd/root.go | 55 ++++++++++++++++++++++++++++++++++++++++ cli/cmd/setState.go | 31 +++++++++++++++++++++++ cli/cmd/status.go | 31 +++++++++++++++++++++++ cli/cmd/users.go | 32 +++++++++++++++++++++++ cli/utils.go | 18 +++++++++++++ cli/utils_test.go | 20 +++++++++++++++ go.mod | 5 ++++ go.sum | 10 ++++++++ main.go | 6 ++--- sdk/Discovery.go | 6 ++++- 13 files changed, 389 insertions(+), 5 deletions(-) create mode 100644 cli/cmd/discover.go create mode 100644 cli/cmd/getName.go create mode 100644 cli/cmd/ping.go create mode 100644 cli/cmd/root.go create mode 100644 cli/cmd/setState.go create mode 100644 cli/cmd/status.go create mode 100644 cli/cmd/users.go create mode 100644 cli/utils.go create mode 100644 cli/utils_test.go create mode 100644 go.sum diff --git a/cli/cmd/discover.go b/cli/cmd/discover.go new file mode 100644 index 0000000..26fe925 --- /dev/null +++ b/cli/cmd/discover.go @@ -0,0 +1,62 @@ +package cmd + +import ( + "bisecure/sdk" + "context" + "fmt" + "os" + "time" + + "github.com/spf13/cobra" +) + +func init() { + var ( + discoveryTime = 20 + ) + + discoverCmd := &cobra.Command{ + Use: "discover", + Short: "Discover Hörmann BiSecur gateways on the local network", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + err := discover(discoveryTime) + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(2) + } + }, + } + + rootCmd.AddCommand(discoverCmd) + + discoverCmd.Flags().IntVar(&discoveryTime, "time", 10, "Time in second while device discovery happens") +} + +func discover(discoveryTime int) error { + ctx := context.Background() + discovery := sdk.NewDiscovery(ctx, func(gateway sdk.Gateway) { + fmt.Printf("Response received: %+v\n", gateway) + }) + + fmt.Printf("Start discovery...\n") + err := discovery.Start() + if err != nil { + return err + } + + fmt.Printf("Waiting for responses...\n") + time.Sleep(time.Second * time.Duration(discoveryTime)) + + list := discovery.GetList() + fmt.Printf("list: %+v\n", list) + + fmt.Printf("Stop disovery...\n") + err = discovery.Stop() + if err != nil { + return nil + } + + fmt.Printf("Terminated\n") + return nil +} diff --git a/cli/cmd/getName.go b/cli/cmd/getName.go new file mode 100644 index 0000000..844c9a1 --- /dev/null +++ b/cli/cmd/getName.go @@ -0,0 +1,58 @@ +package cmd + +import ( + "bisecure/cli" + "bisecure/sdk" + "fmt" + "os" + + "github.com/spf13/cobra" +) + +func init() { + getNameCmd := &cobra.Command{ + Use: "get-name", + Short: "Queries the name of the Hörmann BiSecur gateway", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + mac, err := cli.ParesMacString(deviceMac) + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(1) + } + + name, err := GetName(localMac, mac, host, port) + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(4) + } + fmt.Printf("Received name: %s\n", name) + }, + } + + rootCmd.AddCommand(getNameCmd) +} + +func GetName(localMac, mac [6]byte, host string, port int) (string, error) { + client := sdk.NewClient(localMac, mac, host, port) + err := client.Open() + if err != nil { + fmt.Printf("%v", err) + } + + defer func() { + err2 := client.Close() + if err2 != nil { + fmt.Printf("%v", err2) + os.Exit(2) + } + }() + + name, err := client.GetName() + if err != nil { + fmt.Printf("%v", err) + os.Exit(3) + } + + return name, nil +} diff --git a/cli/cmd/ping.go b/cli/cmd/ping.go new file mode 100644 index 0000000..789fb9e --- /dev/null +++ b/cli/cmd/ping.go @@ -0,0 +1,60 @@ +package cmd + +import ( + "bisecure/cli" + "bisecure/sdk" + "fmt" + "os" + + "github.com/spf13/cobra" +) + +func init() { + var ( + count = 4 + ) + + pingCmd := &cobra.Command{ + Use: "ping", + Short: "Check if your Hörmann BiSecur gateway is reachable or not.", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + mac, err := cli.ParesMacString(deviceMac) + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(1) + } + + err = ping(localMac, mac, host, port, count) + if err != nil { + fmt.Printf("%v\n", err) + os.Exit(2) + } + }, + } + rootCmd.AddCommand(pingCmd) + + pingCmd.Flags().IntVar(&count, "count", 5, "Amount of the ping packages will be sent to the device") +} + +func ping(localMac, mac [6]byte, host string, port int, count int) error { + client := sdk.NewClient(localMac, mac, host, port) + err := client.Open() + if err != nil { + return err + } + + defer func() { + err2 := client.Close() + if err2 != nil { + fmt.Printf("%v", err) // TODO add log message + } + }() + + err = client.Ping(count) + if err != nil { + return err + } + + return nil +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go new file mode 100644 index 0000000..a46773c --- /dev/null +++ b/cli/cmd/root.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" +) + +var ( + host string + port int + username string + password string + deviceMac string + localMac = [6]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x09} + debug bool +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "halsecur", + Short: "Application to manage your Hörmann BiSecur gateway without the central cloud directly on your LAN.", + Long: ``, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cli.yaml)") + + rootCmd.PersistentFlags().StringVar(&username, "username", "", "Valid username") + rootCmd.PersistentFlags().StringVar(&password, "password", "", "Valid password belongs to the given username") + rootCmd.PersistentFlags().StringVar(&host, "host", "", "IP or host name or the Hörmann BiSecure gateway") + rootCmd.PersistentFlags().IntVar(&port, "port", 4000, "") + rootCmd.PersistentFlags().StringVar(&deviceMac, "mac", "", "MAC address of the Hörmann BiSecur gateway") + rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "debug log level") + + // Cobra also supports local flags, which will only run + // when this action is called directly. + //rootCmd.Flags().String("username", "", "") +} diff --git a/cli/cmd/setState.go b/cli/cmd/setState.go new file mode 100644 index 0000000..66165c3 --- /dev/null +++ b/cli/cmd/setState.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// setStateCmd represents the setState command +var setStateCmd = &cobra.Command{ + Use: "set-state", + Short: "Open or close a door connected to your Hörmann BiSecur gateway.", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("setState called") + }, +} + +func init() { + rootCmd.AddCommand(setStateCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // setStateCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // setStateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cli/cmd/status.go b/cli/cmd/status.go new file mode 100644 index 0000000..1f129a9 --- /dev/null +++ b/cli/cmd/status.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// statusCmd represents the status command +var statusCmd = &cobra.Command{ + Use: "status", + Short: "Queries the status (open/closed/etc) of your door.", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("status called") + }, +} + +func init() { + rootCmd.AddCommand(statusCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // statusCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // statusCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cli/cmd/users.go b/cli/cmd/users.go new file mode 100644 index 0000000..c542002 --- /dev/null +++ b/cli/cmd/users.go @@ -0,0 +1,32 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// usersCmd represents the users command +var usersCmd = &cobra.Command{ + Use: "users", + Short: "Manages users defined in your Hörmann BiSecur gateway.", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + // TODO implement query user list and rights, add and delete user, password change of an already existing user + fmt.Println("users called") + }, +} + +func init() { + rootCmd.AddCommand(usersCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // usersCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // usersCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cli/utils.go b/cli/utils.go new file mode 100644 index 0000000..7c15d1a --- /dev/null +++ b/cli/utils.go @@ -0,0 +1,18 @@ +package cli + +import ( + "encoding/hex" + "fmt" + "strings" +) + +func ParesMacString(mac string) ([6]byte, error) { + bytes := strings.Split(mac, ":") + if len(bytes) != 6 { + return [6]byte{}, fmt.Errorf("invalid mac address length: %s", mac) + } + + clearedMacStr := strings.ReplaceAll(mac, ":", "") + macBytes, err := hex.DecodeString(clearedMacStr) + return [6]byte{macBytes[0], macBytes[1], macBytes[2], macBytes[3], macBytes[4], macBytes[5]}, err +} diff --git a/cli/utils_test.go b/cli/utils_test.go new file mode 100644 index 0000000..d569283 --- /dev/null +++ b/cli/utils_test.go @@ -0,0 +1,20 @@ +package cli + +import ( + "reflect" + "testing" +) + +func TestTransmissionContainerEncode(t *testing.T) { + expectedMac := [6]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0xef} + mac, err := ParesMacString("00:01:02:03:04:ef") + if err != nil { + t.Logf("Unexpected error: %v", err) + t.Fail() + } + + if !reflect.DeepEqual(mac, expectedMac) { + t.Logf("Expected MAC: %X, Actual MAC: %X", expectedMac, mac) + t.Fail() + } +} diff --git a/go.mod b/go.mod index a0302e2..b597488 100644 --- a/go.mod +++ b/go.mod @@ -2,3 +2,8 @@ module bisecure go 1.21 +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/cobra v1.8.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d0e8c2c --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= +github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index f0174fc..8382022 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,7 @@ package main -import ( - "fmt" -) +import "bisecure/cli/cmd" func main() { - fmt.Println("Hello") + cmd.Execute() } diff --git a/sdk/Discovery.go b/sdk/Discovery.go index 0c42c53..0d840d4 100644 --- a/sdk/Discovery.go +++ b/sdk/Discovery.go @@ -118,7 +118,11 @@ func (d *Discovery) receivingPackets() { default: data := make([]byte, 10000) - //connection.SetReadDeadline(time.Now().Add(5 * time.Second)) + err = connection.SetReadDeadline(time.Now().Add(5 * time.Second)) + if err != nil { + // TODO add warning log + continue + } _, addr, err := connection.ReadFrom(data) if err != nil { continue