Back to contents pageHow to connect as a client to schema services

How to connect as a client to schema services

Summary

This article shows you how to connect as a client to schema services at different levels of the cell hierarchy using the API.





Prerequisites

You should know the information in the following article:



Example material

Save this link to get a zip file of the example material


Guide

Before you start, remember:

This guide shows you how to connect to example schema services as a client. The example includes services that serve each level of the cell hierarchy.



1) Create the schema clients that you need

In this example, we will connect 3 example schemas at different levels of the cell hierarchy:


    // Create an example event client for a ULocation::Cell
    Ubisense.ULocation.CellData.Schema celldata_schema = new
        Ubisense.ULocation.CellData.Schema(true);

    // Create an example event client for a USpatial::Cell
    Ubisense.USpatial.Monitor.Schema monitor_schema = new
        Ubisense.USpatial.Monitor.Schema(true);

    // Create an example site-wide event client
    Ubisense.UBuilding.Contents.Schema building_schema = new
        Ubisense.UBuilding.Contents.Schema(true);



2) Create a Ubisense.UCell.Config.Schema client and connect to it

We need to get the cell IDs from the Ubisense.UCell.Config schema. These are required when connecting to any cell-level schema. We need a caching client so we can iterate over the cell names in the database:


    // Create a cache client of the UCell.Config schema
    // so that cell IDs can be obtained
    Ubisense.UCell.Config.Schema cell_schema = new
        Ubisense.UCell.Config.Schema(false);
    cell_schema.ConnectAsClient();

    // Iterate over every row in the UCell.Config database
    using (Ubisense.UCell.Config.ReadTransaction xact =
        cell_schema.ReadTransaction())
    {
        foreach (Ubisense.UCell.Config.Names.RowType row in
            Ubisense.UCell.Config.Names.name_(xact))
        {



3) Find your cells of type ULocation::Cell

You can quickly find location cells by using this example. Create a Ubisense.ULocation.Cell instance and call its Narrow(IObject other) method with the Ubisense.UCell.Cell instance as the argument. In this case we use the row from the Ubisense.UCell.Config.Names relation. The operation will only succeed if the cell in question can be downcast to a ULocation::Cell:


    // location_cell will only get a non-nil result if row.cell_ can
    // be successfully downcast to location_cell. This happens if
    // row.cell_'s dynamic type inherits from location_cell's
    // static type. This will only happen for cells of type
    // ULocation::Cell.
    Ubisense.ULocation.Cell location_cell = new
        Ubisense.ULocation.Cell();
    location_cell.Narrow(row.cell_);

Now, location_cell is non-nil for your location cells, and nil for your spatial cells and site.



4) Find your cells of type USpatial::Cell

Spatial cells are found in the same way as location cells, but we use a Ubisense.USpatial.Cell instead:


    // Similarly, spatial_cell will only get a non-nil result
    // for cells of type USpatial::Cell.
    Ubisense.USpatial.Cell spatial_cell = new Ubisense.USpatial.Cell();
    spatial_cell.Narrow(row.cell_);

Now, spatial_cell is non-nil for your geometry cells, and nil for your location cells and site.



5) Connect to your schema depending on cell type

We now use the Nil() method on the cell instances to test whether we can connect as a client. The cell ID string is used as the argument to ConnectAsClient() as follows:


    if (!location_cell.Nil())
    {
        celldata_schema.ConnectAsClient(location_cell.Id.ToString());
    }

    if (!spatial_cell.Nil())
    {
        monitor_schema.ConnectAsClient(spatial_cell.Id.ToString());
    }

    if (location_cell.Nil() && spatial_cell.Nil())
    {
        // Note that to connect to a schema serving the whole site,
        // ConnectAsClient() takes no arguments.
        building_schema.ConnectAsClient();
    }



Back to top