91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
|
package morningalarm
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"time"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
type Device struct {
|
||
|
Name string `json:"name"`
|
||
|
ID string `json:"id"`
|
||
|
}
|
||
|
|
||
|
func (ma *MorningAlarm) setupWebserver() {
|
||
|
ma.ro.POST("/api/alarm", func(c *gin.Context) {
|
||
|
var body alarm
|
||
|
|
||
|
if err := c.BindJSON(&body); err != nil {
|
||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
_, err := ma.addAlarm(body.Time, body.Name)
|
||
|
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||
|
} else {
|
||
|
err = ma.saveAlarms()
|
||
|
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusCreated, gin.H{})
|
||
|
}
|
||
|
})
|
||
|
|
||
|
ma.ro.GET("/api/info", func(c *gin.Context) {
|
||
|
spotifyUser, err := ma.sp.CurrentUser(c)
|
||
|
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
currentTime := time.Now()
|
||
|
zone, offset := currentTime.Zone()
|
||
|
|
||
|
nextIn := ma.nextAlarm()
|
||
|
|
||
|
response := gin.H{
|
||
|
"spotifyUser": spotifyUser.User.DisplayName,
|
||
|
"serverTime": currentTime.Format("15:04:05"),
|
||
|
"timezone": zone,
|
||
|
"timezoneOffsetInH": offset / 60 / 60,
|
||
|
"alarms": len(ma.cr.Entries()),
|
||
|
"wakeupContext": ma.config.PlaylistID,
|
||
|
"deviceId": ma.config.DeviceID,
|
||
|
}
|
||
|
|
||
|
if nextIn != nil {
|
||
|
response["nextAlarmAt"] = nextIn.String()
|
||
|
response["nextAlarmIn"] = nextIn.Sub(currentTime).String()
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, response)
|
||
|
})
|
||
|
|
||
|
ma.ro.GET("/api/device", func(c *gin.Context) {
|
||
|
devices, err := ma.getAvailableDevices()
|
||
|
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, devices)
|
||
|
})
|
||
|
|
||
|
ma.ro.POST("/api/trigger", func(c *gin.Context) {
|
||
|
if _, err := ma.playWakeUpMusic(); err != nil {
|
||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusOK, gin.H{})
|
||
|
})
|
||
|
}
|