private void setNextAlert() { //if (alertTimer != null && alertTimer.Enabled) // alertTimer.Stop(); disableAlert(); alertTimer = new System.Windows.Forms.Timer(); if (User.UserAlerts.Count > 0) { int nextAlertTime = 1000; bool validAlert = false; do { DateTime now = DateTime.Now; int hour = now.Hour; int min = now.Minute; int sec = now.Second; int timeInSec = min * 60 + hour * 3600 + sec; KeyValuePair <int, UserAlert> distToNextAlert = new KeyValuePair <int, UserAlert>(24 * 3600, null); foreach (UserAlert al in User.UserAlerts) { int distance = (al.minute * 60 + al.hour * 3600) - timeInSec; if (distance < distToNextAlert.Key) { if (distance < 0) { distToNextAlert = new KeyValuePair <int, UserAlert>(24 * 3600 + distance, al); } else { distToNextAlert = new KeyValuePair <int, UserAlert>(distance, al); } } } nextAlert = distToNextAlert.Value; alertTimer.Tick += new EventHandler(alertEvent); nextAlertTime = (distToNextAlert.Key) * 1000; if (nextAlertTime < 1000) { validAlert = false; } else { validAlert = true; } } while (!validAlert); alertTimer.Interval = nextAlertTime; alertTimer.Start(); } }
public static bool readUserFile() { alerts = new List <UserAlert>(); //reads user info from file //saves new user to file string filePath = appPath + '\\' + User.CurrentUser + ".usr"; try { FileStream fs = new FileStream(filePath, FileMode.Open); using (StreamReader stream = new StreamReader(fs)) { string buf = stream.ReadLine(); if (buf != divisionMarkers[0]) { return(false); } buf = stream.ReadLine(); if (buf != null) { User.CurrentUser = buf; } else { return(false); } buf = stream.ReadLine(); if (buf != divisionMarkers[1]) { return(false); } buf = stream.ReadLine(); if (buf != null) { User.locationCity = buf; } else { return(false); } //Alert Pref buf = stream.ReadLine(); if (buf != divisionMarkers[2]) { return(false); } buf = stream.ReadLine(); if (buf != null) { if (!Enum.TryParse(buf, out alertPref)) { return(false); } } else { return(false); } //Email Addr buf = stream.ReadLine(); if (buf != divisionMarkers[3]) { return(false); } buf = stream.ReadLine(); if (buf != null) { User.emailAddr = buf; } else { return(false); } //ALERTS buf = stream.ReadLine(); if (buf != divisionMarkers[4]) { return(false); } buf = stream.ReadLine(); while (buf != null) { string[] parts = buf.Split(';'); if (parts.Length == 4) { bool validUR; UserAlert newUR = new UserAlert(parts[0], parts[1], parts[2], parts[3], out validUR); if (validUR && User.alerts.Count < maxNumAlerts) { User.alerts.Add(newUR); } } buf = stream.ReadLine(); } } fs.Close(); } catch { return(false); } return(true); }
private void addAlert() { bool addAlert = true; if (User.UserAlerts.Count <= User.maxNumAlerts) { string name; if (txtbx_NAL_Name.Text.Length > 10) { name = txtbx_NAL_Name.Text.Substring(0, 10); } else { name = txtbx_NAL_Name.Text; } string[] time; int hr = 0, min = 0; time = txtbx_NAL_Time.Text.Split(':'); if (time.Length != 2) { addAlert = false; } else { if (Int32.TryParse(time[0], out hr) && Int32.TryParse(time[1], out min)) { if (!(hr >= 0 && hr <= 12 && min >= 0 && min <= 59)) { addAlert = false; } else { if (cmbx_NAL_Time.SelectedItem.ToString() == "pm" && hr != 12) { hr += 12; } else if (cmbx_NAL_Time.SelectedItem.ToString() == "am" && hr == 12) { hr = 0; } } } } string conditions = ""; if (chbx_NAL_All.Checked) { conditions = "All,"; } else { if (chbx_NAL_Atmosphere.Checked) { conditions += "Atmos,"; } if (chbx_NAL_Rain.Checked) { conditions += "Rain,"; } if (chbx_NAL_Snow.Checked) { conditions += "Snow,"; } if (chbx_NAL_Thunder.Checked) { conditions += "Thund,"; } } if (chbx_NAL_Cold.Checked) { int coldThreshold = 0; if (Int32.TryParse(txtbx_NAL_Cold.Text, out coldThreshold) && coldThreshold >= -100 && coldThreshold <= 330) { conditions += "Cold(" + coldThreshold.ToString() + "),"; } else { addAlert = false; } } if (chbx_NAL_Hot.Checked) { int hotThreshold = 0; if (Int32.TryParse(txtbx_NAL_Hot.Text, out hotThreshold) && hotThreshold >= -100 && hotThreshold <= 330) { conditions += "Hot(" + hotThreshold.ToString() + "),"; } else { addAlert = false; } } if (addAlert) { conditions = conditions.Remove(conditions.Length - 1, 1); bool valid; UserAlert NUR = new UserAlert(name, hr.ToString(), min.ToString(), conditions, out valid); if (valid) { User.UserAlerts.Add(NUR); } removeAlertAddControls(); removeSaveButton(); User.saveUser(); displayAlerts(); alertAddingMode = false; } } }