final tweaks
This commit is contained in:
parent
683a3774c3
commit
df42c35456
2
build
2
build
|
@ -1,2 +1,2 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
CGO_ENABLED=0 go build
|
CGO_ENABLED=0 go build -ldflags="-s -w"
|
||||||
|
|
37
wemo.go
37
wemo.go
|
@ -44,7 +44,9 @@ var client http.Client
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
command = os.Getenv("CMD")
|
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 len(os.Args) > 1 {
|
||||||
if os.Args[1] == "on" {
|
if os.Args[1] == "on" {
|
||||||
movieMode(true)
|
movieMode(true)
|
||||||
|
@ -57,17 +59,30 @@ func main() {
|
||||||
}
|
}
|
||||||
// POST /basement { movieMode: true } OR { movieMode: false }
|
// POST /basement { movieMode: true } OR { movieMode: false }
|
||||||
http.HandleFunc("/basement", func(w http.ResponseWriter, r *http.Request) {
|
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)
|
http.Error(w, "Not found", 404)
|
||||||
return
|
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)
|
fmt.Fprintf(w, "MovieMode: %t", postBody.MovieMode)
|
||||||
movieMode(postBody.MovieMode)
|
movieMode(postBody.MovieMode)
|
||||||
})
|
})
|
||||||
|
@ -76,7 +91,7 @@ func main() {
|
||||||
http.Error(w, "Not found", 404)
|
http.Error(w, "Not found", 404)
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(":8081", nil))
|
log.Fatal(http.ListenAndServe(port, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func movieMode(desiredState bool) {
|
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)
|
logger.Tracef("%s: %d ms per step over %d seconds", device.Device, millisecondsPerTick, device.Seconds)
|
||||||
deltaPerTick := (device.End - device.Start) / (device.Steps - 1)
|
deltaPerTick := (device.End - device.Start) / (device.Steps - 1)
|
||||||
logger.Tracef("%s: %d change per command", device.Device, deltaPerTick)
|
logger.Tracef("%s: %d change per command", device.Device, deltaPerTick)
|
||||||
currentValue := device.Start
|
currentValue := device.Start
|
||||||
currentSteps := 0
|
currentSteps := 0
|
||||||
ticker := time.NewTicker(time.Duration(millisecondsPerTick) * time.Millisecond)
|
ticker := time.NewTicker(time.Duration(millisecondsPerTick) * time.Millisecond)
|
||||||
quit := make(chan struct{})
|
quit := make(chan struct{})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user