Пример #1
0
        //private async Task<List<Sensor>> GetSensorsFromDb()
        //{
        //    using ApplicationDbContext dbContext = new(ConnectionString);
        //    using IUnitOfWork unitOfWork = new UnitOfWork(dbContext);
        //    try
        //    {
        //        var sensors = await unitOfWork.Sensors.GetAsync();
        //        Log.Information($"RuleEngine;GetSensorsFromDb;{sensors.Count()} sensors read");
        //        return sensors.ToList();
        //    }
        //    catch (Exception ex)
        //    {
        //        Log.Error($"RuleEngine,GetSensorsFromDb;Failed to read sensors; ex: {ex.Message}");
        //    }
        //    return null;
        //}

        //private async Task<List<Actor>> GetActorsFromDb()
        //{
        //    using ApplicationDbContext dbContext = new(ConnectionString);
        //    using IUnitOfWork unitOfWork = new UnitOfWork(dbContext);
        //    try
        //    {
        //        var actors = await unitOfWork.Actors.GetAsync();
        //        Log.Information($"RuleEngine;GetSensorsFromDb;{actors.Count()} actors read");
        //        return actors.ToList();
        //    }
        //    catch (Exception ex)
        //    {
        //        Log.Error($"RuleEngine,GetSensorsFromDb;Failed to read actors; ex: {ex.Message}");
        //    }
        //    return null;
        //}

        //async Task<Sensor[]> SyncSensorsWithDb()
        //{
        //    using ApplicationDbContext dbContext = new(ConnectionString);
        //    using IUnitOfWork unitOfWork = new UnitOfWork(dbContext);
        //    foreach (var sensor in StateService.GetSensors())
        //    {
        //        await unitOfWork.Sensors.UpsertAsync(sensor);
        //    }
        //    await unitOfWork.SaveChangesAsync();
        //    var sensors = await unitOfWork.Sensors.GetAsync();
        //    return sensors.OrderBy(s => s.Name).ToArray();
        //}

        //async Task<Actor[]> SyncActorsWithDb()
        //{
        //    using ApplicationDbContext dbContext = new(ConnectionString);
        //    using IUnitOfWork unitOfWork = new UnitOfWork(dbContext);
        //    foreach (var actor in StateService.GetActors())
        //    {
        //        await unitOfWork.Actors.UpsertAsync(actor);
        //    }
        //    await unitOfWork.SaveChangesAsync();
        //    var actors = await unitOfWork.Actors.GetAsync();
        //    return actors.OrderBy(s => s.Name).ToArray();
        //}

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            Log.Information("ExecuteAsync;started");
            //SerialCommunicationService = serialCommunicationService;
            using (var scope = ServiceProvider.CreateScope())
            {
                IHttpClientFactory httpClientFactory = ServiceProvider.GetRequiredService <IHttpClientFactory>();
                SerialCommunicationService        = new SerialCommunicationService(Configuration);
                EspHttpCommunicationService       = new EspHttpCommunicationService(httpClientFactory, Configuration);
                HomematicHttpCommunicationService = new HomematicHttpCommunicationService(httpClientFactory, Configuration);
                RaspberryIoService = new RaspberryIoService();

                StateService.Init(SerialCommunicationService, EspHttpCommunicationService, HomematicHttpCommunicationService);
                //StateService.NewMeasurement += StateService_NewMeasurement;
                OilBurner = new OilBurner(StateService, SerialCommunicationService);
                OilBurner.Fsm.StateChanged      += Fsm_StateChanged;
                HeatingCircuit                   = new HeatingCircuit(StateService, SerialCommunicationService, OilBurner);
                HeatingCircuit.Fsm.StateChanged += Fsm_StateChanged;
                OilBurner.HeatingCircuit         = HeatingCircuit;
                HotWater = new HotWater(StateService, SerialCommunicationService, OilBurner);
                HotWater.Fsm.StateChanged += Fsm_StateChanged;

                try
                {
                    await RaspberryIoService.ResetEspAsync();  // Bei Neustart der RuleEngine auch ESP neu starten
                }
                catch (Exception)
                {
                    Log.Error("ExecuteAsync;Raspberry IO not available!");
                }
                StartFiniteStateMachines();
                HomematicHttpCommunicationService.MeasurementReceived += HomematicHttpCommunicationService_MeasurementReceived;
            }
            Instance = this;  // ab jetzt Zugriff über Singleton erlauben
            int round = 0;

            while (!stoppingToken.IsCancellationRequested)
            {
                await Task.Delay(100, stoppingToken);

                round++;
                if (round >= 100)  // alle 10 Sekunden
                {
                    Log.Information($"RuleEngine;ExecuteAsync;check oilburner and heating");
                    OilBurner.CheckOilBurner();
                    HeatingCircuit.CheckHeating();
                    round = 0;
                }
                // Auf Änderungen des States reagieren, Timeouts bearbeiten, ...
            }
            //return Task.CompletedTask;
        }
Пример #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            Log.Information("Ruleengine started");
            //SerialCommunicationService = serialCommunicationService;
            using (var scope = ServiceProvider.CreateScope())
            {
                SerialCommunicationService = scope.ServiceProvider.GetService <ISerialCommunicationService>();
                HttpCommunicationService   = scope.ServiceProvider.GetService <IHttpCommunicationService>();
                StateService = scope.ServiceProvider.GetService <IStateService>();
                StateService.Init(SerialCommunicationService, HttpCommunicationService);
                OilBurner = new OilBurner(StateService, SerialCommunicationService);
                OilBurner.Fsm.StateChanged      += Fsm_StateChanged;
                HeatingCircuit                   = new HeatingCircuit(StateService, SerialCommunicationService, OilBurner);
                HeatingCircuit.Fsm.StateChanged += Fsm_StateChanged;
                HotWater = new HotWater(StateService, SerialCommunicationService, OilBurner);
                HotWater.Fsm.StateChanged += Fsm_StateChanged;
                try
                {
                    RaspberryIoService = scope.ServiceProvider.GetService <IRaspberryIoService>();
                    await RaspberryIoService.ResetEspAsync();  // Bei Neustart der RuleEngine auch ESP neu starten
                }
                catch (Exception)
                {
                    Log.Error("Raspberry IO not available!");
                }
                StartFiniteStateMachines();
            }
            Instance = this;  // ab jetzt Zugriff über Singleton erlauben
            while (!stoppingToken.IsCancellationRequested)
            {
                await Task.Delay(100, stoppingToken);

                // Auf Änderungen des States reagieren, Timeouts bearbeiten, ...
            }
            //return Task.CompletedTask;
        }
Пример #3
0
 private void StopFiniteStateMachines()
 {
     OilBurner.Stop();
     HotWater.Stop();
     HeatingCircuit.Stop();
 }
Пример #4
0
 public void StartFiniteStateMachines()
 {
     OilBurner.Start();
     HotWater.Start();
     HeatingCircuit.Start();
 }