Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
grumpy-lions committed Dec 22, 2021
1 parent 97c0fe5 commit 65c422b
Show file tree
Hide file tree
Showing 10 changed files with 589 additions and 0 deletions.
25 changes: 25 additions & 0 deletions NWS.net.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.32002.261
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NWS.net", "NWS.net\NWS.net.csproj", "{C4F2EC71-1D8A-48FE-AB07-4748BF9592A8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C4F2EC71-1D8A-48FE-AB07-4748BF9592A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C4F2EC71-1D8A-48FE-AB07-4748BF9592A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C4F2EC71-1D8A-48FE-AB07-4748BF9592A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C4F2EC71-1D8A-48FE-AB07-4748BF9592A8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {97F484EC-91BC-4CDE-A8C8-0B96FA37D678}
EndGlobalSection
EndGlobal
65 changes: 65 additions & 0 deletions NWS.net/Alert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace NWS.net {
public class Alert {

public DateTime Effective { get; private set; }
public DateTime Expires { get; private set; }
public string MessageType { get; private set; }
public string Severity { get; private set; }
public string Certainty { get; private set; }
public string Urgency { get; private set; }
public string Event { get; private set; }
public string SendingOffice { get; private set; }
public string Headline { get; private set; }
public string Description { get; private set; }
public string Instructions { get; private set; }
public string Response { get; private set; }
public string MessageHeadline { get; private set; }
public List<string> AffectedZones { get; private set; } = new();

public Alert(List<string> Data) {
bool Zone = false;
bool NWSHead = false;
foreach (string dataPoint in Data) {
if (dataPoint.Contains("\"effective\": ")) {
string time = dataPoint.Split("\"effective\": ")[1].Split("\"")[1];
Effective = new DateTime(int.Parse(time.Split("-")[0]),
int.Parse(time.Split("-")[1]), int.Parse(time.Split("-")[2].Split("T")[0]),
int.Parse(time.Split("-")[2].Split("T")[1]), 0, 0);
} else if (dataPoint.Contains("\"expires\": ")) {
string time = dataPoint.Split("\"expires\": ")[1].Split("\"")[1];
Expires = new DateTime(int.Parse(time.Split("-")[0]),
int.Parse(time.Split("-")[1]), int.Parse(time.Split("-")[2].Split("T")[0]),
int.Parse(time.Split("-")[2].Split("T")[1]), 0, 0);
} else if (dataPoint.Contains("\"messageType\": ")) {
MessageType = dataPoint.Split("\"messageType\": ")[1].Split("\"")[1];
} else if (dataPoint.Contains("\"severity\": ")) {
Severity = dataPoint.Split("\"severity\": ")[1].Split("\"")[1];
} else if (dataPoint.Contains("\"certainty\": ")) {
Certainty = dataPoint.Split("\"certainty\": ")[1].Split("\"")[1];
} else if (dataPoint.Contains("\"urgency\": ")) {
Urgency = dataPoint.Split("\"urgency\": ")[1].Split("\"")[1];
} else if (dataPoint.Contains("\"event\":")) {
Event = dataPoint.Split("\"event\": ")[1].Split("\"")[1];
} else if (dataPoint.Contains("\"headline\":")) {
Headline = dataPoint.Split("\"headline\": ")[1].Split("\"")[1];
} else if (dataPoint.Contains("\"description\": ")) {
Description = dataPoint.Split("\"description\": ")[1].Split("\"")[1];
} else if (dataPoint.Contains("\"instructions\": ")) {
Instructions = dataPoint.Split("\"instructions\": ")[1].Split("\"")[1];
} else if (dataPoint.Contains("\"NWSheadline\": ")) {
NWSHead = true;
} else if (dataPoint.Contains("\"UGC\" :")) {
Zone = true;
} else if (Zone) {
AffectedZones.Add(dataPoint.Split("\"")[1]);
} else if (NWSHead) {
MessageHeadline = dataPoint.Split("\"")[1];
} else if (dataPoint.Contains("],")) {
Zone = false;
NWSHead = false;
}
}
}

}
}
35 changes: 35 additions & 0 deletions NWS.net/Alerts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace NWS.net {
public class Alerts {

public Alert[] ActiveAlerts { get; private set; }

string API_Base { get; } = "https://api.weather.gov/alerts/active?point=";

public Alerts(double[] Location) {
using WebClient wc = new();
wc.Headers.Add("user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36");
string Response = wc.DownloadString(API_Base + Location[0] + "," + Location[1]);
using StringReader parser = new(Response);
string currentLine = string.Empty;
List<string> dataInput = new();
List<Alert> data = new();
bool start = false;
do {
try {
currentLine = parser.ReadLine();
if (currentLine.Contains("\"id\":")) { start = true; }
if (currentLine.Contains("},") || currentLine.Contains("],")) {
start = false;
data.Add(new Alert(dataInput));
dataInput.Clear();
}
if (start) {
dataInput.Add(currentLine);
}
} catch (NullReferenceException) { }
} while (currentLine != null);
ActiveAlerts = data.ToArray();
}

}
}
35 changes: 35 additions & 0 deletions NWS.net/Forecast.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace NWS.net {
public class Forecast {

public ForecastData[] ForecastData { get; private set; }

public Forecast(string URL) {
using WebClient wc = new();
wc.Headers.Add("user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36");
string Response = wc.DownloadString(URL);
using StringReader parser = new(Response);
string currentLine = string.Empty;
List<string> dataInput = new();
List<ForecastData> data = new();
bool start = false;
do {
currentLine = parser.ReadLine();
try {
if (currentLine.Contains("\"number\":")) { start = true; }
if (currentLine.Contains("},") || currentLine.Contains("]")) {
if (start) {
start = false;
data.Add(new ForecastData(dataInput));
dataInput.Clear();
}
}
if (start) {
dataInput.Add(currentLine);
}
} catch (NullReferenceException) { }
} while (currentLine != null);
ForecastData = data.ToArray();
}

}
}
55 changes: 55 additions & 0 deletions NWS.net/ForecastData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace NWS.net {
public class ForecastData {

public string Day { get; set; } = "Today";
public DateTime Start { get; set; } = DateTime.Now;
public DateTime End { get; set; } = DateTime.Today;
public bool Daytime { get; set; } = true;
public int Temperature { get; set; }= 32;
public string TemperatureUnit { get; set; } = "F";
public string TemperatureTrend { get; set; } = string.Empty;
public int WindSpeed { get; set; } = 0;
public string WindSpeedUnit { get; set; } = "mph";
public string WindDirection { get; set; } = "N";
public string ShortForcast { get; set; } = string.Empty;
public string DetailedForecast { get; set; } = string.Empty;

public ForecastData(List<string> Items) {
foreach(string data in Items) {
if(data.Contains("\"name\": ")) {
Day = data.Split("\"name\": ")[1].Split("\"")[1];
}else if(data.Contains("\"startTime\": ")) {
string time = data.Split("\"startTime\": ")[1].Split("\"")[1];
Start = new DateTime(int.Parse(time.Split("-")[0]),
int.Parse(time.Split("-")[1]), int.Parse(time.Split("-")[2].Split("T")[0]),
int.Parse(time.Split("-")[2].Split("T")[1].Split(":")[0]), 0, 0);
} else if(data.Contains("\"endTime\": ")) {
string time = data.Split("\"endTime\": ")[1].Split("\"")[1];
End = new DateTime(int.Parse(time.Split("-")[0]),
int.Parse(time.Split("-")[1]), int.Parse(time.Split("-")[2].Split("T")[0]),
int.Parse(time.Split("-")[2].Split("T")[1].Split(":")[0]), 0, 0);
} else if(data.Contains("\"isDaytime\": ")) {
Daytime = bool.Parse(data.Split("\"isDaytime\": ")[1].Split(",")[0]);
} else if(data.Contains("\"temperature\": ")) {
Temperature = int.Parse(data.Split("\"temperature\": ")[1].Split(",")[0]);
} else if(data.Contains("\"temperatureUnit\": ")) {
TemperatureUnit = data.Split("\"temperatureUnit\": ")[1].Split("\"")[1];
} else if(data.Contains("\"temperatureTrend\": ")) {
try {
TemperatureTrend = data.Split("\"temperatureTrend\": ")[1].Split("\"")[1];
} catch (IndexOutOfRangeException) { TemperatureTrend = string.Empty; }
} else if(data.Contains("\"windSpeed\": ")) {
WindSpeed = int.Parse(data.Split("\"windSpeed\": ")[1].Split("\"")[1].Split(" ")[0]);
WindSpeedUnit = data.Split("\"windSpeed\": ")[1].Split("\"")[1].Split(" ")[1];
} else if(data.Contains("\"windDirection\" : ")) {
WindDirection = data.Split("\"windDirection\": ")[1].Split("\"")[1].Split(" ")[1];
} else if(data.Contains("\"shortForecast\": ")) {
ShortForcast = data.Split("\"shortForecast\": ")[1].Split(",")[0];
} else if(data.Contains("\"detailedForecast\": ")) {
DetailedForecast = data.Split("\"detailedForecast\":")[1].Split("\"")[1].Split(" ")[1];
}
}
}

}
}
72 changes: 72 additions & 0 deletions NWS.net/Location.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace NWS.net {
public class Location {

public string City { get; private set; } = "Washington";
public string State { get; private set; } = "DC";
public string County { get; private set; } = "District of Columbia";

public TimeZoneInfo TimeZone { get; private set; } = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneTranslate.OlsenToWin32["America/New_York"]);
public string RadarStation { get; private set; } = "KLWX";
public string ForecastURL { get; private set; }

public int[] GridCoordinates { get; private set; } = new int[] { 96, 70 };
public string Zone { get; private set; } = "DCZ001";

string BASE_API { get; } = "https://api.weather.gov/points/";
string API_RESPONSE { get; set; }

public Location(double[] GPS) {
using WebClient wc = new();
wc.Headers.Add("user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36");
API_RESPONSE = wc.DownloadString(BASE_API + GPS[0] + "," + GPS[1]);
using StringReader parser = new(API_RESPONSE);
string currentLine = string.Empty;

do {

currentLine = parser.ReadLine();
try {
if (currentLine.Contains("\"gridX\": ")) {
GridCoordinates[0] = int.Parse(currentLine.Split("\"gridX\": ")[1].Split(",")[0]);
} else if (currentLine.Contains("\"gridY\": ")) {
GridCoordinates[1] = int.Parse(currentLine.Split("\"gridY\": ")[1].Split(",")[0]);
} else if (currentLine.Contains("\"forecast\": ")) {
ForecastURL = currentLine.Split("\"forecast\": ")[1].Split("\"")[1];
} else if (currentLine.Contains("\"city\": ")) {
City = currentLine.Split("\"city\": ")[1].Split("\"")[1];
} else if (currentLine.Contains("\"state\": ")) {
State = currentLine.Split("\"state\": ")[1].Split("\"")[1];
} else if (currentLine.Contains("\"county\": ")) {
try {
County = GetCounty(currentLine.Split("\"county\": ")[1].Split("\"")[1]);
} catch (IndexOutOfRangeException) { }
} else if (currentLine.Contains("\"timeZone\": ")) {
string timeZone = currentLine.Split("\"timeZone\": ")[1].Split("\"")[1];
TimeZone = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneTranslate.OlsenToWin32[timeZone]);
} else if (currentLine.Contains("\"radarStation\": ")) {
RadarStation = currentLine.Split("\"radarStation\": ")[1].Split("\"")[1];
} else if (currentLine.Contains("\"forecastZone\": ")) {
Zone = currentLine.Split("\"forecastZone\": ")[1].Split("\"")[1].Split("/")[5];
}
} catch (NullReferenceException) { }
} while (currentLine != null);

}
private static string GetCounty(string URL) {
using WebClient wc = new();
wc.Headers.Add("user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36");
string Response = wc.DownloadString(URL);
using StringReader parser = new(Response);
string currentLine = string.Empty;

do {
currentLine = parser.ReadLine();
if(currentLine.Contains("\"name\": ")) {
return currentLine.Split("\"name\": ")[1].Split("\"")[1];
}
} while (currentLine != null);

return null;
}
}
}
16 changes: 16 additions & 0 deletions NWS.net/NWS.net.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<AnalysisLevel>preview</AnalysisLevel>
<LangVersion>preview</LangVersion>
<RootNamespace>NWS.net</RootNamespace>
<AssemblyName>NWS.net</AssemblyName>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

