Skip to content

Commit

Permalink
More commands
Browse files Browse the repository at this point in the history
  • Loading branch information
halacs committed Dec 17, 2023
1 parent 07e2e85 commit 44ecaf2
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 63 deletions.
5 changes: 5 additions & 0 deletions cli/cmd/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cmd

const (
devicePortName = "devicePort"
)
59 changes: 59 additions & 0 deletions cli/cmd/groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cmd

import (
"bisecure/cli"
"bisecure/sdk"
"fmt"
"os"

"github.com/spf13/cobra"
)

func init() {
groupsCmd := &cobra.Command{
Use: "groups",
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

mac, err := cli.ParesMacString(deviceMac)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}

err = listGroups(localMac, mac, host, port)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(2)
}
},
}

rootCmd.AddCommand(groupsCmd)
}

func listGroups(localMac [6]byte, mac [6]byte, host string, port 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
}
}()

groups, err := client.GetGroups()
if err != nil {
return err
}

fmt.Printf("Groups: %s\n", groups.String())

return nil
}
2 changes: 1 addition & 1 deletion cli/cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func init() {
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 {
func ping(localMac [6]byte, mac [6]byte, host string, port int, count int) error {
client := sdk.NewClient(localMac, mac, host, port)
err := client.Open()
if err != nil {
Expand Down
10 changes: 0 additions & 10 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,10 @@ func Execute() {
}

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", "", "")
}
73 changes: 56 additions & 17 deletions cli/cmd/setState.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,70 @@
package cmd

import (
"bisecure/cli"
"bisecure/sdk"
"fmt"
"os"

"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() {
var devicePort int

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) {
mac, err := cli.ParesMacString(deviceMac)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}

err = setStatus(localMac, mac, host, port, username, password, byte(devicePort))
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(2)
}
},
}

rootCmd.AddCommand(setStateCmd)

// Here you will define your flags and configuration settings.
setStateCmd.Flags().IntVar(&devicePort, devicePortName, 0, "Port number of the door")
setStateCmd.MarkFlagsOneRequired(devicePortName)
}

func setStatus(localMac [6]byte, mac [6]byte, host string, port int, username string, password string, devicePort byte) 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)
}
}()

err = client.Login(username, password)
if err != nil {
return err
}

fmt.Println("Logged in successfully.")

err = client.SetState(devicePort)
if err != nil {
return err
}

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// setStateCmd.PersistentFlags().String("foo", "", "A help for foo")
fmt.Printf("Done\n")

// 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")
return nil
}
73 changes: 56 additions & 17 deletions cli/cmd/status.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,70 @@
package cmd

import (
"bisecure/cli"
"bisecure/sdk"
"fmt"
"os"

"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() {
var devicePort int

statusCmd := &cobra.Command{
Use: "status",
Short: "Queries the status (open/closed/etc) of your door.",
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 = getStatus(localMac, mac, host, port, username, password, byte(devicePort))
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(2)
}
},
}

rootCmd.AddCommand(statusCmd)

// Here you will define your flags and configuration settings.
statusCmd.Flags().IntVar(&devicePort, devicePortName, 0, "Port number of the door")
statusCmd.MarkFlagsOneRequired(devicePortName)
}

func getStatus(localMac [6]byte, mac [6]byte, host string, port int, username string, password string, devicePort byte) 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)
}
}()

err = client.Login(username, password)
if err != nil {
return err
}

fmt.Println("Logged in successfully.")

status, err := client.GetTransition(devicePort)
if err != nil {
return err
}

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// statusCmd.PersistentFlags().String("foo", "", "A help for foo")
fmt.Printf("Transition: %+v\n", status)

// 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")
return nil
}
62 changes: 44 additions & 18 deletions cli/cmd/users.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,58 @@
package cmd

import (
"bisecure/cli"
"bisecure/sdk"
"fmt"
"os"

"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() {
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
mac, err := cli.ParesMacString(deviceMac)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}

err = listUsers(localMac, mac, host, port)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(2)
}
},
}

rootCmd.AddCommand(usersCmd)
}

func listUsers(localMac [6]byte, mac [6]byte, host string, port 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
}
}()

// Here you will define your flags and configuration settings.
users, err := client.GetUsers()
if err != nil {
return err
}

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// usersCmd.PersistentFlags().String("foo", "", "A help for foo")
fmt.Printf("Users: %s\n", users.String())

// 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")
return nil
}

0 comments on commit 44ecaf2

Please sign in to comment.