Skip to content

Commit

Permalink
Adds example program
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSp1der committed Mar 31, 2019
1 parent f52b827 commit 2d89702
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions examples/remote-control.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"flag"
"fmt"
"os"
"strings"

"github.com/TheSp1der/tplink"
)

func main() {
var (
host string
getState bool
turnOn bool
turnOff bool
)

flag.StringVar(&host, "host", "", "hostname of device")
flag.BoolVar(&getState, "get-state", false, "get device power state")
flag.BoolVar(&turnOn, "on", false, "turn the device on")
flag.BoolVar(&turnOff, "off", false, "turn the device off")
flag.Parse()

if len(strings.TrimSpace(host)) == 0 {
fmt.Println("host must be defined")
flag.PrintDefaults()
os.Exit(100)
}

if !getState && !turnOn && !turnOff {
fmt.Println("an action must be defined")
flag.PrintDefaults()
os.Exit(100)
}

if getState && (turnOn || turnOff) {
fmt.Println("only one action may be defined")
flag.PrintDefaults()
os.Exit(100)
}

if turnOn && turnOff {
fmt.Println("only one action may be defined")
flag.PrintDefaults()
os.Exit(100)
}

device := tplink.Tplink{Host: host}

if getState {
response, err := device.SystemInfo()
if err != nil {
fmt.Println("unable to communicate with device")
os.Exit(100)
}
if response.System.GetSysinfo.RelayState == 1 {
fmt.Println(host + " is ON")
} else {
fmt.Println(host + " is OFF")
}
}

if turnOn {
if err := device.TurnOn(); err != nil {
fmt.Println("unable to communicate with device")
os.Exit(100)
}
}
if turnOff {
if err := device.TurnOff(); err != nil {
fmt.Println("unable to communicate with device")
os.Exit(100)
}
}
}

0 comments on commit 2d89702

Please sign in to comment.