Beispiel #1
0
        private async void SetDCRelayAsync(bool on)
        {
            string result;

            if (on)
            {
                speechSynthesizer.Speak("Turning the DC Bus On at" + DateTime.Now.ToShortTimeString());
                speechSynthesizer.Speak("Please stand By.....");

                result = await netDuino.DenergizeRelay1();

                if (result != "Success")
                {
                    speechSynthesizer.Speak("DC Bus activation failed!");
                }
            }
            else
            {
                speechSynthesizer.Speak("Turning the DC Bus Off at" + DateTime.Now.ToShortTimeString());
                speechSynthesizer.Speak("Please stand By.....");

                result = await netDuino.EnergizeRelay1();

                if (result != "Success")
                {
                    speechSynthesizer.Speak("DC Bus de-activation failed!");
                }
            }
        }
        /// <summary>
        /// Sends commands to set the current state of the NetDuino Relay R1 (DC 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> SetNetDuinoDCRelay()
        {
            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, dcBusOnTimeOffset, 0);
            var offTime = dcBusOffTime;

            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

                speechSynthesizer.Speak("Turning the DC relay off. Please standby...");
                result = await netDuino.EnergizeRelay1();

                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 them to turn off at offTime

                speechSynthesizer.Speak("Turning the DC relay On. Please standby...");
                result = await netDuino.DenergizeRelay1();

                stateChangeInterval = offTime.TimeOfDay - DateTime.Now.TimeOfDay;

                alert.SendSMSAlert("Alert",
                                   $"DC Bus powered on at {DateTime.Now.ToLongTimeString()}. Today's Sunset Time: {sunsetToday.ToLongTimeString()}");
            }
            else
            {
                // Current time is between OffTime and midnight
                // energize the relays to turn the light off and set the interval to onTime + 1 Day
                speechSynthesizer.Speak("Turning the DC relay off. Please standby...");
                result = await netDuino.EnergizeRelay1();

                stateChangeInterval = (new TimeSpan(1, 0, 0, 0) + onTime.TimeOfDay) - DateTime.Now.TimeOfDay;
            }

            if (result == "Success")
            {
                if (changeStateR1Timer == null)
                {
                    //This is the first time this method is executed
                    // set up the timer to trigger next time the Relay state change is needed:
                    changeStateR1Timer = new Timer(OnChangeStateR1Timer, 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 DC 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 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);
        }