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 {
|
|
|
|
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"`
|
2023-09-25 23:07:01 +00:00
|
|
|
DeviceName string `json:"deviceName"`
|
2023-08-25 20:44:44 +00:00
|
|
|
PlaylistID string `json:"wakeupContext"`
|
|
|
|
RedirectURL string `json:"redirectUrl"`
|
|
|
|
ListenAddr string `json:"listenAddr"`
|
2023-09-05 19:01:24 +00:00
|
|
|
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()
|
|
|
|
ma.loadAlarms()
|
|
|
|
ma.cr.Start()
|
|
|
|
|
|
|
|
ma.ro = gin.Default()
|
|
|
|
|
2023-09-05 19:01:24 +00:00
|
|
|
if ma.config.OAuthProxyURL != "" {
|
|
|
|
ma.setupSpotifyWithProxy()
|
|
|
|
} else {
|
|
|
|
ma.setupSpotify()
|
|
|
|
}
|
2023-08-25 20:44:44 +00:00
|
|
|
|
|
|
|
ma.setupWebserver()
|
|
|
|
|
|
|
|
ma.ro.Run(ma.config.ListenAddr)
|
|
|
|
}
|