Skip to content

Commit

Permalink
CLI app skeleton + ping & get name & discover subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
halacs committed Dec 16, 2023
1 parent faa7675 commit 07e2e85
Show file tree
Hide file tree
Showing 13 changed files with 389 additions and 5 deletions.
62 changes: 62 additions & 0 deletions cli/cmd/discover.go
Original file line number Diff line number Diff line change
@@ -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
}
58 changes: 58 additions & 0 deletions cli/cmd/getName.go
Original file line number Diff line number Diff line change
@@ -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
}
60 changes: 60 additions & 0 deletions cli/cmd/ping.go
Original file line number Diff line number Diff line change
@@ -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
}
55 changes: 55 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
@@ -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", "", "")
}
31 changes: 31 additions & 0 deletions cli/cmd/setState.go
Original file line number Diff line number Diff line change
@@ -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")
}
31 changes: 31 additions & 0 deletions cli/cmd/status.go
Original file line number Diff line number Diff line change
@@ -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")
}
32 changes: 32 additions & 0 deletions cli/cmd/users.go
Original file line number Diff line number Diff line change
@@ -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")
}
18 changes: 18 additions & 0 deletions cli/utils.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions cli/utils_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Loading

0 comments on commit 07e2e85

Please sign in to comment.