ThingsBoard 二次开发 - 设备属性

# ThgingsBoard

https://iothub.org.cn/docs/iot/
https://iothub.org.cn/docs/iot/dev/dev-attribute/

一、属性规划

1.属性类型

属性主要分为三种:

  • 服务端属性:服务端定义,服务端使用,设备端不能使用
  • 共享属性:服务端定义,设备端可以使用,不能修改
  • 客户端属性:设备端定义属性,服务端可以使用,不能修改

1.服务端属性

在这里插入图片描述

2.共享属性
在这里插入图片描述

3.客户端属性
在这里插入图片描述

2.属性规划

  1. 通过REST API对属性进行增、删、改、查,需评估
  2. 属性增、删、改,会发送消息;
  3. 属性变化的消息通过规则引擎发送到消息队列(RabbitMQ或Kafka、EMQX)

二、准备工作

1.RabbitMQ

1.创建队列
在这里插入图片描述

2.创建交换机
在这里插入图片描述
在这里插入图片描述

2.规则引擎

2.1.创建规则链

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.2.整合设备信息
{
    "sharedKey": "xxxx"
}

# 元数据
{
    "deviceData": "{\"configuration\":{\"type\":\"DEFAULT\"},\"transportConfiguration\":{\"type\":\"DEFAULT\"}}",
    "originatorId": "671878a0-6747-11ee-afb9-c790163a721a",
    "originatorName": "Test-attribute",
    "originatorType": "default",
    
    
    "scope": "SHARED_SCOPE",
    "userEmail": "tenant@thingsboard.org",
    "userId": "08f69680-64f4-11ee-b6d5-bdc7c43c6c8f",
    "userName": "tenant@thingsboard.org"
}

在这里插入图片描述

2.3.添加消息类型
msg.msgType = msgType;
return {msg: msg, metadata: metadata, msgType: msgType};

{
    "sharedKey": "xxxx",
    "msgType": "ATTRIBUTES_UPDATED"
}

在这里插入图片描述

2.4.整合元数据
{
    "sharedKey": "yyyyy",
    "msgType": "ATTRIBUTES_UPDATED",
    "originatorName": "Test-attribute",
    "scope": "SHARED_SCOPE",
    "userName": "tenant@thingsboard.org",
    "originatorType": "default"
}


{
    "deviceData": "{\"configuration\":{\"type\":\"DEFAULT\"},\"transportConfiguration\":{\"type\":\"DEFAULT\"}}",
    "originatorId": "671878a0-6747-11ee-afb9-c790163a721a",
    "originatorName": "Test-attribute",
    "originatorType": "default",
    "scope": "SHARED_SCOPE",
    "userEmail": "tenant@thingsboard.org",
    "userId": "08f69680-64f4-11ee-b6d5-bdc7c43c6c8f",
    "userName": "tenant@thingsboard.org"
}

在这里插入图片描述

2.5.RabbitMQ

在这里插入图片描述

三、设备(MQTT)

1.上传客户端属性

package com.iothub.attribute;
import com.iothub.mqtt.EmqClient;
import com.iothub.mqtt.MqttProperties;
import com.iothub.mqtt.QosEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@Component
public class Upload {

    @Autowired
    private EmqClient emqClient;

    @Autowired
    private MqttProperties properties;

    @PostConstruct
    public void init(){
        //连接服务端
        emqClient.connect(properties.getUsername(),properties.getPassword());
        //订阅一个主题
        //emqClient.subscribe("testtopic/#", QosEnum.QoS1);
    }

    @Scheduled(fixedRate = 2000)
    public void publish(){

        String data = getData();

        emqClient.publish("v1/devices/me/attributes",data,
                QosEnum.QoS1,false);
    }

    private String getData(){

        String data = "{\n" +
                "\t\"attribute1\": \"value1\",\n" +
                "\t\"attribute2\": true,\n" +
                "\t\"attribute3\": 42.0,\n" +
                "\t\"attribute4\": 73,\n" +
                "\t\"attribute5\": {\n" +
                "\t\t\"someNumber\": 42,\n" +
                "\t\t\"someArray\": [1, 2, 3],\n" +
                "\t\t\"someNestedObject\": {\n" +
                "\t\t\t\"key\": \"value\"\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}";

        return data;
    }
}

