final tweaks

This commit is contained in:
Emil Lerch 2019-07-27 16:22:36 -07:00
parent 683a3774c3
commit df42c35456
Signed by: lobo
GPG Key ID: CEC5F37C1BE5A481
2 changed files with 27 additions and 12 deletions

2
build
View File

@ -1,2 +1,2 @@
#!/bin/sh
CGO_ENABLED=0 go build
CGO_ENABLED=0 go build -ldflags="-s -w"

37
wemo.go
View File

@ -44,7 +44,9 @@ var client http.Client
func main() {
command = os.Getenv("CMD")
port := os.Getenv("PORT")
if port == "" { port = ":8081" } else { port = ":" + port }
logger.Infof("listening on port %s", port)
if len(os.Args) > 1 {
if os.Args[1] == "on" {
movieMode(true)
@ -57,17 +59,30 @@ func main() {
}
// POST /basement { movieMode: true } OR { movieMode: false }
http.HandleFunc("/basement", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
var postBody basementPost
switch r.Method {
case "GET":
query := r.URL.Query().Get("moviemode")
switch query {
case "on", "true", "1":
postBody.MovieMode = true
case "off", "false", "0":
postBody.MovieMode = false
default:
http.Error(w, "Missing query string", 400)
return
}
case "POST":
postBodyBytes, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Could not read body", 400)
return
}
json.Unmarshal(postBodyBytes, &postBody)
default:
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)
})
@ -76,7 +91,7 @@ func main() {
http.Error(w, "Not found", 404)
})
log.Fatal(http.ListenAndServe(":8081", nil))
log.Fatal(http.ListenAndServe(port, nil))
}
func movieMode(desiredState bool) {
@ -122,7 +137,7 @@ func commandDevice(url string, device deviceAction) {
logger.Tracef("%s: %d ms per step over %d seconds", device.Device, millisecondsPerTick, device.Seconds)
deltaPerTick := (device.End - device.Start) / (device.Steps - 1)
logger.Tracef("%s: %d change per command", device.Device, deltaPerTick)
currentValue := device.Start
currentValue := device.Start
currentSteps := 0
ticker := time.NewTicker(time.Duration(millisecondsPerTick) * time.Millisecond)
quit := make(chan struct{})