Example #1
0
def clear_context_broker(url: str, fiware_header: FiwareHeader):
    """
    Function deletes all entities, registrations and subscriptions for a
    given fiware header

    Note:
        Always clear the devices first because the IoT-Agent will otherwise
        through errors if it cannot find its registration anymore.

    Args:
        url: Url of the context broker service
        fiware_header: header of the tenant

    Returns:
        None
    """
    # create client
    client = ContextBrokerClient(url=url, fiware_header=fiware_header)
    # clean entities
    client.delete_entities(entities=client.get_entity_list())

    # clear subscriptions
    for sub in client.get_subscription_list():
        client.delete_subscription(subscription_id=sub.id)
    assert len(client.get_subscription_list()) == 0

    # clear registrations
    for reg in client.get_registration_list():
        client.delete_registration(registration_id=reg.id)
    assert len(client.get_registration_list()) == 0
    ax3.plot(t_simulation, on_off)
    ax3.set_xlabel('time in s')
    ax3.set_ylabel('on/off')

    plt.show()

    # write devices and groups to file and clear server state
    assert WRITE_DEVICES_FILEPATH.suffix == '.json', \
        f"Wrong file extension! {WRITE_DEVICES_FILEPATH.suffix}"
    WRITE_DEVICES_FILEPATH.touch(exist_ok=True)
    with WRITE_DEVICES_FILEPATH.open('w', encoding='utf-8') as f:
        devices = [item.dict() for item in iotac.get_device_list()]
        json.dump(devices, f, ensure_ascii=False, indent=2)

    assert WRITE_GROUPS_FILEPATH.suffix == '.json', \
        f"Wrong file extension! {WRITE_GROUPS_FILEPATH.suffix}"
    WRITE_GROUPS_FILEPATH.touch(exist_ok=True)
    with WRITE_GROUPS_FILEPATH.open('w', encoding='utf-8') as f:
        groups = [item.dict() for item in iotac.get_group_list()]
        json.dump(groups, f, ensure_ascii=False, indent=2)

    assert WRITE_SUBSCRIPTIONS_FILEPATH.suffix == '.json', \
        f"Wrong file extension! {WRITE_SUBSCRIPTIONS_FILEPATH.suffix}"
    WRITE_SUBSCRIPTIONS_FILEPATH.touch(exist_ok=True)
    with WRITE_SUBSCRIPTIONS_FILEPATH.open('w', encoding='utf-8') as f:
        subs = [item.dict() for item in cbc.get_subscription_list()]
        json.dump(subs, f, ensure_ascii=False, indent=2)

    clear_iot_agent(url=IOTA_URL, fiware_header=fiware_header)
    clear_context_broker(url=CB_URL, fiware_header=fiware_header)
    hall_entity = ContextEntity(**hall)
    cb_client.post_entity(hall_entity)

    # ### 2.2 Manage subscriptions
    #
    # create a subscription
    # Note: that the IP must be the ones that orion and quantumleap can access,
    # e.g. service name or static IP, localhost will not work here.
    ql_client.post_subscription(entity_id=hall_entity.id,
                                cb_url="http://orion:1026",
                                ql_url="http://quantumleap:8668",
                                throttling=0)

    # Get all subscriptions
    subscription_list = cb_client.get_subscription_list()

    # Notify QL manually
    subscription_id = ""
    for sub in subscription_list:
        for entity in sub.subject.entities:
            if entity.id == hall_entity.id:
                try:
                    ql_client.post_notification(notification=Message(
                        data=[hall_entity], subscriptionId=sub.id))
                    subscription_id = sub.id
                except:
                    logger.error("Can not notify QL")

    # Notify QL via Orion
    for i in range(5, 10):