Ejemplo n.º 1
0
        /// <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("-----------------------------------");
            }
        }