func main() { // nb. since we have a token, ignore all params client := nest.New("", "", "", "") client.Token = readToken() unit, err := acdash.NewDaikin(ACUnit) if err != nil { log.Fatalf("can't create unit: %v", err) } log.Printf("Streaming...") for devices := range stream(client) { t := devices.Thermostats[Device] if t == nil { log.Fatalf("couldn't find device: %s", Device) } c := acdash.ControlInfo{} target := float64(t.TargetTemperatureC) on := t.HvacState != "off" c.Power = on c.SetPoint = &target if t.HvacState == "heating" { c.Mode = acdash.ModeHeat } else if t.HvacState == "cooling" { c.Mode = acdash.ModeCool } else if on { log.Fatalf("expected state 'heating' or 'cooling', was: %v", t.HvacState) } log.Printf("sending control: %+v", c) unit.Set(&c, 0) } }
func httpAC(w http.ResponseWriter, r *http.Request) { switch r.Method { case "POST": mac := r.FormValue("mac") unit := router.ByMac(mac) if unit == nil { http.Error(w, "no unit found", http.StatusNotFound) return } var info lib.ControlInfo temp := *defaultTemp info.Power = true info.Mode = lib.ModeAuto info.SetPoint = &temp d, err := time.ParseDuration(r.FormValue("duration")) if err == nil && d <= 0 { info.Power = false d = 0 } else if err != nil { d = *defaultDuration } err = unit.Set(&info, d) if err != nil { http.Error(w, fmt.Sprintf("got err: %v", err), http.StatusTeapot) } else { fmt.Fprintf(w, "OK") } case "GET": out := router.All() json.NewEncoder(w).Encode(out) default: http.Error(w, "", http.StatusMethodNotAllowed) } }