2.下载服务端属性

package com.iothub.attribute;
import com.iothub.mqtt.EmqClient;
import com.iothub.mqtt.MqttProperties;
import com.iothub.mqtt.QosEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import javax.annotation.PostConstruct;

//@Component
public class Download {

    @Autowired
    private EmqClient emqClient;

    @Autowired
    private MqttProperties properties;

    @PostConstruct
    public void init(){
        //连接服务端
        emqClient.connect(properties.getUsername(),properties.getPassword());
        //订阅一个主题
        emqClient.subscribe("v1/devices/me/attributes/response/+", QosEnum.QoS1);
    }

    @Scheduled(fixedRate = 2000)
    public void publish(){

        String data = getData();

        emqClient.publish("v1/devices/me/attributes/request/1",data, QosEnum.QoS1,false);
    }

    private String getData(){

        String data = "{\n" +
                "\t\"clientKeys\": \"attribute1,attribute2\",\n" +
                "\t\"sharedKeys\": \"shared1,shared2\"\n" +
                "}";

        return data;
    }
}

3.订阅共享属性

package com.iothub.attribute;
import com.iothub.mqtt.EmqClient;
import com.iothub.mqtt.MqttProperties;
import com.iothub.mqtt.QosEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import javax.annotation.PostConstruct;

//@Component
public class Subscribe {
    @Autowired
    private EmqClient emqClient;

    @Autowired
    private MqttProperties properties;

    @PostConstruct
    public void init(){
        //连接服务端
        emqClient.connect(properties.getUsername(),properties.getPassword());
        //订阅一个主题
        emqClient.subscribe("v1/devices/me/attributes", QosEnum.QoS1);
    }

    @Scheduled(fixedRate = 2000)
    public void publish(){

        String data = getData();

        //emqClient.publish("v1/devices/me/attributes/request/1",data, QosEnum.QoS1,false);
    }

    private String getData(){

        String data = "{\n" +
                "\t\"clientKeys\": \"attribute1,attribute2\",\n" +
                "\t\"sharedKeys\": \"shared1,shared2\"\n" +
                "}";

        return data;
    }
}

四、属性更新测试

1.上传客户端属性

# 数据

{
    "attribute1": "value1",
    "attribute2": true,
    "attribute3": 42,
    "attribute4": 73,
    "attribute5": {
        "someNumber": 42,
        "someArray": [1, 2, 3],
        "someNestedObject": {
            "key": "value"
        }
    },
    "msgType": "POST_ATTRIBUTES_REQUEST",
    "originatorName": "Test-attribute",
    "deviceData": "{\"configuration\":{\"type\":\"DEFAULT\"},\"transportConfiguration\":{\"type\":\"DEFAULT\"}}",
    "originatorId": "671878a0-6747-11ee-afb9-c790163a721a",
    "originatorType": "default"
}


{
	"attribute1": "value1",
	"attribute2": true,
	"attribute3": 42,
	"attribute4": 73,
	"attribute5": {
		"someNumber": 42,
		"someArray": [1, 2, 3],
		"someNestedObject": {
			"key": "value"
		}
	},
	"msgType": "POST_ATTRIBUTES_REQUEST",
	"originatorName": "Test-attribute",
	"deviceData": "{\"configuration\":{\"type\":\"DEFAULT\"},\"transportConfiguration\":{\"type\":\"DEFAULT\"}}",
	"originatorId": "671878a0-6747-11ee-afb9-c790163a721a",
	"originatorType": "default"
}
# 元数据

{
    "deviceData": "{\"configuration\":{\"type\":\"DEFAULT\"},\"transportConfiguration\":{\"type\":\"DEFAULT\"}}",
    "deviceName": "Test-attribute",
    "deviceType": "default",
    "notifyDevice": "false",
    "originatorId": "671878a0-6747-11ee-afb9-c790163a721a",
    "originatorName": "Test-attribute",
    "originatorType": "default"
}

