thingsboard 演示地址:https://www.iotschool.com/topics/8
物模型是对设备在云端的功能描述,包括设备的属性,数据,服务和事件。
物联网平台通过定义一种物的描述语言来描述物模型,称之为 tsl(即 thing specification language),采用 json 格式,您可以根据 tsl 组装上报设备的数据。
最终能达到的效果:
识别 json 中的键值内容,默认情况下,key 始终是一个字符串,而 value 可以是 string,boolean,double 或 long。
解析识别 json 字符串和 json 数组类型的字符串
解析识别带有毫秒精度的 unix 时间戳的 json 字符串
效果如下:
### 引入依赖
使用序列化框架 gson 对 json 格式的键值对进行识别解析,可以通过引入 com.google.code.gson 来配置关系。
com.google.code.gson
gson
键值属性
在 kventry 中提供了获取键值对属性的基本接口,例如获取字符属性的键,值和获取字符串,布尔型和数字类型的接口方法。basickventry 定义了键只能为字符串类型,longdataentry,booleandataentry,doubledataentry 和 stringdataentry 分别定义了相应属性的值。
public interface kventry extends serializable {
string getkey();
datatype getdatatype();
optional getstrvalue();
optional getlongvalue();
optional getbooleanvalue();
optional getdoublevalue();
string getvalueasstring();
object getvalue();
}
通过将来自设备的消息根据类型划分为设备属性 (attributesupdaterequest) 和设备上传数据 (telemetryuploadrequest),
其中 telemetryuploadrequest 包含了 long 型的 unix 时间戳。
json 识别解析
属性识别解析
属性识别解析如下,上传数据解析识别类似
uml 时序图如下:
public class jsonconverter {
private static final gson gson = new gson();
public static final string can_t_parse_value = "can't parse value: ";
//遍历键值属性,对相应键值进行处理
public static list parsevalues(jsonobject valuesobject) {
list result = new arraylist<>();
for (map.entry valueentry : valuesobject.entryset()) {
jsonelement element = valueentry.getvalue();
if (element.isjsonprimitive()) {
jsonprimitive value = element.getasjsonprimitive();
//如果值为字符串
if (value.isstring()) {
//新建stringdataentry
result.add(new stringdataentry(valueentry.getkey(), value.getasstring()));
//如果值为布尔型
} else if (value.isboolean()) {
//新建booleandataentry
result.add(new booleandataentry(valueentry.getkey(), value.getasboolean()));
//如果值为数值类型
} 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 result, map.entry valueentry, jsonprimitive value) {
//数值转化为字符串类型,并判断是不是包含".",来判断是long,还是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();
//将json字符串解析为键值属性的集合
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);
}
}
}
准备工作:
安装 docker
我已经将此工程制作成镜像,并上传到 dockerhub 上。
源代码地址:
从 dockerhub 下载 sanshengshui/iot-guide-tsl 镜像
docker pull sanshengshui/iot-gui-tsl
后台运行 iot-guide-tsl,并将镜像端口 80080 映射到本机的 8080
docker run -d -p 8080:8080 sanshengshui/iot-guide-tsl
利用 curl 测试接口
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"
本文作者: 穆书伟
本文链接: 我是庖丁
澳门人威尼斯3966的版权声明: 本博客所有文章除特别声明外,均采用 by-nc-sa 许可协议。转载请注明出处!