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) } }
func TestSchedulesCreation(t *testing.T) { valve, _ := models.GetValveByRelayId(17) schedule := &models.Schedule{ ValveId: valve.Id, Length: 3, Interval: 2, Active: true, } schedule.SetStart(time.Now().Add(time.Second * 5).String()) db.Orm().Insert(schedule) }
func TestValvesAreUnique(t *testing.T) { query := "select v.RelayId, v.Name " + "from valves v " + "where v.RelayId = 17" valves, err := db.Orm().Select(models.Valve{}, query) if err != nil || len(valves) != 1 { t.Fail() t.Log(valves) t.Log(err) } }
func FirstValveOrCreate(relay int) *Valve { var valve *Valve query := "select v.Id, v.RelayId, v.Name " + "from valves v " + "where v.RelayId = ?" instances, err := db.Orm().Select(Valve{}, query, relay) if err != nil { log.Fatalln(err) } if len(instances) == 0 { valve = &Valve{ RelayId: relay, } db.Orm().Insert(valve) } else { valve = instances[0].(*Valve) } return valve }
func GetValveByRelayId(id int) (valve *Valve, err error) { query := "select v.Id, v.RelayId, v.Name " + "from valves v " + "where v.RelayId = ?" instances, err := db.Orm().Select(Valve{}, query, id) if err != nil { return nil, err } if len(instances) == 0 { return nil, nil } else { return instances[0].(*Valve), nil } }
func updateValve(w http.ResponseWriter, r *http.Request) { id, err := strconv.Atoi(r.URL.Query().Get(":valveId")) if err != nil { log.Println(err) http.Redirect(w, r, "/", 302) } valve, err := models.GetValveById(int32(id)) if err == nil { valve.Name = r.PostFormValue("name") db.Orm().Update(valve) } http.Redirect(w, r, "/", 302) return }
func GetSchedulesForValve(valve *Valve) (schedules []*Schedule, err error) { query := "select * " + "from schedules s " + "where s.ValveId = ?" instances, err := db.Orm().Select(Schedule{}, query, valve.Id) if err != nil { return nil, err } schedules = make([]*Schedule, len(instances)) for i := range instances { schedules[i] = instances[i].(*Schedule) } return schedules, err }
func updateSchedule(w http.ResponseWriter, r *http.Request) { id, err := strconv.Atoi(r.URL.Query().Get(":scheduleId")) if err != nil { log.Println(err) } schedule, err := models.GetScheduleById(int32(id)) err = schedule.SetInterval( r.PostFormValue("interval[multiplicator]"), r.PostFormValue("interval[denominator]")) if err != nil { log.Println(err) return } err = schedule.SetLength( r.PostFormValue("length[multiplicator]"), r.PostFormValue("length[denominator]")) if err != nil { log.Println(err) return } err = schedule.SetStart(r.PostFormValue("date")) if err != nil { log.Println(err) } schedule.SetActive(r.PostFormValue("status")) db.Orm().Update(schedule) valve, err := schedule.Valve() if err != nil { http.Redirect(w, r, "/", 302) return } http.Redirect(w, r, "/valves/"+strconv.Itoa(int(valve.Id)), 302) return }
func scheduledToLaunch() []*models.Schedule { active_at := time.Now().Truncate(time.Second).Unix() query := "select *" + "from schedules s " + "where s.Active = 1 and " + "( s.Start = ? OR ( (strftime('%s', 'now') - strftime(s.Start)) % s.Interval) = 0)" instances, err := db.Orm().Select(models.Schedule{}, query, active_at) if err != nil { log.Fatalln(err) } schedules := make([]*models.Schedule, len(instances)) for i, instance := range instances { schedules[i] = instance.(*models.Schedule) } return schedules }
func TestSchedulerManageSchedules(t *testing.T) { query := "select s.Id, s.ValveId, s.Active, s.Start, s.Interval, s.Length " + "from schedules s " + "where s.Active = 1 " instances, err := db.Orm().Select(models.Schedule{}, query) if err != nil { t.Fail() t.Log(err) } schedules := make([]*models.Schedule, len(instances)) for i, instance := range instances { schedules[i] = instance.(*models.Schedule) } time.Sleep(20 * time.Second) }
func LaunchScheduledEntries(list []*models.Schedule) { for i := 0; i < len(list); i++ { schedule := list[i] valve, err := schedule.Valve() if err != nil { log.Fatalln(err) } entry := &models.Entry{ Length: schedule.Length, ValveId: valve.Id, } err = db.Orm().Insert(entry) if err != nil { log.Fatalln(err) } } }
func GetScheduleById(id int32) (schedule *Schedule, err error) { query := "select * " + "from schedules s " + "where s.Id = ?" instances, err := db.Orm().Select(Schedule{}, query, id) if err != nil { return nil, err } schedules := make([]*Schedule, len(instances)) for i := range instances { schedules[i] = instances[i].(*Schedule) } if len(schedules) > 0 { return schedules[0], err } else { return nil, err } }
func RegisterEntry() { db.Orm().AddTableWithName(Entry{}, "entries").SetKeys(true, "Id") }
func RegisterSchedule() { db.Orm().AddTableWithName(Schedule{}, "schedules").SetKeys(true, "Id") }
func RegisterValve() { db.Orm().AddTableWithName(Valve{}, "valves").SetKeys(true, "Id") }