在这里插入图片描述

2.创建服务端属性

# 数据

{
    "serverKey": "aaaaa",
    "msgType": "ATTRIBUTES_UPDATED",
    "originatorName": "Test-attribute",
    "scope": "SERVER_SCOPE",
    "deviceData": "{\"configuration\":{\"type\":\"DEFAULT\"},\"transportConfiguration\":{\"type\":\"DEFAULT\"}}",
    "originatorId": "671878a0-6747-11ee-afb9-c790163a721a",
    "userName": "tenant@thingsboard.org",
    "originatorType": "default"
}
{
    "serverKey": "aaaaa",
    "msgType": "ATTRIBUTES_UPDATED",
    "originatorName": "Test-attribute",
    "scope": "SERVER_SCOPE",
    "deviceData": "{\"configuration\":{\"type\":\"DEFAULT\"},\"transportConfiguration\":{\"type\":\"DEFAULT\"}}",
    "originatorId": "671878a0-6747-11ee-afb9-c790163a721a",
    "userName": "tenant@thingsboard.org",
    "originatorType": "default"
}

在这里插入图片描述

3.删除服务端属性

# 数据

{
    "attributes": ["serverKey"],
    "msgType": "ATTRIBUTES_DELETED",
    "originatorName": "Test-attribute",
    "scope": "SERVER_SCOPE",
    "deviceData": "{\"configuration\":{\"type\":\"DEFAULT\"},\"transportConfiguration\":{\"type\":\"DEFAULT\"}}",
    "originatorId": "671878a0-6747-11ee-afb9-c790163a721a",
    "userName": "tenant@thingsboard.org",
    "originatorType": "default"
}
# 元数据

{
    "deviceData": "{\"configuration\":{\"type\":\"DEFAULT\"},\"transportConfiguration\":{\"type\":\"DEFAULT\"}}",
    "originatorId": "671878a0-6747-11ee-afb9-c790163a721a",
    "originatorName": "Test-attribute",
    "originatorType": "default",
    "scope": "SERVER_SCOPE",
    "userEmail": "tenant@thingsboard.org",
    "userId": "08f69680-64f4-11ee-b6d5-bdc7c43c6c8f",
    "userName": "tenant@thingsboard.org"
}

4.创建共享属性

# 数据

{
    "sharedKey": 33333,
    "msgType": "ATTRIBUTES_UPDATED",
    "originatorName": "Test-attribute",
    "scope": "SHARED_SCOPE",
    "deviceData": "{\"configuration\":{\"type\":\"DEFAULT\"},\"transportConfiguration\":{\"type\":\"DEFAULT\"}}",
    "originatorId": "671878a0-6747-11ee-afb9-c790163a721a",
    "userName": "tenant@thingsboard.org",
    "originatorType": "default"
}
# 元数据

{
    "deviceData": "{\"configuration\":{\"type\":\"DEFAULT\"},\"transportConfiguration\":{\"type\":\"DEFAULT\"}}",
    "originatorId": "671878a0-6747-11ee-afb9-c790163a721a",
    "originatorName": "Test-attribute",
    "originatorType": "default",
    "scope": "SHARED_SCOPE",
    "userEmail": "tenant@thingsboard.org",
    "userId": "08f69680-64f4-11ee-b6d5-bdc7c43c6c8f",
    "userName": "tenant@thingsboard.org"
}

五、REST API

1.属性REST API

在这里插入图片描述

2.创建、更新设备属性

在这里插入图片描述

3.删除设备属性

在这里插入图片描述

4.获得实体属性Key

在这里插入图片描述
在这里插入图片描述

5.获得实体属性

在这里插入图片描述
在这里插入图片描述

六、REST API测试

1.创建、更新设备属性

# url
/api/plugins/telemetry/{deviceId}/{scope}

# 设备ID
671878a0-6747-11ee-afb9-c790163a721a

