设备管理
更新时间:2019-07-15
创建设备
创建设备可以参考代码如下:
String deviceName = "device_name"; // 设置创建的设备名称
String schemaId = "schema_id"; // 设置绑定的模型ID
String description = "description"; // 设置对该设备的描述
CreateDeviceRequest request = new CreateDeviceRequest()
.withDeviceName(deviceName)
.withSchemaId(schemaId)
.withDescription(description);
DeviceAccessDetailResponse response = client.createDevice(request);
String tcpEndpoint = response.getTcpEndpoint(); // tcp endpoint地址
String sslEndpoint = response.getSslEndpoint(); // ssl endpoint地址
String username = response.getUsername(); // 账户名,用于接入IoT hub服务
String key = response.getKey(); // 密钥,用于接入IoT hub服务
删除设备
删除设备可以参考代码如下:
List<String> devices = Arrays.asList("device_name_1", "device_name_2"); // 设置需要删除的设备列表
DeviceListRequest request = new DeviceListRequest().
withDevices(devices);
DeviceListResponse response = client.removeDevices(request);
List<String> deletedDevices = response.getDevices(); // 删除设备列表
获取设备Profile
获取设备Profile可以参考代码如下:
String deviceName = "device_name"; // 设置需要查询的设备名称
DeviceProfileResponse response = client.getDeviceProfile(deviceName);
String name = response.getName(); // 设备名称
String description = response.getDescription(); // 设备描述
String state = response.getState(); // 设备当前状态
String schemaId = response.getSchemaId(); // 设备使用的模版ID
String schemaName = response.getSchemaName(); // 设备使用的模板名称
Long createTime = response.getCreateTime(); // 设备的创建时间
Long lastActiveTime = response.getLastActiveTime(); // 设备最后一次上报内容的时间
Boolean favourite = response.getFavourite(); // 设备的收藏信息
JsonNode attributes = response.getAttributes(); // 设备在服务端设置的属性
DeviceAttributes device = response.getDevice(); // 设备在设备端的属性
获取设备View
获取设备View可以参考代码如下:
String deviceName = "device_name"; // 设置需要查询的设备名称
DeviceViewResponse response = client.getDeviceView(deviceName);
String name = response.getName(); // 设备名称
String description = response.getDescription(); // 设备描述
String state = response.getState(); // 设备当前状态
String schemaId = response.getSchemaId(); // 设备使用的模版ID
String schemaName = response.getSchemaName(); // 设备使用的模板名称
Long createTime = response.getCreateTime(); // 设备的创建时间
Long lastActiveTime = response.getLastActiveTime(); // 设备最后一次上报内容的时间
Boolean favourite = response.getFavourite(); // 设备的收藏信息
int profileVersion = response.getProfileVersion(); // 设备影子的版本号
List<DeviceViewAttribute> devices = response.getProperties(); // 设备所使用模板的属性
获取及查询影子列表
获取及查询影子列表可以参考代码如下:
int pageNo = 1; // 设置需要获取所有查询结果的第几页
int pageSize = 10; // 设置每页返回的最大个数
String orderBy = "name"; // 设置排序的索引列,支持name/createTime/lastActiveTime
String order = "asc"; // 设置按照升序、降序排列结果,支持asc/desc
String name = "schemaName"; // 设置需要查询的属性名
// 支持设备名字查询(name)/模型名字查询(schemaName)/服务端属性查询(attributes.***)/设备端属性查询(device.reported.***)
String value = "my_schema_name"; // 设置需要查询的属性值,对于设备端属性查询,如果相应属性值为字符串类型,需要用""将字符串包裹
String favourite = "all"; // 设置收藏选择,支持all/true/false
DeviceProfileListResponse response = client.getDeviceProfiles(pageNo, pageSize, orderBy, order, name, value, favourite);
int amount = response.getAmount(); // 满足查询条件的设备数目
int pageNo = response.getPageNo(); // 当前页页码
int pageSize = response.getPageSize(); // 返回的每页的最大个数
List<DeviceProfile> devices = response.getDevices(); // 当前页设备详情列表
获取设备接入详情
获取设备接入详情可以参考代码如下:
String deviceName = "device_name"; // 设置需要查询的设备名称
DeviceAccessDetailResponse response = client.getDeviceAccessDetail(deviceName);
String tcpEndpoint = response.getTcpEndpoint(); // tcp endpoint地址
String sslEndpoint = response.getSslEndpoint(); // ssl endpoint地址
String username = response.getUsername(); // 账户名,用于接入IoT hub服务
String key = response.getKey(); // 调用此方法密钥默认返回"xxxxxxxxx",请在创建设备时妥善保存
更新密钥
更新密钥可以参考代码如下:
String deviceName = "device_name"; // 设置需要更新的设备名称
DeviceAccessDetailResponse response = client.updateDeviceSecretKey(deviceName);
String tcpEndpoint = response.getTcpEndpoint(); // tcp endpoint地址
String sslEndpoint = response.getSslEndpoint(); // ssl endpoint地址
String username = response.getUsername(); // 账户名,用于接入IoT hub服务
String key = response.getKey(); // 更新密钥后会导致原有的连接断开,需要用新的密钥连接。
更新设备属性
更新设备属性可以参考代码如下:
String deviceName = "device_name"; // 设置需要更新的设备名称
// 设置需要更新的服务端属性
ObjectNode attributes = new ObjectMapper().createObjectNode();
attributes.put("regionTag", "Shanghai");
// 设置需要更新的设备影子属性期望值
ObjectNode desired = new ObjectMapper().createObjectNode();
desired.put("light", "red");
// 设置需要更新的设备端属性
DeviceAttributes device = new DeviceAttributes()
.withDesired(desired);
UpdateDeviceProfileRequest request = new UpdateDeviceProfileRequest()
.withAttributes(attributes)
.withDevice(device);
DeviceProfileResponse response = client.updateDeviceProfile(deviceName, request);
更新设备View
更新设备View可以参考代码如下:
String deviceName = "device_name"; // 设置需要更新的设备名称
// 设置需要更新的设备端属性当前汇报值
ObjectNode reported = new ObjectMapper().createObjectNode();
reported.put("light", "red");
// 设置需要更新的设备端属性期望值
ObjectNode desired = new ObjectMapper().createObjectNode();
desired.put("light", "red");
UpdateDeviceViewRequest request = new UpdateDeviceViewRequest()
.withReported(reported)
.withDesired(desired);
DeviceViewResponse response = client.updateDeviceView(deviceName, request);
更新设备注册表信息
更新设备注册表信息可以参考代码如下:
String deviceName = "device_name"; // 设置需要更新的设备名称
String schemaId = "new_schema_id"; // 设置变更的模板ID
String description = "new_description"; // 设置变更的设备描述
boolean favourite = true; // 设置变更的收藏选项
UpdateDeviceRegistryRequest request = new UpdateDeviceRegistryRequest()
.withDescription(description)
.withSchemaId(schemaId)
.withFavourite(favourite);
DeviceProfileResponse response = client.updateDeviceRegistry(deviceName, request);
重置设备影子
重置设备影子可以参考代码如下:
List<String> devices = Arrays.asList("device_name_1", "device_name_2"); // 设置需要重置的设备列表
DeviceListRequest request = new DeviceListRequest().
withDevices(devices);
DeviceListResponse response = client.resetDevices(request);