private static async Task HandleManualCustomizationOnAllSources(CustomizationHelper helper)
        {
            helper.SendFeedback("Handling all sources having no variable");
            const int pageSize = 500;
            var       account  = await helper.GetAccount(AccountId);

            var variablesList = new ConcurrentBag <Variable>();

            var processedCount = 0;
            var tasks          = new List <Task>();

            foreach (var sourceIdsToProcess in helper.Split(account.Sources, pageSize))
            {
                tasks.Add(Task.Run(async() =>
                {
                    Interlocked.Add(ref processedCount, sourceIdsToProcess.Count);
                    helper.SendFeedback($"Start processing {sourceIdsToProcess.Count} sources, {processedCount} sources processed so far");

                    var sourceIdsWithoutVariables = (await helper.GetSourceIdsWithoutVariables(sourceIdsToProcess)).ToList();
                    if (sourceIdsWithoutVariables.Any())
                    {
                        var sourcesToProcess = (await helper.GetSources(sourceIdsWithoutVariables.Distinct())).ToList();
                        if (sourcesToProcess.Any())
                        {
                            if (helper.WhatIf)
                            {
                                helper.SendFeedback($"Processing {sourcesToProcess.Count} sources having no variable");
                            }

                            foreach (var source in sourcesToProcess)
                            {
                                var vars = await Process(source, new List <string>(), helper, outputJsonInsteadOfCreatingVariables);
                                foreach (var v in vars)
                                {
                                    variablesList.Add(v);
                                }
                            }
                        }
                    }
                }));
            }
            ;

            await Task.WhenAll(tasks);

#if __DEBUG
            helper.SendFeedback($"Serializing {variablesList.Count} variables to file '{new FileInfo(outputJsonFileName).FullName}'");
            File.WriteAllText(outputJsonFileName, JsonConvert.SerializeObject(variablesList));
            helper.SendFeedback($"End of process (lasted {(DateTime.UtcNow - start):g})");
#endif
        }
        private static async Task HandleAutomatedCustomization(CustomizationHelper helper)
        {
            var sourceIdStr = helper.EnsureValid(helper.StringValueOf(helper.Context.AdditionalProperties, "Id"), s => s != null,
                                                 $"The source Id is missing in the customization context: {helper.Context}");
            var sourceVariables = helper.ArrayValueOf(helper.Context.AdditionalProperties, "Variables");

            //Get source Id
            helper.EnsureValid(int.TryParse(sourceIdStr, out var sourceId), v => v,
                               $"The source Id<{sourceIdStr}> coming from the customization context is not convertible to an interger");

            var source = (await helper.GetSources(new[] { sourceId })).FirstOrDefault();

            if (source is null)
            {
                helper.SendFeedback($"The source with Id<{sourceId}> does not exist in the database");
                if (!helper.WhatIf)
                {
                    return;
                }

                //In whatif mode, create a fake source
                source = new Source
                {
                    Id         = 0,
                    EanNumber  = "FAKE EAN",
                    ClientData = JObject.Parse("{'No client data'}"),
                    Name       = "FAKE NAME",
                    SiteId     = 0,
                    SiteName   = "NO SITE"
                };
            }
            await Process(source, sourceVariables, helper, false);
        }
