Skip to content
sanshengshui edited this page Apr 20, 2019 · 4 revisions

banner

preface

TSL is a description of the functions of the device in the cloud,including the device's properties,data,services and events.

IOT Platform describe the object model by defining a description language for objects,Call it TSL(Thing Specification Language),In JSON format, you can assemble escalation devices based on TSL.

The end result:

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

  • Parse the string that identifies the JSON string and the JSON array type

  • Parse the JSON string that identifies the Unix timestamp with millisecond precision

Results the following:

Introduction of depend on

Using the serialization framework Gson to identify and parse json-formatted key-value pairs, you can configure the relationship by introducing com.google.code.gson.

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

Key/value pair

KvEntry

KvEntry

The KvEntry provides a basic interface for getting key-value pair attributes,such as keys for character attributes,values and strings,and interface methods for Boolean and numeric types. BasicKvEntry defines the key only for string key. LongDataEntry,BooleanDataEntry,DoubleDataEntry and StringDataEntry defines the attributes of corresponding values respectively.

public interface KvEntry extends Serializable {

    String getKey();

    DataType getDataType();

    Optional<String> getStrValue();

    Optional<Long> getLongValue();

    Optional<Boolean> getBooleanValue();

    Optional<Double> getDoubleValue();

    String getValueAsString();

    Object getValue();

}

FromDeviceMsg

Attributes and Telemetry upload

By dividing messages from the device into device attributes(AttributesUpdateRequest) and device upload data by type(TelemetryUploadRequest).

TelemetryUploadRequest includes a long Unix timestamp

Json recognition parsing

Attribute identification analysis

Attribute identification and identification are as follows

The UML sequence diagram is shown below:

AttributesUpdateRequest

public class JsonConverter {

    private static final Gson GSON = new Gson();
    public static final String CAN_T_PARSE_VALUE = "Can't parse value: ";

   //Iterate through the key-value properties and process the corresponding key-value
   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 string.
                if (value.isString()) {
                	//new StringDataEntry
                    result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString()));
                //If the value is of Boolean type.
                } else if (value.isBoolean()) {
                //new BooleanDataEntry
                    result.add(new BooleanDataEntry(valueEntry.getKey(), value.getAsBoolean()));
                    //If the value is a numeric type.
                } 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);
            }
        }
        return result;
    }
    
    private static void parseNumericValue(List<KvEntry> result, Map.Entry<String, JsonElement> valueEntry, JsonPrimitive value) {
    	//the numeric value is converted to a string type and determines whether is contains "." to determine whether is is Long or a Double.
        if (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();
            //Parses the Json string into a collection of key-value attributes.
            request.add(parseValues(element.getAsJsonObject()).stream().map(kv -> new BaseAttributeKvEntry(kv, ts)).collect(Collectors.toList()));
            return request;
        } else {
            throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element);
        }
    }
}

Run

The preparatory work:

​ Install Docker

I have mirrored this project and uploaded it to DockerHub.

🌟 🌟🌟

Source address: IOT-Guide-TSL

  1. Download sanshengshui/iot-guide-tsl mirror from DockerHub.
 docker pull sanshengshui/iot-gui-tsl

​ 2. Run iot-guide-tsl in the background and map the mirror port 8080 to the native 8080.

 docker run -d -p 8080:8080 sanshengshui/iot-guide-tsl
  1. Use the curl test 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"