thingsboard-client-demo/src/main/java/com/tuoheng/App.java

84 lines
3.1 KiB
Java

package com.tuoheng;
import org.thingsboard.rest.client.RestClient;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.query.*;
import org.thingsboard.server.common.data.util.CollectionsUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Hello world!
*
*/
public class App
{
public static void main(String[] args) {
// ThingsBoard REST API URL
String url = "http://iot.t-aaron.com:18080";
// Default Tenant Administrator credentials
String username = "tenant@thingsboard.org";
String password = "tuoheng2023";
// Creating new rest client and auth with credentials
RestClient client = new RestClient(url);
client.login(username, password);
PageData<Device> tenantDevices;
PageLink pageLink = new PageLink(10);
do {
// Fetch all tenant devices using current page link and print each of them
tenantDevices = client.getTenantDevices("", pageLink);
// tenantDevices.getData().forEach(System.out::println);
for (Device device : tenantDevices.getData()) {
System.out.println(device.getName());
System.out.println(device.getId().getEntityType().name());
ArrayList arrayList = new ArrayList();
arrayList.add("connectorType");
arrayList.add("humidity");
arrayList.add("data.job_number");
List<AttributeKvEntry> attributeKvEntries = client.getAttributeKvEntries(device.getId(), arrayList);
for (AttributeKvEntry attributeKvEntry : attributeKvEntries) {
System.out.println(attributeKvEntry.getKey() + ":" + attributeKvEntry.getValue());
}
if(device.getId().getEntityType().name().equals("DEVICE")) {
List<String> kvEntries = client.getTimeseriesKeys(device.getId());
if(!CollectionsUtil.isEmpty(kvEntries)){
List<TsKvEntry> latest = client.getLatestTimeseries(device.getId(),kvEntries);
if(!CollectionsUtil.isEmpty(latest)){
for(TsKvEntry latestKvEntry : latest){
System.out.println(latestKvEntry.getKey() + ":" + latestKvEntry.getValue());
}
}
}
}
}
pageLink = pageLink.nextPageLink();
} while (tenantDevices.hasNext());
// Get information of current logged in user and print it
// client.getUser().ifPresent(System.out::println);
// Perform logout of current user and close the client
client.logout();
client.close();
}
}