morningalarm/internal/morningalarm.go

54 lines
1.0 KiB
Go
Raw Permalink Normal View History

2023-08-25 20:44:44 +00:00
package morningalarm
import (
"github.com/gin-gonic/gin"
"github.com/robfig/cron/v3"
"github.com/zmb3/spotify/v2"
)
type alarm struct {
2023-10-02 17:54:00 +00:00
Name string `json:"name" binding:"required"`
Time string `json:"time" binding:"required"`
Disabled bool `json:"disabled"`
cronID cron.EntryID
2023-08-25 20:44:44 +00:00
}
type MorningAlarmConfig struct {
DeviceName string `json:"deviceName"`
PlaylistID string `json:"wakeupContext"`
ListenAddr string `json:"listenAddr"`
OAuthProxyURL string `json:"oauthProxyUrl"`
OAuthProxyAPIKey string `json:"oauthProxyApiKey"`
2023-08-25 20:44:44 +00:00
}
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()
2023-10-02 17:54:00 +00:00
ma.loadAlarmsFromFile()
ma.armAllAlarms()
2023-08-25 20:44:44 +00:00
ma.cr.Start()
ma.ro = gin.Default()
ma.setupSpotifyWithProxy()
2023-08-25 20:44:44 +00:00
ma.setupWebserver()
ma.ro.Run(ma.config.ListenAddr)
}