-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesktopWeather.cs
More file actions
374 lines (343 loc) · 13.8 KB
/
DesktopWeather.cs
File metadata and controls
374 lines (343 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using HeadlessBrowser;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Extensions;
using LiveChartsCore.SkiaSharpView.Painting;
using Microsoft.Win32;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace DesktopWeather
{
public partial class weatherForm : Form
{
#region private variables
private WebBroForm myWebBrowser;
private WebBrowser returnedWebPage;
private string browseStatus;
private int humidityValue = 100;
private double pressureValue = 29.95;
private string windText;
private List<string> directionRotation = new List<string>
{"N", "NE", "E", "SE", "S", "SW", "W", "NW"};
private int windDirIndex;
private int windValue = 0;
private DateTime lastDataFetch;
private string returnedAddr;
private int temperatureValue = 70;
private bool hadAforceStop = false;
private tinyDisplay myTinyDisplay = new tinyDisplay();
private Forecast myForecastForm = new Forecast(true);
private bool computerRestarted = false;
private DateTime last30Ticker;
private int retryOnRestore = 0;
private DateTime lastNewDay;
private DateTime checkTheTime;
private DateTime lastWebRefresh;
#endregion
public DateTime lastForecast = DateTime.MinValue;
public bool weAreOffline = false;
public bool itsBeenAday = false;
public bool restartProgramFlag = false;
public string weatherURL = "https://forecast.weather.gov/data/obhistory/KVNY.html";
public string natlMapURL = "https://www.wpc.ncep.noaa.gov/sfc/usfntsfcwbg.gif";
public string forecastPage = "https://forecast.weather.gov/MapClick.php?x=264&y=129&site=lox&zmx=&zmy=&map_x=264&map_y=129";
private DateTime lastWebRefresh;
public DateTime lastForecast;
public weatherForm()
{
InitializeComponent();
getRegistryValues();
pbNatlWeather.ImageLocation = natlMapURL;
DrawTemperature();
DrawHumidity();
DrawPressure();
DrawWind();
myTinyDisplay = new tinyDisplay(this);
myTinyDisplay.Show();
lastDataFetch = DateTime.Now;
lastNewDay = DateTime.Now;
last30Ticker = DateTime.Now;
tmrStartup.Enabled = true;
this.Height = 550;
}
private void getRegistryValues()
{
RegistryKey ThisUser = Registry.CurrentUser;
try
{
RegistryKey weatherURLsettings = ThisUser.OpenSubKey("Software\\DesktopWeather\\weatherURL", true);
weatherURL = weatherURLsettings.GetValue("URLText").ToString();
RegistryKey natlMapURLsettings = ThisUser.OpenSubKey("Software\\DesktopWeather\\natlMapURL", true);
natlMapURL = natlMapURLsettings.GetValue("URLText").ToString();
RegistryKey forecastPagesettings = ThisUser.OpenSubKey("Software\\DesktopWeather\\forecastPage", true);
forecastPage = forecastPagesettings.GetValue("URLText").ToString();
}
catch { }
}
private void DrawTemperature()
{
int tmoSize = this.tmoBackgd.Height;
int newTempHeight = tmoSize * (temperatureValue - 20) / 100;
this.thermometer.Top = tmoBackgd.Bottom - newTempHeight;
this.thermometer.Height = newTempHeight;
this.lblTemperature.Text = temperatureValue.ToString();
this.lblTemperature.Top = thermometer.Top - 10;
}
private void DrawWind()
{
pieWind.Series = new ISeries[]
{
new PieSeries<int> { Values = new int[] { 5 } }
};
pieWind.InitialRotation = -95 + windValue;
}
private void DrawPressure()
{
this.piePressure.Series =
GaugeGenerator.BuildSolidGauge(
new GaugeItem(
pressureValue, // the gauge value
series => // the series style
{
series.MaxRadialColumnWidth = 14;
series.DataLabelsSize = 12;
series.DataLabelsPosition = LiveChartsCore.Measure.PolarLabelsPosition.ChartCenter;
series.DataLabelsPaint = new SolidColorPaint(SKColors.White);
series.Fill = new SolidColorPaint(SKColors.DarkGreen);
series.CornerRadius = 2;
})
);
}
private void DrawHumidity()
{
this.pieHumidity.Series =
GaugeGenerator.BuildSolidGauge(
new GaugeItem(
humidityValue, // the gauge value
series => // the series style
{
series.MaxRadialColumnWidth = 14;
series.DataLabelsSize = 12;
series.DataLabelsPosition = LiveChartsCore.Measure.PolarLabelsPosition.End;
series.DataLabelsPaint = new SolidColorPaint(SKColors.White);
series.Fill = new SolidColorPaint(SKColors.Blue);
series.CornerRadius = 2;
})
);
}
internal void BrowserIsFinished()
{
browseStatus = myWebBrowser.CurrentStatus;
if (browseStatus != "Ready")
{
DisplayStatus("Offline: " + browseStatus);
weAreOffline = true;
return;
}
returnedWebPage = myWebBrowser.myBrowser;
returnedAddr = myWebBrowser.myAddrBar.Text;
tmrForceStopBrowser.Enabled = false;
this.lblStatusBox.Visible = false;
this.Refresh();
Application.DoEvents();
weAreOffline = false;
try { ParseValuesFrom(returnedWebPage.DocumentText.ToString()); }
catch
{
weAreOffline = true;
if (hadAforceStop)
{
restartProgramFlag = true;
tmrStartup.Enabled = true;
DisplayStatus("Conx Err, Restarting");
return;
}
DisplayStatus("Parse Error");
return;
}
lastWebRefresh = DateTime.Now;
UpdateGaugeDisplays();
}
public void UpdateGaugeDisplays()
{
computerRestarted = false;
if (WindowState == FormWindowState.Normal)
{
lblLastUpdate.Text = lastWebRefresh.TimeOfDay.ToString().Substring(0,5);
lblWindBot.Visible = false;
lblWindTop.Visible = false;
if ((windDirIndex > 2) && (windDirIndex < 6))
{
lblWindTop.Visible = true;
lblWindTop.Text = windText;
}
else
{
lblWindBot.Visible = true;
lblWindBot.Text = windText;
}
DrawTemperature();
DrawHumidity();
DrawPressure();
DrawWind();
}
myTinyDisplay.tinyTempValue = temperatureValue;
myTinyDisplay.humidityValue = humidityValue;
myTinyDisplay.pressureValue = pressureValue;
myTinyDisplay.windValue = windValue;
myTinyDisplay.RedrawTiny();
}
private void ParseValuesFrom(string webPageData)
{
int dataTabStart = webPageData.IndexOf("<TBODY>");
string weatherData = webPageData.Substring(dataTabStart, 400);
string weatherData2 = weatherData.Replace("</TD>", "");
weatherData = weatherData2.Replace("<TD>", "*");
weatherData2 = weatherData.Replace("\r\n", "");
string[] cellValues = weatherData2.Split('*');
double tempAsGiven = Convert.ToDouble(cellValues[5]);
temperatureValue = Convert.ToInt16(tempAsGiven);
double dewpoint = Convert.ToDouble(cellValues[6]);
humidityValue = Convert.ToInt16(CalculateRelativeHumidity(70, dewpoint));
if (temperatureValue > 75)
{ humidityValue = Convert.ToInt16(CalculateRelativeHumidity(75, dewpoint)); }
pressureValue = Convert.ToDouble(cellValues[12]);
windText = cellValues[2].Trim();
string[] windValues = windText.Split(' ');
windDirIndex = directionRotation.IndexOf(windValues[0]);
windValue = windDirIndex * 45;
}
public static double CalculateRelativeHumidity(double temperatureFahrenheit, double dewPointFahrenheit)
{
// Convert Fahrenheit to Celsius
double temperatureCelsius = (temperatureFahrenheit - 32) * 5 / 9;
double dewPointCelsius = (dewPointFahrenheit - 32) * 5 / 9;
// Use the August-Roche-Magnus formula
double saturationVaporPressure = 6.1094 * Math.Exp((17.625 * dewPointCelsius) / (dewPointCelsius + 243.04));
double actualVaporPressure = 6.1094 * Math.Exp((17.625 * temperatureCelsius) / (temperatureCelsius + 243.04));
// Calculate relative humidity
return (saturationVaporPressure / actualVaporPressure ) * 100;
}
private void tmr30Seconds_Tick(object sender, EventArgs e)
{
checkTheTime = DateTime.Now;
if (restartProgramFlag)
{
tmrStartup.Enabled = true;
return;
}
HasItBeenADay();
if (!weAreOffline && itsBeenAday && (WindowState == FormWindowState.Normal))
{
restartProgramFlag = true;
tmrStartup.Enabled = true;
return;
}
if (weAreOffline && (WindowState == FormWindowState.Normal))
{
tryGettingData();
last30Ticker = checkTheTime;
return;
}
if ((computerRestarted) && (retryOnRestore < 5))
{
tryGettingData();
retryOnRestore++;
last30Ticker = checkTheTime;
return;
}
TimeSpan timeElapsedSinceCheck = checkTheTime - last30Ticker;
if (timeElapsedSinceCheck.TotalMinutes > 2)
{
computerRestarted = true;
retryOnRestore = 0;
weAreOffline = true;
}
last30Ticker = checkTheTime;
HasItBeenTwentyMins();
}
private void HasItBeenADay()
{
// causes restart between 4 and 10 a.m.
TimeSpan fullDayCheck = checkTheTime - lastNewDay;
if (fullDayCheck.TotalHours < 8) { return; }
if (checkTheTime.Hour < 4) { return; }
if (checkTheTime.Hour > 10) { return; }
itsBeenAday = true;
}
private void tryGettingData()
{
lastDataFetch = DateTime.Now;
DisplayStatus("Retrieving Data...");
myWebBrowser = new WebBroForm(this);
browseStatus = "";
hadAforceStop = false;
tmrForceStopBrowser.Enabled = true;
try { myWebBrowser.myBrowser.Navigate(weatherURL); }
catch
{
DisplayStatus("Offline");
weAreOffline = true;
}
}
private void DisplayStatus(string theStatus)
{
this.lblStatusBox.Text = theStatus;
this.lblStatusBox.Visible = true;
this.Refresh();
Application.DoEvents();
}
private void tmrStartup_Tick(object sender, EventArgs e)
{
if (restartProgramFlag) { Application.Restart(); return; }
tmrStartup.Enabled = false;
tryGettingData();
}
private void tmrForceStopBrowser_Tick(object sender, EventArgs e)
{
tmrForceStopBrowser.Enabled = false;
hadAforceStop = true;
myWebBrowser.processAforceStop();
}
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_RESTORE = 0xF120;
if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_RESTORE)
{ HasItBeenTwentyMins(); }
base.WndProc(ref m);
}
private void HasItBeenTwentyMins()
{
TimeSpan timeElapsedSinceCheck = checkTheTime - lastDataFetch;
if (timeElapsedSinceCheck.TotalMinutes > 19) { tryGettingData(); }
}
private void weatherForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{ myTinyDisplay.Visible = true; }
}
private void btnForecast_Click(object sender, EventArgs e)
{
TimeSpan timeElapsedSinceCheck = DateTime.Now - lastForecast;
if (timeElapsedSinceCheck.TotalMinutes > 180)
{
int saveImgCounter = myForecastForm.imgCounter;
myForecastForm = null;
Application.DoEvents();
myForecastForm = new Forecast(forecastPage, this);
myForecastForm.imgCounter = saveImgCounter;
myForecastForm.ShowDialog();
}
else { myForecastForm.ShowDialog(); }
}
private void btnSettings_Click(object sender, EventArgs e)
{
SettingsForm mySettings = new SettingsForm(this);
mySettings.ShowDialog();
return;
}
}
}