#scope 
CLIENT_SCOPE, SERVER_SCOPE, SHARED_SCOPE


{
 "stringKey":"value1", 
 "booleanKey":true, 
 "doubleKey":42.0, 
 "longKey":73, 
 "jsonKey": {
    "someNumber": 42,
    "someArray": [1,2,3],
    "someNestedObject": {"key": "value"}
 }
}

http://localhost:8999/attri/updatedevice

在这里插入图片描述

    public void deviceUpdeate(){
        String baseURL = "http://82.157.166.86:8080";
        String token = Login.getToken();

        // 地址:/api/plugins/telemetry/{deviceId}/{scope}
        // 设备ID:  671878a0-6747-11ee-afb9-c790163a721a
        String apiUrl = "/api/plugins/telemetry/{deviceId}/{scope}";
        String deviceId = "671878a0-6747-11ee-afb9-c790163a721a";
        HttpHeaders headers = new HttpHeaders();
        //headers.add("Authorization", "Bearer "+ mainToken);
        headers.set("X-Authorization", "Bearer " + token);
        //headers.set("Content-Type", "application/json");
        headers.setContentType(MediaType.APPLICATION_JSON);

        String body = "{\n" +
                " \"stringKey\":\"value1\", \n" +
                " \"booleanKey\":true, \n" +
                " \"doubleKey\":42.0, \n" +
                " \"longKey\":73, \n" +
                " \"jsonKey\": {\n" +
                "    \"someNumber\": 42,\n" +
                "    \"someArray\": [1,2,3],\n" +
                "    \"someNestedObject\": {\"key\": \"value\"}\n" +
                " }\n" +
                "}";

        HttpEntity entity = new HttpEntity<>(body, headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity response = restTemplate.exchange(baseURL + apiUrl, HttpMethod.POST, entity, String.class, new Object[]{deviceId,"SHARED_SCOPE"});
        System.out.println(response);
        if (response.getStatusCodeValue() == 200) {
            String device = (String) response.getBody();
            System.out.println(response.getBody());
        }
    }

2.删除设备属性

# url
/api/plugins/telemetry/{deviceId}/{scope}{?keys}
/api/plugins/telemetry/{deviceId}/SHARED_SCOPE?keys=stringKey

# 设备ID
671878a0-6747-11ee-afb9-c790163a721a

#scope 
CLIENT_SCOPE, SERVER_SCOPE, SHARED_SCOPE


sharedKey

http://localhost:8999/attri/deletedevice
    public void deviceDelete(){

        String baseURL = "http://82.157.166.86:8080";
        String token = Login.getToken();

        // 地址:/api/plugins/telemetry/{deviceId}/{scope}{?keys}
        // 设备ID:  671878a0-6747-11ee-afb9-c790163a721a
        String apiUrl = "/api/plugins/telemetry/{deviceId}/SHARED_SCOPE?keys=stringKey";
        //String apiUrl = "/api/plugins/telemetry/{deviceId}/{scope}{?keys}";
        String deviceId = "671878a0-6747-11ee-afb9-c790163a721a";
        HttpHeaders headers = new HttpHeaders();
        //headers.add("Authorization", "Bearer "+ mainToken);
        headers.set("X-Authorization", "Bearer " + token);
        //headers.set("Content-Type", "application/json");
        headers.setContentType(MediaType.APPLICATION_JSON);

        //String keys = "sharedKey";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("deviceId", deviceId);
//        paramMap.put("scope", "SHARED_SCOPE");
//        paramMap.put("keys", "?keys=stringKey");


        HttpEntity entity = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        //ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.DELETE, entity, String.class, new Object[]{deviceId,"SHARED_SCOPE","keys=stringKey"});
        ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.DELETE, entity, String.class, paramMap);
        System.out.println(response);
        if (response.getStatusCodeValue() == 200) {
            String device = (String) response.getBody();
            System.out.println(response.getBody());
        }
    }

3.删除实体属性

