/
main.go
88 lines (80 loc) · 1.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/BurntSushi/toml"
"github.com/nasa9084/go-switchbot/v3"
"os"
)
const (
STATUS_INVALID_ARGUMENT = 2
STATUS_CONFIG_ERROR = 2
STATUS_API_ERROR = 1
)
type Config struct {
OpenToken string
SecretKey string
}
func help() {
fmt.Println("Usage: switchbot <command> [options]")
fmt.Println("Commands:")
fmt.Println(" list")
fmt.Println(" status <device_id...>")
}
func main() {
if len(os.Args) < 2 {
help()
os.Exit(STATUS_INVALID_ARGUMENT)
}
var config Config
_, err := toml.DecodeFile("config.toml", &config)
if err != nil {
fmt.Printf("config.toml error %v\n", err)
os.Exit(STATUS_CONFIG_ERROR)
}
c := switchbot.New(config.OpenToken, config.SecretKey)
cmd := NewCommands(c)
switch os.Args[1] {
case "list":
// get physical devices and show
pdev, err := cmd.ListDevices(context.Background())
if err != nil {
fmt.Printf(" error %v\n", err)
os.Exit(STATUS_API_ERROR)
} else {
var results []*DeviceItem
for _, d := range pdev {
i := ParseDeviceItem(&d)
results = append(results, i)
}
j, err := json.Marshal(results)
if err != nil {
fmt.Printf(" error %v\n", err)
os.Exit(STATUS_API_ERROR)
}
fmt.Printf("%v\n", string(j))
}
case "status":
if len(os.Args) < 3 {
help()
os.Exit(STATUS_INVALID_ARGUMENT)
}
var results []*DeviceStatus
for i := 2; i < len(os.Args); i++ {
id := os.Args[i]
status, err := cmd.Status(context.Background(), id)
if err != nil {
fmt.Printf("%v error %v\n", id, err)
os.Exit(STATUS_API_ERROR)
}
results = append(results, ParseDeviceStatus(status))
}
j, err := json.Marshal(results)
if err != nil {
fmt.Printf(" error %v\n", err)
} else {
fmt.Printf("%v\n", string(j))
}
}
}