schnutibox/pkg/timer/timer.go

80 lines
1.8 KiB
Go
Raw Normal View History

2021-08-05 09:43:35 +02:00
package timer
import (
"context"
2021-08-05 09:43:35 +02:00
"github.com/golang/protobuf/ptypes/duration"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"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"
"go.xsfx.dev/schnutibox/pkg/mpc"
"google.golang.org/protobuf/types/known/durationpb"
2021-08-05 09:43:35 +02:00
)
2021-08-11 14:24:29 +02:00
// T is the global Timer object.
2021-08-05 09:43:35 +02:00
// nolint:gochecknoglobals
var T = &Timer{}
type Timer struct {
Req *api.Timer
}
2021-08-05 09:43:35 +02:00
func (t *Timer) Handle() {
if t.Req != nil {
2021-08-05 09:43:35 +02:00
// Initialize the current object.
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.
case t.Req.Duration.Seconds != 0 && t.Req.Current.Seconds != 0:
2021-08-05 09:43:35 +02:00
log.Debug().
Int64("current", t.Req.Current.Seconds).
Int64("duration", t.Req.Duration.Seconds).
2021-08-05 09:43:35 +02:00
Msg("timer is running")
if t.Req.Current.Seconds > 0 {
2021-08-11 14:24:29 +02:00
t.Req.Current.Seconds--
2021-08-05 09:43:35 +02:00
return
}
// No timer is running... so setting the duration to 0.
case t.Req.Current.Seconds == 0 && t.Req.Duration.Seconds != 0:
2021-08-05 09:43:35 +02:00
log.Debug().Msg("stoping timer")
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
}
}
}
// Run is the command line interface for triggering the timer.
func Run(cmd *cobra.Command, args []string) {
conn, err := grpcclient.Conn(config.Cfg.Web.Hostname, config.Cfg.Web.Port)
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()
log.Fatal().Err(err).Msg("could not create timer")
}
2021-08-11 14:24:00 +02:00
conn.Close()
log.Info().Msg("added timer")
}