# url
/api/plugins/telemetry/{entityType}/{entityId}/{scope}{?keys}
/api/plugins/telemetry/{entityType}/{entityId}/CLIENT_SCOPE?keys=attribute4

# 设备ID
671878a0-6747-11ee-afb9-c790163a721a

# entityType 
DEVICE

#scope 
CLIENT_SCOPE, SERVER_SCOPE, SHARED_SCOPE


sharedKey


http://localhost:8999/attri/deleteentity
    public void entityDelete() {

        String baseURL = "http://82.157.166.86:8080";
        String token = Login.getToken();

        // 地址:/api/plugins/telemetry/{entityType}/{entityId}/{scope}{?keys}
        // 设备ID:  671878a0-6747-11ee-afb9-c790163a721a
        String apiUrl = "/api/plugins/telemetry/{entityType}/{entityId}/CLIENT_SCOPE?keys=attribute4";
        String deviceId = "671878a0-6747-11ee-afb9-c790163a721a";
        HttpHeaders headers = new HttpHeaders();
        //headers.add("Authorization", "Bearer "+ mainToken);
        headers.set("X-Authorization", "Bearer " + token);
        //headers.set("Content-Type", "application/json");
        headers.setContentType(MediaType.APPLICATION_JSON);

        //String keys = "sharedKey";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("entityType", "DEVICE");
        paramMap.put("entityId", deviceId);


        HttpEntity entity = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        //ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.DELETE, entity, String.class, new Object[]{deviceId,"SHARED_SCOPE","sharedKey"});
        ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.DELETE, entity, String.class, paramMap);
        System.out.println(response);
        if (response.getStatusCodeValue() == 200) {
            String device = (String) response.getBody();
            System.out.println(response.getBody());
        }
    }

4.获得属性keys

# url
/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes

# 设备ID
671878a0-6747-11ee-afb9-c790163a721a

# entityType 
DEVICE

#scope 
CLIENT_SCOPE, SERVER_SCOPE, SHARED_SCOPE


http://localhost:8999/attri/getentitykeys


# 只能获取以下服务端属性
["lastConnectTime","lastDisconnectTime","lastActivityTime","active","inactivityAlarmTime"]
与swagger测试不符
    public void getEntityKeys(){

        String baseURL = "http://82.157.166.86:8080";
        String token = Login.getToken();

        // 地址:/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes
        // 设备ID  452fa1a0-3768-11ee-8f31-b92b1a081d15
        String deviceId = "bbac67d0-6519-11ee-afb9-c790163a721a";
        String apiUrl = "/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes";
        HttpHeaders headers = new HttpHeaders();
        //headers.add("Authorization", "Bearer "+ mainToken);
        headers.set("X-Authorization", "Bearer "+ token);
        //headers.set("Content-Type", "application/json");
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity<>(headers);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("entityType", "DEVICE");
        paramMap.put("entityId", deviceId);

        RestTemplate restTemplate = new RestTemplate();
        //ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.GET, entity, String.class, new Object[]{"DEVICE",deviceId});
        ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.GET, entity, String.class, paramMap);
        System.out.println(response);
        if( response.getStatusCodeValue() == 200 ){
            String device = (String)response.getBody();
            System.out.println(response.getBody());

        }
    }

5.获得属性keys(scope)

# url
/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes/{scope}

# 设备ID
671878a0-6747-11ee-afb9-c790163a721a

# entityType 
DEVICE

#scope 
CLIENT_SCOPE, SERVER_SCOPE, SHARED_SCOPE


http://localhost:8999/attri/getentitykeysscope


