Raspberry Pi alarm clock server.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

120 lines
3.0 KiB

package sonar
import (
"html/template"
"net/http"
"time"
"github.com/gorilla/mux"
)
// Register the routes.
func (s *Sonar) Register() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
templ, err := template.ParseFiles("./www/index.gohtml")
if err != nil {
log.Error(err.Error())
}
flashes := s.GetFlashes(r, w)
v := map[string]interface{}{
"Flashes": flashes,
"Volume": s.Volume(),
"PlaylistStatus": s.Player.Status(),
// Current schedule
"Hour": s.Config.Hour,
"Minute": s.Config.Minute,
"Days": NewSet(s.Config.Days),
}
templ.ExecuteTemplate(w, "index.gohtml", v)
})
// API functions
POST := r.Methods("GET", "POST").Subrouter()
POST.HandleFunc("/playlist/start", func(w http.ResponseWriter, r *http.Request) {
err := s.StartPlaylist()
if err != nil {
log.Info("Start: %s", err.Error())
s.FlashAndRedirect(r, w, "/", "Error: "+err.Error())
} else {
s.FlashAndRedirect(r, w, "/", "Playlist started!")
}
})
POST.HandleFunc("/playlist/stop", func(w http.ResponseWriter, r *http.Request) {
err := s.StopPlaylist()
if err != nil {
s.FlashAndRedirect(r, w, "/", "Error: "+err.Error())
} else {
s.FlashAndRedirect(r, w, "/", "Playlist stopped!")
}
})
POST.HandleFunc("/playlist/next", func(w http.ResponseWriter, r *http.Request) {
err := s.PlayNext()
if err != nil {
s.FlashAndRedirect(r, w, "/", "Error: "+err.Error())
} else {
s.FlashAndRedirect(r, w, "/", "Playlist advanced!")
}
})
POST.HandleFunc("/playlist/schedule", func(w http.ResponseWriter, r *http.Request) {
var (
timestamp = r.FormValue("time")
days = r.Form["day"]
)
// Validate the inputs.
dt, err := time.Parse("15:04", timestamp)
if err != nil {
s.Abort(r, w, "Invalid time stamp.")
return
}
if len(days) == 0 {
s.Abort(r, w, "Select one or more days!")
return
}
// Save the intended schedule to our config on disk.
s.Config.Hour = dt.Hour()
s.Config.Minute = dt.Minute()
s.Config.Days = days
SaveConfig(s.Config)
// Save the crontab to disk.
err = s.SetSchedule(dt, days)
if err != nil {
s.Abort(r, w, err.Error())
} else {
s.FlashAndRedirect(r, w, "/", "Schedule saved!")
}
})
POST.HandleFunc("/volume/higher", func(w http.ResponseWriter, r *http.Request) {
err := s.VolumeUp()
if err != nil {
s.FlashAndRedirect(r, w, "/", "Error:"+err.Error())
} else {
s.FlashAndRedirect(r, w, "/", "Volume increased!")
}
})
POST.HandleFunc("/volume/lower", func(w http.ResponseWriter, r *http.Request) {
err := s.VolumeDown()
if err != nil {
s.FlashAndRedirect(r, w, "/", "Error:"+err.Error())
} else {
s.FlashAndRedirect(r, w, "/", "Volume lowered!")
}
})
POST.HandleFunc("/volume/mute", func(w http.ResponseWriter, r *http.Request) {
err := s.VolumeMute()
if err != nil {
s.FlashAndRedirect(r, w, "/", "Error:"+err.Error())
} else {
s.FlashAndRedirect(r, w, "/", "Volume mute toggled!")
}
})
return r
}