package daemon import ( "bytes" "errors" "fmt" "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" "io" "os" "path/filepath" "time" ) var action = func(ctx *cli.Context) error { var ( cfg = config.Get[Configuration](ctx) p platform.Platform downloader *download.HLS fOut *os.File exit = make(chan error) chMessage = make(chan proto.Message, 256) chStream chan []byte err error ) defer func() { if fOut != nil { _ = fOut.Close() } if chMessage != nil { close(chMessage) } if chStream != nil { close(chStream) } }() if cfg == nil { return errors.New("could not load Configuration") } if p, err = cfg.Platform.Detect(pid, bid); err != nil { return err } if fOut, err = os.OpenFile( filepath.Join(path, fmt.Sprintf("%s_%s.TS", bid, time.Now().String())), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666); err != nil { return err } go func() { if err = p.Connect(chMessage); err != nil { exit <- err } }() // usually HLS stream segments have 2s length; // if downloader, err = download.NewHLS(p.HLSStream, time.Second*2); err != nil { return err } chStream = downloader.Start() for { select { case err := <-exit: return err case buff := <-chStream: if buff == nil { go func() { <-time.After(time.Second * 5) chStream = downloader.Start() }() log.Info().Msg("stream has been closed; we arranged next retry attempt") continue } if _, err = io.Copy(fOut, bytes.NewBuffer(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") } } } }