DeviceHive Client Library is an open source project based on Java. It can be used to build Android
or Java
applications.
DeviceHive turns any connected device into the part of Internet of Things. It provides the communication layer, control software and multi-platform libraries to bootstrap development of smart energy, home automation, remote sensing, telemetry, remote control and monitoring software and much more.
Creating a client with a new version of library is very simple. First of all you need to initiate DeviceHive
client:
DeviceHive deviceHive = DeviceHive.getInstance()
.init("http://devicehive.server.rest.url", "refreshToken", "accessToken");
or you can initiate the client without Access token. In this case DeviceHive
will automaticaly get it:
DeviceHive deviceHive = DeviceHive.getInstance()
.init("http://devicehive.server.rest.url", "refreshToken");
DeviceHive class methods
getInfo()
- gets server infogetClusterInfo()
- gets cluster infocreateToken(List<String> actions, Long userId, List<String> networkIds, List<String> deviceIds, DateTime expiration)
- creates tokenrefreshToken()
- refreshes tokengetProperty(String name)
- gets property by namesetProperty(String name, String value)
- creates propertyremoveProperty(String name)
- removes property by namesubscribeCommands(List<String> ids, CommandFilter commandFilter, DeviceCommandsCallback commandsCallback)
- subcribes on specific commands bydevice ids
andcommand filter
parameterssubscribeNotifications(List<String> ids, NotificationFilter notificationFilter, notificationsCallback)
- subcribes on specific notifications bydevice ids
andnotification filter
parametersunsubscribeCommands(List<String> ids, CommandFilter commandFilter)
- updates current subcription or unsubscribes at allunsubscribeNotifications(List<String> ids, NotificationFilter notificationFilter)
- updates current subcription or unsubscribes at alllistNetworks(NetworkFilter filter)
- gets list ofNetwork
getNetwork(long id)
- getsNetwork
by network's idremoveNetwork(long id)
- removesNetwork
by idcreateNetwork(String name, String description)
- createsNetwork
by idlistDevices(DeviceFilter filter)
- gets list ofDevice
removeDevice(String id)
- removesDevice
by idgetDevice(String id)
- gets existingDevice
by id or creates newDevice
putDevice(String id, String name)
- createsDevice
To create device you just need an instance of DeviceHive
and getDevice(String deviceId)
that returns DHResponse<Device>
object where will be FailureData
object in case of any errors or Device object:
DHResponse<Device> devicehiveResponse = deviceHive.getDevice("example-device-Id");
if(devicehiveResponse.isSuccessful(){
Device device = devicehiveResponse.getData();
}else{
FailureData failureData = devicehiveResponse.getFailureData();
int code = failureData.getCode();
String message = failureData.getMessage();
}
Device class properties and methods
Device
contains such properties and methods:
Properties:
id
(read only)name
data
network_id
is_blocked
Methods:
save()
- updates DevicegetCommands(DateTime startTimestamp, DateTime endTimestamp, int maxNumber)
- gets Device's DeviceCommandsgetNotifications(DateTime startTimestamp, DateTime endTimestamp)
- gets Device's DeviceNotificationssendCommand(String command, List<Parameter> parameters)
- sends DeviceCommandsendNotification(String notification, List<Parameter> parameters)
- sends DeviceNotificationsubscribeCommands(CommandFilter commandFilter, DeviceCommandsCallback commandCallback)
- subscribes for DeviceCommandssubscribeNotifications(NotificationFilter notificationFilter, DeviceNotificationsCallback notificationCallback)
- subscribes for DeviceNotificationsunsubscribeCommands(CommandFilter commandFilter)
- unsubscribes from DeviceCommands that are not meeting filter criteriaunsubscribeAllCommands()
- subscribes from all DeviceCommandsunsubscribeNotifications(NotificationFilter notificationFilter)
- subscribes for DeviceNotifications that are not meeting filter criteriaunsubscribeAllNotifications()
- subscribes from all DeviceNotifications
To create command you just need to call
sendCommand(String command, List<Parameter> parameters)
method that will return DHResponse<DeviceCommand>
with DeviceCommand
in case of success or FailureData
with error message and HTTP response code in case of failure:
DHResponse<DeviceCommand> response = device.sendCommand("command name", parameters);
To subscribe on commands you just need to create CommandFilter
where you can set all needed parameters and call subscribeCommands(CommandFilter commandFilter, final DeviceCommandsCallback commandCallback)
method:
CommandFilter commandFilter = new CommandFilter();
commandFilter.setCommandNames(COM_A, COM_B);
commandFilter.setStartTimestamp(DateTime.now());
commandFilter.setMaxNumber(30);
device.subscribeCommands(commandFilter, new DeviceCommandsCallback() {
public void onSuccess(List<DeviceCommand> commands) {
}
}
public void onFail(FailureData failureData) {
}
});
DeviceCommand class properties and methods
DeviceCommand
contains such properties:
id
(read only)user_id
(read only)command
(read only)parameters
(read only)lifetime
(read only)timestamp
(read only)last_updated
(read only)status
result
DeviceCommand
contains such methods:
updateCommand
- updates current commandfetchCommandStatus
- gets command statusfetchCommandResult
- gets command result
There us the same logic regarding DeviceNotification
to create notification
you just need to call
sendNotification(String notification, List<Parameter> parameters)
method that will return DHResponse<DeviceNotification>
with DeviceNotification
in case of success or FailureData
with error message and HTTP response code in case of failure:
DHResponse<DeviceNotification> response = device.sendNotification("notification name", parameters);
To subscribe on notifications you just need to create NotificationFilter
where you can set all needed parameters and call subscribeNotifications(NotificationFilter notificationFilter, DeviceNotificationsCallback notificationCallback)
method:
NotificationFilter notificationFilter = new NotificationFilter();
notificationFilter.setNotificationNames(NOTIFICATION_A, NOTIFICATION_B);
notificationFilter.setStartTimestamp(DateTime.now());
notificationFilter.setEndTimestamp(DateTime.now().plusSeconds(10));
device.subscribeNotifications(notificationFilter, new DeviceNotificationsCallback(){
public void onSuccess(List<DeviceNotification> notifications) {
}
public void onFail(FailureData failureData) {
}
});
DeviceNotification class properties
DeviceNotification
contains such properties:
device_id
(read only)id
(read only)notification
(read only)parameters
(read only)timestamp
(read only)
To create Network
you just need to call createNetwork(String name, String description)
method of DeviceHive
class
DHResponse<Network> response = deviceHive.createNetwork("My Network's name", "My network's description");
also you can get Network
by id deviceHive.getNetwork(long id)
or get list of Network
with deviceHive. listNetworks(NetworkFilter filter)
DHResponse<NetworkVO> response = deviceHive.getNetwork(response.getData().getId());
Network class properties and methods
Properties:
id
(read only)name
description
Methods:
save()
- updatesNetwork
To create User
you just need to call createUser(String lastLogin, String password, User.RoleEnum role, StatusEnum status, JsonObject data)
method of DeviceHive
class
DHResponse<User> response = deviceHive.createUser("javaLibTest", "123456", RoleEnum.ADMIN, StatusEnum.ACTIVE, null);
also you can get User
by critria deviceHive.getUsers(UserFilter filter)
or just get current User
deviceHive.getCurrentUser()
DHResponse<User> response = deviceHive.getCurrentUser();
User class properties and methods
Properties:
id
(read only)lastLogin
role
password
(write only)data
Methods:
save()
- updatesUser
getNetworks()
- gets list of Networks assigned to this userassignNetwork(long networkId)
- assigns Network to this userunassignNetwork(long networkId)
- unassigns Network to this user
Add the JitPack repository to your project build
file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Gradle is the only supported build configuration, so just add the dependency to your project build.gradle
file:
dependencies {
compile 'com.github.devicehive:devicehive-java:3.1.2'
}
Java Server code was moved to a separate repository: https://github.com/devicehive/devicehive-java-server
[DeviceHive] is developed by [DataArt] Apps and distributed under Open Source Apache 2.0. This basically means you can do whatever you want with the software as long as the copyright notice is included. This also means you don't have to contribute the end product or modified sources back to Open Source, but if you feel like sharing, you are highly encouraged to do so!
© Copyright 2018 DataArt Apps © All Rights Reserved