2021-08-05 09:43:35 +02:00
|
|
|
package timer
|
|
|
|
|
|
|
|
import (
|
2021-08-09 10:49:04 +02:00
|
|
|
"context"
|
|
|
|
|
2021-08-05 09:43:35 +02:00
|
|
|
"github.com/golang/protobuf/ptypes/duration"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/cobra"
|
2021-08-09 10:49:04 +02:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.xsfx.dev/schnutibox/internal/config"
|
|
|
|
"go.xsfx.dev/schnutibox/internal/grpcclient"
|
2021-08-05 09:43:35 +02:00
|
|
|
api "go.xsfx.dev/schnutibox/pkg/api/v1"
|
2021-08-09 10:49:04 +02:00
|
|
|
"go.xsfx.dev/schnutibox/pkg/mpc"
|
|
|
|
"google.golang.org/protobuf/types/known/durationpb"
|
2021-08-05 09:43:35 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// nolint:gochecknoglobals
|
2021-08-09 10:49:04 +02:00
|
|
|
var T = &Timer{}
|
|
|
|
|
|
|
|
type Timer struct {
|
|
|
|
Req *api.Timer
|
|
|
|
}
|
2021-08-05 09:43:35 +02:00
|
|
|
|
2021-08-09 10:49:04 +02:00
|
|
|
func (t *Timer) Handle() {
|
|
|
|
if t.Req != nil {
|
2021-08-05 09:43:35 +02:00
|
|
|
// Initialize the current object.
|
2021-08-09 10:49:04 +02:00
|
|
|
if t.Req.Current == nil {
|
|
|
|
t.Req.Current = &duration.Duration{}
|
|
|
|
t.Req.Current.Seconds = t.Req.Duration.Seconds
|
2021-08-05 09:43:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// There is some timing going on.
|
2021-08-09 10:49:04 +02:00
|
|
|
case t.Req.Duration.Seconds != 0 && t.Req.Current.Seconds != 0:
|
2021-08-05 09:43:35 +02:00
|
|
|
log.Debug().
|
2021-08-09 10:49:04 +02:00
|
|
|
Int64("current", t.Req.Current.Seconds).
|
|
|
|
Int64("duration", t.Req.Duration.Seconds).
|
2021-08-05 09:43:35 +02:00
|
|
|
Msg("timer is running")
|
|
|
|
|
2021-08-09 10:49:04 +02:00
|
|
|
if t.Req.Current.Seconds > 0 {
|
|
|
|
t.Req.Current.Seconds -= 1
|
2021-08-05 09:43:35 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// No timer is running... so setting the duration to 0.
|
2021-08-09 10:49:04 +02:00
|
|
|
case t.Req.Current.Seconds == 0 && t.Req.Duration.Seconds != 0:
|
2021-08-05 09:43:35 +02:00
|
|
|
log.Debug().Msg("stoping timer")
|
|
|
|
|
2021-08-09 10:49:04 +02:00
|
|
|
if err := mpc.Stop(log.Logger); err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not stop")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Req.Duration.Seconds = 0
|
2021-08-05 09:43:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 10:49:04 +02:00
|
|
|
// Run is the command line interface for triggering the timer.
|
|
|
|
func Run(cmd *cobra.Command, args []string) {
|
2021-08-11 13:46:22 +02:00
|
|
|
conn, err := grpcclient.Conn(config.Cfg.Web.Hostname, config.Cfg.Web.Port)
|
2021-08-09 10:49:04 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("could not connect")
|
|
|
|
}
|
|
|
|
|
|
|
|
c := api.NewTimerServiceClient(conn)
|
|
|
|
|
|
|
|
d := durationpb.New(viper.GetDuration("timer.duration"))
|
|
|
|
|
|
|
|
_, err = c.Create(context.Background(), &api.Timer{Duration: d})
|
|
|
|
if err != nil {
|
2021-08-11 14:24:00 +02:00
|
|
|
conn.Close()
|
2021-08-09 10:49:04 +02:00
|
|
|
log.Fatal().Err(err).Msg("could not create timer")
|
|
|
|
}
|
|
|
|
|
2021-08-11 14:24:00 +02:00
|
|
|
conn.Close()
|
2021-08-09 10:49:04 +02:00
|
|
|
log.Info().Msg("added timer")
|
|
|
|
}
|