/* * Button1_Click: * When a new request is added, the changes are first reflected in the savefile, * then the form is updated based on the savefile. There is likely a better way to do this, * but it ensures the save file is always up to date. */ private void Button1_Click(object sender, EventArgs e) //As soon as the BrightnessRequest is added, changes are reflected in the file { running.stopThread(); FileManager fm = new FileManager(fileLoc, fileNameR); String[] rawData = null; double inputBrightness = 1.0; //If the input is not valid if (!Time24Hours.isValid24HTime(textBox1.Text, ':') || !Time24Hours.isValid24HTime(textBox2.Text, ':') || !double.TryParse(textBox3.Text, out inputBrightness)) { inputError1(); return; } //Convert user input to time values Time24Hours startT = Time24Hours.stringTo24HTime(textBox1.Text + ":00", ':'); Time24Hours endT = Time24Hours.stringTo24HTime(textBox2.Text + ":00", ':'); //Assemble request BrightnessRequest newReq = BrightnessRequest.fromString(0, startT.toFileString() + ";" + endT.toFileString() + ";" + inputBrightness.ToString()); if (newReq != null && !newReq.isValid()) //Check if request is valid { inputError1(); return; } foreach (BrightnessRequest BR in requests) { if (newReq.getStartTime().fallsInbetween(BR.getStartTime(), BR.getEndTime()) || newReq.getEndTime().fallsInbetween(BR.getStartTime(), BR.getEndTime())) { inputError2(BR.orderNum); return; } if (newReq.getStartTime().equals(BR.getStartTime()) || newReq.getEndTime().equals(BR.getEndTime())) { inputError2(BR.orderNum); return; } } requests.Add(newReq); //Add request to list fm.saveAllBR(requests); //Save all requests fm.initialize(ref rawData); //Load all requests to string loadRequests(rawData); //Overwrite displayEvents(); textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); running.startThread(); }
private void serviceRequests() { if (toService == null) { return; } try { while (true) { if (useRR && isValidRefreshRate(refreshRate)) { System.Threading.Thread.Sleep(refreshRate); } else { System.Threading.Thread.Sleep(3000); } Console.WriteLine("TICK\n"); DateTime localDT = DateTime.Now; Time24Hours localTime = Time24Hours.stringTo24HTime(localDT.ToString("HH:mm:ss"), ':'); if (localTime != null) { bool defaultB = true; foreach (BrightnessRequest BR in toService) { if (localTime.fallsInbetween(BR.getStartTime(), BR.getEndTime())) //If the current time is in any request in toService { setBrightness(BR.getBrightness()); defaultB = false; } } if (defaultB && useDefBright) { setBrightness(this.defaultBrightness); } } } } catch (ThreadInterruptedException) { return; } }
public static BrightnessRequest fromString(int orderN, String toConvert) { if (toConvert == null) { return(null); } BrightnessRequest toReturn = new BrightnessRequest(); toReturn.orderNum = orderN; String[] split = toConvert.Split(';'); if (split.Length != 3) { return(null); } Time24Hours startT = Time24Hours.stringTo24HTime(split[0], ','); Time24Hours endT = Time24Hours.stringTo24HTime(split[1], ','); int lightL = 1; //We never want the light level to default to 0% if (!int.TryParse(split[2], out lightL)) { return(null); } if (lightL < 0 || lightL > 100) { return(null); } if (startT == null || endT == null) { return(null); } if (!startT.isValid() || !endT.isValid()) { return(null); } toReturn.startTime = startT; toReturn.endTime = endT; toReturn.brightness = lightL; return(toReturn); }