-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
76 lines (64 loc) · 2.13 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
package main
import (
"encoding/json"
"flag"
"github.com/achetronic/tapogo/api/types"
"github.com/achetronic/tapogo/pkg/tapogo"
"log"
"os"
"time"
)
const (
EmailFlagDescription = "(Required) Email to access the device"
PasswordFlagDescription = "(Required) Password to access the device"
IpFlagDescription = "(Required) Device IP address"
CommandFlagDescription = "Command to execute: device-info (default), on, off, energy-usage"
HandshakeDurationFlagDescription = "Duration to sleep between handshake phases (default: '1s')"
)
func main() {
var tapoClient *tapogo.Tapo
var response *types.ResponseSpec
var err error
// 1. Parse the flags from the command line
emailFlag := flag.String("email", "", EmailFlagDescription)
passwordFlag := flag.String("password", "", PasswordFlagDescription)
ipFlag := flag.String("ip", "", IpFlagDescription)
commandFlag := flag.String("command", "device-info", CommandFlagDescription)
handshakeDurationFlag := flag.String("handshake-duration", "1s", HandshakeDurationFlagDescription)
flag.Parse()
if *emailFlag == "" || *passwordFlag == "" || *ipFlag == "" {
log.Print("Some required flags are missing. execute 'tapogo --help' to help yourself")
os.Exit(0)
}
// TODO
handshakeDuration, err := time.ParseDuration(*handshakeDurationFlag)
extraOptions := tapogo.TapoOptions{
HandshakeDelayDuration: handshakeDuration,
}
// 2. Init the client: handshake
tapoClient, err = tapogo.NewTapo(*ipFlag, *emailFlag, *passwordFlag, &extraOptions)
if err != nil {
log.Fatalln(err)
}
// 3. Execute the action
switch *commandFlag {
case "off":
response, err = tapoClient.TurnOff()
case "on":
response, err = tapoClient.TurnOn()
case "energy-usage":
response, err = tapoClient.GetEnergyUsage()
case "device-info":
response, err = tapoClient.DeviceInfo()
default:
log.Fatalf("invalid argument: '%s'. expected one of [off, on, energy-usage, device-info]", *commandFlag)
}
if err != nil {
log.Fatalln(err)
}
jsonBytes, err := json.Marshal(*response)
if err != nil {
log.Print(err)
}
log.Printf("Response (processed): %s", string(jsonBytes))
}