Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added simpler status #1

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cmd/close.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ var closeCmd = &cobra.Command{
fmt.Printf("device ID must me a number: %s", err)
return
}
shadeconnector.Operation(device_id, int(shadeconnector.Close))
fmt.Printf("Close device %s sucessfully", args[0])
fmt.Println()
message, err := shadeconnector.Operation(device_id, int(shadeconnector.Close))
if err != nil {
fmt.Printf("Cannot execute the operation to device:%d, error: %s", device_id, err.Error())
return
}
PrintStatus(message)

},
}
Expand Down
9 changes: 6 additions & 3 deletions cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ var openCmd = &cobra.Command{
fmt.Printf("device ID must me a number: %s", err)
return
}
shadeconnector.Operation(device_id, int(shadeconnector.Open))
fmt.Printf("Open device %s sucessfully", args[0])
fmt.Println()
message, err := shadeconnector.Operation(device_id, int(shadeconnector.Open))
if err != nil {
fmt.Printf("Cannot execute the operation to device:%d, error: %s", device_id, err.Error())
return
}
PrintStatus(message)
},
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

var version = "0.0.1"
var host, port, apiKey string
var simple_status, invert bool

var rootCmd = &cobra.Command{
Use: "sconnector-cli",
Expand All @@ -22,6 +23,8 @@ func init() {
rootCmd.PersistentFlags().StringVar(&host, "host", os.Getenv("CONNECTOR_BRIDGE_HOST"), "The hostname of IP address of the Connector Bridge. Default 238.0.0.18")
rootCmd.PersistentFlags().StringVar(&port, "port", os.Getenv("CONNECTOR_BRIDGE_PORT"), "The port for the Connector Bridge connection. Default 32100")
rootCmd.PersistentFlags().StringVar(&apiKey, "apikey", os.Getenv("CONNECTOR_BRIDGE_APIKEY"), "The ApiKey from Connector Bridge. On the mobile app: Go to Settings (gear), About. Tap 5 times on the screen")
rootCmd.PersistentFlags().BoolVar(&simple_status, "simple-status", false, "The commands just provide the position as respose. Useful when integrating with other devices like HomeBridge")
rootCmd.PersistentFlags().BoolVar(&invert, "invert", false, "Inverts the posiion value when used with simple-status")
}

func Execute() {
Expand Down
9 changes: 6 additions & 3 deletions cmd/set-position.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ var setpositionCmd = &cobra.Command{
fmt.Printf("postion must me a number between 0 and 100: %s", err)
return
}
shadeconnector.SetPosition(device_id, position)
fmt.Printf("Device %s position set to position %s%% sucessfully", args[0], args[1])
fmt.Println()
message, err := shadeconnector.SetPosition(device_id, position)
if err != nil {
fmt.Printf("Cannot execute the operation to device:%d, error: %s", device_id, err.Error())
return
}
PrintStatus(message)
},
}

Expand Down
39 changes: 19 additions & 20 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/spf13/cobra"
)

var position_only, invert_position bool

// openCmd represents the open command
var statusCmd = &cobra.Command{
Use: "status [device id]",
Expand All @@ -32,29 +30,30 @@ var statusCmd = &cobra.Command{
fmt.Printf("Cannot execute the operation to device:%d, error: %s", device_id, err.Error())
return
}
if position_only {
position := message.CurrentPosition
if invert_position {
fmt.Println(100 - position)
return
} else {
fmt.Println(position)
return
}
}
output, err := json.Marshal(message)
if err != nil {
fmt.Printf("Cannot covert message: %s", err.Error())
return
}
fmt.Println(string(output))
PrintStatus(message)

},
}

func init() {
statusCmd.Flags().BoolVar(&position_only, "position-only", false, "Return the position of the device")
statusCmd.Flags().BoolVar(&invert_position, "invert", false, "Return the position of the device")
rootCmd.AddCommand(statusCmd)
}

func PrintStatus(message *shadeconnector.DeviceStatus) {
if simple_status {
position := message.CurrentPosition
if invert {
fmt.Println(100 - position)
return
} else {
fmt.Println(position)
return
}
}
output, err := json.Marshal(message)
if err != nil {
fmt.Printf("Cannot covert message: %s", err.Error())
return
}
fmt.Println(string(output))
}
9 changes: 6 additions & 3 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ var stopCmd = &cobra.Command{
fmt.Printf("device ID must me a number: %s", err)
return
}
shadeconnector.Operation(device_id, int(shadeconnector.Stop))
fmt.Printf("Stop device %s sucessfully", args[0])
fmt.Println()
message, err := shadeconnector.Operation(device_id, int(shadeconnector.Stop))
if err != nil {
fmt.Printf("Cannot execute the operation to device:%d, error: %s", device_id, err.Error())
return
}
PrintStatus(message)
},
}

Expand Down