Skip to content

Commit

Permalink
Merge pull request #7 from lucasvmx/dev
Browse files Browse the repository at this point in the history
Bugfixes and improvements
  • Loading branch information
lucasvmx authored Jul 29, 2023
2 parents 768f962 + ef8f59b commit dcd8c66
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 51 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ 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 ./...

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 ./...
Expand All @@ -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 ./...
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,33 @@ go get -u github.com/lucasvmx/WarTelemetry/telemetry
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)
}
}
}

```

## Build program
Expand Down
10 changes: 7 additions & 3 deletions controller/gamechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -19,12 +19,16 @@ 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
json.Unmarshal(body, &gc)
err = json.Unmarshal(body, &gc)
if err != nil {
logger.LogError("failed to get gamechat data: %v", err)
return
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()
Expand Down
8 changes: 6 additions & 2 deletions controller/hudmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,12 +17,16 @@ 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 {
logger.LogError("failed to get hud message data: %v", err)
return
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()
Expand Down
10 changes: 7 additions & 3 deletions controller/indicators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,11 +17,15 @@ 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
}

json.Unmarshal(data, &id)
err = json.Unmarshal(data, &id)
if err != nil {
logger.LogError("failed to get indicators data: %v", err)
return
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()
Expand Down
10 changes: 7 additions & 3 deletions controller/mapinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -17,11 +17,15 @@ 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
}

json.Unmarshal(data, &mi)
err = json.Unmarshal(data, &mi)
if err != nil {
logger.LogError("failed to get map information data: %v", err)
return
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()
Expand Down
10 changes: 7 additions & 3 deletions controller/mapobjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,12 +18,16 @@ 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
json.Unmarshal(data, &mo)
err = json.Unmarshal(data, &mo)
if err != nil {
logger.LogError("failed to get map objects data: %v", err)
return
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()
Expand Down
6 changes: 3 additions & 3 deletions controller/mission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions controller/players.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package controller

import (
"errors"
"log"
"strings"

"github.com/lucasvmx/WarTelemetry/logger"
"github.com/lucasvmx/WarTelemetry/model/mapobjects"
)

Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 7 additions & 3 deletions controller/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -19,15 +19,19 @@ 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
}

// Process JSON into a readable format
data = utils.ProcessJSON(data)

// Decode JSON into a struct
json.Unmarshal(data, &st)
err = json.Unmarshal(data, &st)
if err != nil {
logger.LogError("failed to get state data: %v", err)
return
}

model.TelemetryInstance.LockMux()
defer model.TelemetryInstance.UnlockMux()
Expand Down
49 changes: 49 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -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...)
}
}
Loading

0 comments on commit dcd8c66

Please sign in to comment.