Initial commit: thingsboard-client-demo

This commit is contained in:
孙小云 2025-12-04 14:25:41 +08:00
commit 9031a2f4e7
4 changed files with 225 additions and 0 deletions

61
.gitignore vendored Normal file
View File

@ -0,0 +1,61 @@
# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
# Java
*.class
*.jar
*.war
*.ear
*.log
*.ctxt
.mtj.tmp/
hs_err_pid*
replay_pid*
# IntelliJ IDEA
.idea/
*.iml
*.iws
*.ipr
out/
# Eclipse
.classpath
.project
.settings/
bin/
# VS Code
.vscode/
# macOS
.DS_Store
.AppleDouble
.LSOverride
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
# Linux
*~
# Logs
logs/
*.log
# Temporary files
*.tmp
*.bak
*.swp
*~.nib

43
pom.xml Normal file
View File

@ -0,0 +1,43 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.tuoheng</groupId>
<artifactId>hyf</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>thingsboard-client-demo</artifactId>
<packaging>jar</packaging>
<name>thingsboard-client-demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>thingsboard</id>
<url>https://repo.thingsboard.io/artifactory/libs-release-public</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.thingsboard</groupId>
<artifactId>rest-client</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,83 @@
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();
}
}

View File

@ -0,0 +1,38 @@
package com.tuoheng;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}