|
| 1 | +using DarkSky.Models; |
| 2 | +using DarkSky.Services; |
| 3 | +using Microsoft.AspNetCore.Hosting; |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using System.Globalization; |
| 7 | +using System.IO; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Runtime.InteropServices; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using static System.Math; |
| 12 | + |
| 13 | +namespace SystemInfo.Controllers |
| 14 | +{ |
| 15 | + public class InformationController : Controller |
| 16 | + { |
| 17 | + public string PublicIP { get; set; } = "IP Lookup Failed"; |
| 18 | + public double Long { get; set; } |
| 19 | + public double Latt { get; set; } |
| 20 | + public string City { get; set; } |
| 21 | + public string CurrentWeatherIcon { get; set; } |
| 22 | + public string WeatherAttribution { get; set; } |
| 23 | + public string CurrentTemp { get; set; } = "undetermined"; |
| 24 | + public string DayWeatherSummary { get; set; } |
| 25 | + public string TempUnitOfMeasure { get; set; } |
| 26 | + private readonly IHostingEnvironment _hostEnv; |
| 27 | + |
| 28 | + public InformationController(IHostingEnvironment hostingEnvironment) |
| 29 | + { |
| 30 | + _hostEnv = hostingEnvironment; |
| 31 | + } |
| 32 | + |
| 33 | + public IActionResult Index() |
| 34 | + { |
| 35 | + return View(); |
| 36 | + } |
| 37 | + |
| 38 | + public IActionResult GetInfo() |
| 39 | + { |
| 40 | + Models.InformationModel model = new Models.InformationModel(); |
| 41 | + model.OperatingSystem = RuntimeInformation.OSDescription; |
| 42 | + model.FrameworkDescription = RuntimeInformation.FrameworkDescription; |
| 43 | + model.OSArchitecture = RuntimeInformation.OSArchitecture.ToString(); |
| 44 | + model.ProcessArchitecture = RuntimeInformation.ProcessArchitecture.ToString(); |
| 45 | + |
| 46 | + string title = string.Empty; |
| 47 | + string OSArchitecture = string.Empty; |
| 48 | + |
| 49 | + if (model.OSArchitecture.ToUpper().Equals("X64")) { OSArchitecture = "64-bit"; } else { OSArchitecture = "32-bit"; } |
| 50 | + |
| 51 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { title = $"Windows {OSArchitecture}"; } |
| 52 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { title = $"OSX {OSArchitecture}"; } |
| 53 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { title = $"Linux {OSArchitecture}"; } |
| 54 | + |
| 55 | + GetLocationInfo().Wait(); |
| 56 | + model.IPAddressString = PublicIP; |
| 57 | + |
| 58 | + GetWeatherInfo().Wait(); |
| 59 | + model.CurrentIcon = CurrentWeatherIcon; |
| 60 | + model.WeatherBy = WeatherAttribution; |
| 61 | + model.CurrentTemperature = CurrentTemp; |
| 62 | + model.DailySummary = DayWeatherSummary; |
| 63 | + model.CurrentCity = City; |
| 64 | + model.UnitOfMeasure = TempUnitOfMeasure; |
| 65 | + |
| 66 | + model.InfoTitle = title; |
| 67 | + return View(model); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + #region GetLocationInfo |
| 73 | + /// <summary> |
| 74 | + /// https://ipapi.co |
| 75 | + /// </summary> |
| 76 | + /// <returns>Full location information</returns> |
| 77 | + private async Task GetLocationInfo() |
| 78 | + { |
| 79 | + var httpClient = new HttpClient(); |
| 80 | + string json = await httpClient.GetStringAsync("https://ipapi.co/json"); |
| 81 | + LocationInfo info = JsonConvert.DeserializeObject<LocationInfo>(json); |
| 82 | + |
| 83 | + PublicIP = info.ip; |
| 84 | + Long = info.longitude; |
| 85 | + Latt = info.latitude; |
| 86 | + City = info.city; |
| 87 | + } |
| 88 | + #endregion |
| 89 | + |
| 90 | + #region GetWeatherInfo |
| 91 | + /// <summary> |
| 92 | + /// https://darksky.net |
| 93 | + /// </summary> |
| 94 | + /// <returns>Weather Info for current location from Dark Sky</returns> |
| 95 | + private async Task GetWeatherInfo() |
| 96 | + { |
| 97 | + string apiKey = "e17b4001a6010f38fceb6ec98672431c"; |
| 98 | + DarkSkyService weather = new DarkSkyService(apiKey); |
| 99 | + DarkSkyService.OptionalParameters optParms = GetUnitOfMeasure(); |
| 100 | + var foreCast = await weather.GetForecast(Latt, Long, optParms); |
| 101 | + |
| 102 | + string iconFilename = GetCurrentWeatherIcon(foreCast.Response.Currently.Icon); |
| 103 | + string svgFile = Path.Combine(_hostEnv.ContentRootPath, "climacons", iconFilename); |
| 104 | + CurrentWeatherIcon = System.IO.File.ReadAllText($"{svgFile}"); |
| 105 | + |
| 106 | + WeatherAttribution = foreCast.AttributionLine; |
| 107 | + DayWeatherSummary = foreCast.Response.Daily.Summary; |
| 108 | + if (foreCast.Response.Currently.Temperature.HasValue) |
| 109 | + CurrentTemp = Round(foreCast.Response.Currently.Temperature.Value, 0).ToString(); |
| 110 | + } |
| 111 | + #endregion |
| 112 | + |
| 113 | + #region Find Unit of Measure |
| 114 | + /// <summary> |
| 115 | + /// Find Imperial or Metric UOM |
| 116 | + /// </summary> |
| 117 | + /// <returns>A boolean = Imperial or Metric UOM</returns> |
| 118 | + private DarkSkyService.OptionalParameters GetUnitOfMeasure() |
| 119 | + { |
| 120 | + bool blnMetric = RegionInfo.CurrentRegion.IsMetric; |
| 121 | + DarkSkyService.OptionalParameters optParms = new DarkSkyService.OptionalParameters(); |
| 122 | + if (blnMetric) |
| 123 | + { |
| 124 | + optParms.MeasurementUnits = "si"; |
| 125 | + TempUnitOfMeasure = "C"; |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + optParms.MeasurementUnits = "us"; |
| 130 | + TempUnitOfMeasure = "F"; |
| 131 | + } |
| 132 | + return optParms; |
| 133 | + } |
| 134 | + #endregion |
| 135 | + |
| 136 | + #region GetCurrentWeatherIcon |
| 137 | + /// <summary> |
| 138 | + /// Climacons |
| 139 | + /// 75 climatically categorised pictographs for web and UI design by @adamwhitcroft. |
| 140 | + /// Visit http://adamwhitcroft.com/climacons/ for more information. |
| 141 | + /// |
| 142 | + /// License |
| 143 | + /// You are free to use any of the Climacons Icons (the "icons") in any personal or commercial work without obligation of payment(monetary or otherwise) or attribution, |
| 144 | + /// however a credit for the work would be appreciated. |
| 145 | + /// Do not redistribute or sell and do not claim creative credit. |
| 146 | + /// Intellectual property rights are not transferred with the download of the icons. |
| 147 | + /// </summary> |
| 148 | + /// <param name="ic"></param> |
| 149 | + /// <returns>The icon name</returns> |
| 150 | + private string GetCurrentWeatherIcon(Icon ic) |
| 151 | + { |
| 152 | + string iconFilename = string.Empty; |
| 153 | + |
| 154 | + switch (ic) |
| 155 | + { |
| 156 | + case Icon.ClearDay: |
| 157 | + iconFilename = "Sun.svg"; |
| 158 | + break; |
| 159 | + |
| 160 | + case Icon.ClearNight: |
| 161 | + iconFilename = "Moon.svg"; |
| 162 | + break; |
| 163 | + |
| 164 | + case Icon.Cloudy: |
| 165 | + iconFilename = "Cloud.svg"; |
| 166 | + break; |
| 167 | + |
| 168 | + case Icon.Fog: |
| 169 | + iconFilename = "Cloud-Fog.svg"; |
| 170 | + break; |
| 171 | + |
| 172 | + case Icon.PartlyCloudyDay: |
| 173 | + iconFilename = "Cloud-Sun.svg"; |
| 174 | + break; |
| 175 | + |
| 176 | + case Icon.PartlyCloudyNight: |
| 177 | + iconFilename = "Cloud-Moon.svg"; |
| 178 | + break; |
| 179 | + |
| 180 | + case Icon.Rain: |
| 181 | + iconFilename = "Cloud-Rain.svg"; |
| 182 | + break; |
| 183 | + |
| 184 | + case Icon.Snow: |
| 185 | + iconFilename = "Snowflake.svg"; |
| 186 | + break; |
| 187 | + |
| 188 | + case Icon.Wind: |
| 189 | + iconFilename = "Wind.svg"; |
| 190 | + break; |
| 191 | + default: |
| 192 | + iconFilename = "Thermometer.svg"; |
| 193 | + break; |
| 194 | + } |
| 195 | + return iconFilename; |
| 196 | + } |
| 197 | + #endregion |
| 198 | + } |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + public class LocationInfo |
| 203 | + { |
| 204 | + public string ip { get; set; } |
| 205 | + public string city { get; set; } |
| 206 | + public string region { get; set; } |
| 207 | + public string region_code { get; set; } |
| 208 | + public string country { get; set; } |
| 209 | + public string country_name { get; set; } |
| 210 | + public string postal { get; set; } |
| 211 | + public double latitude { get; set; } |
| 212 | + public double longitude { get; set; } |
| 213 | + public string timezone { get; set; } |
| 214 | + public string asn { get; set; } |
| 215 | + public string org { get; set; } |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | + |
| 220 | + |
0 commit comments