/* * 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(); }
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); }
private void loadRequests(string[] rawData) //Loads requests from a string array to the BR list { requests.Clear(); if (rawData == null) { return; } int count = 1; foreach (string s in rawData) { BrightnessRequest temp = BrightnessRequest.fromString(count, s); if (temp != null && temp.isValid()) { requests.Add(temp); count++; } } }
private void Button2_Click(object sender, EventArgs e) //Remove request from requestlist, modify data file to reflect changes { running.stopThread(); FileManager fm = new FileManager(fileLoc, fileNameR); String[] rawData = null; int toRemove = 1; if (!int.TryParse(textBox4.Text, out toRemove)) { consoleAppend("Please enter a valid number to remove"); return; } BrightnessRequest toRem = null; foreach (BrightnessRequest BR in requests) { if (BR.orderNum == toRemove) { toRem = BR; } } if (toRem == null) { consoleAppend("The number you have entered is not a valid setting number"); return; } requests.Remove(toRem); fm.saveAllBR(requests); //Save all requests fm.initialize(ref rawData); //Load all requests to string loadRequests(rawData); //Overwrite displayEvents(); consoleAppend("Brightness setting removed succesfully"); running.startThread(); }