Skip to content

Topic structure extension

Jimmy Fjällid edited this page Jun 3, 2017 · 8 revisions

The topic structure in the application is used for two things: handling broker connections and handling api connections. For broker connections, the topic structure is used to retrieve information out of the topic of a broker message. For an API connection, it is used to define how api calls will be made and how the sensor types should be retrieved at start.

Topic data retrieval

topicStructures.add(new TopicStructure("GreenIoT","\\/([a-zA-Z0-9-_]{1,30})\\/([a-zA-Z0-9-_]{1,30})\\/([a-zA-Z0-9-_]{1,30})\\/([a-zA-Z0-9-_]{1,30})",1,4,2,"broker"));

Figure 4, topic structure for data retrieval in the broker topic

When using the topic structure for a broker connection, the last field of the TopicStructure constructor needs to be set to “broker”. The first field defines the name of the topic structure, followed by the RegEx of the topic and then the id of the RegEx capture groups that will be used. The order of the capture groups are: location, nodeName and organisation. The RegEx is used to parse the topic and identify the parts that contain the required information.

API calls

The Api support is structured in a three calls manner. The first is a call to get all the sensor types which will be used at the start of the application to retrieve all the sensor types that the API supports. The second one is to retrieve the latest values of a certain type. The last one is to receive sensor data history of a specific type from one sensor. The API structure is defined in the program as an interface that should be implemented.

topicStructures.add(new TopicStructure("Gortz","",0,0,0,"api"));

Figure 5, topic structure for API

public interface BasicRestApi {
  RestApiCall getAllSensorTypesTopic();
 
  RestApiCall getDataFromSensorOfDataTypeTopic(String sensorID, String sensorType, int amountOfData, Calendar lastCall);
 
  RestApiCall getLatestSensorValuesOfSensorTypeTopic(StringsensorTypeName,Calendar currentTime);
}

Figure 6, BasicRestApi interface for API calls

When using the topic structures for an API connection, the last field in the constructor should state “api”. In this case will there be no need for the regex or the ids so these can be left empty for the regex and zeroed for the ids. This can be seen in figure 5. The topic structure will define what the URL will be for all the calls and what format will be used for response of the "getAllSensorTypesTopic" call.

public class Gortz implements BasicRestApi {
   @Override
   public RestApiCall getAllSensorTypesTopic() {
       return new RestApiCall("/sensortypes", HttpMethod.GET);
   }
 
   @Override
   public RestApiCall getDataFromSensorOfDataTypeTopic(String sensorID, String sensorType, int amountOfData, Calendar lastCall) {
       return new RestApiCall("/sensors/"+sensorID+"/sensortypes/"+sensorType+"/size/"+amountOfData,HttpMethod.GET);
   }
 
   @Override
   public RestApiCall getLatestSensorValuesOfSensorTypeTopic(String sensorType, Calendar currentTime) {
       return new RestApiCall("/sensors/sensortypes/"+sensorType+"/size/1", HttpMethod.GET);
   }
}

Figure 7, an example on a Request api class.

The class that implements the BasicRestApi interface will return the endpoints that should called by the program. So for example in the case of the gortz api structure, the url “gortz.org/sensortypes” will be called for retrieving all supported sensor types in the program.

switch (topicStructure){
 
   case "Gortz":
       JSONArray arr = new JSONArray(result);
       for(int i = 0; i< arr.length(); i++){
           JSONObject t = arr.getJSONObject(i);
           sensorTypes.add(new SensorType(t.getString("name"),t.getString("unit")));
       }
       break;
    ...
   default:
       Log.i(TAG,"No support for the ["+topicStructure+"] topicStructure");
}

Figure 6, showing the handling of the inofficial gortz sensor type structure.

The data will be sent to the DataStructureConverter class and handled by the convertSensorTypesResponse function. Each supported type will be handled in a switch case and the identifier is the name of the api sensor topic. The result of the function should be a list of sensorTypes that will be checked later if they exists and if not, they will be added as unsupported and inactive.

To add support for the API to handle a new topic structure, three things needs to be done. The first is to add the name of the topic structure in the switch case as seen in figure 6 with the conversation from response to list of sensor type. The second is define the calls in a new class implenting the BaseicRestAPI Interface. The last things is to add the new topic to the insertStaticTopicStructures function in the SensorDataDbHelper class.

Clone this wiki locally