Exemplo n.º 1
0
 private async Task <ActionResult <DialogFlowResponseDTO> > HandleSendReminderIntent(DialogFlowRequestDTO request)
 {
     try
     {
         var response = new DialogFlowResponseDTO()
         {
             outputContexts  = request.queryResult.outputContexts,
             fulfillmentText = request.queryResult.fulfillmentText
         };
         return(response);
     }
     catch (Exception ex)
     {
         _logger.LogError(ex, ex.Message, null);
         return(StatusCode(StatusCodes.Status500InternalServerError));
     }
 }
Exemplo n.º 2
0
        // private async Task<ActionResult<DialogFlowResponseDTO>> HandleConsumeElectricityExplain(DialogFlowRequestDTO request)
        // {
        //     try
        //     {
        //         Parameters parameters = request.queryResult.outputContexts
        //                     .FirstOrDefault(oc => oc.name.EndsWith("consumeelectricity-followup"))
        //                     .parameters;

        //         DialogFlowResponseDTO response = new DialogFlowResponseDTO();
        //         response.outputContexts = request.queryResult.outputContexts;
        //         var co2perkwh = parameters.initialemissions;

        //         response.fulfillmentText = request.queryResult.fulfillmentText
        //                     .Replace("$initialemissions", parameters.initialemissions.ToString())
        //                     .Replace("$optimalemissions", parameters.optimalemissions.ToString())
        //                     .Replace("$savingspercentage", parameters.savingspercentage.ToString());
        //         return response;
        //     }
        //     catch (Exception ex)
        //     {
        //         _logger.LogError(ex, ex.Message, null);
        //         return StatusCode(StatusCodes.Status500InternalServerError);
        //     }
        // }

        private async Task <ActionResult <DialogFlowResponseDTO> > HandleCurrentCo2QueryIntent(DialogFlowRequestDTO request)
        {
            try
            {
                var currentEmission = (await EmissionsClient.GetMostRecentEmissionsAsync()).FirstOrDefault().Emission;

                DialogFlowResponseDTO response = new DialogFlowResponseDTO();
                response.fulfillmentText = request.queryResult.fulfillmentText.Replace("$co2perkwh", currentEmission.ToString());

                return(response);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message, null);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Exemplo n.º 3
0
        private async Task <ActionResult <DialogFlowResponseDTO> > HandleChargeCarIntent(DialogFlowRequestDTO request, Boolean doCharge)
        {
            try
            {
                var parameters = request.queryResult.outputContexts
                                 .FirstOrDefault(oc => oc.name.EndsWith("drivesomewhereintent-followup"))
                                 .parameters;

                DateTime driveTime = parameters.time.ToUniversalTime();
                DateTime driveDate = parameters.time.Date;

                double hoursNeeded = parameters.kilometers / kilometersPerKwh / kwhPerHour;

                DateTime chargeTime = new DateTime(driveDate.Year, driveDate.Month, driveDate.Day, driveTime.Hour, driveTime.Minute, driveTime.Second, DateTimeKind.Utc).AddHours(-hoursNeeded);

                chargeTime = TimeZoneInfo.ConvertTime(chargeTime, _copenhagenTimeZoneInfo);

                var response = new DialogFlowResponseDTO();
                response.outputContexts = request.queryResult.outputContexts;

                if (doCharge)
                {
                    // do something to remember to charge the car
                }
                response.fulfillmentText = request.queryResult.fulfillmentText
                                           .Replace("$chargeDay", chargeTime.DayOfWeek.ToString())
                                           .Replace("$chargeTime", chargeTime.ToShortTimeString());

                return(response);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Exemplo n.º 4
0
        private async Task <ActionResult <DialogFlowResponseDTO> > HandleConsumeElectricityIntent(DialogFlowRequestDTO request)
        {
            try
            {
                Parameters parameters = request.queryResult.outputContexts
                                        .FirstOrDefault(oc => oc.name.EndsWith("consume-electricity-output-context"))
                                        .parameters;

                var nowUTC = DateTime.Now.ToUniversalTime();
                var now    = TimeZoneInfo.ConvertTime(nowUTC, _copenhagenTimeZoneInfo);

                DateTime finishNoLaterThanUTC;

                if (parameters.time != DateTime.MinValue && parameters.date != DateTime.MinValue)
                {
                    DateTime byTime = parameters.time.ToUniversalTime();
                    DateTime byDate = parameters.time.Date;
                    finishNoLaterThanUTC = new DateTime(byDate.Year, byDate.Month, byDate.Day, byTime.Hour, byTime.Minute, byTime.Second, DateTimeKind.Utc);
                }
                else
                {
                    finishNoLaterThanUTC = now.AddDays(1).Date.AddHours(6).ToUniversalTime();
                    if ((finishNoLaterThanUTC - nowUTC).TotalHours < 6)
                    {
                        finishNoLaterThanUTC = finishNoLaterThanUTC.AddDays(1);
                    }
                }

                if (parameters.duration == null)
                {
                    if (parameters.devicetype != null)
                    {
                        parameters.duration = LookupDefaultDeviceDuration(parameters.devicetype);
                    }
                    else
                    {
                        parameters.duration = new Duration {
                            amount = 1, unit = "h"
                        };
                    }
                }

                var prognosis = await PrognosisClient.OptimalConsumptionTimeAsync("DK1", parameters.duration.toHours(), DateTimeOffset.UtcNow.ToString("o"), finishNoLaterThanUTC.ToString("o"));

                // var best = await PrognosisClient.OptimalConsumptionTimeAsync(

                //     consumptionMinutes: parameters.duration.toMinutes(),
                //     consumptionRegion: "DK1",
                //     startNoEarlierThan: nowUTC,
                //     finishNoLaterThan: finishNoLaterThanUTC
                // );

                if (prognosis != null)
                {
                    DialogFlowResponseDTO response = new DialogFlowResponseDTO();
                    response.outputContexts = request.queryResult.outputContexts;

                    var optimalConsumptionStart = TimeZoneInfo.ConvertTime(prognosis.Best.StartUTC, _copenhagenTimeZoneInfo);
                    var prognosisEnd            = TimeZoneInfo.ConvertTime(prognosis.PrognosisEndUTC, _copenhagenTimeZoneInfo).AddMinutes(5);
                    var prognosisLookaheadHours = Math.Round((prognosis.PrognosisEndUTC - nowUTC).TotalHours, 0);
                    var finishNoLaterThan       = TimeZoneInfo.ConvertTime(finishNoLaterThanUTC, _copenhagenTimeZoneInfo);

                    var lang    = request.queryResult.languageCode;
                    var culture = CultureInfo.CreateSpecificCulture(lang);

                    double savingsPercentage = (prognosis.Earliest.Emissions - prognosis.Best.Emissions) / prognosis.Earliest.Emissions;

                    OutputContext ctx = response.outputContexts
                                        .Find(oc => oc.name.EndsWith("consumeelectricity-followup"));
                    ctx.parameters.prognosisend            = prognosis.PrognosisEndUTC;
                    ctx.parameters.savingspercentage       = (float)Math.Round(savingsPercentage * 100, 0);
                    ctx.parameters.optimalemissions        = Math.Round(prognosis.Best.Emissions, 0);
                    ctx.parameters.initialemissions        = Math.Round(prognosis.Earliest.Emissions, 0);
                    ctx.parameters.lastEmissions           = Math.Round(prognosis.Latest.Emissions, 0);
                    ctx.parameters.optimalconsumptionstart = optimalConsumptionStart.ToString("dddd HH:mm", culture);
                    ctx.parameters.finishnolaterthan       = finishNoLaterThan.ToString("dddd HH:mm", culture);
                    ctx.parameters.readableduration        = parameters.duration.toReadableString();
                    ctx.parameters.waitinghours            = Math.Round((prognosis.Best.StartUTC - nowUTC).TotalHours, 0);

                    // ctx.parameters.test1 = optimalConsumptionStart.ToUniversalTime().ToString("o");
                    // ctx.parameters.test2 = best.optimalConsumptionStart.ToUniversalTime().ToString("o");

                    if (ctx.parameters.savingspercentage < 2)
                    {
                        response.fulfillmentText = "Now is a good time! Do you want to know why?"
                                                   .Replace("$device", ctx.parameters.devicetype)
                                                   .Replace("$duration", parameters.duration.toReadableString());
                    }
                    else
                    {
                        response.fulfillmentText = request.queryResult.fulfillmentText
                                                   .Replace("$optimalEmissions", ctx.parameters.optimalemissions.ToString())
                                                   .Replace("$consumption-start", ctx.parameters.optimalconsumptionstart)
                                                   .Replace("$prognosis-end", prognosisEnd.ToString("dddd HH:mm", culture))
                                                   .Replace("$savingspercentage", ctx.parameters.savingspercentage.ToString())
                                                   .Replace("$prognosislookaheadhours", prognosisLookaheadHours.ToString() + " hours")
                                                   .Replace("$finishnolaterthan", ctx.parameters.finishnolaterthan)
                                                   .Replace("$readableduration", ctx.parameters.readableduration)
                                                   .Replace("$waitinghours", ctx.parameters.waitinghours.ToString());
                    }

                    return(response);
                }
                else
                {
                    _logger.LogError("Received null return value from OptimalFutureConsumptionTime");
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }