Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
280 changes: 224 additions & 56 deletions Main.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extends Node

#Variables
var city
var state
var stateCode
Expand All @@ -23,63 +24,103 @@ var AlertBase = "https://api.weather.gov/alerts/active/zone/"
var ActiveAlertURL
var Alerts
var AlertCount
var EPAKey
var EPABaseURL = "https://www.airnowapi.org/"
var EPAObsURL = "aq/observation/zipCode/current/?format=application/json&zipCode="
var EPADist = "&distance=50"
var EPAAPIKey = "&API_KEY="

func _ready():
print("Program Starting")
#Set Alert Display to be invisble
$GUI/AlertDisplay.visible = false
#Show Forecasts Page
$GUI/Forecasts.visible = true
#More alert stuff. Invisible to start.
$GUI/Forecasts/Window/NowCast/Alert.visible = false
#Hide server failure message at start.
$GUI/ServerFailure.visible = false
#Hide Settings Menu
$GUI/Settings.visible = false
#Hide Air Quality Page
$GUI/AirQuality.visible = false
findLocation()
#Loads Settings
LoadSettings()

func findLocation():
#Request location data from the ip-api API
print("Finding Location...")
$NetworkController/Location.request(GeoAPI)

func _on_Location_request_completed(result, response_code, headers, body):
#ip-api response.
print("Location Found.")
var json = JSON.parse(body.get_string_from_utf8())
city = json.result.city
state = json.result.regionName
stateCode = json.result.region
zipCode = json.result.zip
lat = json.result.lat
long = json.result.lon
#lat = 33.4025 #For Testing.
#long = -84.522 #For Testing
fullURL = str(baseURL, lat, ",", long)
CityState = str(city," ",state)
$GUI/Forecasts/Window/Header/Location.text = String(CityState)
print("Lat:")
print(lat)
print("Long:")
print(long)
print("Full URL:")
print(fullURL)
print("Fetching Forecast URLs...")
getGridPoints()
print(json.result.status)
#Validate return data
if json.result.status == "success":
#If a request is successful, do this:
city = json.result.city
state = json.result.regionName
stateCode = json.result.region
zipCode = json.result.zip
lat = json.result.lat
long = json.result.lon
#lat = 33.4025 #For Testing.
#long = -84.522 #For Testing
fullURL = str(baseURL, lat, ",", long)
CityState = str(city," ",state)
$GUI/Forecasts/Window/Header/Location.text = String(CityState)
print("Lat:")
print(lat)
print("Long:")
print(long)
print("Full URL:")
print(fullURL)
print("Fetching Forecast URLs...")
getGridPoints()
else:
print("Location API Server Failure")
#Otherwise, display server failure screen
$GUI/Forecasts.visible = false
$GUI/AlertDisplay.visible = false
$GUI/ServerFailure.visible = true
$GUI/ServerFailure/Body/Response.text = json.result.status

func getGridPoints():
#Find NWS gridpoints using the ip-api data
$NetworkController/Forecast.request(fullURL)

func _on_Forecast_request_completed(result, response_code, headers, body):
print("Forecast URLs Returned.")
var GPjson = JSON.parse(body.get_string_from_utf8())
ForecastURL = GPjson.result.properties.forecast
HForecastURL = GPjson.result.properties.forecastHourly
CatForecastURL = str(ForecastURL,ImpUnits)
ZoneURL = GPjson.result.properties.county
print("Forecast URL:")
print(ForecastURL)
print("Hourly URL:")
print(HForecastURL)
print("Full Weekly URL:")
print(CatForecastURL)
print("Zone URL:")
print(ZoneURL)
print("Getting Weekly Forecast")
getHourlyForecast()
getWeeklyForecast()
getZoneID()
#Validate return data
if response_code == 200:
#Build URLs needed for forecast requests
ForecastURL = GPjson.result.properties.forecast
HForecastURL = GPjson.result.properties.forecastHourly
CatForecastURL = str(ForecastURL,ImpUnits)
ZoneURL = GPjson.result.properties.county
print("Forecast URL:")
print(ForecastURL)
print("Hourly URL:")
print(HForecastURL)
print("Full Weekly URL:")
print(CatForecastURL)
print("Zone URL:")
print(ZoneURL)
print("Getting Weekly Forecast")
getHourlyForecast()
getWeeklyForecast()
getZoneID()
else:
print("Forecast URL Server Error")
#Display server failure page
$GUI/Forecasts.visible = false
$GUI/AlertDisplay.visible = false
$GUI/ServerFailure.visible = true
$GUI/ServerFailure/Body/Response.text = String(response_code)

func getHourlyForecast():
$NetworkController/HoulyForecast.request(HForecastURL)
Expand All @@ -92,31 +133,59 @@ func getZoneID():

