Java客户端

客户端可帮助您从Java应用程序与ThingsBoard进行交互。
使用客户端您可以在ThingsBoard中以编程方式创建资产、设备、客户、用户和其他实体及其关系。
推荐使用Maven构建工具安装Java客户端。

社区版Java客户端

使用以下依赖项将REST客户端添加至Maven/Gradle项目中:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.thingsboard</groupId>
  4. <artifactId>rest-client</artifactId>
  5. <version>3.1.1</version>
  6. </dependency>
  7. </dependencies>

注意:客户端基于Spring RestTemplate构建因此依赖于Spring Web(在撰写本文时为5.1.5.RELEASE)。
下载Java客户端依赖您应该将以下代码库添加到项目中或者您可以从github构建REST Client。

  1. <repositories>
  2. <repository>
  3. <id>thingsboard</id>
  4. <url>https://repo.thingsboard.io/artifactory/libs-release-public</url>
  5. </repository>
  6. </repositories>

代码审查

您可以在 此处 找到示例应用程序。

  1. // ThingsBoard REST API URL
  2. String url = "http://localhost:8080";
  3. // Default Tenant Administrator credentials
  4. String username = "tenant@thingsboard.org";
  5. String password = "tenant";
  6. // Creating new rest client and auth with credentials
  7. RestClient client = new RestClient(url);
  8. client.login(username, password);
  9. // Creating an Asset
  10. Asset asset = new Asset();
  11. asset.setName("Building 1");
  12. asset.setType("building");
  13. asset = client.saveAsset(asset);
  14. // creating a Device
  15. Device device = new Device();
  16. device.setName("Thermometer 1");
  17. device.setType("thermometer");
  18. device = client.saveDevice(device);
  19. // creating relations from device to asset
  20. EntityRelation relation = new EntityRelation();
  21. relation.setFrom(asset.getId());
  22. relation.setTo(device.getId());
  23. relation.setType("Contains");
  24. client.saveRelation(relation);

专业版Java客户端

使用以下依赖项将Java客户端添加至Maven/Gradle项目中:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.thingsboard</groupId>
  4. <artifactId>rest-client</artifactId>
  5. <version>3.1.1PE</version>
  6. </dependency>
  7. </dependencies>

注意:Java客户端基于Spring RestTemplate构建,因此依赖于Spring Web(在撰写本文时为5.1.5.RELEASE)。
Java客户端仓库地址

  1. <repositories>
  2. <repository>
  3. <id>thingsboard</id>
  4. <url>https://repo.thingsboard.io/artifactory/libs-release-public</url>
  5. </repository>
  6. </repositories>

代码审查