# 只能获取以下服务端属性
["lastConnectTime","lastDisconnectTime","lastActivityTime","active","inactivityAlarmTime"]
#共享属性、客户端属性获取不了
与swagger测试不符
    public void getEntityKeysScope(){
        String baseURL = "http://82.157.166.86:8080";
        String token = Login.getToken();

        // 地址:/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes
        // 设备ID  452fa1a0-3768-11ee-8f31-b92b1a081d15
        String deviceId = "bbac67d0-6519-11ee-afb9-c790163a721a";
        String apiUrl = "/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes/{scope}";
        HttpHeaders headers = new HttpHeaders();
        //headers.add("Authorization", "Bearer "+ mainToken);
        headers.set("X-Authorization", "Bearer "+ token);
        //headers.set("Content-Type", "application/json");
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity<>(headers);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("entityType", "DEVICE");
        paramMap.put("entityId", deviceId);
        paramMap.put("scope", "SHARED_SCOPE");

        RestTemplate restTemplate = new RestTemplate();
        //ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.GET, entity, String.class, new Object[]{"DEVICE",deviceId});
        ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.GET, entity, String.class, paramMap);
        System.out.println(response);
        if( response.getStatusCodeValue() == 200 ){
            String device = (String)response.getBody();
            System.out.println(response.getBody());

        }

    }

6.获得属性

# url
/api/plugins/telemetry/{entityType}/{entityId}/values/attributes{?keys}
/api/plugins/telemetry/{entityType}/{entityId}/values/attributes?keys=active,testKey

# 设备ID
671878a0-6747-11ee-afb9-c790163a721a

# entityType 
DEVICE

#scope 
CLIENT_SCOPE, SERVER_SCOPE, SHARED_SCOPE


http://localhost:8999/attri/getentityattri


# 获取以下属性
[{"lastUpdateTs":1696903103362,"key":"active","value":false}]



# 例子
/api/plugins/telemetry/{entityType}/{entityId}/values/attributes
[{
	"lastUpdateTs": 1696902420986,
	"key": "lastConnectTime",
	"value": 1696902420986
}, {
	"lastUpdateTs": 1696902453145,
	"key": "lastDisconnectTime",
	"value": 1696902453145
}, {
	"lastUpdateTs": 1696902455853,
	"key": "lastActivityTime",
	"value": 1696902453130
}, {
	"lastUpdateTs": 1696903103362,
	"key": "active",
	"value": false
}, {
	"lastUpdateTs": 1696903103362,
	"key": "inactivityAlarmTime",
	"value": 1696903103362
}]
    public void getEntityAttri(){
        String baseURL = "http://82.157.166.86:8080";
        String token = Login.getToken();

        // 地址:/api/plugins/telemetry/{entityType}/{entityId}/values/attributes{?keys}
        // 设备ID  452fa1a0-3768-11ee-8f31-b92b1a081d15
        String deviceId = "bbac67d0-6519-11ee-afb9-c790163a721a";
        //String apiUrl = "/api/plugins/telemetry/{entityType}/{entityId}/values/attributes?keys=active,testKey";
        String apiUrl = "/api/plugins/telemetry/{entityType}/{entityId}/values/attributes";
        HttpHeaders headers = new HttpHeaders();
        //headers.add("Authorization", "Bearer "+ mainToken);
        headers.set("X-Authorization", "Bearer "+ token);
        //headers.set("Content-Type", "application/json");
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity<>(headers);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("entityType", "DEVICE");
        paramMap.put("entityId", deviceId);


        RestTemplate restTemplate = new RestTemplate();
        //ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.GET, entity, String.class, new Object[]{"DEVICE",deviceId});
        ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.GET, entity, String.class, paramMap);
        System.out.println(response);
        if( response.getStatusCodeValue() == 200 ){
            String device = (String)response.getBody();
            System.out.println(response.getBody());

        }

    }

7.获得属性(scope)

# url
/api/plugins/telemetry/{entityType}/{entityId}/values/attributes/{scope}{?keys}
/api/plugins/telemetry/{entityType}/{entityId}/values/attributes/SERVER_SCOPE?keys=active,testKey

# 设备ID
671878a0-6747-11ee-afb9-c790163a721a

# entityType 
DEVICE

#scope 
CLIENT_SCOPE, SERVER_SCOPE, SHARED_SCOPE


http://localhost:8999/attri/getentityattriscope


# 获取以下属性
[{"lastUpdateTs":1696903103362,"key":"active","value":false}]