func _on_HoulyForecast_request_completed(result, response_code, headers, body):
var HourlyForecastJSON = JSON.parse(body.get_string_from_utf8())
HourlyForecastResult = HourlyForecastJSON.result.properties.periods
PrintHourlyResults()
if response_code == 200:
HourlyForecastResult = HourlyForecastJSON.result.properties.periods
PrintHourlyResults()
else:
print("Hourly Forecast Server Failure")
$GUI/Forecasts.visible = false
$GUI/AlertDisplay.visible = false
$GUI/ServerFailure.visible = true
$GUI/ServerFailure/Body/Response.text = String(response_code)

func _on_WeeklyForecast_request_completed(result, response_code, headers, body):
var WeeklyForecastJSON = JSON.parse(body.get_string_from_utf8())
WeeklyForecastResult = WeeklyForecastJSON.result.properties.periods
PrintWeeklyResults()
if response_code == 200:
WeeklyForecastResult = WeeklyForecastJSON.result.properties.periods
PrintWeeklyResults()
else:
print("Weekly Forecast Server Failure")
$GUI/Forecasts.visible = false
$GUI/AlertDisplay.visible = false
$GUI/ServerFailure.visible = true
$GUI/ServerFailure/Body/Response.text = String(response_code)

func _on_ZoneIDRequest_request_completed(result, response_code, headers, body):
var ZoneIDRequestResult = JSON.parse(body.get_string_from_utf8())
ZoneID = ZoneIDRequestResult.result.properties.id
ActiveAlertURL = str(AlertBase,ZoneID)
print("ZoneID:")
print(ZoneID)
print("Active Alerts URL:")
print(ActiveAlertURL)
getAlerts()
if response_code == 200:
ZoneID = ZoneIDRequestResult.result.properties.id
ActiveAlertURL = str(AlertBase,ZoneID)
print("ZoneID:")
print(ZoneID)
print("Active Alerts URL:")
print(ActiveAlertURL)
getAlerts()
else:
print("Zone ID Request Server Failure")
$GUI/Forecasts.visible = false
$GUI/AlertDisplay.visible = false
$GUI/ServerFailure.visible = true
$GUI/ServerFailure/Body/Response.text = String(response_code)

func getAlerts():
$NetworkController/AlertRequest.request(ActiveAlertURL)

func _on_AlertRequest_request_completed(result, response_code, headers, body):
var AlertJSON = JSON.parse(body.get_string_from_utf8())
Alerts = AlertJSON.result.features
AlertHandler()
if response_code == 200:
Alerts = AlertJSON.result.features
AlertHandler()
else:
print("Alert Request Server Failure")
$GUI/Forecasts.visible = false
$GUI/AlertDisplay.visible = false
$GUI/ServerFailure.visible = true
$GUI/ServerFailure/Body/Response.text = String(response_code)

func AlertHandler():
var AlertNumberUpdate
Expand Down Expand Up @@ -152,12 +221,12 @@ func alertProcessing():

for number in range(i):
#Make Instance
print("Loop:", Counter)
#print("Loop:", Counter)
var NewInstance = scene.instance()
NewInstance.name = "AlertInfo" + String(Counter)
path.add_child(NewInstance)
print("New Instance Name:")
print(NewInstance.name)
#print("New Instance Name:")
#print(NewInstance.name)
NewInstance.get_node("Intro/AlertNum").text = String(AlertNumber)
NewInstance.get_node("Headline/HLabel").text = String(Alerts[Counter].properties.headline)
NewInstance.get_node("AlertInfo1/A1Status").text = String(Alerts[Counter].properties.severity)
Expand Down Expand Up @@ -194,7 +263,7 @@ func PrintHourlyResults():
$GUI/Forecasts/Window/NowCast/NowWind/WindSpeed.text = String(HourlyForecastResult[0].windSpeed)
$GUI/Forecasts/Window/NowCast/NowWind/Direction.text = String(HourlyForecastResult[0].windDirection)
$GUI/Forecasts/Window/NowCast/ShortCast.text = String(HourlyForecastResult[0].shortForecast)
#Hourly Forecast for Next 24 Hours
#Hourly Forecast for Next 155 Hours
var Counter = 1
var HourlyCount = HourlyForecastResult.size()
var scene = ResourceLoader.load("res://T0.tscn")
Expand All @@ -205,14 +274,14 @@ func PrintHourlyResults():
print(HourlyCount)
print(Counter)

for number in range(23):
print("Loop:", Counter)
for number in range(155):
#print("Loop:", Counter)
var NewInstance = scene.instance()
var vInst = SepScene.instance()
NewInstance.name = "T" + String(Counter)
path.add_child(NewInstance)
print("New Instance Name:")
print(NewInstance.name)
#print("New Instance Name:")
#print(NewInstance.name)
#Handle Time
var DateTime = HourlyForecastResult[Counter].startTime
var Date = DateTime.split("T")[0].split("-")
Expand Down Expand Up @@ -245,7 +314,106 @@ func PrintHourlyResults():
path.add_child(vInst)
Counter = Counter + 1

