Ejemplo n.º 1
0
func (lc LightController) LightPower(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	lightId, err := strconv.Atoi(p.ByName("id"))
	if !utils.LogError(err) {

		var light models.Light
		body, err := ioutil.ReadAll(r.Body)
		fmt.Println("state body:", string(body))
		err = json.Unmarshal(body, &light)
		if !utils.LogError(err) {
			active := "OFF"
			if light.Active {
				active = "ON"
			}

			args := []string{"-u", "-m" + strconv.Itoa(lightId), "-t1", "-v" + active}
			cmd := exec.Command("/usr/sbin/aprontest", args...)
			err = cmd.Run()
			if !utils.LogError(err) {
				w.WriteHeader(200)
			} else {
				fmt.Fprintf(w, "Error: %s", err.Error())
			}
		}
	}
}
Ejemplo n.º 2
0
func (lc LightController) SetName(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	lightId, err := strconv.Atoi(p.ByName("id"))
	if !utils.LogError(err) {
		var light models.Light
		body, err := ioutil.ReadAll(r.Body)
		fmt.Println("state body:", string(body))
		err = json.Unmarshal(body, &light)
		if !utils.LogError(err) {
			args := []string{"-m", strconv.Itoa(lightId), "--set-name", light.Username}
			utils.RunCommand(w, "/usr/sbin/aprontest", args)
		}
	}
}
Ejemplo n.º 3
0
func (lc LEDController) HandleLED(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	var colors models.LED
	body, err := ioutil.ReadAll(r.Body)
	err = json.Unmarshal(body, &colors)
	if !utils.LogError(err) {
		args := []string{strconv.Itoa(colors.Red), strconv.Itoa(colors.Green), strconv.Itoa(colors.Blue)}
		cmd := exec.Command("/usr/sbin/set_rgb", args...)
		err = cmd.Run()
		if !utils.LogError(err) {
			w.WriteHeader(200)
		} else {
			fmt.Fprintf(w, "Error: %s", err.Error())
		}
	}
}
Ejemplo n.º 4
0
func (lc LightController) RemoveLight(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	lightId, err := strconv.Atoi(p.ByName("id"))
	if !utils.LogError(err) {
		args := []string{"-d", "-m", strconv.Itoa(lightId)}
		utils.RunCommand(w, "/usr/sbin/aprontest", args)
	}
}