Exemplo n.º 1
0
        // Notes:
        //  - Device updates come from within the EweLinkAPI. The websocket receives a message
        //    for a device. It then calls 'Update' on the device, which the Consumer observes.

        public Model()
        {
            ConsumersList = new ConsumerList();
            Settings      = new SettingsData(SettingsData.Filepath);
            Shutdown      = new CancellationTokenSource();
            History       = new History();
            Fronius       = new FroniusAPI(Settings.SolarInverterIP, Shutdown.Token);
            Ewe           = new EweLinkAPI(Shutdown.Token);
            Solar         = new SolarData();
            Sched         = new Schedule();

            Sched.Add(new Schedule.Range("Monitor Active", Schedule.ERepeat.Daily,
                                         new DateTimeOffset(1, 1, 1, 8, 0, 0, TimeSpan.Zero),
                                         new DateTimeOffset(1, 1, 1, 18, 0, 0, TimeSpan.Zero)));

            Log.Write(ELogLevel.Info, "Model initialised");
            m_settings.NotifyAllSettingsChanged();

            // Prevent system sleep
            WinOS.SystemSleep(keep_awake: true);
        }
Exemplo n.º 2
0
        /// <summary></summary>
        protected override async Task ExecuteAsync(CancellationToken shutdown)
        {
            var settings = new SettingsData(Path.Combine(OutputDirectory, "settings.xml")) ?? throw new Exception("Settings unavailable");

            using var fronius = new FroniusAPI(settings.SolarInverterIP, shutdown);
            using var history = new History(OutputDirectory);

            var prev = (SolarData?)null;

            for (; !shutdown.IsCancellationRequested; await Task.Delay(settings.SolarPollPeriod, shutdown))
            {
                try
                {
                    // Read the inverter data
                    var solar = await fronius.RealTimeData(shutdown);

                    // Ignore consecutive zero output records
                    if (solar.CurrentPower == 0 && prev?.CurrentPower == 0)
                    {
                        prev = solar;
                        continue;
                    }

                    // Add the zero record before a non-zero record
                    if (prev?.CurrentPower == 0)
                    {
                        history.Add(prev);
                    }

                    // Add the solar data to the history
                    history.Add(solar);
                    prev = solar;
                }
                catch (OperationCanceledException) { }
                catch (Exception ex)
                {
                    Log.LogError(ex, "{time} - Error reading solar data", DateTimeOffset.Now);
                }
            }
        }