</Project>
89 changes: 89 additions & 0 deletions NWS.net/NWSAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
global using System;
global using System.ComponentModel;
global using System.IO;
global using System.Net;
global using System.Collections.Generic;

namespace NWS.net {
public class NWSAPI {

#region Non Visible
static readonly double[] DEFAULT = new double[] { 38.9072, -77.0369 };
private double[] Position { get; set; } = new double[2];
#endregion

public Location Location { get { return new Location(Position); } }
public Forecast Forecast { get { return new Forecast(Location.ForecastURL); } }
public Alerts Alerts { get { return new Alerts(Position); } }
public WeatherMaps WeatherMap { get { return new WeatherMaps(Location.RadarStation); } }

public NWSAPI(string IP) {
using WebClient wc = new();
wc.Headers.Add("user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36");
string LOCATION_API = wc.DownloadString("http://ip-api.com/json/" + IP);
try {
Position[0] = double.Parse(LOCATION_API.Split("\"lon\":")[1].Split(",")[0]);
Position[1] = double.Parse(LOCATION_API.Split("\"lat\":")[1].Split(",")[0]);
} catch (Exception) {
Position = DEFAULT;
}
}

public void SetPosition(double Latitude, double Longitude) {
Position[0] = Latitude;
Position[1] = Longitude;
}
public void SetPosition(string Latitude, string Longitude) {
try {
Position[0] = int.Parse(Latitude);
Position[1] = int.Parse(Longitude);
} catch (Exception) {
Position = DEFAULT;
}
}
public void SetPosition(string IP) {
using WebClient wc = new();
wc.Headers.Add("user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36");
string LOCATION_API = wc.DownloadString("http://ip-api.com/json/" + IP);
try {
Position[0] = double.Parse(LOCATION_API.Split("\"lon\":")[1].Split(",")[0]);
Position[1] = double.Parse(LOCATION_API.Split("\"lat\":")[1].Split(",")[0]);
} catch (Exception) {
Position = DEFAULT;
}
}
public void SetPosition() {
using WebClient wc = new();
wc.Headers.Add("user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36");
string LOCATION_API = wc.DownloadString("http://ip-api.com/json/");
try {
Position[0] = double.Parse(LOCATION_API.Split("\"lon\":")[1].Split(",")[0]);
Position[1] = double.Parse(LOCATION_API.Split("\"lat\":")[1].Split(",")[0]);
} catch (Exception) {
Position = DEFAULT;
}
}

public void Reset() {
Position = DEFAULT;
}

public NWSAPI() {
Position = DEFAULT;
}

public NWSAPI(double Latitude, double Longitude) {
Position[0] = Latitude;
Position[1] = Longitude;
}

public NWSAPI(string Latitude, string Longitude) {
try {
Position[0] = int.Parse(Latitude);
Position[1] = int.Parse(Longitude);
} catch (Exception) {
Position = DEFAULT;
}
}
}
}
Loading

0 comments on commit 65c422b

Please sign in to comment.