Esempio n. 1
0
        IPeriodVariableReader <I> IPeriodVariableAssigment <I> .Assign <T>(Expression <Func <TimeArg <I>, T> > writer)
        {
            var variable = new CalculatedVariable <H, T, I>
            {
                Period       = _currentPeriod,
                Group        = _currentGroup,
                MetaProperty = _currentPropertyInfo,
                Name         = _currentVariableName ?? _currentPropertyInfo.Name,
                Writer       = writer
            };

            _variables.Add(variable);
            _currentVariableName = null;
            _currentPropertyInfo = null;

            return(this);
        }
Esempio n. 2
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);
        }
 public OperatingTimeVariable(CalculatedVariable host)
 {
     this.host = host;
     resetAfterPeriod = TimeSpan.Zero;
 }