public static IDictionary <Guid, ISysComponent> ParseConfig(String jsonString) { Dictionary <Guid, ISysComponent> result = new Dictionary <Guid, ISysComponent>(); dynamic jsonObject = JValue.Parse(jsonString); foreach (JObject j in jsonObject as JArray) { ModelConfig model = j.ToObject <ModelConfig>(); ISysComponent component = null; if (!VerifyModel(model)) { //Throw error? continue; } if (!Caches.Interfaces.TryGetValue(model.Interface.ToLower(), out Type Interface)) { //Do error handling continue; } //Generic implementation of ISysComponent component = new SysComponent(model.Id, model.Name); component.LoadComponent(model.Type, model.Data); //Dynamic interface implementation var newType = DRII.DynamicInterfaceImplementation(Interface, (SysComponent)component); if (newType is ISysComponent comp) { result.Add(comp.Id, comp); } } return(result); }
public int CalculateSetpoints(IDictionary <Guid, ISysComponent> components, double demand) { var weights = CalculateWeights(components); var restDemand = demand; if (weights != null) { //Attempt to use max output of each component foreach (var w in weights) { ISysComponent comp = components[w.Key]; if (comp is IStorage storage) { var setpoint = Math.Min(Math.Min(storage.MaxRate, demand), restDemand); var retval = storage.Setpoint(setpoint); restDemand = restDemand - retval; } else if (comp is IProducer producer) { var setpoint = Math.Min(Math.Min(producer.MaxOutput, demand), restDemand); var retval = producer.Setpoint(setpoint); restDemand = restDemand - retval; } } if (restDemand <= 0) { if (restDemand < 0) { return(1); } return(0); } } //Demand not met return(-1); }