action.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package daemon
  2. import (
  3. "errors"
  4. "git.beejay.kim/WatchDog/ward/internal/download"
  5. "git.beejay.kim/WatchDog/ward/model"
  6. "git.beejay.kim/WatchDog/ward/platform"
  7. "git.beejay.kim/tool/service/config"
  8. "github.com/rs/zerolog/log"
  9. "github.com/urfave/cli/v2"
  10. "google.golang.org/protobuf/proto"
  11. "time"
  12. )
  13. var action = func(ctx *cli.Context) error {
  14. var (
  15. cfg = config.Get[Configuration](ctx)
  16. p platform.Platform
  17. downloader *download.HLS
  18. chMessage = make(chan proto.Message, 256)
  19. chStream chan []byte
  20. err error
  21. )
  22. defer func() {
  23. for i := range players {
  24. if players[i] == nil {
  25. continue
  26. }
  27. _ = players[i].Close()
  28. }
  29. close(chMessage)
  30. close(ctrlSignal)
  31. }()
  32. if cfg == nil {
  33. return errors.New("could not load Configuration")
  34. }
  35. if p, err = cfg.Platform.Detect(pid, bid); err != nil {
  36. return err
  37. }
  38. go func() {
  39. if err = p.Connect(chMessage); err != nil {
  40. exit <- err
  41. }
  42. }()
  43. if len(players) > 0 {
  44. if downloader, err = download.NewHLS(p.HLSStream, download.HLSOptions{
  45. Timeout: time.Second * 10,
  46. MaxAttempts: 9,
  47. }); err != nil {
  48. return err
  49. }
  50. for i := range players {
  51. if err = players[i].Start(exit); err != nil {
  52. return err
  53. }
  54. }
  55. chStream = downloader.Start()
  56. }
  57. for {
  58. select {
  59. case err := <-exit:
  60. return err
  61. case buff := <-chStream:
  62. if buff == nil {
  63. continue
  64. }
  65. for i := range players {
  66. if err = players[i].Write(buff); err != nil {
  67. return err
  68. }
  69. }
  70. case m := <-chMessage:
  71. switch t := m.(type) {
  72. case *model.Message:
  73. log.Info().
  74. Any("user", t.User).
  75. Str("text", t.Text).
  76. Bool("emoticon", t.Sticker).
  77. Msg("message")
  78. case *model.RosterChange:
  79. //TODO:
  80. case *model.Online:
  81. log.Info().
  82. Uint64("total", t.Total).
  83. Uint64("mobile", t.DeviceMobile).
  84. Uint64("pc", t.DevicePc).
  85. Msg("online")
  86. case *model.Donation:
  87. log.Info().
  88. Any("user", t.User).
  89. Uint64("amount", t.Amount).
  90. Msg("donation")
  91. default:
  92. log.Info().Any("data", m).Msg("message")
  93. }
  94. }
  95. }
  96. }