Beispiel #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                dbContext = scope.ServiceProvider.GetService <ApplicationDbContext>();

                var test = dbContext.Meters.AsQueryable();
            }

            LogInformation("Background thread started");

            var result = await MqttClnt.ConnectAsync(options);

            LogInformation($"Connection result: {result.ResultCode}");

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

                //Check if we can increase the charge current
                //Loop through meters
                foreach (BaseCache cCache in bcLookup.Values)
                {
                    if (cCache is MeterCache)
                    {
                        MeterCache mc = (MeterCache)cCache;

                        //Get max current we can increase with
                        //Checks all phases
                        LogInformation($"{mc.sName}, checking possible currentChange");
                        //TODO Check one phase at a time
                        float fSuggestedCurrentChange = mc.GetMaxPhaseAddCurrent(7);

                        //Check all chargers that is connected
                        var connChargers = mc.cChildren.Where(m => m.bConnected);

                        //Set new current
                        //Number of chargers to even out on
                        int iChargers = connChargers.Count();

                        LogInformation($"{mc.sName}, possible currentChange for {iChargers} chargers: {fSuggestedCurrentChange}A");

                        fSuggestedCurrentChange = fSuggestedCurrentChange / iChargers;

                        foreach (ChargerCache cc in connChargers)
                        {
                            //TODO Handle the case where one charger uses less current than given.
                            //TODO Distribute to the others.

                            float fNewCurrent = cc.AdjustNewChargeCurrent(fSuggestedCurrentChange);
                            await PostAdjustedCurrent(fNewCurrent, cc);
                        }
                    }
                }
                LogInformation($"Background svc loop end");
            }

            stoppingToken.Register(() => LogInformation($" MQTTSvc background task is stopping."));


/* Python rules
 *  client.subscribe("EVCharger/#")
 *  client.subscribe("CurrentMeter/#")
 *
 *
 *  if msg.topic == "EVCharger/status/current":
 *      charge_current = float(msg.payload)
 *      print("Charge current %.1f A" % charge_current)
 *  if msg.topic == "CurrentMeter/status/current":
 *      current = float(msg.payload)
 *      mean_current = (9*mean_current + current) / 10
 *      print("Current %.1f (mean %.1f)" % (current, mean_current))
 *      if mean_current > max_current:
 *          new_charge_current = charge_current - (mean_current-max_current)
 *          client.publish("EVCharger/set/current", payload=str(int(new_charge_current)), qos=0, retain=False)
 *          if new_charge_current < 2:
 *              client.publish("EVCharger/set/enable", payload=str(0), qos=0, retain=False)
 *      else:
 *          client.publish("EVCharger/set/current", payload=str(int(max_current)), qos=0, retain=False)
 *          client.publish("EVCharger/set/enable", payload=str(1), qos=0, retain=False)
 */
            //MqttClient client=(MqttClient)MqttClnt;

            //result = await MqttClnt.ConnectAsync(options);

/*            async Task Handler1(MqttApplicationMessageReceivedEventArgs e)
 *          {
 * //                await client1.PublishAsync($"reply/{eventArgs.ApplicationMessage.Topic}");
 *              string _logstr=$"{DateTime.Now} {e.ApplicationMessage.Topic} \t {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}";
 *              //Logger.LogInformation(logstr);
 *              Console.WriteLine(logstr);
 *          }
 *
 *          client.UseApplicationMessageReceivedHandler(Handler1);
 *          while (!stoppingToken.IsCancellationRequested)
 *          {
 *              await Task.Delay(1000, stoppingToken);
 *
 *              Console.WriteLine("Background svc looping");
 *
 *          }
 */

            Console.WriteLine("Background svc is stopping.");

            //return Task.CompletedTask;
        }