def get_standard_profiles_list(self): profiles = Profiles.get_instance().get_profiles()['profiles'] profiles_list = list() for pkey in profiles: p = profiles[pkey] profiles_list.append({'id': pkey, 'name': p['profile_uiname']}) return profiles_list
def generate_profile_info(profile_list): profile_info_list = [] for profile in profile_list: profile_class = type(profile).__name__ qau = profile.profileQuantityAndUnit if isinstance(qau, esdl.QuantityAndUnitReference): qau = qau.reference if qau: profile_type = ESDLQuantityAndUnits.qau_to_string(qau) else: profile_type = profile.profileType.name profile_name = profile.name profile_id = profile.id if profile_class == 'SingleValue': value = profile.value profile_info_list.append({'id': profile_id, 'class': 'SingleValue', 'value': value, 'type': profile_type, 'uiname': profile_name}) if profile_class == 'InfluxDBProfile': database = profile.database multiplier = profile.multiplier measurement = profile.measurement field = profile.field profiles = Profiles.get_instance().get_profiles()['profiles'] for pkey in profiles: p = profiles[pkey] if p['database'] == database and p['measurement'] == measurement and p['field'] == field: profile_name = p['profile_uiname'] if profile_name == None: profile_name = field profile_info_list.append({'id': profile_id, 'class': 'InfluxDBProfile', 'multiplier': multiplier, 'type': profile_type, 'uiname': profile_name}) if profile_class == 'DateTimeProfile': profile_info_list.append({'id': profile_id, 'class': 'DateTimeProfile', 'type': profile_type, 'uiname': profile_name}) return profile_info_list
def get_environmental_profiles(self): active_es_id = get_session('active_es_id') esh = get_handler() result = list() es = esh.get_energy_system(active_es_id) esi = es.energySystemInformation for x in esdl.EnvironmentalProfiles.eClass.eAllStructuralFeatures(): if isinstance(x, EReference): ep_instance = dict() ep_instance['key'] = str(uuid4()) ep_instance['name'] = x.name ep_instance['uiname'] = camelCaseToWords(x.name) ep_instance['type'] = 'Unset' ep_instance[ 'default_unit'] = self.get_default_qau_for_env_profile( x.name) if esi and esi.environmentalProfiles: env_profiles = esi.environmentalProfiles profile = env_profiles.eGet(x) if profile: if isinstance(profile, esdl.SingleValue): ep_instance['type'] = 'SingleValue' ep_instance['value'] = profile.value elif isinstance(profile, esdl.InfluxDBProfile): ep_instance['type'] = 'InfluxDBProfile' # check if it is a 'standard profile' that is present in our configuration profiles = Profiles.get_instance().get_profiles( )['profiles'] for pkey in profiles: std_profile = profiles[pkey] if profile.database == std_profile['database'] and \ profile.measurement == std_profile['measurement'] and \ profile.field == std_profile['field']: ep_instance['profile_id'] = pkey else: logger.warn( 'Environmental profiles other than SingleValue/InfluxDB are not supported' ) ep_instance['value'] = '' if profile.profileQuantityAndUnit: qau = profile.profileQuantityAndUnit if isinstance(qau, esdl.QuantityAndUnitReference): qau = qau.reference ep_instance['unit'] = unit_to_string(qau) else: ep_instance['unit'] = '' else: ep_instance['value'] = '' result.append(ep_instance) return result
def get_profile_panel(profile_id): esh = get_handler() active_es_id = get_session('active_es_id') profile = esh.get_by_id(active_es_id, profile_id) if profile: profile_class = type(profile).__name__ if profile_class == "InfluxDBProfile": profile_name = None qau = profile.profileQuantityAndUnit if isinstance(qau, esdl.QuantityAndUnitReference): qau = qau.reference if qau: profile_type = ESDLQuantityAndUnits.qau_to_string(qau) else: profile_type = profile.profileType.name database = profile.database multiplier = profile.multiplier measurement = profile.measurement profiles = Profiles.get_instance().get_profiles( )['profiles'] for pkey in profiles: p = profiles[pkey] if p['database'] == database and p[ 'measurement'] == measurement and p[ 'field'] == field: profile_name = p['profile_uiname'] if not profile_name: profile_name = profile.field + " (Multiplier: " + str( multiplier) + " - Type: " + profile_type + ")" embedUrl = create_panel(graph_title=profile_name, axis_title="", host=profile.host + ':' + str(profile.port), database=profile.database, measurement=profile.measurement, field=profile.field, filters=profile.filters, qau=qau, prof_aggr_type="sum", start_datetime=profile.startDate, end_datetime=profile.endDate) if embedUrl: return embedUrl else: send_alert( 'ProfileType other than InfluxDBProfile not supported yet' ) return None
def get_control_strategy_info(asset): """ Builds up a dictionary with information about the control strategy for the asset :param EnergyAsset asset: the asset for which the control strategy info must be built :return dict: the control strategy information """ control_strategy = asset.controlStrategy cs_info = dict() if control_strategy: cs_info = { 'id': control_strategy.id, 'name': control_strategy.name, 'type': type(control_strategy).__name__ } if isinstance(control_strategy, esdl.DrivenByDemand): if control_strategy.outPort: cs_info['port_id'] = control_strategy.outPort.id if isinstance(control_strategy, esdl.DrivenBySupply): if control_strategy.inPort: cs_info['port_id'] = control_strategy.inPort.id if isinstance(control_strategy, esdl.DrivenByProfile): if control_strategy.port: cs_info['port_id'] = control_strategy.port.id # EDate.to_string = lambda d: d.isoformat() profile_info = dict() if isinstance(control_strategy.profile, esdl.DateTimeProfile): profile_info['type'] = 'datetime' pe_list = list() for pe in control_strategy.profile.element: pe_list.append({ 'key': str(uuid4()), 'datetime': pe.from_.strftime("%Y-%m-%d %H:%M:%S"), 'profilevalue': pe.value }) profile_info['list'] = pe_list if isinstance(control_strategy.profile, esdl.InfluxDBProfile): profiles = Profiles.get_instance().get_profiles()['profiles'] for pkey in profiles: std_p = profiles[pkey] p = control_strategy.profile if p.database == std_p[ 'database'] and p.measurement == std_p[ 'measurement'] and p.field == std_p['field']: profile_info['id'] = pkey profile_info['type'] = 'database' # TODO: support external profiles cs_info['profile'] = profile_info if isinstance(control_strategy, esdl.StorageStrategy): mcc_sv = control_strategy.marginalChargeCosts if isinstance(mcc_sv, esdl.SingleValue): mcc = mcc_sv.value else: mcc = 0 mdc_sv = control_strategy.marginalDischargeCosts if isinstance(mdc_sv, esdl.SingleValue): mdc = mdc_sv.value else: mdc = 0 cs_info['marginal_charge_costs'] = mcc cs_info['marginal_discharge_costs'] = mdc if isinstance(control_strategy, esdl.CurtailmentStrategy): cs_info['max_power'] = control_strategy.maxPower if isinstance(control_strategy, esdl.PIDController): cs_info['kp'] = control_strategy.Kp cs_info['ki'] = control_strategy.Ki cs_info['kd'] = control_strategy.Kd if control_strategy.sensor: cs_info['sensor_id'] = control_strategy.sensor.id else: cs_info['sensor_id'] = None if control_strategy.setPoint: cs_info['pid_setpoint'] = control_strategy.setPoint.value else: cs_info['pid_setpoint'] = None return cs_info
def set_control_strategy(asset, cs_info): """ Sets or updates the control strategy for the asset :param EnergyAsset asset: the asset for which the control strategy info must be set or updated :param dict cs_info: dictionairy with the control strategy information """ print(cs_info) active_es_id = get_session('active_es_id') esh = get_handler() control_strategy = asset.controlStrategy # If no control strategy yet or change of type if not control_strategy or type( control_strategy).__name__ != cs_info['type']: if control_strategy: # remove current strategy services = control_strategy.eContainer() services.service.remove(control_strategy) esh.remove_object_from_dict(active_es_id, control_strategy, True) # create new control strategy control_strategy = instantiate_type(cs_info['type']) control_strategy.id = str(uuid4()) control_strategy.name = cs_info['type'] + ' for ' + asset.name control_strategy.energyAsset = asset es = esh.get_energy_system(active_es_id) services = es.services if not services: services = esdl.Services(id=str(uuid4())) es.services = services esh.add_object_to_dict(active_es_id, services) services.service.append(control_strategy) esh.add_object_to_dict(active_es_id, control_strategy) if isinstance(control_strategy, esdl.DrivenByDemand): control_strategy.outPort = esh.get_by_id(active_es_id, cs_info['port_id']) if isinstance(control_strategy, esdl.DrivenBySupply): control_strategy.inPort = esh.get_by_id(active_es_id, cs_info['port_id']) if isinstance(control_strategy, esdl.StorageStrategy): control_strategy.marginalChargeCosts = esdl.SingleValue( id=str(uuid4()), name='marginalChargeCosts for ' + asset.name, value=str2float(cs_info['marginal_charge_costs'])) control_strategy.marginalDischargeCosts = esdl.SingleValue( id=str(uuid4()), name='marginalChargeCosts for ' + asset.name, value=str2float(cs_info['marginal_discharge_costs'])) esh.add_object_to_dict(active_es_id, control_strategy.marginalChargeCosts) esh.add_object_to_dict(active_es_id, control_strategy.marginalDischargeCosts) if isinstance(control_strategy, esdl.CurtailmentStrategy): control_strategy.maxPower = str2float(cs_info['max_power']) if isinstance(control_strategy, esdl.PIDController): control_strategy.Kp = str2float(cs_info['kp']) control_strategy.Ki = str2float(cs_info['ki']) control_strategy.Kd = str2float(cs_info['kd']) if cs_info['sensor_id'] != 'Select sensor...': control_strategy.sensor = esh.get_by_id(active_es_id, cs_info['sensor_id']) control_strategy.setPoint = esdl.SingleValue( id=str(uuid4()), name='PID setPoint for ' + asset.name, value=str2float(cs_info['pid_setpoint'])) esh.add_object_to_dict(active_es_id, control_strategy.setPoint) if isinstance(control_strategy, esdl.DrivenByProfile): control_strategy.port = esh.get_by_id(active_es_id, cs_info['port_id']) if cs_info['profile']['type'] == 'database': pid = cs_info['profile']['id'] profiles = Profiles.get_instance().get_profiles()['profiles'] for pkey in profiles: if pkey == pid: p = profiles[pkey] profile = esdl.InfluxDBProfile( id=str(uuid4()), name=p['profile_uiname'], startDate=EDate.from_string(p['start_datetime']), endDate=EDate.from_string(p['end_datetime']), database=p['database'], measurement=p['measurement'], field=p['field']) control_strategy.profile = profile esh.add_object_to_dict(active_es_id, profile) if cs_info['profile']['type'] == 'datetime': list = cs_info['profile']['list'] control_strategy.profile = esdl.DateTimeProfile( id=str(uuid4()), name="Profile for DrivenByProfile strategy") for pe in list: profile_element = esdl.ProfileElement( from_=EDate.from_string(str(parse_date(pe['datetime']))), value=str2float(pe['profilevalue'])) control_strategy.profile.element.append(profile_element) esh.add_object_to_dict(active_es_id, control_strategy.profile)
def update_environmental_profiles(self, action, profile_info): active_es_id = get_session('active_es_id') esh = get_handler() es = esh.get_energy_system(active_es_id) esi = es.energySystemInformation print(profile_info) if action == 'save': for x in esdl.EnvironmentalProfiles.eClass.eAllStructuralFeatures( ): if isinstance(x, EReference): if profile_info['name'] == x.name: if not esi: esi = es.energySystemInformation = esdl.EnergySystemInformation( id=str(uuid4())) esh.add_object_to_dict(active_es_id, esi) if not esi.environmentalProfiles: esi.environmentalProfiles = esdl.EnvironmentalProfiles( id=str(uuid4())) esh.add_object_to_dict(active_es_id, esi.environmentalProfiles) env_profiles = esi.environmentalProfiles profile = env_profiles.eGet(x) if not profile: profile = instantiate_type(profile_info['type']) profile.id = str(uuid4()) esh.add_object_to_dict(active_es_id, profile) else: if type(profile) != profile_info['type']: profile = instantiate_type( profile_info['type']) if isinstance(profile, esdl.SingleValue): profile.value = str2float(profile_info['value']) if isinstance(profile, esdl.InfluxDBProfile): std_profiles = Profiles.get_instance( ).get_profiles()['profiles'] if profile_info['profile_id'] in std_profiles: std_profile = std_profiles[ profile_info['profile_id']] print(std_profile) profile.id = str(uuid4()) profile.name = std_profile.name profile.host = std_profile.host profile.port = std_profile.port profile.database = std_profile.database profile.measurement = std_profile.measurement profile.field = std_profile.field profile.filters = std_profile.filters profile.startDate = std_profile.startDate profile.endDate = std_profile.endDate else: logger.error( 'Profile is referenced that was not communicated before' ) # Create a QuantityAndUnit instance and set the unit based on the information received if 'unit' in profile_info: profile.profileQuantityAndUnit = build_qau_from_unit_string( profile_info['unit']) esh.add_object_to_dict( active_es_id, profile.profileQuantityAndUnit) elif 'default_unit' in profile_info: profile.profileQuantityAndUnit = build_qau_from_unit_string( profile_info['default_unit']) esh.add_object_to_dict( active_es_id, profile.profileQuantityAndUnit) # Set the physical quantity based on the name of the profile reference self.update_quantity_in_qau( profile.profileQuantityAndUnit, x.name) env_profiles.eSet(x, profile) elif action == 'delete': env_profile_refname = profile_info['name'] env_profiles = esi.environmentalProfiles env_profiles.eSet(env_profile_refname, None) else: logger.error('Unknown action on environmental profiles')