From 88c438aa491a1bc8771fc5551aa87ca8b2fd4577 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Sat, 29 Jul 2023 16:37:11 -0300 Subject: [PATCH 1/6] chore: :zap: adds internal logger Signed-off-by: Lucas Vieira --- logger/logger.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 logger/logger.go diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000..38b2b98 --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,49 @@ +package logger + +import ( + "fmt" + "log" + "os" +) + +var debug bool = false +var prefix string = "wartelemetry" +var internalLogger *log.Logger + +func EnableDebug() { + internalLogger = log.New(os.Stdout, "", log.Lshortfile) + internalLogger.SetFlags(0) + debug = true +} + +func DisableLogger() { + debug = false +} + +func LogInfo(format string, args ...any) { + if debug { + fmt.Printf("%v:INFO ", prefix) + internalLogger.Printf(format, args...) + } +} + +func LogError(format string, args ...any) { + if debug { + fmt.Printf("%v:ERROR ", prefix) + internalLogger.Printf(format, args...) + } +} + +func LogSuccess(format string, args ...any) { + if debug { + fmt.Printf("%v:SUCCESS ", prefix) + internalLogger.Printf(format, args...) + } +} + +func LogCritical(format string, args ...any) { + if debug { + fmt.Printf("%v:CRITICAL ", prefix) + internalLogger.Printf(format, args...) + } +} From 2b7ac0ef432a6e5766a2ca4b9920a829bbc5a7d3 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Sat, 29 Jul 2023 16:37:50 -0300 Subject: [PATCH 2/6] chore: :zap: improves error checking Signed-off-by: Lucas Vieira --- network/http/client.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/network/http/client.go b/network/http/client.go index 24dfe78..afdfbfe 100644 --- a/network/http/client.go +++ b/network/http/client.go @@ -1,16 +1,18 @@ package network import ( + "fmt" "io" - "log" "net/http" + + "github.com/lucasvmx/WarTelemetry/logger" ) // ret function sends a GET request to the specified URL func get(url string) (response *http.Response, err error) { response, err = http.Get(url) if err != nil { - log.Printf("[ERROR] Failed to send request because of: %v", err) + logger.LogError("Failed to send request because of: %v", err) return nil, err } @@ -36,6 +38,10 @@ func GetDataFromURL(url string) (data []byte, err error) { return nil, err } + if payload.StatusCode != http.StatusOK { + return nil, fmt.Errorf("received unexpected HTTP code: %v", payload.StatusCode) + } + // Reads the HTTP response body data, err = readResponse(payload) if err != nil { From 3bb626ef7fce11893e13024e20312be6cc232104 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Sat, 29 Jul 2023 16:38:19 -0300 Subject: [PATCH 3/6] refactor: :recycle: improves coding format and error detection Signed-off-by: Lucas Vieira --- telemetry/telemetry.go | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/telemetry/telemetry.go b/telemetry/telemetry.go index 587cb00..147d3a5 100644 --- a/telemetry/telemetry.go +++ b/telemetry/telemetry.go @@ -1,22 +1,35 @@ package telemetry import ( - "log" + "fmt" "sync" "github.com/lucasvmx/WarTelemetry/controller" + "github.com/lucasvmx/WarTelemetry/logger" "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/utils" ) var wg *sync.WaitGroup +var ( + collectors = map[string]func(*sync.WaitGroup){ + "gamechat": controller.GetGamechatData, + "indicators": controller.GetIndicatorsData, + "map_info": controller.GetMapInfoData, + "hud_msg": controller.GetHudMessagesData, + "map_obj": controller.GetMapObjsData, + "state": controller.GetStateData, + "mission": controller.GetMissionData, + } +) + // Initialize function setup the library to be used func Initialize(hostname string) { if len(hostname) > 0 { utils.SetHostname(hostname) } else { - log.Printf("[INFO] Hostname omitted. Using localhost") + logger.LogInfo("Hostname omitted. Using localhost") } model.SetupTelemetry() @@ -24,14 +37,13 @@ func Initialize(hostname string) { } func getTelemetryData() { - wg.Add(7) - go controller.GetGamechatData(wg) - go controller.GetIndicatorsData(wg) - go controller.GetMapInfoData(wg) - go controller.GetHudMessagesData(wg) - go controller.GetMapObjsData(wg) - go controller.GetStateData(wg) - go controller.GetMissionData(wg) + + for name, callback := range collectors { + logger.LogInfo("running task go get %v data", name) + wg.Add(1) + go callback(wg) + } + wg.Wait() } @@ -51,5 +63,13 @@ func GetTelemetryData() (data *model.TelemetryData, err error) { data.MapObjects = model.GetInstance().MapObjects data.MissionInfo = model.GetInstance().MissionInfo + if data.MapInfo == nil || data.Gamechat == nil || + data.Indicators == nil || data.State == nil || + data.HudMessages == nil || data.MapObjects == nil || + data.MissionInfo == nil { + + return nil, fmt.Errorf("invalid data returned from some (or all) endpoints") + } + return } From 69f86757d6c803c59043acb999fe6ff0cb4ce094 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Sat, 29 Jul 2023 16:39:54 -0300 Subject: [PATCH 4/6] ci: :green_heart: fixes pipeline Signed-off-by: Lucas Vieira --- .github/workflows/go.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 1384fa3..a91222e 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -11,15 +11,15 @@ jobs: build-linux: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: - go-version: 1.20 + go-version: '>=1.19' - name: Get packages - run: go get -v ./... + run: go get -u ./... - name: Build run: go build -v ./... @@ -27,12 +27,12 @@ jobs: build-windows: runs-on: windows-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: - go-version: 1.20 + go-version: '>=1.19' - name: Get packages run: go get -v ./... @@ -43,15 +43,15 @@ jobs: build-mac: runs-on: macos-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v3 with: - go-version: 1.20 + go-version: '>=1.19' - name: Get packages - run: go get -v ./... + run: go get -u ./... - name: Build run: go build -v ./... From fa1b5edd5025f36d19878cf701c6d5c62b2cd676 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Sat, 29 Jul 2023 16:52:46 -0300 Subject: [PATCH 5/6] docs: :books: updates example code Signed-off-by: Lucas Vieira --- README.md | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 77f6b3d..293ad69 100644 --- a/README.md +++ b/README.md @@ -13,24 +13,33 @@ A simple library to get WarThunder data provided by the localhost server package main import ( - "fmt" + "log" + "time" + "github.com/lucasvmx/WarTelemetry/logger" wartelemetry "github.com/lucasvmx/WarTelemetry/telemetry" ) func main() { // Not needed if war thunder is running on localhost - wartelemetry.Initialize("192.168.0.34") + wartelemetry.Initialize("192.168.1.35") + + // Enable internal debug logging (disabled by default) + logger.EnableDebug() for { - t, _ := wartelemetry.GetTelemetryData() - objs := t.MapObjects - for _, obj := range objs { - fmt.Printf("type: %v, x: %v, y: %v\n", obj.Type, obj.X, obj.Y) + t, err := wartelemetry.GetTelemetryData() + if err != nil { + log.Printf("failed to collect data: %v", err) + time.Sleep(time.Second * 3) + continue + } else { + log.Printf("Aircraft Name: %v", t.Indicators.AircraftName) } } } + ``` ## Get Packages From 611eff91c7249ce197186ab16d6092ef96a0d7d8 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Sat, 29 Jul 2023 16:53:00 -0300 Subject: [PATCH 6/6] chore: :zap: adjusts library to use internal logger Signed-off-by: Lucas Vieira --- controller/gamechat.go | 6 +++--- controller/hudmsg.go | 6 +++--- controller/indicators.go | 6 +++--- controller/mapinfo.go | 6 +++--- controller/mapobjects.go | 6 +++--- controller/mission.go | 6 +++--- controller/players.go | 4 ++-- controller/state.go | 6 +++--- 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/controller/gamechat.go b/controller/gamechat.go index 4f95780..5a44315 100644 --- a/controller/gamechat.go +++ b/controller/gamechat.go @@ -2,9 +2,9 @@ package controller import ( "encoding/json" - "log" "sync" + "github.com/lucasvmx/WarTelemetry/logger" "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/gamechat" client "github.com/lucasvmx/WarTelemetry/network/http" @@ -19,14 +19,14 @@ func GetGamechatData(wg *sync.WaitGroup) { // Sends the GET request body, err := client.GetDataFromURL(gamechat.GetURL()) if err != nil { - log.Printf("[ERROR] failed to get gamechat data: %v", err) + logger.LogError("failed to get gamechat data: %v", err) return } // Decode data into a struct err = json.Unmarshal(body, &gc) if err != nil { - log.Printf("[ERROR] failed to get gamechat data: %v", err) + logger.LogError("failed to get gamechat data: %v", err) return } diff --git a/controller/hudmsg.go b/controller/hudmsg.go index 8399df8..5d11d46 100644 --- a/controller/hudmsg.go +++ b/controller/hudmsg.go @@ -2,9 +2,9 @@ package controller import ( "encoding/json" - "log" "sync" + "github.com/lucasvmx/WarTelemetry/logger" "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/hudmsg" network "github.com/lucasvmx/WarTelemetry/network/http" @@ -17,14 +17,14 @@ func GetHudMessagesData(wg *sync.WaitGroup) { data, err := network.GetDataFromURL(hudmsg.GetURL()) if err != nil { - log.Printf("[ERROR] failed to get hud message data: %v", err) + logger.LogError("failed to get hud message data: %v", err) return } // Decode data into a struct err = json.Unmarshal(data, &messages) if err != nil { - log.Printf("[ERROR] failed to get hud message data: %v", err) + logger.LogError("failed to get hud message data: %v", err) return } diff --git a/controller/indicators.go b/controller/indicators.go index 2f1842a..f0b1a3f 100644 --- a/controller/indicators.go +++ b/controller/indicators.go @@ -2,9 +2,9 @@ package controller import ( "encoding/json" - "log" "sync" + "github.com/lucasvmx/WarTelemetry/logger" "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/indicators" network "github.com/lucasvmx/WarTelemetry/network/http" @@ -17,13 +17,13 @@ func GetIndicatorsData(wg *sync.WaitGroup) { data, err := network.GetDataFromURL(indicators.GetURL()) if err != nil { - log.Printf("[ERROR] failed to get indicators data: %v", err) + logger.LogError("failed to get indicators data: %v", err) return } err = json.Unmarshal(data, &id) if err != nil { - log.Printf("[ERROR] failed to get indicators data: %v", err) + logger.LogError("failed to get indicators data: %v", err) return } diff --git a/controller/mapinfo.go b/controller/mapinfo.go index f32bfd8..6eba991 100644 --- a/controller/mapinfo.go +++ b/controller/mapinfo.go @@ -2,9 +2,9 @@ package controller import ( "encoding/json" - "log" "sync" + "github.com/lucasvmx/WarTelemetry/logger" "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/mapinfo" network "github.com/lucasvmx/WarTelemetry/network/http" @@ -17,13 +17,13 @@ func GetMapInfoData(wg *sync.WaitGroup) { data, err := network.GetDataFromURL(mapinfo.GetURL()) if err != nil { - log.Printf("[ERROR] failed to get map information data: %v", err) + logger.LogError("failed to get map information data: %v", err) return } err = json.Unmarshal(data, &mi) if err != nil { - log.Printf("[ERROR] failed to get map information data: %v", err) + logger.LogError("failed to get map information data: %v", err) return } diff --git a/controller/mapobjects.go b/controller/mapobjects.go index 10ab6ff..a8388e2 100644 --- a/controller/mapobjects.go +++ b/controller/mapobjects.go @@ -2,9 +2,9 @@ package controller import ( "encoding/json" - "log" "sync" + "github.com/lucasvmx/WarTelemetry/logger" "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/mapobjects" network "github.com/lucasvmx/WarTelemetry/network/http" @@ -18,14 +18,14 @@ func GetMapObjsData(wg *sync.WaitGroup) { // Sends GET request data, err := network.GetDataFromURL(mapobjects.GetURL()) if err != nil { - log.Printf("[ERROR] failed to get map objects data: %v", err) + logger.LogError("failed to get map objects data: %v", err) return } // Decode data into a json struct err = json.Unmarshal(data, &mo) if err != nil { - log.Printf("[ERROR] failed to get map objects data: %v", err) + logger.LogError("failed to get map objects data: %v", err) return } diff --git a/controller/mission.go b/controller/mission.go index 66dd2a4..adeb2e9 100644 --- a/controller/mission.go +++ b/controller/mission.go @@ -2,9 +2,9 @@ package controller import ( "encoding/json" - "log" "sync" + "github.com/lucasvmx/WarTelemetry/logger" "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/mission" network "github.com/lucasvmx/WarTelemetry/network/http" @@ -18,14 +18,14 @@ func GetMissionData(wg *sync.WaitGroup) { // Sends a HTTP request data, err := network.GetDataFromURL(mission.GetURL()) if err != nil { - log.Printf("[ERROR] failed to get mission data: %v", err) + logger.LogError("failed to get mission data: %v", err) return } // Decode JSON into a struct err = json.Unmarshal(data, &mi) if err != nil { - log.Printf("[ERROR] failed to get mission data: %v", err) + logger.LogError("failed to get mission data: %v", err) return } diff --git a/controller/players.go b/controller/players.go index 0cbe19b..14e2bcc 100644 --- a/controller/players.go +++ b/controller/players.go @@ -2,9 +2,9 @@ package controller import ( "errors" - "log" "strings" + "github.com/lucasvmx/WarTelemetry/logger" "github.com/lucasvmx/WarTelemetry/model/mapobjects" ) @@ -42,7 +42,7 @@ func GetAlliesColor(mo []mapobjects.MapObjects) (color string) { // Gets current player player, err := GetCurrentPlayer(mo) if err != nil { - log.Printf("[ERROR] Failed to get current player: %v", err) + logger.LogError("failed to get current player: %v", err) return } diff --git a/controller/state.go b/controller/state.go index 8e41a75..b3ce9ef 100644 --- a/controller/state.go +++ b/controller/state.go @@ -2,9 +2,9 @@ package controller import ( "encoding/json" - "log" "sync" + "github.com/lucasvmx/WarTelemetry/logger" "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/state" network "github.com/lucasvmx/WarTelemetry/network/http" @@ -19,7 +19,7 @@ func GetStateData(wg *sync.WaitGroup) { // Sends a HTTP request data, err := network.GetDataFromURL(state.GetURL()) if err != nil { - log.Printf("[ERROR] failed to get state data: %v", err) + logger.LogError("failed to get state data: %v", err) return } @@ -29,7 +29,7 @@ func GetStateData(wg *sync.WaitGroup) { // Decode JSON into a struct err = json.Unmarshal(data, &st) if err != nil { - log.Printf("[ERROR] failed to get state data: %v", err) + logger.LogError("failed to get state data: %v", err) return }