Beispiel #3
0
        public static async Task <List <Variable> > Process(Source source, List <string> sourceVariablesNames, CustomizationHelper helper, bool returnVariableList)
        {
            //Get existing variables from source (can be used in logic to prevent re-creating variables upon source updates)
            var sourceVariables = returnVariableList
                ? new[] { new Variable {
                              Name = Guid.NewGuid().ToString()
                          } }.ToLookup(k => k.Name)
                : (await helper.GetVariables(source.Id)).ToLookup(k => k.Name);

            var outputVariables = new List <Variable>();

            if (helper.WhatIf && !returnVariableList)
            {
                helper.SendFeedback($"The source <{source.Id}> contains {sourceVariables.Count} variables: [{string.Join(",", sourceVariables.Select(v => v.Key))}]");
            }

            var consumptionVariable = new CalculatedVariable
            {
                Name                = "Consumption",
                SourceId            = source.Id,
                UnitId              = (int)Unit.Kwh,
                Granularity         = 10,
                GranularityTimeBase = TimePeriod.Minute,
                QuantityType        = QuantityType.Integrated,
                VariableTypeId      = 0,
                Calculated          = new Calculated
                {
                    FriendlyName = "Consumption",
                    CalculatedVariableFormulas = new List <CalculatedVariableFormula>
                    {
                        new CalculatedVariableFormula
                        {
                            Formula   = GetConsumptionScript(),
                            Variables = new List <CalculatedVariableDependencyVariable>
                            {
                                new CalculatedVariableDependencyVariable
                                {
                                    Alias               = "x",
                                    SiteId              = source.SiteId,
                                    SourceId            = source.Id,
                                    VariableId          = 0,
                                    Granularity         = Granularity.Raw,
                                    PeriodType          = PeriodType.LastNPoints,
                                    Period              = 2,
                                    UnitId              = (int)Unit.Kwh,
                                    CanBeUsedAsATrigger = true
                                }
                            }
                        }
                    }
                }
            };

            switch (source.EnergyTypeId)
            {
            case (int)EnergyType.Electricity:
                outputVariables.Add(new Variable
                {
                    Id                  = helper.NextVariableId(),
                    Name                = "Index",
                    SourceId            = source.Id,
                    VariableTypeId      = 0,
                    UnitId              = (int)Unit.Kwh,
                    Granularity         = 10,
                    GranularityTimeBase = TimePeriod.Minute,
                    QuantityType        = QuantityType.Cumulative,
                    MappingConfig       = "Index",
                });
                break;

            case (int)EnergyType.Gas:
                outputVariables.Add(new Variable
                {
                    Id                  = helper.NextVariableId(),
                    Name                = "Index",
                    SourceId            = source.Id,
                    VariableTypeId      = 0,
                    UnitId              = (int)Unit.Kwh,
                    Granularity         = 10,
                    GranularityTimeBase = TimePeriod.Minute,
                    QuantityType        = QuantityType.Cumulative,
                    MappingConfig       = "Index",
                });
                break;
            }

            foreach (var outputVariable in outputVariables)
            {
                if (!sourceVariables.Contains(outputVariable.Name))
                {
                    var variableId = await helper.CreateVariable(outputVariable);

                    consumptionVariable.Calculated.CalculatedVariableFormulas[0].Variables[0].VariableId = variableId;
                    await helper.CreateVariable(consumptionVariable);
                }
            }
            return(outputVariables);
        }
 private static async Task HandleManualCustomizationOnSingleSourceId(CustomizationHelper helper)
 {
     helper.SendFeedback("Handling single source");
     await HandleAutomatedCustomization(helper);
 }
        public static async Task UpdateStandardVariablesConfiguration(AuthenticatedClient client, CustomizationContext ctx, Action <string> onFeedback)
        {
            try
            {
                if (ctx.Step == CustomizationSteps.Manual)
                {
                    onFeedback = async msg => { await Console.Out.WriteLineAsync(msg); };
                }
                else if (onFeedback == null)
                {
                    onFeedback = _ => { }
                }
                ;

                var helper = new CustomizationHelper(client, ctx, onFeedback);

                switch (ctx.Step)
                {
                case CustomizationSteps.Manual:
                    if (helper.WhatIf)
                    {
                        helper.SendFeedback("***********************************************************");
                        helper.SendFeedback("**  RUNNING IN WHATIF MODE, NO CHANGES WILL BE APPLIED   **");
                        helper.SendFeedback("***********************************************************");
                    }
                    if (outputJsonInsteadOfCreatingVariables)
                    {
                        helper.SendFeedback("***********************************************************");
                        helper.SendFeedback("**  PRODUCTION OF A JSON FILE, NO VARIABLES CREATION     **");
                        helper.SendFeedback("***********************************************************");
                    }
                    if (SingleSourceIdIsSpecified(helper))
                    {
                        await HandleManualCustomizationOnSingleSourceId(helper);
                    }
                    else
                    {
                        await HandleManualCustomizationOnAllSources(helper);
                    }
                    await Console.Out.WriteLineAsync("Press any key to exit...");

                    Console.ReadKey();
                    break;

                case CustomizationSteps.AfterSourceCreation:
                case CustomizationSteps.AfterSourceMove:
                case CustomizationSteps.AfterSourceUpdate:
                    await HandleAutomatedCustomization(helper);

                    break;

                default:
                    //helper.SendFeedback($"The step {ctx.Step} is not implemented in Eoly customization");
                    return;
                }
            }
            catch (Exception ex)
            {
                onFeedback?.Invoke($"Exception handling customization event <{ctx.Step}> for business key<{ctx.BusinessKey}> on account <{ctx.AccountId}>: {ex}");
            }
        }