# 例子
String apiUrl = "/api/plugins/telemetry/{entityType}/{entityId}/values/attributes/SERVER_SCOPE";
[{
	"lastUpdateTs": 1696902420986,
	"key": "lastConnectTime",
	"value": 1696902420986
}, {
	"lastUpdateTs": 1696902453145,
	"key": "lastDisconnectTime",
	"value": 1696902453145
}, {
	"lastUpdateTs": 1696902455853,
	"key": "lastActivityTime",
	"value": 1696902453130
}, {
	"lastUpdateTs": 1696903103362,
	"key": "active",
	"value": false
}, {
	"lastUpdateTs": 1696903103362,
	"key": "inactivityAlarmTime",
	"value": 1696903103362
}]
    public void getEntityAttriScope(){
        String baseURL = "http://82.157.166.86:8080";
        String token = Login.getToken();

        // 地址:/api/plugins/telemetry/{entityType}/{entityId}/values/attributes/{scope}{?keys}
        // 设备ID  452fa1a0-3768-11ee-8f31-b92b1a081d15
        String deviceId = "bbac67d0-6519-11ee-afb9-c790163a721a";
        //String apiUrl = "/api/plugins/telemetry/{entityType}/{entityId}/values/attributes/SERVER_SCOPE?keys=active,testKey";
        String apiUrl = "/api/plugins/telemetry/{entityType}/{entityId}/values/attributes/SERVER_SCOPE";
        HttpHeaders headers = new HttpHeaders();
        //headers.add("Authorization", "Bearer "+ mainToken);
        headers.set("X-Authorization", "Bearer "+ token);
        //headers.set("Content-Type", "application/json");
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity<>(headers);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("entityType", "DEVICE");
        paramMap.put("entityId", deviceId);


        RestTemplate restTemplate = new RestTemplate();
        //ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.GET, entity, String.class, new Object[]{"DEVICE",deviceId});
        ResponseEntity response = restTemplate.exchange(baseURL+apiUrl, HttpMethod.GET, entity, String.class, paramMap);
        System.out.println(response);
        if( response.getStatusCodeValue() == 200 ){
            String device = (String)response.getBody();
            System.out.println(response.getBody());

        }

    }
# ThgingsBoard

https://iothub.org.cn/docs/iot/
https://iothub.org.cn/docs/iot/dev/dev-attribute/
  • 22
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Thingsboard是一个开源的IoT平台,提供了许多基础功能,包括设备管理、数据可视化、规则引擎等。如果需要根据自己的业务需求进行二次开发,可以通过自定义插件、调用API或者直接修改源代码来实现。下面是基于Thingsboard源代码的CURD后端开发流程: 1. 安装并启动Thingsboard 首先需要安装并启动Thingsboard,可以参考官方文档进行操作。 2. 创建新的数据模型 在Thingsboard中,数据模型是指设备、传感器、属性等实体间的关系。如果需要添加新的实体,可以通过创建新的数据模型来实现。具体操作可以参考官方文档。 3. 创建新的REST API 在Thingsboard中,可以通过创建新的REST API来实现CURD操作。具体操作如下: (1)在源代码中创建新的Java类,继承AbstractWebsocketHandler类。 (2)在新的Java类中实现对应的HTTP请求处理方法,比如GET、POST、DELETE等。 (3)在新的Java类中实现对应的数据模型CURD操作,比如查询、新增、修改、删除等。 (4)在新的Java类中定义对应的路由信息,比如URL路径、请求方法等。 (5)在Thingsboard配置文件中定义新的REST API路由信息。 4. 测试新的REST API 完成以上步骤后,可以启动Thingsboard并测试新的REST API是否能够正常工作。可以使用Postman等工具进行测试,也可以在自己的应用中调用该API。 总的来说,通过自定义REST API可以实现对Thingsboard二次开发,满足不同业务需求。当然,需要注意的是,修改源代码可能会影响到系统的稳定性和可维护性,需要谨慎操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IoTHub - 物联网开源技术社区

支持开源技术! 传播开源文化!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值