194 lines
6.2 KiB
Go
194 lines
6.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"github.com/ilyakaznacheev/cleanenv"
|
|
"github.com/rs/zerolog/pkgerrors"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"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"`
|
|
}
|
|
|
|
var Judges = [5]JugdeRaceInfo{}
|
|
var cfg Config
|
|
|
|
func main() {
|
|
zerolog.TimeFieldFormat = time.RFC1123Z
|
|
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC1123Z}
|
|
log.Logger = log.Output(consoleWriter).With().Timestamp().Caller().Logger()
|
|
|
|
if err := cleanenv.ReadConfig("config.toml", &cfg); err != nil {
|
|
log.Fatal().Msgf("No configfile: ", err)
|
|
}
|
|
|
|
fset := flag.NewFlagSet("config", flag.ContinueOnError)
|
|
fset.Usage = cleanenv.FUsage(fset.Output(), &cfg, nil, fset.Usage)
|
|
fset.Parse(os.Args[1:])
|
|
|
|
if cfg.LogFile != "" {
|
|
l, _ := os.OpenFile(cfg.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0664)
|
|
multi := zerolog.MultiLevelWriter(consoleWriter, l)
|
|
log.Logger = zerolog.New(multi).With().Timestamp().Caller().Logger()
|
|
}
|
|
if log.Logger.GetLevel() == zerolog.DebugLevel {
|
|
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
|
|
}
|
|
|
|
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)
|
|
|
|
go func() {
|
|
jumperInfo := JumperInfo{}
|
|
jumperscore := JumperScore{}
|
|
for {
|
|
message := <-b.RX
|
|
var jumpMessage BerkutschiJumpUpdateMessages
|
|
err := json.Unmarshal(message, &jumpMessage)
|
|
//log.Debug().Msgf("Received message: %v", string(message))
|
|
if err == nil && jumpMessage[0].Data.Current.Lastname != "" {
|
|
jumper, err := fillJumperInfo(jumpMessage)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if newJumperInfoData(jumperInfo, jumper) {
|
|
jumperInfo = jumper
|
|
log.Info().Msgf("New jumper info: %+v", jumperInfo)
|
|
}
|
|
score, _ := fillJumperScore(jumpMessage)
|
|
if score.Rank != 0 && newJumperScoreData(jumperscore, score) {
|
|
jumperscore = score
|
|
log.Info().Msgf("New jumper score: %+v", jumperscore)
|
|
}
|
|
} else if err != nil {
|
|
log.Error().RawJSON("receivedMessage", message).Err(fmt.Errorf("%v", err))
|
|
} else {
|
|
if jumpMessage[0].Data.Next.Lastname == "" {
|
|
log.Debug().RawJSON("receivedMessage", message).Msg("Received message i cant decode")
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
|
|
ctx, _ := context.WithCancel(context.Background())
|
|
<-ctx.Done()
|
|
}
|
|
|
|
func (d JumperInfo) createPipeline() (string, error) {
|
|
overlays := d.convertToAPIOverlays()
|
|
}
|
|
|
|
func fillJumperInfo(m BerkutschiJumpUpdateMessages) (JumperInfo, error) {
|
|
var err error
|
|
JumperInfo := JumperInfo{}
|
|
if m[0].Data.Current.Firstname == "" || m[0].Data.Current.Lastname == "" {
|
|
err = fmt.Errorf("Name is empty")
|
|
return JumperInfo, err
|
|
}
|
|
JumperInfo.Name = fmt.Sprint(m[0].Data.Current.Firstname, " ", strings.ToUpper(m[0].Data.Current.Lastname))
|
|
JumperInfo.Bib = m[0].Data.Current.Bib
|
|
JumperInfo.Nation = strings.ToUpper(m[0].Data.Current.Nat)
|
|
JumperInfo.Image = m[0].Data.Current.Image2
|
|
|
|
return JumperInfo, err
|
|
}
|
|
|
|
func fillJumperScore(m BerkutschiJumpUpdateMessages) (JumperScore, error) {
|
|
var err error
|
|
JumperScore := JumperScore{}
|
|
if m[0].Data.Current.Firstname == "" || m[0].Data.Current.Lastname == "" {
|
|
err = fmt.Errorf("Name is empty")
|
|
return JumperScore, err
|
|
}
|
|
JumperScore.Name = fmt.Sprint(m[0].Data.Current.Firstname, " ", strings.ToUpper(m[0].Data.Current.Lastname))
|
|
JumperScore.Bib = m[0].Data.Current.Bib
|
|
JumperScore.Nation = strings.ToUpper(m[0].Data.Current.Nat)
|
|
JumperScore.Points = m[0].Data.Current.Cumul.Points
|
|
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 {
|
|
switch i {
|
|
case 0:
|
|
JumperScore.Judges[i].Score = m[0].Data.Current.Judge.One.Rate
|
|
JumperScore.Judges[i].Nation = Judges[i].Nation
|
|
JumperScore.Judges[i].Discard = m[0].Data.Current.Judge.One.Discard
|
|
case 1:
|
|
JumperScore.Judges[i].Score = m[0].Data.Current.Judge.Two.Rate
|
|
JumperScore.Judges[i].Nation = Judges[i].Nation
|
|
JumperScore.Judges[i].Discard = m[0].Data.Current.Judge.Two.Discard
|
|
case 2:
|
|
JumperScore.Judges[i].Score = m[0].Data.Current.Judge.Three.Rate
|
|
JumperScore.Judges[i].Nation = Judges[i].Nation
|
|
JumperScore.Judges[i].Discard = m[0].Data.Current.Judge.Three.Discard
|
|
case 3:
|
|
JumperScore.Judges[i].Score = m[0].Data.Current.Judge.Four.Rate
|
|
JumperScore.Judges[i].Nation = Judges[i].Nation
|
|
JumperScore.Judges[i].Discard = m[0].Data.Current.Judge.Four.Discard
|
|
case 4:
|
|
JumperScore.Judges[i].Score = m[0].Data.Current.Judge.Five.Rate
|
|
JumperScore.Judges[i].Nation = Judges[i].Nation
|
|
JumperScore.Judges[i].Discard = m[0].Data.Current.Judge.Five.Discard
|
|
}
|
|
}
|
|
|
|
return JumperScore, err
|
|
}
|
|
|
|
func fillJudgeraceinfo(data berkutschi.PollData) {
|
|
for i, _ := range Judges {
|
|
switch i {
|
|
case 0:
|
|
Judges[i].Nation = strings.ToUpper(data.Data.Raceinfo.Judges.One.Nation)
|
|
case 1:
|
|
Judges[i].Nation = strings.ToUpper(data.Data.Raceinfo.Judges.Two.Nation)
|
|
case 2:
|
|
Judges[i].Nation = strings.ToUpper(data.Data.Raceinfo.Judges.Three.Nation)
|
|
case 3:
|
|
Judges[i].Nation = strings.ToUpper(data.Data.Raceinfo.Judges.Four.Nation)
|
|
case 4:
|
|
Judges[i].Nation = strings.ToUpper(data.Data.Raceinfo.Judges.Five.Nation)
|
|
}
|
|
}
|
|
}
|
|
|
|
func newJumperInfoData(oldData, newData JumperInfo) bool {
|
|
if oldData.Name != newData.Name || oldData.Bib != newData.Bib || oldData.Nation != newData.Nation || oldData.Image != newData.Image {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
func newJumperScoreData(oldData, newData JumperScore) bool {
|
|
if oldData.Name != newData.Name || oldData.Bib != newData.Bib || oldData.Nation != newData.Nation ||
|
|
oldData.Points != newData.Points || oldData.Rank != newData.Rank || oldData.Wind != newData.Wind || oldData.Length != newData.Length {
|
|
return true
|
|
}
|
|
for i, oldJudge := range oldData.Judges {
|
|
if oldJudge.Score != newData.Judges[i].Score || oldJudge.Nation != newData.Judges[i].Nation || oldJudge.Discard != newData.Judges[i].Discard {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|