Example #1
0
func showValve(w http.ResponseWriter, r *http.Request) {
	valve, err := models.GetValveById(helpers.Int32ValueFrom(r.URL.Query().Get(":valveId"), -1))

	if err != nil {
		log.Println(err)
		http.Redirect(w, r, "/", 302)
		return
	}

	if valve == nil {
		http.Redirect(w, r, "/", 302)
		return
	}

	schedules, err := models.GetSchedulesForValve(valve)

	if err != nil {
		log.Println(err)
		http.Redirect(w, r, "/", 302)
	}

	err = templates["showValve"].Execute(w, map[string]interface{}{
		"Schedules": schedules,
		"Valve":     valve,
	})
	return
}
Example #2
0
func closeValve(w http.ResponseWriter, r *http.Request) {
	valve, err := models.GetValveById(helpers.Int32ValueFrom(r.URL.Query().Get(":valveId"), -1))

	if err == nil && valve != nil {
		valve.Close()

	}
	http.Redirect(w, r, "/manual", 302)
	return
}
Example #3
0
func createSchedule(w http.ResponseWriter, r *http.Request) {
	var schedule *models.Schedule
	var valve *models.Valve
	var err error

	valve, err = models.GetValveById(helpers.Int32ValueFrom(r.PostFormValue("valve"), -1))

	if err != nil {
		goto Error
	}

	schedule = &models.Schedule{
		ValveId: valve.Id,
		Active:  true,
	}

	err = schedule.SetInterval(
		r.PostFormValue("interval[multiplicator]"),
		r.PostFormValue("interval[denominator]"))

	if err != nil {
		goto Error
	}

	err = schedule.SetLength(
		r.PostFormValue("length[multiplicator]"),
		r.PostFormValue("length[denominator]"))

	if err != nil {
		goto Error
	}

	err = schedule.SetStart(r.PostFormValue("date"))
	if err != nil {
		goto Error
	}

	err = db.Orm().Insert(schedule)
	if err != nil {
		goto Error
	}

	http.Redirect(w, r, "/", 302)
	return

Error:
	tmpl_err := templates["error"].Execute(w, err)
	if tmpl_err != nil {
		log.Panicln(err)
	}

}