func _on_CloseApp_pressed():
get_tree().quit()

#Settings
func SaveSettings():
#Save User Settings
var file = File.new()
file.open("user://MiniWeatherSettings.dat", File.WRITE)
file.store_var(EPAKey)
file.close()

func LoadSettings():
#Load User Settings
var file = File.new()
file.open("user://MiniWeatherSettings.dat", File.READ)
EPAKey = file.get_var()
file.close()
$GUI/Settings/Body/EPAKey/LineEdit.text = EPAKey

func _on_Settings_pressed():
#Hide Everything, then show Settings page
$GUI/Forecasts.visible = false
$GUI/AlertDisplay.visible = false
$GUI/Settings.visible = true

func _on_LineEdit_text_changed(new_text):
EPAKey = new_text
print("New Text:")
print(EPAKey)

func _on_Return_pressed():
SaveSettings()
$GUI/Settings.visible = false
$GUI/Forecasts.visible = true

#Air Quality
func _on_AirQuality_pressed():
#Hide Everything
$GUI/AlertDisplay.visible = false
$GUI/Forecasts.visible = false
#Show Air Quality Page
$GUI/AirQuality.visible = true
EPAQuery()

func EPAQuery():
#Cat EPA API URL Request
var EPAFullURL = str(EPABaseURL,EPAObsURL,zipCode,EPADist,EPAAPIKey,EPAKey)
print("Full EPA API URL:")
print(EPAFullURL)
$NetworkController/EPAReq.request(EPAFullURL)

func _on_EPAReq_request_completed(result, response_code, headers, body):
var EPAResult = JSON.parse(body.get_string_from_utf8())
if response_code == 200:
var EPAJson = EPAResult.result
print(EPAJson)
$GUI/AirQuality/Body/DateTimeObs/Date.text = String(EPAJson[0].DateObserved)
$GUI/AirQuality/Body/DateTimeObs/Time.text = String(EPAJson[0].HourObserved)
$GUI/AirQuality/Body/DateTimeObs/TimeZone.text = String(EPAJson[0].LocalTimeZone)
$GUI/AirQuality/Body/Values/AQIValue/AQI2.text = String(EPAJson[0].AQI)
$GUI/AirQuality/Body/Values/CatNumVal/CatNumber2.text = String(EPAJson[0].Category.Number)
$GUI/AirQuality/Body/Values/CatNumVal/CatName.text = String(EPAJson[0].Category.Name)
#Modulate CatName Color
if EPAJson[0].Category.Number == 1:
$GUI/AirQuality/Body/Values/CatNumVal/CatName.modulate = Color(0,1,0,1)
elif EPAJson[0].Category.Number == 2:
$GUI/AirQuality/Body/Values/CatNumVal/CatName.modulate = Color(1,1,0,1)
elif EPAJson[0].Category.Number == 3:
$GUI/AirQuality/Body/Values/CatNumVal/CatName.modulate = Color(1,0.65,0,1)
elif EPAJson[0].Category.Number == 4:
$GUI/AirQuality/Body/Values/CatNumVal/CatName.modulate = Color(1,0,0,1)
elif EPAJson[0].Category.Number == 5:
$GUI/AirQuality/Body/Values/CatNumVal/CatName.modulate = Color(0.63,0.13,0.94,1)
elif EPAJson[0].Category.Number == 6:
$GUI/AirQuality/Body/Values/CatNumVal/CatName.modulate = Color(1,0,1,1)
#Convert HourObserved from 24h to 12h
var TimeStamp = EPAJson[0].HourObserved
if TimeStamp > 12:
TimeStamp = TimeStamp - 12
$GUI/AirQuality/Body/DateTimeObs/Time.text = String(TimeStamp)
$GUI/AirQuality/Body/DateTimeObs/Time3.text = "PM"
elif TimeStamp <= 12:
$GUI/AirQuality/Body/DateTimeObs/Time.text = String(TimeStamp)
$GUI/AirQuality/Body/DateTimeObs/Time3.text = "AM"
elif response_code == 401:
$GUI/AirQuality.visible = false
_on_Settings_pressed()
else:
print("EPA Request Server Failure")
$GUI/Forecasts.visible = false
$GUI/AlertDisplay.visible = false
$GUI/AirQuality.visible = false
$GUI/ServerFailure.visible = true
$GUI/ServerFailure/Body/Response.text = String(response_code)

func _on_ReturnAQ_pressed():
#Hide Air Quality Page, show Forecasts
$GUI/AirQuality.visible = false
$GUI/Forecasts.visible = true

func PrintWeeklyResults():
$GUI/Forecasts/Window/WeeklyScroll.get_v_scrollbar().modulate = Color(0, 0, 0, 0) #Hide Scroll Bar
$GUI/Forecasts/Window/HourlyScroll.get_h_scrollbar().modulate = Color(0, 0, 0, 0)
Expand Down
Loading