您可以在 此处 找到示例应用程序。

  1. // ThingsBoard REST API URL
  2. final String url = "http://localhost:8080";
  3. // Default System Administrator credentials
  4. final String username = "sysadmin@thingsboard.org";
  5. final String password = "sysadmin";
  6. // creating new rest restClient and auth with system administrator credentials
  7. restClient = new RestClient(url);
  8. login(username, password);
  9. // Creating Tenant
  10. Tenant tenant = new Tenant();
  11. tenant.setTitle("Test Tenant");
  12. tenant = restClient.saveTenant(tenant);
  13. final String tenantUsername = "testtenant@thingsboard.org";
  14. final String tenantPassword = "testtenant";
  15. // Created User for Tenant
  16. User tenantUser = new User();
  17. tenantUser.setAuthority(Authority.TENANT_ADMIN);
  18. tenantUser.setEmail(tenantUsername);
  19. tenantUser.setTenantId(tenant.getId());
  20. tenantUser = restClient.saveUser(tenantUser, false);
  21. restClient.activateUser(tenantUser.getId(), tenantPassword);
  22. // login with Tenant
  23. login(tenantUsername, tenantPassword);
  24. // Loading Widget from file
  25. // Path widgetFilePath = Paths.get("src/main/resources/custom_widget.json");
  26. // JsonNode widgetJson = mapper.readTree(Files.readAllBytes(widgetFilePath));
  27. // loadWidget(widgetJson);
  28. // Loading Rule Chain from file
  29. // Path ruleChainFilePath = Paths.get("src/main/resources/rule_chain.json");
  30. // JsonNode ruleChainJson = mapper.readTree(Files.readAllBytes(ruleChainFilePath));
  31. // loadRuleChain(ruleChainJson, false);
  32. // Creating Dashboard Group on the Tenant Level
  33. EntityGroup sharedDashboardsGroup = new EntityGroup();
  34. sharedDashboardsGroup.setName("Shared Dashboards");
  35. sharedDashboardsGroup.setType(EntityType.DASHBOARD);
  36. sharedDashboardsGroup = restClient.saveEntityGroup(sharedDashboardsGroup);
  37. // Loading Dashboard from file
  38. JsonNode dashboardJson = mapper.readTree(RestClientExample.class.getClassLoader().getResourceAsStream("watermeters.json"));
  39. Dashboard dashboard = new Dashboard();
  40. dashboard.setTitle(dashboardJson.get("title").asText());
  41. dashboard.setConfiguration(dashboardJson.get("configuration"));
  42. dashboard = restClient.saveDashboard(dashboard);
  43. // Adding Dashboard to the Shared Dashboards Group
  44. restClient.addEntitiesToEntityGroup(sharedDashboardsGroup.getId(), Collections.singletonList(dashboard.getId()));
  45. // Creating Customer 1
  46. Customer customer1 = new Customer();
  47. customer1.setTitle("Customer 1");
  48. customer1 = restClient.saveCustomer(customer1);
  49. Device waterMeter1 = new Device();
  50. waterMeter1.setCustomerId(customer1.getId());
  51. waterMeter1.setName("WaterMeter1");
  52. waterMeter1.setType("waterMeter");
  53. waterMeter1 = restClient.saveDevice(waterMeter1);
  54. // Update device token
  55. DeviceCredentials deviceCredentials = restClient.getDeviceCredentialsByDeviceId(waterMeter1.getId()).get();
  56. deviceCredentials.setCredentialsId("new_device_token");
  57. restClient.saveDeviceCredentials(deviceCredentials);
  58. // Fetching automatically created "Customer Administrators" Group.
  59. EntityGroupInfo customer1Administrators = restClient.getEntityGroupInfoByOwnerAndNameAndType(customer1.getId(), EntityType.USER, "Customer Administrators").get();
  60. // Creating Read-Only Role
  61. Role readOnlyRole = restClient.createGroupRole("Read-Only", Arrays.asList(Operation.READ, Operation.READ_ATTRIBUTES, Operation.READ_TELEMETRY, Operation.READ_CREDENTIALS));
  62. // Assigning Shared Dashboards to the Customer 1 Administrators
  63. GroupPermission groupPermission = new GroupPermission();
  64. groupPermission.setRoleId(readOnlyRole.getId());
  65. groupPermission.setUserGroupId(customer1Administrators.getId());
  66. groupPermission.setEntityGroupId(sharedDashboardsGroup.getId());
  67. groupPermission.setEntityGroupType(sharedDashboardsGroup.getType());
  68. groupPermission = restClient.saveGroupPermission(groupPermission);
  69. // Creating User for Customer 1 with default dashboard from Tenant "Shared Dashboards" group.
  70. String userEmail = "user@thingsboard.org";
  71. String userPassword = "secret";
  72. User user = new User();
  73. user.setAuthority(Authority.CUSTOMER_USER);
  74. user.setCustomerId(customer1.getId());
  75. user.setEmail(userEmail);
  76. ObjectNode additionalInfo = mapper.createObjectNode();
  77. additionalInfo.put("defaultDashboardId", dashboard.getId().toString());
  78. additionalInfo.put("defaultDashboardFullscreen", false);
  79. user.setAdditionalInfo(additionalInfo);
  80. user = restClient.saveUser(user, false);
  81. restClient.activateUser(user.getId(), userPassword);
  82. restClient.addEntitiesToEntityGroup(customer1Administrators.getId(), Collections.singletonList(user.getId()));