morningalarm/internal/morningalarm.go
2023-08-25 22:44:44 +02:00

51 lines
1018 B
Go

package morningalarm
import (
"github.com/gin-gonic/gin"
"github.com/robfig/cron/v3"
"github.com/zmb3/spotify/v2"
)
type alarm struct {
Name string `json:"name" binding:"required"`
Time string `json:"time" binding:"required"`
id cron.EntryID
}
type MorningAlarmConfig struct {
SpotifyClientID string `json:"spotifyClientId"`
SpotifyClientSecret string `json:"spotifyClientSecret"`
DeviceID string `json:"deviceId"`
PlaylistID string `json:"wakeupContext"`
RedirectURL string `json:"redirectUrl"`
ListenAddr string `json:"listenAddr"`
}
type MorningAlarm struct {
config MorningAlarmConfig
sp *spotify.Client
cr *cron.Cron
ro *gin.Engine
alarms []alarm
}
func New(config MorningAlarmConfig) *MorningAlarm {
return &MorningAlarm{
config: config,
}
}
func (ma *MorningAlarm) Start() {
ma.cr = cron.New()
ma.loadAlarms()
ma.cr.Start()
ma.ro = gin.Default()
ma.setupSpotify()
ma.setupWebserver()
ma.ro.Run(ma.config.ListenAddr)
}