91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"git.lerch.org/lobo/wemo/wemodiscovery"
|
||
|
)
|
||
|
|
||
|
type basementPost struct {
|
||
|
MovieMode bool
|
||
|
}
|
||
|
|
||
|
type controlData struct {
|
||
|
Bar, Basement, Jack string
|
||
|
BasicEvent string
|
||
|
}
|
||
|
|
||
|
var command string
|
||
|
|
||
|
func main() {
|
||
|
command = os.Getenv("CMD")
|
||
|
|
||
|
if len(os.Args) > 1 {
|
||
|
if os.Args[1] == "scan" {
|
||
|
scan()
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
movieMode(true)
|
||
|
time.Sleep(60 * time.Second)
|
||
|
movieMode(false)
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
// POST /basement { movieMode: true } OR { movieMode: false }
|
||
|
http.HandleFunc("/basement", func(w http.ResponseWriter, r *http.Request) {
|
||
|
if r.Method != "POST" {
|
||
|
http.Error(w, "Not found", 404)
|
||
|
return
|
||
|
}
|
||
|
postBodyBytes, err := ioutil.ReadAll(r.Body)
|
||
|
if err != nil {
|
||
|
http.Error(w, "Could not read body", 500)
|
||
|
return
|
||
|
}
|
||
|
var postBody basementPost
|
||
|
json.Unmarshal(postBodyBytes, &postBody)
|
||
|
fmt.Fprintf(w, "MovieMode: %t", postBody.MovieMode)
|
||
|
movieMode(postBody.MovieMode)
|
||
|
})
|
||
|
|
||
|
http.HandleFunc("*", func(w http.ResponseWriter, r *http.Request) {
|
||
|
http.Error(w, "Not found", 404)
|
||
|
})
|
||
|
|
||
|
log.Fatal(http.ListenAndServe(":8081", nil))
|
||
|
}
|
||
|
|
||
|
func movieMode(desiredState bool) {
|
||
|
fmt.Fprintf(os.Stdout, "setting movieMode: %t", desiredState)
|
||
|
// addresses := readAddresses()
|
||
|
}
|
||
|
|
||
|
func readAddresses() controlData {
|
||
|
var rc controlData
|
||
|
bytes, err := ioutil.ReadFile("controlData.json")
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "could not read controlData.json: %s", err)
|
||
|
return rc
|
||
|
}
|
||
|
json.Unmarshal(bytes, &rc)
|
||
|
return rc
|
||
|
}
|
||
|
|
||
|
func scan() {
|
||
|
devices, err := wemodiscovery.Scan(wemodiscovery.DTAllBelkin, 2)
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "error during scan: %s", err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
for _, device := range devices {
|
||
|
device.Load(1 * time.Second)
|
||
|
fmt.Fprintf(os.Stdout, "Device %s: %s %s %s\n", device.Scan.DeviceId, device.Scan.Location, device.FriendlyName, device.Scan.Urn)
|
||
|
}
|
||
|
}
|