Beispiel #1
0
func RunCommand(w http.ResponseWriter, command string, args []string) {
	out, err := exec.Command(command, args...).Output()
	res := models.Response{Result: string(out), Status: 200}
	if err != nil {
		res.Status = 500
		res.Error = err.Error()
	}
	res.Respond(w)
}
Beispiel #2
0
func (lc LightController) CancelTimer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	var res models.Response
	if lightId, err := strconv.Atoi(p.ByName("id")); err == nil {
		res = models.Response{Status: 200, Result: "Canceling Timer for " + p.ByName("id")}
		fmt.Printf("Canceling timer for %d", lightId)
		go timers[lightId].Stop()
		res.Respond(w)
	} else {
		res = models.Response{Status: 400, Error: err.Error()}
		res.Respond(w)
	}
}
Beispiel #3
0
func (lc LightController) Timer(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	if lightId, err := strconv.Atoi(p.ByName("id")); err == nil {
		var light models.Light
		body, err := ioutil.ReadAll(r.Body)
		if err != nil {
			res := models.Response{Status: 404, Error: err.Error()}
			res.Respond(w)
		}
		fmt.Println("state body:", string(body))
		err = json.Unmarshal(body, &light)
		if err != nil {
			res := models.Response{Status: 400, Error: err.Error()}
			res.Respond(w)
			return
		}

		var state string
		if light.Active == true {
			state = "-vON"
		} else if light.Active == false {
			state = "-vOFF"
		}

		args := []string{"-u", "-m" + strconv.Itoa(lightId), "-t1", state}
		countDown := light.CountDownInSeconds
		if countDown > 0 {
			fmt.Printf("Setting %d to %s in %d seconds\n", lightId, state, countDown)
			go timer(lightId, args, time.Duration(countDown))
			res := models.Response{Status: 200}
			res.Respond(w)
			return
		} else {
			res := models.Response{Status: 400, Error: "Time must be > 0"}
			res.Respond(w)
			return
		}
	} else {
		res := models.Response{Status: 400, Error: err.Error()}
		res.Respond(w)
	}
}