private async void SetACRelayAsync(bool on) { string result; if (on) { speechSynthesizer.Speak("Turning the AC Bus On at" + DateTime.Now.ToShortTimeString()); speechSynthesizer.Speak("Please stand By....."); result = await netDuino.DenergizeRelay2(); if (result != "Success") { speechSynthesizer.Speak("AC Bus activation failed!"); } } else { speechSynthesizer.Speak("Turning the AC Bus Off at" + DateTime.Now.ToShortTimeString()); speechSynthesizer.Speak("Please stand By....."); result = await netDuino.EnergizeRelay2(); if (result != "Success") { speechSynthesizer.Speak("AC Bus de-activation failed!"); } } }
/// <summary> /// Sends commands to set the current state of the NetDuino Relay R2 (AC Relay) based on the /// configured OnTime and OffTime values /// </summary> /// <returns> returns a TimeSpan that tells us when we need to call this method again</returns> private async Task <TimeSpan> SetNetDuinoACRelay() { string result; var alert = new AlertSender(); // First calculate the DC Bus On Time value using Today's Sunset time & // given on Time offset value: DateTime sunriseToday, sunsetToday; SunTimes.GetSunTimes(out sunriseToday, out sunsetToday); var onTime = sunsetToday - new TimeSpan(0, 0, acBusOnTimeOffset, 0); var offTime = acBusOffTime; if (onTime.TimeOfDay > offTime.TimeOfDay) { LogMessage("Invalid Configuration!. Please check the On/Off Time values"); return(TimeSpan.MinValue); } // time to wait for next state change trigger TimeSpan stateChangeInterval; if (DateTime.Now.TimeOfDay < onTime.TimeOfDay) { // we are in daytime // energize the relay 1 to turn lights off and set the interval for next state change result = await netDuino.EnergizeRelay2(); stateChangeInterval = onTime.TimeOfDay - DateTime.Now.TimeOfDay; } else if (DateTime.Now.TimeOfDay >= onTime.TimeOfDay && DateTime.Now.TimeOfDay <= offTime.TimeOfDay) { // we are in the onTime.. // de-energize relay1 to turn the lights on and set then to turn off at offTime result = await netDuino.DenergizeRelay2(); stateChangeInterval = offTime.TimeOfDay - DateTime.Now.TimeOfDay; } else { // Current time is between OffTime and midnight // energize the relays to turn the light off and set the interval to onTime + 1 Day result = await netDuino.EnergizeRelay2(); stateChangeInterval = (new TimeSpan(1, 0, 0, 0) + onTime.TimeOfDay) - DateTime.Now.TimeOfDay; } if (result == "Success") { if (changeStateR2Timer == null) { //This is the first time this method is executed // set up the timer to trigger next time the Relay state change is needed: changeStateR2Timer = new Timer(OnChangeStateR2Timer, null, stateChangeInterval, Timeout.InfiniteTimeSpan); } } else { // here we deal with the failure... // set the TimeSpan to min value to return an indication of failure and log appropriate messages and alerts stateChangeInterval = TimeSpan.MinValue; var @address = NetDuinoPlus.DeviceIPAddress.Substring(7, 13); var msg = "Netduino has failed to respond to Energize/Denergize AC relay request(s) dispatched to address: " + @address + "in a timely fashion" + $"{Environment.NewLine}Event Date & Time: {DateTime.Now.ToLongDateString()} {DateTime.Now.ToLongTimeString()} {Environment.NewLine}" + $"{Environment.NewLine}Please check Netduino and make sure that it is still online!"; LogMessage(alert.SendEmailAlert("Atert: Energize/Denergize AC Relay request to NetDuino Failed", bodyText: msg) ? "Alert dispatch via Email completed successfully" : "Attempt to send an email alert failed!"); LogMessage(alert.SendSMSAlert("Atert: Energize/Denergize Relay request to NetDuino Failed", msg) ? "Alert dispatch via SMS completed successfully" : "Attempt to send an SMS alert failed!"); } return(stateChangeInterval); }