package daemon import ( "errors" "git.beejay.kim/WatchDog/ward/internal/download" "git.beejay.kim/WatchDog/ward/model" "git.beejay.kim/WatchDog/ward/platform" "git.beejay.kim/tool/service/config" "github.com/rs/zerolog/log" "github.com/urfave/cli/v2" "google.golang.org/protobuf/proto" "time" ) var action = func(ctx *cli.Context) error { var ( cfg = config.Get[Configuration](ctx) p platform.Platform downloader *download.HLS chMessage = make(chan proto.Message, 256) chStream chan []byte err error ) defer func() { for i := range players { if players[i] == nil { continue } _ = players[i].Close() } close(chMessage) close(ctrlSignal) }() if cfg == nil { return errors.New("could not load Configuration") } if p, err = cfg.Platform.Detect(pid, bid); err != nil { return err } go func() { if err = p.Connect(chMessage); err != nil { exit <- err } }() if len(players) > 0 { if downloader, err = download.NewHLS(p.HLSStream, download.HLSOptions{ Timeout: time.Second * 10, MaxAttempts: 9, }); err != nil { return err } for i := range players { if err = players[i].Start(exit); err != nil { return err } } chStream = downloader.Start() } for { select { case err := <-exit: return err case buff := <-chStream: if buff == nil { continue } for i := range players { if err = players[i].Write(buff); err != nil { return err } } case m := <-chMessage: switch t := m.(type) { case *model.Message: log.Info(). Any("user", t.User). Str("text", t.Text). Bool("emoticon", t.Sticker). Msg("message") case *model.RosterChange: //TODO: case *model.Online: log.Info(). Uint64("total", t.Total). Uint64("mobile", t.DeviceMobile). Uint64("pc", t.DevicePc). Msg("online") case *model.Donation: log.Info(). Any("user", t.User). Uint64("amount", t.Amount). Msg("donation") default: log.Info().Any("data", m).Msg("message") } } } }