publicclassLogin{publicstaticvoidmain(String[]args){// ThingsBoard REST API URLStringurl="http://192.168.202.188:8080";// Default Tenant Administrator credentials//String username = "tenant@thingsboard.org";//String password = "tenant";Stringusername="iiotos@thingsboard.org";Stringpassword="iiotos";// Creating new rest client and auth with credentialsRestClientclient=newRestClient(url);client.login(username,password);// Get information of current logged in user and print itclient.getUser().ifPresent(System.out::println);// Perform logout of current user and close the clientclient.logout();client.close();}}
publicclassTenantDevice{publicstaticvoidmain(String[]args){// ThingsBoard REST API URLStringurl="http://192.168.202.188:8080";// Default Tenant Administrator credentialsStringusername="tenant@thingsboard.org";Stringpassword="tenant";//String username = "iiotos@thingsboard.org";//String password = "iiotos";// Creating new rest client and auth with credentialsRestClientclient=newRestClient(url);client.login(username,password);PageData<Device>tenantDevices;PageLinkpageLink=newPageLink(10);do{// Fetch all tenant devices using current page link and print each of themtenantDevices=client.getTenantDevices("",pageLink);tenantDevices.getData().forEach(System.out::println);pageLink=pageLink.nextPageLink();}while(tenantDevices.hasNext());// Perform logout of current user and close the clientclient.logout();client.close();}}
publicclassTenantDashboard{publicstaticvoidmain(String[]args){// ThingsBoard REST API URLStringurl="http://192.168.202.188:8080";// Default Tenant Administrator credentialsStringusername="tenant@thingsboard.org";Stringpassword="tenant";//String username = "iiotos@thingsboard.org";//String password = "iiotos";// Creating new rest client and auth with credentialsRestClientclient=newRestClient(url);client.login(username,password);PageData<DashboardInfo>pageData;PageLinkpageLink=newPageLink(10);do{// Fetch all tenant dashboards using current page link and print each of thempageData=client.getTenantDashboards(pageLink);pageData.getData().forEach(System.out::println);pageLink=pageLink.nextPageLink();}while(pageData.hasNext());// Perform logout of current user and close the clientclient.logout();client.close();}}
publicclassCustomerDevice{publicstaticvoidmain(String[]args){// ThingsBoard REST API URLStringurl="http://192.168.202.188:8080";// Perform login with default Customer User credentialsStringusername="customer@thingsboard.org";Stringpassword="customer";RestClientclient=newRestClient(url);client.login(username,password);PageData<Device>pageData;PageLinkpageLink=newPageLink(10);do{// Get current userUseruser=client.getUser().orElseThrow(()->newIllegalStateException("No logged in user has been found"));// Fetch customer devices using current page linkpageData=client.getCustomerDevices(user.getCustomerId(),"",pageLink);pageData.getData().forEach(System.out::println);pageLink=pageLink.nextPageLink();}while(pageData.hasNext());// Perform logout of current user and close the clientclient.logout();client.close();}}
publicclassCountEntities{publicstaticvoidmain(String[]args){// ThingsBoard REST API URLStringurl="http://192.168.202.188:8080";// Perform login with default Customer User credentialsStringusername="tenant@thingsboard.org";Stringpassword="tenant";//String username = "iiotos@thingsboard.org";//String password = "iiotos";RestClientclient=newRestClient(url);client.login(username,password);// Create entity filter to get all devicesEntityTypeFiltertypeFilter=newEntityTypeFilter();typeFilter.setEntityType(EntityType.DEVICE);// Create entity count query with provided filterEntityCountQuerytotalDevicesQuery=newEntityCountQuery(typeFilter);// Execute entity count query and get total devices countLongtotalDevicesCount=client.countEntitiesByQuery(totalDevicesQuery);System.out.println("Total devices by the first query: "+totalDevicesCount);// Set key filter to existing query to get only active devicesKeyFilterkeyFilter=newKeyFilter();keyFilter.setKey(newEntityKey(EntityKeyType.ATTRIBUTE,"active"));keyFilter.setValueType(EntityKeyValueType.BOOLEAN);BooleanFilterPredicatefilterPredicate=newBooleanFilterPredicate();filterPredicate.setOperation(BooleanFilterPredicate.BooleanOperation.EQUAL);filterPredicate.setValue(newFilterPredicateValue<>(true));keyFilter.setPredicate(filterPredicate);// Create entity count query with provided filterEntityCountQuerytotalActiveDevicesQuery=newEntityCountQuery(typeFilter,List.of(keyFilter));// Execute active devices query and print total devices countLongtotalActiveDevicesCount=client.countEntitiesByQuery(totalActiveDevicesQuery);System.out.println("Total devices by the second query: "+totalActiveDevicesCount);// Perform logout of current user and close the clientclient.logout();client.close();}}
publicclassQueryEntities{publicstaticvoidmain(String[]args){// ThingsBoard REST API URLStringurl="http://192.168.202.188:8080";// Perform login with default Customer User credentialsStringusername="tenant@thingsboard.org";Stringpassword="tenant";RestClientclient=newRestClient(url);client.login(username,password);// Create entity filter to get only devicesEntityTypeFiltertypeFilter=newEntityTypeFilter();typeFilter.setEntityType(EntityType.DEVICE);// Create key filter to query only active devicesKeyFilterkeyFilter=newKeyFilter();keyFilter.setKey(newEntityKey(EntityKeyType.ATTRIBUTE,"active"));keyFilter.setValueType(EntityKeyValueType.BOOLEAN);BooleanFilterPredicatefilterPredicate=newBooleanFilterPredicate();filterPredicate.setOperation(BooleanFilterPredicate.BooleanOperation.EQUAL);filterPredicate.setValue(newFilterPredicateValue<>(true));keyFilter.setPredicate(filterPredicate);// Prepare list of queried device fieldsList<EntityKey>fields=List.of(newEntityKey(EntityKeyType.ENTITY_FIELD,"name"),newEntityKey(EntityKeyType.ENTITY_FIELD,"type"),newEntityKey(EntityKeyType.ENTITY_FIELD,"createdTime"));// Prepare list of queried device attributesList<EntityKey>attributes=List.of(newEntityKey(EntityKeyType.ATTRIBUTE,"active"));// Prepare page linkEntityDataSortOrdersortOrder=newEntityDataSortOrder();sortOrder.setKey(newEntityKey(EntityKeyType.ENTITY_FIELD,"createdTime"));sortOrder.setDirection(EntityDataSortOrder.Direction.DESC);EntityDataPageLinkentityDataPageLink=newEntityDataPageLink(10,0,"",sortOrder);// Create entity query with provided entity filter, key filter, queried fields and page linkEntityDataQuerydataQuery=newEntityDataQuery(typeFilter,entityDataPageLink,fields,attributes,List.of(keyFilter));PageData<EntityData>entityPageData;do{// Fetch active devices using entities query and print thementityPageData=client.findEntityDataByQuery(dataQuery);entityPageData.getData().forEach(System.out::println);dataQuery=dataQuery.next();}while(entityPageData.hasNext());// Perform logout of current user and close clientclient.logout();client.close();}}
publicclassManageDevice{publicstaticvoidmain(String[]args){// ThingsBoard REST API URLStringurl="http://192.168.202.188:8080";// Perform login with default Customer User credentials//String username = "tenantg@thingsboard.org";//String password = "tenant";Stringusername="iiotos@thingsboard.org";Stringpassword="iiotos";RestClientclient=newRestClient(url);client.login(username,password);// Construct device objectStringnewDeviceName="Test Device";DevicenewDevice=newDevice();newDevice.setName(newDeviceName);// Create Json Object Node and set it as additional infoObjectMappermapper=newObjectMapper();ObjectNodeadditionalInfoNode=mapper.createObjectNode().put("description","My brand new device");newDevice.setAdditionalInfo(additionalInfoNode);// Save deviceDevicesavedDevice=client.saveDevice(newDevice);System.out.println("Saved device: "+savedDevice);// Find device by device id or throw an exception otherwiseOptional<DeviceInfo>optionalDevice=client.getDeviceInfoById(savedDevice.getId());DeviceInfofoundDevice=optionalDevice.orElseThrow(()->newIllegalArgumentException("Device with id "+newDevice.getId().getId()+" hasn't been found"));// Save device shared attributesObjectNoderequestNode=mapper.createObjectNode().put("temperature",22.4).put("humidity",57.4);booleanisSuccessful=client.saveEntityAttributesV2(foundDevice.getId(),"SHARED_SCOPE",requestNode);System.out.println("Attributes have been successfully saved: "+isSuccessful);// Get device shared attributesvarattributes=client.getAttributesByScope(foundDevice.getId(),"SHARED_SCOPE",List.of("temperature","humidity"));System.out.println("Found attributes: ");attributes.forEach(System.out::println);// Delete the deviceclient.deleteDevice(savedDevice.getId());// Perform logout of current user and close clientclient.logout();client.close();}}