/// <summary> /// Polls the powerFail-warning- (if enabled in configuration) and powerBack-signal. /// <para>Requires serial-mode</para> /// </summary> public void Poll() { CheckSettings(); if (State != EUpsState.PowerOk) { Console.WriteLine("***Error: Strompi3 can't start monitoring"); return; } if (!Port.IsOpen) { Port.Open(); } while (State != EUpsState.ShutdownNow) { Thread.Sleep(PollDelayMilliSec); string data = String.Empty; try { data = Port.ReadLine(); } catch (TimeoutException) { }// ignore timeouts SwitchState(data); } Console.WriteLine($"Start Pi shutdown in 3 seconds..."); Thread.Sleep(3000); Os.ShutDown(); }
/// <summary> /// Compares the actual SystemTime of the Raspberry Pi with the RTC of StromPi3. /// In the case of a deviation, the more recent time is adopted. /// <para>Requires serial-mode</para> /// <remarks>The functionality of RTCSerial.py from joy-it is ported by this method.The original py-script /// uses commands 'Q', '\r', 'date-rpi' and 'time-rpi' to read the current datetime /// of Strompi3. This steps could not be implemented successfully and were replaced by calling 'ReadStatus'. /// </remarks> /// </summary> public void SyncRTC() { // workaround to get the current settings, // because commands '"date-rpi' and '"time-rpi' don't work (produce timeout..) until now Port.ReceiveConfiguration(Settings); if (Port.IsOpen) { Port.Close(); } Port.Open(); Console.WriteLine("TimeSync-Process | Please Wait"); Console.WriteLine($"StromPi3: Current dateTime {Settings.CurrentDateTime} "); var rpiDateTime = DateTime.Now; Console.WriteLine($"Raspi: Current dateTime {rpiDateTime} "); if (rpiDateTime > Settings.CurrentDateTime) // sync the Strompi { Console.WriteLine("The date und time will be synced: Raspberry Pi -> StromPi'"); int dayOfWeekPython = (int)rpiDateTime.DayOfWeek; // map value of sunday (0 in .net to 7 on Strompi3) if (dayOfWeekPython == 0) { dayOfWeekPython = 7; } string argumentsDate = $"{rpiDateTime.Day:D2} {rpiDateTime.Month:D2} {rpiDateTime.Year % 100:D2} {dayOfWeekPython}"; Console.WriteLine($"serial write 'set-date {argumentsDate}'"); Port.Write($"set-date {argumentsDate}"); Thread.Sleep(500); Port.Write("\r"); Thread.Sleep(1000); string argumentsTime = $"{rpiDateTime.Hour:D2} {rpiDateTime.Minute:D2} {rpiDateTime.Second:D2}"; Console.WriteLine($"serial write 'set-clock {argumentsTime}'"); Port.Write($"set-clock {argumentsTime}"); Thread.Sleep(500); Port.Write("\r"); Port.Close(); Port.ReceiveConfiguration(Settings); // re-read to get the updated datetime Console.WriteLine("-----------------------------------"); Console.WriteLine("The date und time has been synced: Raspberry Pi -> StromPi'"); Console.WriteLine($"Strompi3 is up-to-date: {Settings.CurrentDateTime}"); Console.WriteLine("-----------------------------------"); } if (rpiDateTime < Settings.CurrentDateTime) // sync the Raspi { //TODO: not tested so far.. Console.WriteLine("The date und time will be synced: StromPi -> Raspberry Pi'"); Os.SetDateTime(Settings.CurrentDateTime); Console.WriteLine("-----------------------------------"); Console.WriteLine("The date und time has been synced: StromPi -> Raspberry Pi'"); Console.WriteLine("-----------------------------------"); } }
/// <summary> /// Reads all status-related characteristics of the Strompi3. /// <para> /// <remarks>Requires serial-mode</remarks></para> /// </summary> public static string ReceiveConfiguration(this SerialPort port, StromPi3Settings settings, bool bSilent = true) { if (!Os.ShowAvailableSerialPorts("tty", true)) { return(String.Empty); } bool isPortOpen = port.IsOpen; if (!isPortOpen) { port.Open(); } port.Write("quit"); port.Write("\r"); // \x0d = {13} Carriage Return port.Write("status-rpi"); port.Write("\r"); // \x0d = {13} Carriage Return string sp3Time = port.ReadLine(); // timeout string sp3Date = port.ReadLine(); settings.SetRTCDateTime(sp3Time, sp3Date); string sp3_weekday = port.ReadLine(); // not used string sp3_modus = port.ReadLine(); settings.SetInputPriorityMode(sp3_modus); settings.AlarmSettings.GetAlarmEnabled(port.ReadLine()); settings.AlarmSettings.GetAlarmMode(port.ReadLine()); string sp3AlarmHour = port.ReadLine(); string sp3AlarmMin = port.ReadLine(); string sp3AlarmDay = port.ReadLine(); string sp3AlarmMonth = port.ReadLine(); string sp3AlarmWeekday = port.ReadLine(); settings.AlarmSettings.GetAlarmDateTime(sp3AlarmHour, sp3AlarmMin, sp3AlarmDay, sp3AlarmMonth, sp3AlarmWeekday); settings.AlarmSettings.GetAlarmPowerOffEnabled(port.ReadLine()); string alarmPowerOffHours = port.ReadLine(); string alarmPowerOffMinutes = port.ReadLine(); settings.AlarmSettings.GetAlarmPowerOffTimePeriod(alarmPowerOffHours, alarmPowerOffMinutes); string sp3ShutdownEnable = port.ReadLine(); string sp3ShutdownSeconds = port.ReadLine(); settings.SetShutDown(sp3ShutdownEnable, Convert.ToInt32(sp3ShutdownSeconds), (int)EBatteryLevel.nothing); settings.SetPowerFailWarningEnable(port.ReadLine()); settings.SetSerialLessEnable(port.ReadLine()); string sp3IntervalAlarm = port.ReadLine(); string sp3IntervallAlarmOnTimeMinutes = port.ReadLine(); string sp3IntervallAlarmOffTimeMinutes = port.ReadLine(); settings.AlarmSettings.GetAlarmIntervall(sp3IntervalAlarm, sp3IntervallAlarmOnTimeMinutes, sp3IntervallAlarmOffTimeMinutes); string sp3BatLevelShutdown = port.ReadLine(); string sp3BatLevel = port.ReadLine(); string sp3Charging = port.ReadLine(); settings.BatteryHat.GetBatteryHat(Convert.ToInt32(sp3BatLevelShutdown), sp3BatLevel, sp3Charging); string sp3PowerOnButtonEnable = port.ReadLine(); string sp3PowerOnButtonTime = port.ReadLine(); string sp3PowersaveEnable = port.ReadLine(); string sp3PoweroffMode = port.ReadLine(); string poweroffTimeEnableMode = port.ReadLine(); settings.StartStopSettings.GetStartStopSettings(sp3PowerOnButtonEnable, sp3PowerOnButtonTime, sp3PowersaveEnable, sp3PoweroffMode, poweroffTimeEnableMode); string wakeupTimerMinutes = port.ReadLine(); string sp3WakeupweekendEnable = port.ReadLine(); settings.AlarmSettings.GetAlarmWakeupTimerAndWeekend(wakeupTimerMinutes, sp3WakeupweekendEnable); string sp3AdcWide = port.ReadLine(); string sp3AdcBat = port.ReadLine(); string sp3AdcUsb = port.ReadLine(); string outputVolt = port.ReadLine(); settings.VoltageMeter.GetVoltageMeter(sp3AdcWide, sp3AdcBat, sp3AdcUsb, outputVolt); settings.SetOutputStatus(port.ReadLine()); settings.SetPowerFailureCounter(port.ReadLine()); settings.SetFirmwareVersion(port.ReadLine()); if (!isPortOpen) { port.Close(); } return(settings.ToString()); }