commit 9031a2f4e78af15dfc7865c5a6d13e708c99d4da Author: 孙小云 Date: Thu Dec 4 14:25:41 2025 +0800 Initial commit: thingsboard-client-demo diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc60829 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f595f6b --- /dev/null +++ b/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + + com.tuoheng + hyf + 1.0-SNAPSHOT + + + thingsboard-client-demo + jar + + thingsboard-client-demo + http://maven.apache.org + + + UTF-8 + + + + + thingsboard + https://repo.thingsboard.io/artifactory/libs-release-public + + + + + + + org.thingsboard + rest-client + 4.2.1 + + + + junit + junit + 3.8.1 + test + + + + diff --git a/src/main/java/com/tuoheng/App.java b/src/main/java/com/tuoheng/App.java new file mode 100644 index 0000000..4d0a07e --- /dev/null +++ b/src/main/java/com/tuoheng/App.java @@ -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 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 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 kvEntries = client.getTimeseriesKeys(device.getId()); + if(!CollectionsUtil.isEmpty(kvEntries)){ + + List 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(); + } +} diff --git a/src/test/java/com/tuoheng/AppTest.java b/src/test/java/com/tuoheng/AppTest.java new file mode 100644 index 0000000..bdb91ad --- /dev/null +++ b/src/test/java/com/tuoheng/AppTest.java @@ -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 ); + } +}