Skip to content

Commit 2c74601

Browse files
authored
Merge pull request #1 from alexbacchin/simple-status
added simpler status
2 parents 5316b66 + cb11a96 commit 2c74601

File tree

6 files changed

+46
-32
lines changed

6 files changed

+46
-32
lines changed

cmd/close.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ var closeCmd = &cobra.Command{
2424
fmt.Printf("device ID must me a number: %s", err)
2525
return
2626
}
27-
shadeconnector.Operation(device_id, int(shadeconnector.Close))
28-
fmt.Printf("Close device %s sucessfully", args[0])
29-
fmt.Println()
27+
message, err := shadeconnector.Operation(device_id, int(shadeconnector.Close))
28+
if err != nil {
29+
fmt.Printf("Cannot execute the operation to device:%d, error: %s", device_id, err.Error())
30+
return
31+
}
32+
PrintStatus(message)
3033

3134
},
3235
}

cmd/open.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ var openCmd = &cobra.Command{
2424
fmt.Printf("device ID must me a number: %s", err)
2525
return
2626
}
27-
shadeconnector.Operation(device_id, int(shadeconnector.Open))
28-
fmt.Printf("Open device %s sucessfully", args[0])
29-
fmt.Println()
27+
message, err := shadeconnector.Operation(device_id, int(shadeconnector.Open))
28+
if err != nil {
29+
fmt.Printf("Cannot execute the operation to device:%d, error: %s", device_id, err.Error())
30+
return
31+
}
32+
PrintStatus(message)
3033
},
3134
}
3235

cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
var version = "0.0.1"
1111
var host, port, apiKey string
12+
var simple_status, invert bool
1213

1314
var rootCmd = &cobra.Command{
1415
Use: "sconnector-cli",
@@ -22,6 +23,8 @@ func init() {
2223
rootCmd.PersistentFlags().StringVar(&host, "host", os.Getenv("CONNECTOR_BRIDGE_HOST"), "The hostname of IP address of the Connector Bridge. Default 238.0.0.18")
2324
rootCmd.PersistentFlags().StringVar(&port, "port", os.Getenv("CONNECTOR_BRIDGE_PORT"), "The port for the Connector Bridge connection. Default 32100")
2425
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")
26+
rootCmd.PersistentFlags().BoolVar(&simple_status, "simple-status", false, "The commands just provide the position as respose. Useful when integrating with other devices like HomeBridge")
27+
rootCmd.PersistentFlags().BoolVar(&invert, "invert", false, "Inverts the posiion value when used with simple-status")
2528
}
2629

2730
func Execute() {

cmd/set-position.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ var setpositionCmd = &cobra.Command{
2929
fmt.Printf("postion must me a number between 0 and 100: %s", err)
3030
return
3131
}
32-
shadeconnector.SetPosition(device_id, position)
33-
fmt.Printf("Device %s position set to position %s%% sucessfully", args[0], args[1])
34-
fmt.Println()
32+
message, err := shadeconnector.SetPosition(device_id, position)
33+
if err != nil {
34+
fmt.Printf("Cannot execute the operation to device:%d, error: %s", device_id, err.Error())
35+
return
36+
}
37+
PrintStatus(message)
3538
},
3639
}
3740

cmd/status.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"github.com/spf13/cobra"
1313
)
1414

15-
var position_only, invert_position bool
16-
1715
// openCmd represents the open command
1816
var statusCmd = &cobra.Command{
1917
Use: "status [device id]",
@@ -32,29 +30,30 @@ var statusCmd = &cobra.Command{
3230
fmt.Printf("Cannot execute the operation to device:%d, error: %s", device_id, err.Error())
3331
return
3432
}
35-
if position_only {
36-
position := message.CurrentPosition
37-
if invert_position {
38-
fmt.Println(100 - position)
39-
return
40-
} else {
41-
fmt.Println(position)
42-
return
43-
}
44-
}
45-
output, err := json.Marshal(message)
46-
if err != nil {
47-
fmt.Printf("Cannot covert message: %s", err.Error())
48-
return
49-
}
50-
fmt.Println(string(output))
33+
PrintStatus(message)
5134

5235
},
5336
}
5437

5538
func init() {
56-
statusCmd.Flags().BoolVar(&position_only, "position-only", false, "Return the position of the device")
57-
statusCmd.Flags().BoolVar(&invert_position, "invert", false, "Return the position of the device")
5839
rootCmd.AddCommand(statusCmd)
40+
}
5941

42+
func PrintStatus(message *shadeconnector.DeviceStatus) {
43+
if simple_status {
44+
position := message.CurrentPosition
45+
if invert {
46+
fmt.Println(100 - position)
47+
return
48+
} else {
49+
fmt.Println(position)
50+
return
51+
}
52+
}
53+
output, err := json.Marshal(message)
54+
if err != nil {
55+
fmt.Printf("Cannot covert message: %s", err.Error())
56+
return
57+
}
58+
fmt.Println(string(output))
6059
}

cmd/stop.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ var stopCmd = &cobra.Command{
2424
fmt.Printf("device ID must me a number: %s", err)
2525
return
2626
}
27-
shadeconnector.Operation(device_id, int(shadeconnector.Stop))
28-
fmt.Printf("Stop device %s sucessfully", args[0])
29-
fmt.Println()
27+
message, err := shadeconnector.Operation(device_id, int(shadeconnector.Stop))
28+
if err != nil {
29+
fmt.Printf("Cannot execute the operation to device:%d, error: %s", device_id, err.Error())
30+
return
31+
}
32+
PrintStatus(message)
3033
},
3134
}
3235

0 commit comments

Comments
 (0)