From 854d4db07cca4193207e1a0d93bf10cfbab6cc75 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Mon, 5 Jun 2023 00:42:53 -0300 Subject: [PATCH 1/9] fix(wip): deadlock while waiting for goroutines Signed-off-by: Lucas Vieira --- controller/gamechat.go | 19 +++++++++++++------ controller/hudmsg.go | 19 +++++++++++-------- controller/indicators.go | 14 +++++++++----- controller/mapinfo.go | 18 +++++++++++------- controller/mapobjects.go | 16 +++++++++------- controller/mission.go | 14 +++++++++----- controller/objects.go | 12 ++---------- controller/state.go | 11 ++++++----- network/http/client.go | 6 ++++-- telemetry/telemetry.go | 8 ++++++-- 10 files changed, 80 insertions(+), 57 deletions(-) diff --git a/controller/gamechat.go b/controller/gamechat.go index 609d7ab..9315831 100644 --- a/controller/gamechat.go +++ b/controller/gamechat.go @@ -2,6 +2,7 @@ package controller import ( "encoding/json" + "log" "sync" "github.com/lucasvmx/WarTelemetry/model/gamechat" @@ -9,23 +10,29 @@ import ( ) // GetGamechatData retrieves data about gamechat -func GetGamechatData(wg *sync.WaitGroup) (gc []gamechat.GameChat, err error) { +func GetGamechatData(wg *sync.WaitGroup) { - defer wg.Done() + var gc []gamechat.GameChat + defer func() { + log.Printf("cleaning up context") + wg.Done() + }() // Sends the GET request body, err := client.GetDataFromURL(gamechat.GetURL()) if err != nil { - return nil, err + log.Printf("[ERROR] failed to get gamechat data: %v", err) + DataChan <- err + return } // Decode data into a struct err = json.Unmarshal(body, &gc) if err != nil { - return nil, err + log.Printf("[ERROR] failed to get gamechat data: %v", err) + DataChan <- err + return } DataChan <- gc - - return } diff --git a/controller/hudmsg.go b/controller/hudmsg.go index cf58c95..a4321d2 100644 --- a/controller/hudmsg.go +++ b/controller/hudmsg.go @@ -2,6 +2,7 @@ package controller import ( "encoding/json" + "log" "sync" "github.com/lucasvmx/WarTelemetry/model/hudmsg" @@ -9,22 +10,24 @@ import ( ) // GetHudMessagesData function retrieves all messages from HUD -func GetHudMessagesData(wg *sync.WaitGroup) (messages *hudmsg.Hudmsg, err error) { - +func GetHudMessagesData(wg *sync.WaitGroup) { + var messages *hudmsg.Hudmsg defer wg.Done() data, err := network.GetDataFromURL(hudmsg.GetURL()) if err != nil { - return nil, err + log.Printf("[ERROR] failed to get hud message data: %v", err) + DataChan <- err + return } // Decode data into a struct - failure := json.Unmarshal(data, &messages) - if failure != nil { - return nil, err + err = json.Unmarshal(data, &messages) + if err != nil { + log.Printf("[ERROR] failed to get hud message data: %v", err) + DataChan <- err + return } DataChan <- messages - - return } diff --git a/controller/indicators.go b/controller/indicators.go index 254b036..507e507 100644 --- a/controller/indicators.go +++ b/controller/indicators.go @@ -2,6 +2,7 @@ package controller import ( "encoding/json" + "log" "sync" "github.com/lucasvmx/WarTelemetry/model/indicators" @@ -9,20 +10,23 @@ import ( ) // GetIndicatorsData function retrieves data about aircraft indicators -func GetIndicatorsData(wg *sync.WaitGroup) (id *indicators.Indicators, err error) { +func GetIndicatorsData(wg *sync.WaitGroup) { + var id *indicators.Indicators defer wg.Done() data, err := network.GetDataFromURL(indicators.GetURL()) if err != nil { - return nil, err + log.Printf("[ERROR] failed to get indicators data: %v", err) + DataChan <- err + return } err = json.Unmarshal(data, &id) if err != nil { - return nil, err + log.Printf("[ERROR] failed to get indicators data: %v", err) + DataChan <- err + return } DataChan <- id - - return } diff --git a/controller/mapinfo.go b/controller/mapinfo.go index 407d8a5..c21dac7 100644 --- a/controller/mapinfo.go +++ b/controller/mapinfo.go @@ -2,6 +2,7 @@ package controller import ( "encoding/json" + "log" "sync" "github.com/lucasvmx/WarTelemetry/model/mapinfo" @@ -9,20 +10,23 @@ import ( ) // GetMapInfoData function retrieves information about current map -func GetMapInfoData(wg *sync.WaitGroup) (mi *mapinfo.MapInformation, err error) { +func GetMapInfoData(wg *sync.WaitGroup) { + var mi *mapinfo.MapInformation defer wg.Done() data, err := network.GetDataFromURL(mapinfo.GetURL()) if err != nil { - return nil, err + log.Printf("[ERROR] failed to get map information data: %v", err) + DataChan <- err + return } - failure := json.Unmarshal(data, &mi) - if failure != nil { - return nil, err + err = json.Unmarshal(data, &mi) + if err != nil { + log.Printf("[ERROR] failed to get map information data: %v", err) + DataChan <- err + return } DataChan <- mi - - return } diff --git a/controller/mapobjects.go b/controller/mapobjects.go index b2f7cde..7b2f6d9 100644 --- a/controller/mapobjects.go +++ b/controller/mapobjects.go @@ -2,31 +2,33 @@ package controller import ( "encoding/json" + "log" "sync" "github.com/lucasvmx/WarTelemetry/model/mapobjects" network "github.com/lucasvmx/WarTelemetry/network/http" ) -var objs *mapobjects.MapObjects - // GetMapObjsData function retrives data about all map objects -func GetMapObjsData(wg *sync.WaitGroup) (mo []mapobjects.MapObjects, err error) { +func GetMapObjsData(wg *sync.WaitGroup) { + var mo []mapobjects.MapObjects defer wg.Done() // Sends GET request data, err := network.GetDataFromURL(mapobjects.GetURL()) if err != nil { - return nil, err + log.Printf("[ERROR] failed to get map objects data: %v", err) + DataChan <- err + return } // Decode data into a json struct err = json.Unmarshal(data, &mo) if err != nil { - return nil, err + log.Printf("[ERROR] failed to get map objects data: %v", err) + DataChan <- err + return } DataChan <- mo - - return } diff --git a/controller/mission.go b/controller/mission.go index 393c735..faa92e9 100644 --- a/controller/mission.go +++ b/controller/mission.go @@ -2,6 +2,7 @@ package controller import ( "encoding/json" + "log" "sync" "github.com/lucasvmx/WarTelemetry/model/mission" @@ -9,22 +10,25 @@ import ( ) // GetMissionData function retrieves data about running missions -func GetMissionData(wg *sync.WaitGroup) (mi *mission.MissionInfo, err error) { +func GetMissionData(wg *sync.WaitGroup) { + var mi *mission.MissionInfo defer wg.Done() // Sends a HTTP request data, err := network.GetDataFromURL(mission.GetURL()) if err != nil { - return nil, err + log.Printf("[ERROR] failed to get mission data: %v", err) + DataChan <- err + return } // Decode JSON into a struct err = json.Unmarshal(data, &mi) if err != nil { - return nil, err + log.Printf("[ERROR] failed to get mission data: %v", err) + DataChan <- err + return } DataChan <- mi - - return } diff --git a/controller/objects.go b/controller/objects.go index b73e528..939d6cf 100644 --- a/controller/objects.go +++ b/controller/objects.go @@ -14,11 +14,7 @@ const distanceFactor float64 = 66.32072 func IsAircraft(object *mapobjects.MapObjects) bool { // Check type - if object.Type == "aircraft" { - return true - } - - return false + return object.Type == "aircraft" } // IsCurrentPlayer function determines if current object is the current player @@ -27,11 +23,7 @@ func IsCurrentPlayer(object *mapobjects.MapObjects) bool { iconName := strings.ToLower(object.Icon) // Check object - if iconName == "player" { - return true - } - - return false + return iconName == "player" } // CalculateDistanceInKm gets the distance in kilometers between two objects diff --git a/controller/state.go b/controller/state.go index 3000e6c..651f55a 100644 --- a/controller/state.go +++ b/controller/state.go @@ -10,13 +10,15 @@ import ( ) // GetStateData function retrieves data about running missions -func GetStateData(wg *sync.WaitGroup) (st *state.AircraftState, err error) { +func GetStateData(wg *sync.WaitGroup) { + var st *state.AircraftState defer wg.Done() // Sends a HTTP request data, err := network.GetDataFromURL(state.GetURL()) if err != nil { - return nil, err + DataChan <- err + return } // Process JSON into a readable format @@ -25,8 +27,7 @@ func GetStateData(wg *sync.WaitGroup) (st *state.AircraftState, err error) { // Decode JSON into a struct err = json.Unmarshal(data, &st) if err != nil { - return nil, err + DataChan <- err + return } - - return } diff --git a/network/http/client.go b/network/http/client.go index a970c3d..0793d66 100644 --- a/network/http/client.go +++ b/network/http/client.go @@ -1,7 +1,7 @@ package network import ( - "io/ioutil" + "io" "log" "net/http" ) @@ -23,7 +23,7 @@ func readResponse(response *http.Response) (data []byte, err error) { defer response.Body.Close() // Read response body - data, err = ioutil.ReadAll(response.Body) + data, err = io.ReadAll(response.Body) return } @@ -42,5 +42,7 @@ func GetDataFromURL(url string) (data []byte, err error) { return nil, err } + log.Printf("read %d bytes from body (%s)", len(data), url) + return } diff --git a/telemetry/telemetry.go b/telemetry/telemetry.go index 1e70acb..2f1aa5a 100644 --- a/telemetry/telemetry.go +++ b/telemetry/telemetry.go @@ -32,7 +32,7 @@ func Initialize(hostname string) { // GetTelemetryData function retrieves all telemetry data func GetTelemetryData() (data *model.TelemetryData, err error) { - wg.Add(7) + wg.Add(8) go controller.GetGamechatData(wg) go controller.GetIndicatorsData(wg) @@ -42,7 +42,9 @@ func GetTelemetryData() (data *model.TelemetryData, err error) { go controller.GetStateData(wg) go controller.GetMissionData(wg) - wg.Wait() + log.Printf("waiting go routines to finish") + v := <-controller.DataChan + log.Printf("received %v", v) // Initialize struct data = &model.TelemetryData{} @@ -63,6 +65,8 @@ func GetTelemetryData() (data *model.TelemetryData, err error) { data.MapObjects = value case *mission.MissionInfo: data.MissionInfo = value + case error: + log.Printf("error: %v", value) } } From 1afc2a3752c38b1ee30fb4490a6d817d1017c1b7 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Mon, 5 Jun 2023 00:44:15 -0300 Subject: [PATCH 2/9] chore: adds test data Signed-off-by: Lucas Vieira --- samples/gamechat_lastId=0 | 9 ++++ samples/hudmsg_lastEvt=0&lastDmg=0 | 74 ++++++++++++++++++++++++++++++ samples/indicators | 61 ++++++++++++++++++++++++ samples/map_info.json | 9 ++++ samples/map_obj.json | 50 ++++++++++++++++++++ samples/mission.json | 10 ++++ samples/state | 25 ++++++++++ 7 files changed, 238 insertions(+) create mode 100644 samples/gamechat_lastId=0 create mode 100644 samples/hudmsg_lastEvt=0&lastDmg=0 create mode 100644 samples/indicators create mode 100644 samples/map_info.json create mode 100644 samples/map_obj.json create mode 100644 samples/mission.json create mode 100644 samples/state diff --git a/samples/gamechat_lastId=0 b/samples/gamechat_lastId=0 new file mode 100644 index 0000000..af2633b --- /dev/null +++ b/samples/gamechat_lastId=0 @@ -0,0 +1,9 @@ +[ +{ "id": 1, "msg": "fuck my gear was down lol", "sender": "Dedrich", "enemy": true, "mode": "All", "time": 739 }, +{ "id": 2, "msg": "Two exeperimentals that never saw service", "sender": "Anisim", "enemy": true, "mode": "All", "time": 740 }, +{ "id": 3, "msg": "rip", "sender": "Foka", "enemy": true, "mode": "All", "time": 772 }, +{ "id": 4, "msg": "wait what catchall?", "sender": "Atsuko", "enemy": false, "mode": "All", "time": 774 }, +{ "id": 5, "msg": "\tbruh really", "sender": "Adalwulf", "enemy": false, "mode": "All", "time": 793 }, +{ "id": 6, "msg": "G8N was a prototype", "sender": "Anisim", "enemy": true, "mode": "All", "time": 797 }, +{ "id": 7, "msg": "lol well i caught up", "sender": "Dedrich", "enemy": true, "mode": "All", "time": 808 } +] \ No newline at end of file diff --git a/samples/hudmsg_lastEvt=0&lastDmg=0 b/samples/hudmsg_lastEvt=0&lastDmg=0 new file mode 100644 index 0000000..e05419a --- /dev/null +++ b/samples/hudmsg_lastEvt=0&lastDmg=0 @@ -0,0 +1,74 @@ +{"events":[ + +], +"damage":[ +{ "id": 1, "msg": "Gertraud has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 697 }, +{ "id": 2, "msg": "Gertraudtd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 697 }, +{ "id": 3, "msg": "Atsuko (J7W1) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 733 }, +{ "id": 4, "msg": "Yasuichi (F-4J) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 733 }, +{ "id": 5, "msg": "Cassidy (Yak-141) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 739 }, +{ "id": 6, "msg": "Meinhard (F4U-1) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 745 }, +{ "id": 7, "msg": "Ada (Su-22M3) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 748 }, +{ "id": 8, "msg": "Boris (P-26A) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 749 }, +{ "id": 9, "msg": "Baxter (F-104A) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 751 }, +{ "id": 10, "msg": "Cassidy (Yak-141) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 758 }, +{ "id": 11, "msg": "Cassidy (Yak-141) damaged Yoshinori (BV 238)", "sender": "", "enemy": false, "mode": "", "time": 758 }, +{ "id": 12, "msg": "Cassidy (Yak-141) shot down Yoshinori (BV 238)", "sender": "", "enemy": false, "mode": "", "time": 758 }, +{ "id": 13, "msg": "Cassidy (Yak-141) set afire Yoshinori (BV 238)", "sender": "", "enemy": false, "mode": "", "time": 758 }, +{ "id": 14, "msg": "Meinhard has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 763 }, +{ "id": 15, "msg": "Meinhardtd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 763 }, +{ "id": 16, "msg": "Graham (F4U-4) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 768 }, +{ "id": 17, "msg": "Hiroichi (F-14A) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 769 }, +{ "id": 18, "msg": "Aldo (Fw 190 A) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 770 }, +{ "id": 19, "msg": "Yoshikazu (A-4E Early) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 778 }, +{ "id": 20, "msg": "Hisashi (J21A-1) shot down Katsuo (P-51C)", "sender": "", "enemy": false, "mode": "", "time": 799 }, +{ "id": 21, "msg": "Baxter (F-104A) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 799 }, +{ "id": 22, "msg": "Yasuichi (F-4J) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 801 }, +{ "id": 23, "msg": "Hisashi (J21A-1) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 801 }, +{ "id": 24, "msg": "Katsuo has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 802 }, +{ "id": 25, "msg": "Katsuotd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 802 }, +{ "id": 26, "msg": "Friedhelm (P-26A) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 804 }, +{ "id": 27, "msg": "Graham has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 805 }, +{ "id": 28, "msg": "Grahamtd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 805 }, +{ "id": 29, "msg": "Yasuichi has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 806 }, +{ "id": 30, "msg": "Yasuichitd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 806 }, +{ "id": 31, "msg": "Cassidy (Yak-141) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 808 }, +{ "id": 32, "msg": "Klemens (F-16A) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 812 }, +{ "id": 33, "msg": "Yoshiichi (Kfir Canard) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 820 }, +{ "id": 34, "msg": "Yoshiichi has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 825 }, +{ "id": 35, "msg": "Yoshiichitd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 825 }, +{ "id": 36, "msg": "Friedhelm (P-26B) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 835 }, +{ "id": 37, "msg": "Cassidy (Yak-141) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 851 }, +{ "id": 38, "msg": "Ada has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 852 }, +{ "id": 39, "msg": "Adatd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 852 }, +{ "id": 40, "msg": "Ada (Su-22M3) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 862 }, +{ "id": 41, "msg": "Yasuichi (F-4J) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 877 }, +{ "id": 42, "msg": "Hiroichi (F-14A) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 889 }, +{ "id": 43, "msg": "Takayuki (A5M4) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 889 }, +{ "id": 44, "msg": "Yoshikazu (B-26B) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 890 }, +{ "id": 45, "msg": "Pyotr (Mirage 2000) shot down Yoshisuke (JA37C)", "sender": "", "enemy": false, "mode": "", "time": 901 }, +{ "id": 46, "msg": "Nazar (MiG-23MLD) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 907 }, +{ "id": 47, "msg": "Yoshihiko (F-105D) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 909 }, +{ "id": 48, "msg": "Nazar has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 911 }, +{ "id": 49, "msg": "Nazartd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 911 }, +{ "id": 50, "msg": "Katsuo (Me 262 A-1a/U1) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 914 }, +{ "id": 51, "msg": "Yoshisuke has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 915 }, +{ "id": 52, "msg": "Yoshisuketd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 915 }, +{ "id": 53, "msg": "Boris (P-26A) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 919 }, +{ "id": 54, "msg": "Pyotr (Mirage 2000) shot down Friedhelm (P-26B)", "sender": "", "enemy": false, "mode": "", "time": 943 }, +{ "id": 55, "msg": "Dedrich (F4U-4) has crashed.", "sender": "", "enemy": false, "mode": "", "time": 952 }, +{ "id": 56, "msg": "Pyotr has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 57, "msg": "Pyotrtd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 58, "msg": "Eduard has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 59, "msg": "Eduardtd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 60, "msg": "Dexter has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 61, "msg": "Dextertd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 62, "msg": "Katsuo has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 63, "msg": "Katsuotd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 64, "msg": "Bennett has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 65, "msg": "Bennetttd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 66, "msg": "Gamhard has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 67, "msg": "Gamhardtd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 68, "msg": "Yasuichi has disconnected from the game.", "sender": "", "enemy": false, "mode": "", "time": 961 }, +{ "id": 69, "msg": "Yasuichitd! kd?NET_PLAYER_DISCONNECT_FROM_GAME", "sender": "", "enemy": false, "mode": "", "time": 961 } +]} \ No newline at end of file diff --git a/samples/indicators b/samples/indicators new file mode 100644 index 0000000..1a347d6 --- /dev/null +++ b/samples/indicators @@ -0,0 +1,61 @@ +{"valid": true, +"type": "mig_23mld", +"speed": -0.022197, +"speed_01": -0.022197, +"pedals": 0.000000, +"pedals1": 0.000000, +"pedals2": 0.000000, +"stick_elevator": 0.000000, +"stick_ailerons": 0.000000, +"vario": -0.015015, +"altitude_min": 59.182709, +"altitude_10k": 59.182709, +"altitude1_hour": 59.182709, +"altitude1_10k": 59.182709, +"radio_altitude": 1.882836, +"aviahorizon_roll": 0.038568, +"aviahorizon_pitch": -1.540892, +"bank": -0.038568, +"bank1": -0.038568, +"turn": -0.000046, +"compass": 219.846481, +"clock_hour": 0.533333, +"clock_min": 32.000000, +"clock_sec": 16.000000, +"rpm": 3243.290283, +"rpm1": 0.000000, +"oil_pressure": 54.506897, +"water_temperature": 453.031036, +"fuel": 1021.500122, +"oxygen": 0.000000, +"airbrake_lever": 0.000000, +"gears": 0.500000, +"gears1": 0.500000, +"gear_lamp_down": 1.000000, +"gear_lamp_up": 1.000000, +"gear_lamp_off": 1.000000, +"flaps_position_retracted": 0.000000, +"flaps_position_takeoff": 0.000000, +"flaps_position_landing": 0.000000, +"wing_sweep_lever": 0.000000, +"wing_sweep_lever1": 0.000000, +"trimmer": 0.000000, +"throttle": 0.000000, +"throttle_1": 0.000000, +"weapon2": 0.000000, +"wing_sweep_indicator": 0.000000, +"wing_sweep_indicator1": 0.000000, +"wing_sweep_indicator2": 0.000000, +"wing_sweep_indicator3": 0.000000, +"mach": -0.000065, +"g_meter": 0.998822, +"g_meter_min": 0.996760, +"g_meter_max": 1.007712, +"aoa": 147.040558, +"blister1": 1.000000, +"blister2": 1.000000, +"blister3": 1.000000, +"blister4": 1.000000, +"blister5": 1.000000, +"blister6": 1.000000, +"blister10": 1.000000} \ No newline at end of file diff --git a/samples/map_info.json b/samples/map_info.json new file mode 100644 index 0000000..8cbfc92 --- /dev/null +++ b/samples/map_info.json @@ -0,0 +1,9 @@ +{ + "grid_size" : [ 65536.0, 65536.0 ], + "grid_steps" : [ 6500.0, 6500.0 ], + "grid_zero" : [ -28672.0, 28672.0 ], + "map_generation" : 3, + "map_max" : [ 32768.0, 32768.0 ], + "map_min" : [ -32768.0, -32768.0 ], + "valid" : true +} diff --git a/samples/map_obj.json b/samples/map_obj.json new file mode 100644 index 0000000..95eab5e --- /dev/null +++ b/samples/map_obj.json @@ -0,0 +1,50 @@ +[ +{"type":"airfield","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"none","icon_bg":"none","sx":0.338597,"sy":0.720108,"ex":0.357913,"ey":0.692523}, +{"type":"airfield","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"none","icon_bg":"none","sx":0.353288,"sy":0.630410,"ex":0.369172,"ey":0.660105}, +{"type":"airfield","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"none","icon_bg":"none","sx":0.526287,"sy":0.513440,"ex":0.513073,"ey":0.529298}, +{"type":"airfield","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"none","icon_bg":"none","sx":0.525317,"sy":0.512667,"ex":0.512104,"ey":0.528499}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.478599,"y":0.568650,"dx":-0.742012,"dy":0.670386}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.431700,"y":0.602203,"dx":-0.711417,"dy":0.702770}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.301187,"y":0.693608,"dx":0.539346,"dy":-0.842085}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.388018,"y":0.649404,"dx":-0.830121,"dy":0.557584}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.669275,"y":0.421099,"dx":0.808295,"dy":-0.588778}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.521864,"y":0.517257,"dx":-0.291101,"dy":-0.956693}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Assault","icon_bg":"none","x":0.332325,"y":0.726647,"dx":-0.714867,"dy":-0.699260}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.372026,"y":0.672683,"dx":-0.841973,"dy":0.539520}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.482393,"y":0.564556,"dx":-0.737540,"dy":0.675304}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Bomber","icon_bg":"none","x":0.351287,"y":0.701965,"dx":-0.554750,"dy":0.832017}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.346534,"y":0.679707,"dx":-0.971907,"dy":-0.235368}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Assault","icon_bg":"none","x":0.300473,"y":0.695553,"dx":0.553829,"dy":-0.832630}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Bomber","icon_bg":"none","x":0.370401,"y":0.572528,"dx":-0.087378,"dy":-0.996175}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.340906,"y":0.627900,"dx":0.498156,"dy":-0.867087}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.520596,"y":0.521540,"dx":0.704325,"dy":-0.709878}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.421746,"y":0.632874,"dx":0.830047,"dy":0.557694}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.345984,"y":0.709734,"dx":-0.561559,"dy":0.827436}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.513651,"y":0.530591,"dx":-0.537051,"dy":0.843550}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Bomber","icon_bg":"none","x":0.355880,"y":0.695723,"dx":-0.569093,"dy":0.822273}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Bomber","icon_bg":"none","x":0.661859,"y":0.427697,"dx":0.880234,"dy":-0.474541}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.306907,"y":0.682122,"dx":0.301175,"dy":-0.953569}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Bomber","icon_bg":"none","x":0.333346,"y":0.723764,"dx":-0.996317,"dy":-0.085751}, +{"type":"aircraft","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.310339,"y":0.667393,"dx":-0.048244,"dy":-0.998836}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.441759,"y":0.644102,"dx":-0.883140,"dy":0.469110}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.423171,"y":0.586430,"dx":-0.101289,"dy":0.994857}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.423933,"y":0.643750,"dx":0.047252,"dy":0.998883}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Player","icon_bg":"none","x":0.524113,"y":0.514118,"dx":-0.640733,"dy":0.767764}, +{"type":"aircraft","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Fighter","icon_bg":"none","x":0.521346,"y":0.516986,"dx":-0.641051,"dy":0.767498}, +{"type":"ground_model","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Ground","icon_bg":"none","x":0.279770,"y":0.671426}, +{"type":"ground_model","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"Ground","icon_bg":"none","x":0.279770,"y":0.671433}, +{"type":"ground_model","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Structure","icon_bg":"none","x":0.525987,"y":0.443988}, +{"type":"ground_model","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Structure","icon_bg":"none","x":0.529237,"y":0.449681}, +{"type":"ground_model","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Structure","icon_bg":"none","x":0.530626,"y":0.450011}, +{"type":"ground_model","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Structure","icon_bg":"none","x":0.525510,"y":0.450343}, +{"type":"ground_model","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Structure","icon_bg":"none","x":0.527029,"y":0.449355}, +{"type":"ground_model","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Structure","icon_bg":"none","x":0.524358,"y":0.451281}, +{"type":"ground_model","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Structure","icon_bg":"none","x":0.525906,"y":0.446712}, +{"type":"ground_model","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Structure","icon_bg":"none","x":0.527498,"y":0.442606}, +{"type":"ground_model","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"Structure","icon_bg":"none","x":0.530664,"y":0.442409}, +{"type":"respawn_base_fighter","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"respawn_base_fighter","icon_bg":"none","x":0.527250,"y":0.512806,"dx":-57.357601,"dy":81.915199}, +{"type":"respawn_base_fighter","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"respawn_base_fighter","icon_bg":"none","x":0.526090,"y":0.511830,"dx":-57.357601,"dy":81.915199}, +{"type":"respawn_base_fighter","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"respawn_base_fighter","icon_bg":"none","x":0.276552,"y":0.640349,"dx":-19.183800,"dy":219.263000}, +{"type":"respawn_base_fighter","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"respawn_base_fighter","icon_bg":"none","x":0.273227,"y":0.640124,"dx":-9.600380,"dy":219.889999}, +{"type":"respawn_base_fighter","color":"#fa0C00","color[]":[250,12,0],"blink":0,"icon":"respawn_base_fighter","icon_bg":"none","x":0.266530,"y":0.716136,"dx":19.183800,"dy":-219.263000}, +{"type":"respawn_base_fighter","color":"#174DFF","color[]":[23,77,255],"blink":0,"icon":"respawn_base_fighter","icon_bg":"none","x":0.269838,"y":0.716449,"dx":19.183800,"dy":-219.263000}] \ No newline at end of file diff --git a/samples/mission.json b/samples/mission.json new file mode 100644 index 0000000..65d3b33 --- /dev/null +++ b/samples/mission.json @@ -0,0 +1,10 @@ +{ + "objectives" : [ + { + "primary" : true, + "status" : "in_progress", + "text" : "Follow the route" + } + ], + "status" : "running" +} diff --git a/samples/state b/samples/state new file mode 100644 index 0000000..7ce221e --- /dev/null +++ b/samples/state @@ -0,0 +1,25 @@ +{"valid": true, +"aileron, %": 0, +"elevator, %": 0, +"rudder, %": 0, +"flaps, %": 0, +"gear, %": 100, +"airbrake, %": 0, +"H, m": 59, +"TAS, km/h": 0, +"IAS, km/h": 0, +"M": 0.00, +"AoA, deg": 29.4, +"AoS, deg": -25.8, +"Ny": 0.75, +"Vy, m/s": -0.0, +"Wx, deg/s": 0, +"Mfuel, kg": 1022, +"Mfuel0, kg": 3405, +"throttle 1, %": 0, +"power 1, hp": 0.0, +"RPM 1": 3242, +"manifold pressure 1, atm": 1.00, +"oil temp 1, C": 54, +"thrust 1, kgs": 168, +"efficiency 1, %": 0} \ No newline at end of file From faa4b629d0d25211c8d3fe45db2bb72a34861ece Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Mon, 5 Jun 2023 02:09:22 -0300 Subject: [PATCH 3/9] chore: atualiza dados de teste Signed-off-by: Lucas Vieira --- samples/{gamechat_lastId=0 => gamechat} | 0 samples/{hudmsg_lastEvt=0&lastDmg=0 => hudmsg} | 0 samples/state | 4 ++-- 3 files changed, 2 insertions(+), 2 deletions(-) rename samples/{gamechat_lastId=0 => gamechat} (100%) rename samples/{hudmsg_lastEvt=0&lastDmg=0 => hudmsg} (100%) diff --git a/samples/gamechat_lastId=0 b/samples/gamechat similarity index 100% rename from samples/gamechat_lastId=0 rename to samples/gamechat diff --git a/samples/hudmsg_lastEvt=0&lastDmg=0 b/samples/hudmsg similarity index 100% rename from samples/hudmsg_lastEvt=0&lastDmg=0 rename to samples/hudmsg diff --git a/samples/state b/samples/state index 7ce221e..4e5f923 100644 --- a/samples/state +++ b/samples/state @@ -6,8 +6,8 @@ "gear, %": 100, "airbrake, %": 0, "H, m": 59, -"TAS, km/h": 0, -"IAS, km/h": 0, +"TAS, km/h": 350, +"IAS, km/h": 560, "M": 0.00, "AoA, deg": 29.4, "AoS, deg": -25.8, From 475abfb9f7ed7fa4f8730a29f0285e0c2e6cc70f Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Mon, 5 Jun 2023 02:10:11 -0300 Subject: [PATCH 4/9] fix: :bug: corrige deadlock ao obter dados de telemetria Signed-off-by: Lucas Vieira --- controller/gamechat.go | 13 +++++----- controller/hudmsg.go | 7 +++--- controller/indicators.go | 7 +++--- controller/mapinfo.go | 8 ++++--- controller/mapobjects.go | 7 +++--- controller/mission.go | 8 ++++--- controller/setup.go | 9 ------- controller/state.go | 10 ++++++-- model/hudmsg/hudmsg.go | 1 + model/telemetry.go | 26 ++++++++++++++++++++ network/http/client.go | 2 -- telemetry/telemetry.go | 51 +++++++++++++--------------------------- 12 files changed, 79 insertions(+), 70 deletions(-) delete mode 100644 controller/setup.go diff --git a/controller/gamechat.go b/controller/gamechat.go index 9315831..4f95780 100644 --- a/controller/gamechat.go +++ b/controller/gamechat.go @@ -5,6 +5,7 @@ import ( "log" "sync" + "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/gamechat" client "github.com/lucasvmx/WarTelemetry/network/http" ) @@ -13,16 +14,12 @@ import ( func GetGamechatData(wg *sync.WaitGroup) { var gc []gamechat.GameChat - defer func() { - log.Printf("cleaning up context") - wg.Done() - }() + defer wg.Done() // Sends the GET request body, err := client.GetDataFromURL(gamechat.GetURL()) if err != nil { log.Printf("[ERROR] failed to get gamechat data: %v", err) - DataChan <- err return } @@ -30,9 +27,11 @@ func GetGamechatData(wg *sync.WaitGroup) { err = json.Unmarshal(body, &gc) if err != nil { log.Printf("[ERROR] failed to get gamechat data: %v", err) - DataChan <- err return } - DataChan <- gc + model.TelemetryInstance.LockMux() + defer model.TelemetryInstance.UnlockMux() + + model.TelemetryInstance.Gamechat = gc } diff --git a/controller/hudmsg.go b/controller/hudmsg.go index a4321d2..8399df8 100644 --- a/controller/hudmsg.go +++ b/controller/hudmsg.go @@ -5,6 +5,7 @@ import ( "log" "sync" + "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/hudmsg" network "github.com/lucasvmx/WarTelemetry/network/http" ) @@ -17,7 +18,6 @@ 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) - DataChan <- err return } @@ -25,9 +25,10 @@ func GetHudMessagesData(wg *sync.WaitGroup) { err = json.Unmarshal(data, &messages) if err != nil { log.Printf("[ERROR] failed to get hud message data: %v", err) - DataChan <- err return } - DataChan <- messages + model.TelemetryInstance.LockMux() + defer model.TelemetryInstance.UnlockMux() + model.TelemetryInstance.HudMessages = messages } diff --git a/controller/indicators.go b/controller/indicators.go index 507e507..2f1842a 100644 --- a/controller/indicators.go +++ b/controller/indicators.go @@ -5,6 +5,7 @@ import ( "log" "sync" + "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/indicators" network "github.com/lucasvmx/WarTelemetry/network/http" ) @@ -17,16 +18,16 @@ func GetIndicatorsData(wg *sync.WaitGroup) { data, err := network.GetDataFromURL(indicators.GetURL()) if err != nil { log.Printf("[ERROR] failed to get indicators data: %v", err) - DataChan <- err return } err = json.Unmarshal(data, &id) if err != nil { log.Printf("[ERROR] failed to get indicators data: %v", err) - DataChan <- err return } - DataChan <- id + model.TelemetryInstance.LockMux() + defer model.TelemetryInstance.UnlockMux() + model.TelemetryInstance.Indicators = id } diff --git a/controller/mapinfo.go b/controller/mapinfo.go index c21dac7..f32bfd8 100644 --- a/controller/mapinfo.go +++ b/controller/mapinfo.go @@ -5,6 +5,7 @@ import ( "log" "sync" + "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/mapinfo" network "github.com/lucasvmx/WarTelemetry/network/http" ) @@ -17,16 +18,17 @@ 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) - DataChan <- err return } err = json.Unmarshal(data, &mi) if err != nil { log.Printf("[ERROR] failed to get map information data: %v", err) - DataChan <- err return } - DataChan <- mi + model.TelemetryInstance.LockMux() + defer model.TelemetryInstance.UnlockMux() + + model.TelemetryInstance.MapInfo = mi } diff --git a/controller/mapobjects.go b/controller/mapobjects.go index 7b2f6d9..10ab6ff 100644 --- a/controller/mapobjects.go +++ b/controller/mapobjects.go @@ -5,6 +5,7 @@ import ( "log" "sync" + "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/mapobjects" network "github.com/lucasvmx/WarTelemetry/network/http" ) @@ -18,7 +19,6 @@ func GetMapObjsData(wg *sync.WaitGroup) { data, err := network.GetDataFromURL(mapobjects.GetURL()) if err != nil { log.Printf("[ERROR] failed to get map objects data: %v", err) - DataChan <- err return } @@ -26,9 +26,10 @@ func GetMapObjsData(wg *sync.WaitGroup) { err = json.Unmarshal(data, &mo) if err != nil { log.Printf("[ERROR] failed to get map objects data: %v", err) - DataChan <- err return } - DataChan <- mo + model.TelemetryInstance.LockMux() + defer model.TelemetryInstance.UnlockMux() + model.TelemetryInstance.MapObjects = mo } diff --git a/controller/mission.go b/controller/mission.go index faa92e9..66dd2a4 100644 --- a/controller/mission.go +++ b/controller/mission.go @@ -5,6 +5,7 @@ import ( "log" "sync" + "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/mission" network "github.com/lucasvmx/WarTelemetry/network/http" ) @@ -18,7 +19,6 @@ func GetMissionData(wg *sync.WaitGroup) { data, err := network.GetDataFromURL(mission.GetURL()) if err != nil { log.Printf("[ERROR] failed to get mission data: %v", err) - DataChan <- err return } @@ -26,9 +26,11 @@ func GetMissionData(wg *sync.WaitGroup) { err = json.Unmarshal(data, &mi) if err != nil { log.Printf("[ERROR] failed to get mission data: %v", err) - DataChan <- err return } - DataChan <- mi + model.TelemetryInstance.LockMux() + defer model.TelemetryInstance.UnlockMux() + + model.TelemetryInstance.MissionInfo = mi } diff --git a/controller/setup.go b/controller/setup.go deleted file mode 100644 index aa70350..0000000 --- a/controller/setup.go +++ /dev/null @@ -1,9 +0,0 @@ -package controller - -const numDataTypes = 8 - -var DataChan chan interface{} - -func Setup() { - DataChan = make(chan interface{}, numDataTypes) -} diff --git a/controller/state.go b/controller/state.go index 651f55a..8e41a75 100644 --- a/controller/state.go +++ b/controller/state.go @@ -2,8 +2,10 @@ package controller import ( "encoding/json" + "log" "sync" + "github.com/lucasvmx/WarTelemetry/model" "github.com/lucasvmx/WarTelemetry/model/state" network "github.com/lucasvmx/WarTelemetry/network/http" "github.com/lucasvmx/WarTelemetry/utils" @@ -17,7 +19,7 @@ func GetStateData(wg *sync.WaitGroup) { // Sends a HTTP request data, err := network.GetDataFromURL(state.GetURL()) if err != nil { - DataChan <- err + log.Printf("[ERROR] failed to get state data: %v", err) return } @@ -27,7 +29,11 @@ func GetStateData(wg *sync.WaitGroup) { // Decode JSON into a struct err = json.Unmarshal(data, &st) if err != nil { - DataChan <- err + log.Printf("[ERROR] failed to get state data: %v", err) return } + + model.TelemetryInstance.LockMux() + defer model.TelemetryInstance.UnlockMux() + model.TelemetryInstance.State = st } diff --git a/model/hudmsg/hudmsg.go b/model/hudmsg/hudmsg.go index 1a945ad..c4eb304 100644 --- a/model/hudmsg/hudmsg.go +++ b/model/hudmsg/hudmsg.go @@ -28,6 +28,7 @@ func GetURL() string { url = utils.GetBaseURL() url = strings.ReplaceAll(url, "$hostname$", utils.GetHostname()) url = strings.ReplaceAll(url, "$path$", path) + //url += "?lastEvt=0&lastDmg=0" } return url diff --git a/model/telemetry.go b/model/telemetry.go index c4457b3..cbfd7f3 100644 --- a/model/telemetry.go +++ b/model/telemetry.go @@ -1,6 +1,8 @@ package model import ( + "sync" + "github.com/lucasvmx/WarTelemetry/model/gamechat" "github.com/lucasvmx/WarTelemetry/model/hudmsg" "github.com/lucasvmx/WarTelemetry/model/indicators" @@ -19,4 +21,28 @@ type TelemetryData struct { State *state.AircraftState MapObjects []mapobjects.MapObjects MissionInfo *mission.MissionInfo + mux *sync.Mutex +} + +var TelemetryInstance *TelemetryData + +func (t *TelemetryData) setup() { + t.mux = &sync.Mutex{} +} + +func (t *TelemetryData) LockMux() { + t.mux.Lock() +} + +func (t *TelemetryData) UnlockMux() { + t.mux.Unlock() +} + +func GetInstance() *TelemetryData { + return TelemetryInstance +} + +func SetupTelemetry() { + TelemetryInstance = &TelemetryData{} + TelemetryInstance.setup() } diff --git a/network/http/client.go b/network/http/client.go index 0793d66..24dfe78 100644 --- a/network/http/client.go +++ b/network/http/client.go @@ -42,7 +42,5 @@ func GetDataFromURL(url string) (data []byte, err error) { return nil, err } - log.Printf("read %d bytes from body (%s)", len(data), url) - return } diff --git a/telemetry/telemetry.go b/telemetry/telemetry.go index 2f1aa5a..587cb00 100644 --- a/telemetry/telemetry.go +++ b/telemetry/telemetry.go @@ -6,13 +6,6 @@ import ( "github.com/lucasvmx/WarTelemetry/controller" "github.com/lucasvmx/WarTelemetry/model" - "github.com/lucasvmx/WarTelemetry/model/gamechat" - "github.com/lucasvmx/WarTelemetry/model/hudmsg" - "github.com/lucasvmx/WarTelemetry/model/indicators" - "github.com/lucasvmx/WarTelemetry/model/mapinfo" - "github.com/lucasvmx/WarTelemetry/model/mapobjects" - "github.com/lucasvmx/WarTelemetry/model/mission" - "github.com/lucasvmx/WarTelemetry/model/state" "github.com/lucasvmx/WarTelemetry/utils" ) @@ -26,14 +19,12 @@ func Initialize(hostname string) { log.Printf("[INFO] Hostname omitted. Using localhost") } + model.SetupTelemetry() wg = &sync.WaitGroup{} } -// GetTelemetryData function retrieves all telemetry data -func GetTelemetryData() (data *model.TelemetryData, err error) { - - wg.Add(8) - +func getTelemetryData() { + wg.Add(7) go controller.GetGamechatData(wg) go controller.GetIndicatorsData(wg) go controller.GetMapInfoData(wg) @@ -41,34 +32,24 @@ func GetTelemetryData() (data *model.TelemetryData, err error) { go controller.GetMapObjsData(wg) go controller.GetStateData(wg) go controller.GetMissionData(wg) + wg.Wait() +} - log.Printf("waiting go routines to finish") - v := <-controller.DataChan - log.Printf("received %v", v) +// GetTelemetryData function retrieves all telemetry data +func GetTelemetryData() (data *model.TelemetryData, err error) { // Initialize struct data = &model.TelemetryData{} - for e := range controller.DataChan { - switch value := e.(type) { - case *mapinfo.MapInformation: - data.MapInfo = value - case []gamechat.GameChat: - data.Gamechat = value - case *indicators.Indicators: - data.Indicators = value - case *state.AircraftState: - data.State = value - case *hudmsg.Hudmsg: - data.HudMessages = value - case []mapobjects.MapObjects: - data.MapObjects = value - case *mission.MissionInfo: - data.MissionInfo = value - case error: - log.Printf("error: %v", value) - } - } + getTelemetryData() + + data.MapInfo = model.GetInstance().MapInfo + data.Gamechat = model.GetInstance().Gamechat + data.Indicators = model.GetInstance().Indicators + data.State = model.GetInstance().State + data.HudMessages = model.GetInstance().HudMessages + data.MapObjects = model.GetInstance().MapObjects + data.MissionInfo = model.GetInstance().MissionInfo return } From 3aa5635ccf2016c39509b1ac3696d998ed86bf92 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Mon, 5 Jun 2023 02:10:21 -0300 Subject: [PATCH 5/9] chore: :zap: atualiza model do gamechat Signed-off-by: Lucas Vieira --- model/gamechat/gamechat.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/model/gamechat/gamechat.go b/model/gamechat/gamechat.go index 749c1bb..dc49833 100644 --- a/model/gamechat/gamechat.go +++ b/model/gamechat/gamechat.go @@ -13,6 +13,7 @@ type GameChat struct { Sender string `json:"sender"` Enemy bool `json:"enemy"` Mode string `json:"mode"` + Time int `json:"time"` } // path string @@ -25,6 +26,7 @@ func GetURL() string { url = utils.GetBaseURL() url = strings.ReplaceAll(url, "$hostname$", utils.GetHostname()) url = strings.ReplaceAll(url, "$path$", path) + //url += "?lastId=0" } return url From b0f02311a75d8b70aee0c0be33d34e0d4e8ac96f Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Mon, 5 Jun 2023 02:10:32 -0300 Subject: [PATCH 6/9] chore: :zap: atualiza model dos indicators Signed-off-by: Lucas Vieira --- model/indicators/indicators.go | 1 + 1 file changed, 1 insertion(+) diff --git a/model/indicators/indicators.go b/model/indicators/indicators.go index 904bdfd..baead0d 100644 --- a/model/indicators/indicators.go +++ b/model/indicators/indicators.go @@ -34,6 +34,7 @@ type Indicators struct { ClockSec float32 `json:"clock_sec"` RpmMin float32 `json:"rpm_min"` RpmMin1 float32 `json:"rpm1_min"` + AngleOfAttacl float32 `json:"aoa"` } var path string = "indicators" From b488ff04f0110f2d36c2a329be72047a43afed68 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Mon, 5 Jun 2023 02:12:51 -0300 Subject: [PATCH 7/9] breaking change: :boom: drops support for Go 1.15 Signed-off-by: Lucas Vieira --- .github/workflows/go.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 66819a3..1384fa3 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -16,14 +16,14 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.15 + go-version: 1.20 - name: Get packages run: go get -v ./... - name: Build run: go build -v ./... - + build-windows: runs-on: windows-latest steps: @@ -32,7 +32,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.15 + go-version: 1.20 - name: Get packages run: go get -v ./... @@ -48,7 +48,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.15 + go-version: 1.20 - name: Get packages run: go get -v ./... From a7b4337c5b29c5f1befa025052c1ca2388a04ecb Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Mon, 5 Jun 2023 06:07:01 -0300 Subject: [PATCH 8/9] chore: adds supercharger field to model Signed-off-by: Lucas Vieira --- model/indicators/indicators.go | 1 + 1 file changed, 1 insertion(+) diff --git a/model/indicators/indicators.go b/model/indicators/indicators.go index baead0d..ac29553 100644 --- a/model/indicators/indicators.go +++ b/model/indicators/indicators.go @@ -35,6 +35,7 @@ type Indicators struct { RpmMin float32 `json:"rpm_min"` RpmMin1 float32 `json:"rpm1_min"` AngleOfAttacl float32 `json:"aoa"` + SuperCharger float32 `json:"supercharger"` } var path string = "indicators" From 3db4e0329162b168b9e02c99cf4cdc49d03f3917 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Mon, 5 Jun 2023 06:09:00 -0300 Subject: [PATCH 9/9] chore: :zap: removes error handling when unmarshaling JSON Signed-off-by: Lucas Vieira --- controller/gamechat.go | 8 ++------ controller/hudmsg.go | 4 ---- controller/indicators.go | 8 ++------ controller/mapinfo.go | 8 ++------ controller/mapobjects.go | 8 ++------ controller/state.go | 8 ++------ 6 files changed, 10 insertions(+), 34 deletions(-) diff --git a/controller/gamechat.go b/controller/gamechat.go index 4f95780..2a57ae0 100644 --- a/controller/gamechat.go +++ b/controller/gamechat.go @@ -13,7 +13,7 @@ import ( // GetGamechatData retrieves data about gamechat func GetGamechatData(wg *sync.WaitGroup) { - var gc []gamechat.GameChat + var gc []gamechat.GameChat = []gamechat.GameChat{} defer wg.Done() // Sends the GET request @@ -24,11 +24,7 @@ func GetGamechatData(wg *sync.WaitGroup) { } // Decode data into a struct - err = json.Unmarshal(body, &gc) - if err != nil { - log.Printf("[ERROR] failed to get gamechat data: %v", err) - return - } + json.Unmarshal(body, &gc) model.TelemetryInstance.LockMux() defer model.TelemetryInstance.UnlockMux() diff --git a/controller/hudmsg.go b/controller/hudmsg.go index 8399df8..8b08e34 100644 --- a/controller/hudmsg.go +++ b/controller/hudmsg.go @@ -23,10 +23,6 @@ func GetHudMessagesData(wg *sync.WaitGroup) { // Decode data into a struct err = json.Unmarshal(data, &messages) - if err != nil { - log.Printf("[ERROR] failed to get hud message data: %v", err) - return - } model.TelemetryInstance.LockMux() defer model.TelemetryInstance.UnlockMux() diff --git a/controller/indicators.go b/controller/indicators.go index 2f1842a..5d36b54 100644 --- a/controller/indicators.go +++ b/controller/indicators.go @@ -12,7 +12,7 @@ import ( // GetIndicatorsData function retrieves data about aircraft indicators func GetIndicatorsData(wg *sync.WaitGroup) { - var id *indicators.Indicators + var id *indicators.Indicators = &indicators.Indicators{} defer wg.Done() data, err := network.GetDataFromURL(indicators.GetURL()) @@ -21,11 +21,7 @@ func GetIndicatorsData(wg *sync.WaitGroup) { return } - err = json.Unmarshal(data, &id) - if err != nil { - log.Printf("[ERROR] failed to get indicators data: %v", err) - return - } + json.Unmarshal(data, &id) model.TelemetryInstance.LockMux() defer model.TelemetryInstance.UnlockMux() diff --git a/controller/mapinfo.go b/controller/mapinfo.go index f32bfd8..c1aa85a 100644 --- a/controller/mapinfo.go +++ b/controller/mapinfo.go @@ -12,7 +12,7 @@ import ( // GetMapInfoData function retrieves information about current map func GetMapInfoData(wg *sync.WaitGroup) { - var mi *mapinfo.MapInformation + var mi *mapinfo.MapInformation = &mapinfo.MapInformation{} defer wg.Done() data, err := network.GetDataFromURL(mapinfo.GetURL()) @@ -21,11 +21,7 @@ func GetMapInfoData(wg *sync.WaitGroup) { return } - err = json.Unmarshal(data, &mi) - if err != nil { - log.Printf("[ERROR] failed to get map information data: %v", err) - return - } + json.Unmarshal(data, &mi) model.TelemetryInstance.LockMux() defer model.TelemetryInstance.UnlockMux() diff --git a/controller/mapobjects.go b/controller/mapobjects.go index 10ab6ff..c2be920 100644 --- a/controller/mapobjects.go +++ b/controller/mapobjects.go @@ -12,7 +12,7 @@ import ( // GetMapObjsData function retrives data about all map objects func GetMapObjsData(wg *sync.WaitGroup) { - var mo []mapobjects.MapObjects + var mo []mapobjects.MapObjects = []mapobjects.MapObjects{} defer wg.Done() // Sends GET request @@ -23,11 +23,7 @@ func GetMapObjsData(wg *sync.WaitGroup) { } // 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) - return - } + json.Unmarshal(data, &mo) model.TelemetryInstance.LockMux() defer model.TelemetryInstance.UnlockMux() diff --git a/controller/state.go b/controller/state.go index 8e41a75..7db0791 100644 --- a/controller/state.go +++ b/controller/state.go @@ -13,7 +13,7 @@ import ( // GetStateData function retrieves data about running missions func GetStateData(wg *sync.WaitGroup) { - var st *state.AircraftState + var st *state.AircraftState = &state.AircraftState{} defer wg.Done() // Sends a HTTP request @@ -27,11 +27,7 @@ func GetStateData(wg *sync.WaitGroup) { data = utils.ProcessJSON(data) // Decode JSON into a struct - err = json.Unmarshal(data, &st) - if err != nil { - log.Printf("[ERROR] failed to get state data: %v", err) - return - } + json.Unmarshal(data, &st) model.TelemetryInstance.LockMux() defer model.TelemetryInstance.UnlockMux()