diff --git a/berkutschi/websocket.go b/berkutschi/websocket.go index 74c8bd2..bd25219 100644 --- a/berkutschi/websocket.go +++ b/berkutschi/websocket.go @@ -24,7 +24,7 @@ type Berkutschi struct { RX chan []byte } -func Init(event int) *Berkutschi { +func Init(event int, ctx context.Context) *Berkutschi { l := log.With().Int("event", event).Logger() b := &Berkutschi{ event: event, diff --git a/go.mod b/go.mod index c7c78e7..7b712f3 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,6 @@ module git.entr0py.de/garionion/berkutschi go 1.17 - require ( git.entr0py.de/garionion/gstreamer-graphix v0.0.0-20220205233405-935e7232de45 github.com/disintegration/imaging v1.6.2 @@ -19,8 +18,11 @@ require ( github.com/google/uuid v1.3.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/joho/godotenv v1.4.0 // indirect - github.com/klauspost/compress v1.10.3 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/compress v1.14.1 // indirect github.com/mattn/go-pointer v0.0.1 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/tinyzimmer/go-glib v0.0.24 // indirect github.com/tinyzimmer/go-gst v0.2.32 // indirect diff --git a/go.sum b/go.sum index 94da2fd..0df3c38 100644 --- a/go.sum +++ b/go.sum @@ -87,8 +87,11 @@ github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8= github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/klauspost/compress v1.14.1/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -105,6 +108,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/monaco-io/request v1.0.15 h1:krhHE0yL6yL+xP0YwX5udyd+xnfQfHccop3Nsixec54= github.com/monaco-io/request v1.0.15/go.mod h1:voq81GC2YDynQM3W/4D6oqGhqb+u7zULvbLzFkTmDfo= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= diff --git a/main.go b/main.go index 926be14..abc0a4c 100644 --- a/main.go +++ b/main.go @@ -2,11 +2,14 @@ package main import ( "context" - "encoding/json" "flag" "fmt" + "git.entr0py.de/garionion/berkutschi/client" + "git.entr0py.de/garionion/gstreamer-graphix/api" "github.com/ilyakaznacheev/cleanenv" + jsoniter "github.com/json-iterator/go" "github.com/rs/zerolog/pkgerrors" + "io/ioutil" "os" "strings" "time" @@ -17,17 +20,20 @@ import ( "git.entr0py.de/garionion/berkutschi/berkutschi" ) -const logFile = "my.log" - type Config struct { - GstServer string `yaml:"gstServer"` - ImagePath string `yaml:"imagePath" env-default:"./images"` - Event int `yaml:"event"` - LogFile string `yaml:"logFile"` + GstServer string `yaml:"gstServer"` + ImagePath string `yaml:"imagePath" env-default:"./images"` + PositionsPath string `yaml:"positionsPath" env-default:"./positions.json"` + Event int `yaml:"event"` + LogFile string `yaml:"logFile"` } +//TODO config struct thingy var Judges = [5]JugdeRaceInfo{} +var PositionsConfig = Positions{} var cfg Config +var json = jsoniter.ConfigCompatibleWithStandardLibrary +var apiClient *client.APIClient func main() { zerolog.TimeFieldFormat = time.RFC1123Z @@ -51,17 +57,27 @@ func main() { zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack } + //TODO: error handling + jsonFile, err := os.Open(cfg.PositionsPath) + byteValue, _ := ioutil.ReadAll(jsonFile) + jsoniter.Unmarshal(byteValue, PositionsConfig) + + apiClient = client.Init(cfg.GstServer) + pollData, err := berkutschi.Poll(cfg.Event) if err != nil { log.Panic().Err(fmt.Errorf("Error while polling: %s", err)).Send() } fillJudgeraceinfo(pollData) - b := berkutschi.Init(cfg.Event) + ctx, _ := context.WithCancel(context.Background()) + b := berkutschi.Init(cfg.Event, ctx) go func() { + jumperInfoMap := make(map[string]string) + jumperScoreMap := make(map[string]string) jumperInfo := JumperInfo{} - jumperscore := JumperScore{} + jumperScore := JumperScore{} for { message := <-b.RX var jumpMessage BerkutschiJumpUpdateMessages @@ -73,13 +89,51 @@ func main() { continue } if newJumperInfoData(jumperInfo, jumper) { - jumperInfo = jumper log.Info().Msgf("New jumper info: %+v", jumperInfo) + if jumper.Name == jumperInfo.Name { + overlays, _ := jumper.convertToAPIOverlays(PositionsConfig) + err = apiClient.UpdatePipeline(jumperInfoMap[jumper.Name], overlays) + if err != nil { + log.Error().Err(err).Msgf("Error while updating jumperinfo of %s pipeline", jumper.Name) + } + } else { + id, err := jumper.createPipeline() + if err != nil { + log.Error().Err(err).Msgf("Error while creating jumperInfo pipeline for %s", jumper.Name) + } + log.Info().Msgf("Created jumperInfo pipeline for %s", jumper.Name) + jumperInfoMap[jumper.Name] = id + err = apiClient.DeletePipeline(jumperInfoMap[jumperInfo.Name]) + if err != nil { + log.Error().Err(err).Msgf("Error while deleting pipeline of %s", jumperInfo.Name) + } + delete(jumperInfoMap, jumperInfo.Name) + } + jumperInfo = jumper } score, _ := fillJumperScore(jumpMessage) - if score.Rank != 0 && newJumperScoreData(jumperscore, score) { - jumperscore = score - log.Info().Msgf("New jumper score: %+v", jumperscore) + if score.Rank != 0 && newJumperScoreData(jumperScore, score) { + log.Info().Msgf("New jumper score: %+v", jumperScore) + if jumperScore.Name == score.Name { + overlays, _ := score.convertToAPIOverlays(PositionsConfig) + err = apiClient.UpdatePipeline(jumperScoreMap[jumperScore.Name], overlays) + if err != nil { + log.Error().Err(err).Msgf("Error while updating jumperScore of %s pipeline", jumperScore.Name) + } + } else { + id, err := score.createPipeline() + if err != nil { + log.Error().Err(err).Msgf("Error while creating jumperScore pipeline for %s", score.Name) + } + log.Info().Msgf("Created jumperScore pipeline for %s", score.Name) + jumperScoreMap[score.Name] = id + err = apiClient.DeletePipeline(jumperScoreMap[jumperScore.Name]) + if err != nil { + log.Error().Err(err).Msgf("Error while deleting pipeline of %s", jumperScore.Name) + } + delete(jumperScoreMap, jumperScore.Name) + } + jumperScore = score } } else if err != nil { log.Error().RawJSON("receivedMessage", message).Err(fmt.Errorf("%v", err)) @@ -91,12 +145,35 @@ func main() { } }() - ctx, _ := context.WithCancel(context.Background()) <-ctx.Done() } func (d JumperInfo) createPipeline() (string, error) { - overlays := d.convertToAPIOverlays() + overlays, err := d.convertToAPIOverlays(PositionsConfig) + if err != nil { + return "", err + } + pipeline := &api.Pipeline{ + Name: "jumperinfo", + Clip: "CoC_20_Jumper_Infos_Fast.raw", + Output: "decklinkvideosink", + Overlays: overlays, + } + return apiClient.CreatePipeline(pipeline) +} + +func (d JumperScore) createPipeline() (string, error) { + overlays, err := d.convertToAPIOverlays(PositionsConfig) + if err != nil { + return "", err + } + pipeline := &api.Pipeline{ + Name: "jumperscore", + Clip: "CoC_20_Jumper_Score_Fast.raw", + Output: "decklinkvideosink", + Overlays: overlays, + } + return apiClient.CreatePipeline(pipeline) } func fillJumperInfo(m BerkutschiJumpUpdateMessages) (JumperInfo, error) { @@ -128,7 +205,7 @@ func fillJumperScore(m BerkutschiJumpUpdateMessages) (JumperScore, error) { JumperScore.Rank = m[0].Data.Current.Cumul.Rank JumperScore.Wind = m[0].Data.Current.Wind.Wind JumperScore.Length = fmt.Sprintf("%vm", m[0].Data.Current.Length.Length) - for i, _ := range JumperScore.Judges { + for i := range JumperScore.Judges { switch i { case 0: JumperScore.Judges[i].Score = m[0].Data.Current.Judge.One.Rate @@ -157,7 +234,7 @@ func fillJumperScore(m BerkutschiJumpUpdateMessages) (JumperScore, error) { } func fillJudgeraceinfo(data berkutschi.PollData) { - for i, _ := range Judges { + for i := range Judges { switch i { case 0: Judges[i].Nation = strings.ToUpper(data.Data.Raceinfo.Judges.One.Nation)