public void readWarnings(SpreadsheetFile sf, WarningFile wf) { //Get temperature at which a warning will be produced wf.temperature = wf.getTemp(wf.pathname); float tempFloat = float.Parse(wf.temperature); string timeOfError = ""; //Console.Write(dataList); bool warning = false; foreach (var dataRow in sf.datalist) { try { //check if temperatures are greater than the warning level if (Convert.ToDouble(dataRow.reading) > tempFloat) { warning = true; timeOfError = dataRow.dateAndTime; break; } } catch (FormatException) { continue; } } if (warning) { string output = Regex.Replace(wf.temperature, "[\r,\n]", ""); Console.WriteLine($"A temperature of over {output}°C has been Recorded. It was Recorded at {timeOfError}"); } }
public static void composeEmail(SpreadsheetFile sf, string receiver) { sf.datalist = sf.assembleData(sf); string emailBody = "Thermometer Data:\n\n"; foreach (Record row in sf.datalist) { emailBody += $"{row.dateAndTime}\t{row.serialNo}\t{row.reading}\t{row.units}\n"; } //Assemble new message and client and execute delivery of message MailMessage mail = new MailMessage("*****@*****.**", receiver, "Thermometer Data", emailBody); SmtpClient client = new SmtpClient("smtp.sparkpostmail.com", 587); client.EnableSsl = true; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential("SMTP_Injection", "fae8840c80dd966210101be0dfdc9ea4e709014d"); try { client.Send(mail); Console.WriteLine("Email Sent! Check your inbox!"); } catch (Exception e) { Console.WriteLine($"Email failed to send due to this error: {e}"); } }
public static void displayData(SpreadsheetFile sf) { foreach (Record row in sf.datalist) { //Output contents of object to console Console.WriteLine($"{row.dateAndTime} \t {row.serialNo} \t {row.reading} \t {row.units}"); } }
public static void RunTimer(SpreadsheetFile sf) { string receiver = ""; Console.Write("Please enter the email address you'd like the data to be sent to: "); bool validated = false; while (!validated) { receiver = Console.ReadLine(); if (InputValidation.IsValidEmailAddress(receiver)) { validated = true; } else { Console.WriteLine("Sorry, this email address is invalid, please try again: "); } } Console.WriteLine("How often would you like to be emailed (in hours)?"); bool numValid = false; float interval = 0f; while (!numValid) { if (float.TryParse(Console.ReadLine(), out interval)) { numValid = true; } else { Console.Write("Number of hours was invalid, please try again: "); } } //get service on local machine using (TaskService ts = new TaskService()) { //set up new task definition TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Description = "Send emails concerning thermometer data"; //create trigger DailyTrigger dt = new DailyTrigger(); dt.DaysInterval = 1; dt.Repetition.Interval = TimeSpan.FromHours(interval); td.Triggers.Add(dt); //assign action of sending an email evertime the tigger is activated td.Actions.Add(@"C:\Users\peter.macaldowie\Documents\Visual Studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe", $"{receiver} {sf.pathname}"); // Register the task in the root folder of the local machine TaskService.Instance.RootFolder.RegisterTaskDefinition("Send Temperature Data", td); } }
public List <Record> assembleData(SpreadsheetFile sf) { List <Record> dataList = new List <Record>(); //int counter = 0; string[] x = File.ReadAllLines(pathname); foreach (var row in File.ReadAllLines(pathname)) { //Debug.Write(row); //rowArray contains data in each specified row string[] rowArray = row.Split(','); Record rowObj = new Record(rowArray[0], rowArray[1], rowArray[2], rowArray[3]); dataList.Add(rowObj); } return(dataList); }
static void Main(string[] args) { string location = System.Reflection.Assembly.GetEntryAssembly().Location; string executableDirectory = System.IO.Path.GetDirectoryName(location); Console.WriteLine(location); Console.WriteLine(executableDirectory); SpreadsheetFile sf = new SpreadsheetFile(); sf.pathname = sf.checkFile("Please enter the filepath of the data: "); sf.datalist = sf.assembleData(sf); WarningFile wf = new WarningFile(); wf.pathname = wf.checkFile("Please enter the file containing the warning temperature: "); wf.readWarnings(sf, wf); ConsoleWindow.consoleRequest(sf, wf.pathname); }
public static void consoleRequest(SpreadsheetFile sf, string pathname) { bool finished = false; string prompt = ""; while (!finished) { Console.WriteLine("What would you like to do? Type 'help' for more information"); string userResponse = Console.ReadLine(); switch (userResponse) { case "help": { Console.Write(@"Commands you can use: changetemp : Allows you to input a new temperature at which a warning will be produced. read: display the data stored in the spreadsheet. runtimer : Set up a timer for scheduling emails about the status of the thermometer. display: Displays data collected so far. email: recieve the data as an email. reload: Reload data stored in the spreadsheet. quit: Exit the program."); break; } case "changetemp": { prompt = "Please enter the warning temperature you would like to set: "; bool satisfied = false; while (!satisfied) { bool floatEntered = false; string temp = ""; //ensure value entered is a float while (!floatEntered) { Console.Write(prompt); temp = Console.ReadLine(); double result; floatEntered = Double.TryParse(temp, out result); prompt = "The value for a temperature must be a decimal number, please try again: "; } StreamWriter file = new StreamWriter(pathname); file.WriteLine(temp); Console.WriteLine(temp); file.Close(); satisfied = true; Console.WriteLine($"Teperature has been set to: {temp}°C"); } break; } case "read": { SpreadsheetFile.displayData(sf); break; } case "runtimer": { TimerCheck.RunTimer(sf); break; } case "email": { Console.Write("Please enter the email address you'd like the data to be sent to: "); bool validated = false; //check if email address given is valid while (!validated) { string receiver = Console.ReadLine(); if (InputValidation.IsValidEmailAddress(receiver)) { email.composeEmail(sf, receiver); validated = true; } else { Console.WriteLine("Sorry, this email address is invalid, please try again: "); } } break; } case "reload": { sf.datalist = sf.assembleData(sf); break; } case "updatedatabase": { WriteToDatabase.writeData(sf); break; } case "quit": { Environment.Exit(0); break; } default: { Console.WriteLine("Sorry, that command is invalid. Please try again. Note: Type 'help' for more information."); break; } } } }