morningalarm/internal/morningalarm.go

54 lines
1.0 KiB
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"`
Disabled bool `json:"disabled"`
cronID cron.EntryID
}
type MorningAlarmConfig struct {
DeviceName string `json:"deviceName"`
PlaylistID string `json:"wakeupContext"`
ListenAddr string `json:"listenAddr"`
OAuthProxyURL string `json:"oauthProxyUrl"`
OAuthProxyAPIKey string `json:"oauthProxyApiKey"`
}
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.loadAlarmsFromFile()
ma.armAllAlarms()
ma.cr.Start()
ma.ro = gin.Default()
ma.setupSpotifyWithProxy()
ma.setupWebserver()
ma.ro.Run(ma.config.ListenAddr)
}