preface

Object model is the function description of the device in the cloud, including the attributes, data, services and events of the device.

The Internet of Things platform describes the object model by defining a description Language called TSL (Thing Specification Language) in JSON format. You can assemble and report device data according to TSL.

The final result:

  • Identify Key value content in JSON. By default, Key is always a String, and value can be String, Boolean,double, or long.

  • Parse strings that recognize JSON strings and JSON array types

  • Parsing recognizes JSON strings with Unix timestamps with millisecond precision

The effect is as follows:

Introduction of depend on

The serialization framework GSON is used to identify and parse jSON-formatted key-value pairs. Relationships can be configured by introducing com.google.code.gson.

 <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
 </dependency>
Copy the code

Key attributes

KvEntry

In KvEntry, there are basic interfaces for getting key-value pair attributes, such as keys, values for character attributes, and interface methods for getting string, Boolean, and number types. BasicKvEntry defines the key only for string type, LongDataEntry, BooleanDataEntry, DoubleDataEntry and StringDataEntry defines the attributes of the corresponding values respectively.

public interface KvEntry extends Serializable {

    String getKey(a);

    DataType getDataType(a);

    Optional<String> getStrValue(a);

    Optional<Long> getLongValue(a);

    Optional<Boolean> getBooleanValue(a);

    Optional<Double> getDoubleValue(a);

    String getValueAsString(a);

    Object getValue(a);

}

Copy the code

Properties and upload data

By dividing messages from devices by type into device attributes (UpdateRequest) and device upload data (TelemetryUploadRequest),

TelemetryUploadRequest contains a Unix timestamp of type Long.

Json recognition parsing

Attribute recognition and analysis

Attributes are identified as follows, and uploaded data is identified similarly

The UML sequence diagram is as follows:

public class JsonConverter {

    private static final Gson GSON = new Gson();
    public static final String CAN_T_PARSE_VALUE = "Can't parse value: "; // Iterate over the key attributes, Public static List<KvEntry> parseValues(JsonObject valuesObject) {List<KvEntry> result = new ArrayList<>();for (Map.Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) {
            JsonElement element = valueEntry.getValue();
            if(element.isJsonPrimitive()) { JsonPrimitive value = element.getAsJsonPrimitive(); // If the value is a stringif(value.isString()) {// Create StringDataEntry result.add(new StringDataEntry(ValueEntry.getKey (), value.getAsString()))); // If the value is Boolean}else if(value.isBoolean()) {// Create new BooleanDataEntry result.add(new BooleanDataEntry(valueEntry.getKey(), value.getasBoolean ())); // If the value is numeric}else if (value.isNumber()) {
                    parseNumericValue(result, valueEntry, value);
                } else{ throw new JsonSyntaxException(CAN_T_PARSE_VALUE + value); }}else{ throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element); }}returnresult; } private static void parseNumericValue(List<KvEntry> result, Map.Entry<String, JsonElement> valueEntry, JsonPrimitive Value) {// Convert the value to a string and check if it is included"."To determine whether it's a Long or a Doubleif (value.getAsString().contains(".")) {
            result.add(new DoubleDataEntry(valueEntry.getKey(), value.getAsDouble()));
        } else {
            try {
                long longValue = Long.parseLong(value.getAsString());
                result.add(new LongDataEntry(valueEntry.getKey(), longValue));
            } catch (NumberFormatException e) {
                throw new JsonSyntaxException("Big integer values are not supported!");
            }
        }
    }

    public static AttributesUpdateRequest convertToAttributes(JsonElement element) {
        return convertToAttributes(element, BasicRequest.DEFAULT_REQUEST_ID);
    }

    public static AttributesUpdateRequest convertToAttributes(JsonElement element, int requestId) {
        if(element.isJsonObject()) { BasicAttributesUpdateRequest request = new BasicAttributesUpdateRequest(requestId); long ts = System.currentTimeMillis(); Request.add (parseValues(element.getasjsonObject ()).stream().map(kv -> new BaseAttributeKvEntry(kv,)) request.add(parseValues(element.getasjsonObject ()).stream().map(kv,) ts)).collect(Collectors.toList()));return request;
        } else{ throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element); }}}Copy the code

run

Preparations:

Install the Docker

I have made a mirror image of this project and uploaded it to DockerHub.

Source code address iot-guide-TSL

  1. Download the sanshengshui/ iot-guide-TSL image from DockerHub
 docker pull sanshengshui/iot-gui-tsl
Copy the code

2. Run the iot-Guide-TSL command in the background and map the mirror port 80080 to 8080 on the local PC

 docker run -d -p 8080:8080 sanshengshui/iot-guide-tsl
Copy the code
  1. Using curl to test the interface
curl -v -X POST -d '{" key1 ", "value1", "key2" : true, "key3" : 3.0, "key4" : 4}' http://localhost:8080/api/v1/tsl --header "Content-Type:application/json"
Copy the code