From d100535f89ef810226a2aba08cbf32db33452eca Mon Sep 17 00:00:00 2001 From: Ethan Vogelsang Date: Mon, 15 Apr 2024 18:43:10 -0500 Subject: [PATCH] add option to use custom weather data --- .gitignore | 1 + README.md | 16 ++++++--- checkwx.json | 79 ------------------------------------------- config/config.go | 1 + config/config.json | 3 +- examples/checkwx.json | 44 ++++++++++++++++++++++++ main.go | 20 +++++++++-- 7 files changed, 78 insertions(+), 86 deletions(-) delete mode 100644 checkwx.json create mode 100644 examples/checkwx.json diff --git a/.gitignore b/.gitignore index e5e6078..114aaa6 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ resource.syso # testing files config.json +checkwx.json mission.miz realweather.miz tmp/ diff --git a/README.md b/README.md index b5f01d0..3116aa7 100644 --- a/README.md +++ b/README.md @@ -60,10 +60,11 @@ each parameter. "log": "logfile.log" // path of log file, "" disables }, "metar": { - "icao": "KDLH", // ICAO of the aiport to fetch METAR from - "runway-elevation": 0, // elevation of runway in meters MSL - "remarks": "", // addtional remarks for METAR, customization only - "add-to-brief": true // add METAR text to bottom of mission brief + "icao": "KDLH", // ICAO of the aiport to fetch METAR from + "runway-elevation": 0, // elevation of runway in meters MSL + "remarks": "", // addtional remarks for METAR, customization only + "add-to-brief": true, // add METAR text to bottom of mission brief + "use-custom-data": false // use custom data from checkwx.json file }, "options": { "update-time": true, // set to false to disable time being updated @@ -133,6 +134,13 @@ each parameter. * `add-to-brief`: boolean * If true, Real Weather will add the generated METAR to the bottom of your mission brief. + * `use-custom-data`: boolean + * If true, Real Weather will load METAR data from a checkwx.json file + instead of accessing the CheckWX API. This data must be in the same format + CheckWX would provide. See [examples/checkwx.json](examples/checkwx.json) + and [checkwxapi.com](https://www.checkwxapi.com/documentation/metar) for + more info. Only the parameters shown in the example are currently + supported. * `options` * `update-time`: boolean * Disable/enable Real Weather modifying your mission time. diff --git a/checkwx.json b/checkwx.json deleted file mode 100644 index 47ef3da..0000000 --- a/checkwx.json +++ /dev/null @@ -1,79 +0,0 @@ -{ - "results": 1, - "data": [ - { - "icao": "UGKO", - "barometer": { - "hg": 30.15, - "hpa": 1021, - "kpa": 102.1, - "mb": 1021 - }, - "ceiling": { - "feet": 4500, - "meters": 1372 - }, - "clouds": [ - { - "base_feet_agl": 1800, - "base_meters_agl": 549, - "code": "OVC", - "text": "Overcast", - "feet": 1800, - "meters": 549 - }, - { - "base_feet_agl": 4500, - "base_meters_agl": 1372, - "code": "BKN", - "text": "Broken", - "feet": 4500, - "meters": 1372 - } - ], - "dewpoint": { - "celsius": 11, - "fahrenheit": 52 - }, - "elevation": { - "feet": 144, - "meters": 44 - }, - "flight_category": "MVFR", - "humidity": { - "percent": 94 - }, - "observed": "2024-04-13T01:00:00", - "station": { - "geometry": { - "coordinates": [ - 42.482393, - 42.176768 - ], - "type": "Point" - }, - "location": "Kopitnari, Imereti, Georgia", - "name": "David the Builder Kutaisi International Airport", - "type": "Airport" - }, - "temperature": { - "celsius": 12, - "fahrenheit": 54 - }, - "raw_text": "UGKO 130100Z 22003KT 8000 SCT018 BKN045 12/11 Q1021 NOSIG", - "visibility": { - "miles": "4.97", - "miles_float": 4.97, - "meters": "8,000", - "meters_float": 8000 - }, - "wind": { - "degrees": 220, - "speed_kph": 6, - "speed_kts": 3, - "speed_mph": 3, - "speed_mps": 2 - } - } - ] -} diff --git a/config/config.go b/config/config.go index 6a4cfda..8af04d8 100644 --- a/config/config.go +++ b/config/config.go @@ -32,6 +32,7 @@ type Configuration struct { RunwayElevation int `json:"runway-elevation"` Remarks string `json:"remarks"` AddToBrief bool `json:"add-to-brief"` + UseCustomData bool `json:"use-custom-data"` } `json:"metar"` Options struct { UpdateTime bool `json:"update-time"` diff --git a/config/config.json b/config/config.json index 007f2c9..e4d00bf 100644 --- a/config/config.json +++ b/config/config.json @@ -9,7 +9,8 @@ "icao": "UGKO", "runway-elevation": 160, "remarks": "RMK Generated by DCS Real Weather", - "add-to-brief": true + "add-to-brief": true, + "use-custom-data": false }, "options": { "update-time": true, diff --git a/examples/checkwx.json b/examples/checkwx.json new file mode 100644 index 0000000..afdb92d --- /dev/null +++ b/examples/checkwx.json @@ -0,0 +1,44 @@ +{ + "results": 1, + "data": [ + { + "icao": "UGKO", + "barometer": { + "hg": 30.15 + }, + "clouds": [ + { + "code": "OVC", + "meters": 549 + }, + { + "code": "BKN", + "meters": 1372 + } + ], + "dewpoint": { + "celsius": 11 + }, + "observed": "2024-04-13T01:00:00", + "station": { + "geometry": { + "coordinates": [ + 42.482393, + 42.176768 + ] + } + }, + "temperature": { + "celsius": 12 + }, + "visibility": { + "meters_float": 8000 + }, + "wind": { + "degrees": 220, + "speed_mps": 2, + "gust_mps": 0 + } + } + ] +} diff --git a/main.go b/main.go index 6dc0fca..2556cb7 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main //go:generate goversioninfo versioninfo/versioninfo.json import ( + "bytes" "encoding/json" "flag" "fmt" @@ -108,17 +109,32 @@ func getWx() weather.WeatherData { var err error var data weather.WeatherData + // use value from config if exists. CLI argument will override a false + // parameter in the config + if config.Get().METAR.UseCustomData { + debugCheckWx = true + } + if debugCheckWx { - log.Println("Debugging CheckWx. Reading data from file...") + log.Println("Using custom weather data from file...") b, err := os.ReadFile("checkwx.json") if err != nil { log.Fatalf("Could not read checkwx.json: %v", err) } + + var minify bytes.Buffer + if err := json.Compact(&minify, b); err == nil { + log.Println("Read weather data: ", minify.String()) + } else { + log.Println("Couldn't minify custom weather data:", err) + } + err = json.Unmarshal(b, &data) if err != nil { log.Fatalf("Could not parse checkwx.json: %v", err) } - log.Println("Parsed weather data") + log.Println("Parsed weather data: ") + } else { data, err = weather.GetWeather(config.Get().METAR.ICAO, config.Get().APIKey) if err != nil {