def create_entity(self): """Creates entitiy of PID controller in orion context broker""" try: self.ORION_CB.get_entity(entity_id=self.params['controller_name'], entity_type=self.params['type']) print('Entity name already assigned') except requests.exceptions.HTTPError as err: msg = err.args[0] if "NOT FOUND" not in msg.upper(): raise # throw other errors except "entity not found" print('[INFO]: Create new PID entity') pid_entity = ContextEntity(id=f"{self.params['controller_name']}", type=self.params['type']) cb_attrs = [] for attr in ['Kp', 'Ki', 'Kd', 'lim_low', 'lim_upper', 'setpoint']: cb_attrs.append( NamedContextAttribute(name=attr, type="Number", value=self.params[attr])) pid_entity.add_attributes(attrs=cb_attrs) self.ORION_CB.post_entity(entity=pid_entity, update=True)
def build_context_entity_from_device(device: Device) -> ContextEntity: from filip.models.base import DataType entity = ContextEntity(id=device.entity_name, type=device.entity_type) for command in device.commands: entity.add_attributes([ # Command attribute will be registered by the device_update NamedContextAttribute(name=f"{command.name}_info", type=DataType.COMMAND_RESULT), NamedContextAttribute(name=f"{command.name}_status", type=DataType.COMMAND_STATUS) ]) for attribute in device.attributes: entity.add_attributes([ NamedContextAttribute(name=attribute.name, type=DataType.STRUCTUREDVALUE, metadata=attribute.metadata) ]) for static_attribute in device.static_attributes: entity.add_attributes([ NamedContextAttribute(name=static_attribute.name, type=static_attribute.type, value=static_attribute.value, metadata=static_attribute.metadata) ]) return entity
# print complete weather station object print("+"*80) print("Building with weather station with one property from a sensor") print("+"*80) print(weather_station.json(indent=2)) # create additional properties of the weather station windspeed_metadata = NamedMetadata(name="unit", type="Unit", value=Unit(name="kilometre per " "hour").dict()) # create the temperature attribute of the weather station windspeed = ContextAttribute(type="Number", value=60, metadata=windspeed_metadata) weather_station.add_attributes(attrs={"windspeed": windspeed}) # print complete model print("+"*80) print("Building with weather station with two properties from a sensor") print("+"*80) print(weather_station.json(indent=2)) building_1 = ContextEntity(id="urn:ngsi-ld:building:001", type="Building") print("+"*80) print("Building without own properties") print("+"*80) print(building_1.json(indent=2))
"value": 111, "type": "Integer" } } room1_entity = ContextEntity(**room1) # ### 2.1.2 Using the constructor and interfaces # room2_entity = ContextEntity(id="Room2", type="Room") temp_attr = NamedContextAttribute(name="temperature", value=22, type=DataType.FLOAT) pressure_attr = NamedContextAttribute(name="pressure", value=222, type="Integer") room2_entity.add_attributes([temp_attr, pressure_attr]) # ## 2.2 Post Entities # print(cb_client.get_entity_list()) cb_client.post_entity(entity=room1_entity) cb_client.post_entity(entity=room2_entity) # # 3 Access entities in Fiware # # Get all entities from context broker logger.info(cb_client.get_entity_list()) # Get entities by id logger.info(cb_client.get_entity_list(entity_ids=["Room1"]))
type="Array", value=["office"]) # ToDo: create a property `address` for your building. Follow the full yaml # description in the specifications. It reuses the specification from # here: https://schema.org/PostalAddress address = NamedContextAttribute(name="address", type="PostalAddress", value={...}) # ToDo: create a `description` property for your building building_description = NamedContextAttribute(...) # add all properties to your building using the # `add_attribute` function of your building object building.add_attributes(attrs=[building_description, category, address]) # ToDo: Create a context broker client and add the fiware_header cbc = ... # ToDo: Send your building model to the context broker. Check the client # for the proper function ... # Update your local building model with the one from the server building = cbc.get_entity(entity_id=building.id, entity_type=building.type) # print your `building model` as json print(f"This is your building model: \n {building.json(indent=2)} \n") # ToDo: create a `opening hours` property and add it to the building object # in the context broker. Do not update the whole entity! In real
def test_patch_entity(self) -> None: """ Test the methode: patch_entity Returns: None """ # setup test-entity entity = ContextEntity(id="test_id1", type="test_type1") attr1 = NamedContextAttribute(name="attr1", value="1") attr1.metadata["m1"] = \ NamedMetadata(name="meta1", type="metatype", value="2") attr2 = NamedContextAttribute(name="attr2", value="2") attr1.metadata["m2"] = \ NamedMetadata(name="meta2", type="metatype", value="3") entity.add_attributes([attr1, attr2]) # sub-Test1: Post new self.client.patch_entity(entity=entity) self.assertEqual(entity, self.client.get_entity(entity_id=entity.id)) self.tearDown() # sub-Test2: ID/type of old_entity changed self.client.post_entity(entity=entity) test_entity = ContextEntity(id="newID", type="newType") test_entity.add_attributes([attr1, attr2]) self.client.patch_entity(test_entity, old_entity=entity) self.assertEqual(test_entity, self.client.get_entity(entity_id=test_entity.id)) self.assertRaises(RequestException, self.client.get_entity, entity_id=entity.id) self.tearDown() # sub-Test3: a non valid old_entity is provided, entity exists self.client.post_entity(entity=entity) old_entity = ContextEntity(id="newID", type="newType") self.client.patch_entity(entity, old_entity=old_entity) self.assertEqual(entity, self.client.get_entity(entity_id=entity.id)) self.tearDown() # sub-Test4: no old_entity provided, entity is new old_entity = ContextEntity(id="newID", type="newType") self.client.patch_entity(entity, old_entity=old_entity) self.assertEqual(entity, self.client.get_entity(entity_id=entity.id)) self.tearDown() # sub-Test5: no old_entity provided, entity is new old_entity = ContextEntity(id="newID", type="newType") self.client.patch_entity(entity, old_entity=old_entity) self.assertEqual(entity, self.client.get_entity(entity_id=entity.id)) self.tearDown() # sub-Test6: New attr, attr del, and attr changed. No Old_entity given self.client.post_entity(entity=entity) test_entity = ContextEntity(id="test_id1", type="test_type1") attr1_changed = NamedContextAttribute(name="attr1", value="2") attr1_changed.metadata["m4"] = \ NamedMetadata(name="meta3", type="metatype5", value="4") attr3 = NamedContextAttribute(name="attr3", value="3") test_entity.add_attributes([attr1_changed, attr3]) self.client.patch_entity(test_entity) self.assertEqual(test_entity, self.client.get_entity(entity_id=entity.id)) self.tearDown() # sub-Test7: Attr changes, concurrent changes in Fiware, # old_entity given self.client.post_entity(entity=entity) concurrent_entity = ContextEntity(id="test_id1", type="test_type1") attr1_changed = copy.deepcopy(attr1) attr1_changed.metadata["m1"].value = "3" attr1_changed.value = "4" concurrent_entity.add_attributes([attr1_changed, attr2]) self.client.patch_entity(concurrent_entity) user_entity = copy.deepcopy(entity) attr3 = NamedContextAttribute(name="attr3", value="3") user_entity.add_attributes([attr3]) self.client.patch_entity(user_entity, old_entity=entity) result_entity = concurrent_entity result_entity.add_attributes([attr2, attr3]) self.assertEqual(result_entity, self.client.get_entity(entity_id=entity.id)) self.tearDown()