Skip to content

Commit

Permalink
Merge pull request #55 from devicehive/development
Browse files Browse the repository at this point in the history
Updated error parsing; Now getDevice returns DHResponse<Device> object;
  • Loading branch information
edubovik authored Nov 24, 2017
2 parents f6c7a4d + 58dbc3c commit 4ed57fb
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 43 deletions.
4 changes: 2 additions & 2 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
<parent>
<groupId>com.github.devicehive</groupId>
<artifactId>devicehive-java</artifactId>
<version>3.0.3</version>
<version>3.0.4</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>devicehive-java</name>
<version>3.0.3</version>
<version>3.0.4</version>
<prerequisites>
<maven>2.2.0</maven>
</prerequisites>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public interface MainDeviceHive {

DHResponse<Void> removeDevice(String id);

Device getDevice(String id) ;
DHResponse<Device> getDevice(String id) ;

DHResponse<Void> putDevice(String id, String name) ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.github.devicehive.rest.model.JsonStringWrapper;
import com.github.devicehive.rest.model.JwtAccessToken;
import com.github.devicehive.rest.model.JwtRefreshToken;
import com.github.devicehive.websocket.model.repsonse.ErrorResponse;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

Expand All @@ -45,6 +46,7 @@

public class BaseService {
public static final String ERROR_MESSAGE_KEY = "message";
public static final String FAILED_CONNECT_TO_SERVER = "Failed connect to server";
ApiClient apiClient;

BaseService() {
Expand Down Expand Up @@ -102,6 +104,9 @@ final <T> DHResponse<T> execute(Call<T> call) {
} catch (IOException e) {
FailureData failureData = createFailData(FailureData.NO_CODE, e.getMessage());
return new DHResponse<T>(null, failureData);
} catch (NullPointerException e) {
return new DHResponse<T>(null, FailureData.create(
ErrorResponse.create(FAILED_CONNECT_TO_SERVER)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,45 @@
import com.github.devicehive.client.api.MainDeviceHive;
import com.github.devicehive.client.callback.ResponseCallback;
import com.github.devicehive.client.exceptions.IncorrectUrlException;
import com.github.devicehive.client.model.*;
import com.github.devicehive.client.model.CommandFilter;
import com.github.devicehive.client.model.DHResponse;
import com.github.devicehive.client.model.DeviceCommandsCallback;
import com.github.devicehive.client.model.DeviceFilter;
import com.github.devicehive.client.model.DeviceNotification;
import com.github.devicehive.client.model.DeviceNotificationsCallback;
import com.github.devicehive.client.model.FailureData;
import com.github.devicehive.client.model.NetworkFilter;
import com.github.devicehive.client.model.NotificationFilter;
import com.github.devicehive.client.model.TokenAuth;
import com.github.devicehive.client.model.UserFilter;
import com.github.devicehive.rest.api.AuthApi;
import com.github.devicehive.rest.model.*;
import com.github.devicehive.rest.model.ApiInfo;
import com.github.devicehive.rest.model.ClusterConfig;
import com.github.devicehive.rest.model.Configuration;
import com.github.devicehive.rest.model.JsonStringWrapper;
import com.github.devicehive.rest.model.JwtAccessToken;
import com.github.devicehive.rest.model.JwtRequest;
import com.github.devicehive.rest.model.JwtToken;
import com.github.devicehive.rest.model.NetworkVO;
import com.github.devicehive.rest.model.RoleEnum;
import com.github.devicehive.rest.model.StatusEnum;
import com.github.devicehive.websocket.api.CommandWS;
import com.github.devicehive.websocket.api.NotificationWS;
import com.github.devicehive.websocket.api.WebSocketClient;
import com.github.devicehive.websocket.listener.CommandListener;
import com.github.devicehive.websocket.listener.NotificationListener;
import com.github.devicehive.websocket.model.repsonse.*;
import org.joda.time.DateTime;
import com.github.devicehive.websocket.model.repsonse.CommandGetResponse;
import com.github.devicehive.websocket.model.repsonse.CommandInsertResponse;
import com.github.devicehive.websocket.model.repsonse.CommandListResponse;
import com.github.devicehive.websocket.model.repsonse.CommandSubscribeResponse;
import com.github.devicehive.websocket.model.repsonse.ErrorResponse;
import com.github.devicehive.websocket.model.repsonse.NotificationGetResponse;
import com.github.devicehive.websocket.model.repsonse.NotificationInsertResponse;
import com.github.devicehive.websocket.model.repsonse.NotificationListResponse;
import com.github.devicehive.websocket.model.repsonse.NotificationSubscribeResponse;
import com.github.devicehive.websocket.model.repsonse.ResponseAction;

import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import org.joda.time.DateTime;

import java.io.IOException;
import java.net.URI;
Expand All @@ -48,6 +71,11 @@
import java.util.Collections;
import java.util.List;

import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DeviceHive implements MainDeviceHive {

private static final String WEBSOCKET_PATH = "/api/websocket";
Expand Down Expand Up @@ -408,7 +436,7 @@ public DHResponse<Void> removeDevice(String id) {
return deviceService.removeDevice(id);
}

public Device getDevice(String id) {
public DHResponse<Device> getDevice(String id) {
return deviceService.getDevice(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,31 @@ DHResponse<Void> createDevice(String deviceId, String name) {
}
}

Device getDevice(String deviceId) {
DHResponse<Device> getDevice(String deviceId) {
deviceApi = createService(DeviceApi.class);
DHResponse<Device> response;
DHResponse<com.github.devicehive.rest.model.Device> result;

result = execute(deviceApi.get(deviceId));
response = new DHResponse<Device>(Device.create(result.getData()), result.getFailureData());
if (response.isSuccessful()) {
return response.getData();
return response;
} else if (response.getFailureData().getCode() == 401) {
authorize();
deviceApi = createService(DeviceApi.class);
result = execute(deviceApi.get(deviceId));
response = new DHResponse<Device>(Device.create(result.getData()), result.getFailureData());
if (response.isSuccessful()) {
return response.getData();
return response;
} else if (response.getFailureData().getCode() == 404 || response.getFailureData().getCode() == 403) {
return createAndGetDevice(deviceId);
} else {
return null;
return response;
}
} else if (response.getFailureData().getCode() == 404 || response.getFailureData().getCode() == 403) {
return createAndGetDevice(deviceId);
} else {
return null;
return response;
}
}

Expand Down Expand Up @@ -114,11 +114,10 @@ DHResponse<Void> removeDevice(String deviceId) {
}
}

private Device createAndGetDevice(String id) {
private DHResponse<Device> createAndGetDevice(String id) {
createDevice(id, id);
DHResponse<com.github.devicehive.rest.model.Device> result = execute(deviceApi.get(id));
DHResponse<Device> response = new DHResponse<Device>(Device.create(result.getData()), result.getFailureData());
return response.isSuccessful() ? response.getData() : null;
return new DHResponse<>(Device.create(result.getData()), result.getFailureData());

}
}
9 changes: 8 additions & 1 deletion client/src/main/java/example/Echo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
package example;

import com.github.devicehive.client.model.CommandFilter;
import com.github.devicehive.client.model.DHResponse;
import com.github.devicehive.client.model.DeviceCommandsCallback;
import com.github.devicehive.client.model.FailureData;
import com.github.devicehive.client.model.TokenAuth;
import com.github.devicehive.client.service.Device;
import com.github.devicehive.client.service.DeviceCommand;
import com.github.devicehive.client.service.DeviceHive;

import org.joda.time.DateTime;

import java.util.Collections;
Expand All @@ -46,7 +48,12 @@ public class Echo {
public static void main(String[] args) {
final DeviceHive deviceHive = DeviceHive.getInstance().init(URL, new TokenAuth(refreshToken, accessToken));
final String deviceId = UUID.randomUUID().toString();
final Device device = deviceHive.getDevice(deviceId);
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
if (!deviceResponse.isSuccessful()) {
System.out.println(deviceResponse);
return;
}
Device device = deviceResponse.getData();
CommandFilter filter = getFilter();
deviceHive.subscribeCommands(Collections.singletonList(deviceId), filter, new DeviceCommandsCallback() {
@Override
Expand Down
12 changes: 9 additions & 3 deletions client/src/main/java/example/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package example;

import com.github.devicehive.client.model.CommandFilter;
import com.github.devicehive.client.model.DHResponse;
import com.github.devicehive.client.model.DeviceCommandsCallback;
import com.github.devicehive.client.model.DeviceNotification;
import com.github.devicehive.client.model.DeviceNotificationsCallback;
Expand Down Expand Up @@ -47,7 +48,12 @@ public static void main(String[] args) {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
String DEVICE_ID = "123456-example";
//Device Initiating
final Device device = deviceHive.getDevice(DEVICE_ID);
DHResponse<Device> deviceResponse = deviceHive.getDevice(DEVICE_ID);
if (!deviceResponse.isSuccessful()) {
System.out.println(deviceResponse);
return;
}
final Device device = deviceResponse.getData();
//Creating filter to listen commands from the server
CommandFilter commandFilter = new CommandFilter();
commandFilter.setCommandNames(PING);
Expand All @@ -65,8 +71,8 @@ public void onSuccess(List<DeviceCommand> commands) {

if (params != null) {
//Getting param value
Gson gson=new Gson();
JsonObject jsonObject =gson.fromJson(params.getJsonString(),JsonObject.class);
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(params.getJsonString(), JsonObject.class);

boolean needToSend = false;
if (jsonObject.has(PRODUCE_NOTIFICATION)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.github.devicehive.client.service.DeviceCommand;
import com.github.devicehive.client.service.DeviceHive;
import com.github.devicehive.rest.model.JsonStringWrapper;

import org.junit.Assert;
import org.junit.Test;

Expand All @@ -41,11 +42,12 @@ public class CommandTest {

private DeviceHive deviceHive = DeviceHive.getInstance().init(URL, WS_URL, new TokenAuth(refreshToken, accessToken));

private Device device = deviceHive.getDevice(DEVICE_ID);
private DHResponse<Device> deviceResponse = deviceHive.getDevice(DEVICE_ID);

@Test
public void createAndUpdate() throws InterruptedException {
Assert.assertNotNull(device);
Assert.assertTrue(deviceResponse.isSuccessful());
Device device = deviceResponse.getData();
DHResponse<DeviceCommand> response = device.sendCommand(COM_A, null);
Assert.assertTrue(response.isSuccessful());
DeviceCommand command = response.getData();
Expand Down
41 changes: 28 additions & 13 deletions client/src/test/java/com/github/devicehive/client/DeviceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@

package com.github.devicehive.client;

import com.github.devicehive.client.model.*;
import com.github.devicehive.client.model.CommandFilter;
import com.github.devicehive.client.model.DHResponse;
import com.github.devicehive.client.model.DeviceCommandsCallback;
import com.github.devicehive.client.model.DeviceNotification;
import com.github.devicehive.client.model.DeviceNotificationsCallback;
import com.github.devicehive.client.model.FailureData;
import com.github.devicehive.client.model.NotificationFilter;
import com.github.devicehive.client.model.Parameter;
import com.github.devicehive.client.model.TokenAuth;
import com.github.devicehive.client.service.Device;
import com.github.devicehive.client.service.DeviceCommand;
import com.github.devicehive.client.service.DeviceHive;

import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
Expand Down Expand Up @@ -61,17 +69,19 @@ public class DeviceTest {
@Test
public void createDevice() throws IOException {
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
Device device = deviceHive.getDevice(deviceId);
Assert.assertNotNull(device);
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
Assert.assertTrue(deviceResponse.isSuccessful());
final Device device = deviceResponse.getData();
Assert.assertTrue(device != null);
Assert.assertTrue(deviceHive.removeDevice(deviceId).isSuccessful());
}

@Test
public void getCommands() throws IOException {
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
final Device device = deviceHive.getDevice(deviceId);
Assert.assertNotNull(device);
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
Assert.assertTrue(deviceResponse.isSuccessful());
final Device device = deviceResponse.getData();
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.schedule(new Thread(new Runnable() {
public void run() {
Expand All @@ -94,8 +104,9 @@ public void run() {
@Test
public void subscribeCommands() throws InterruptedException {
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
final Device device = deviceHive.getDevice(deviceId);
Assert.assertNotNull(device);
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
Assert.assertTrue(deviceResponse.isSuccessful());
final Device device = deviceResponse.getData();
final ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
final String commandName1 = COM_A + new Random().nextInt();
final String commandName2 = COM_B + new Random().nextInt();
Expand Down Expand Up @@ -169,8 +180,9 @@ public void onFail(FailureData failureData) {
public void subscribeNotifications() throws IOException, InterruptedException {
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();

final Device device = deviceHive.getDevice(deviceId);
Assert.assertNotNull(device);
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
Assert.assertTrue(deviceResponse.isSuccessful());
final Device device = deviceResponse.getData();
final CountDownLatch latch = new CountDownLatch(3);
final String notificationName1 = NOTIFICATION_A + new Random().nextInt();
final String notificationName2 = NOTIFICATION_B + new Random().nextInt();
Expand Down Expand Up @@ -244,8 +256,9 @@ public void run() {
@Test
public void getNotification() throws IOException {
String deviceId = UUID.randomUUID().toString();
final Device device = deviceHive.getDevice(deviceId);
Assert.assertNotNull(device);
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
Assert.assertTrue(deviceResponse.isSuccessful());
final Device device = deviceResponse.getData();
ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.schedule(new Thread(new Runnable() {
public void run() {
Expand All @@ -267,7 +280,9 @@ public void run() {
@Test
public void sendNotification() throws IOException {
String deviceId = DEVICE_PREFIX + UUID.randomUUID().toString();
Device device = deviceHive.getDevice(deviceId);
DHResponse<Device> deviceResponse = deviceHive.getDevice(deviceId);
Assert.assertTrue(deviceResponse.isSuccessful());
final Device device = deviceResponse.getData();
Assert.assertNotNull(device);
List<Parameter> parameters = new ArrayList<Parameter>();

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<artifactId>devicehive-java</artifactId>
<packaging>pom</packaging>
<name>devicehive-java</name>
<version>3.0.3</version>
<version>3.0.4</version>
<prerequisites>
<maven>2.2.0</maven>
</prerequisites>
Expand Down
2 changes: 1 addition & 1 deletion rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.github.devicehive</groupId>
<artifactId>devicehive-java</artifactId>
<version>3.0.3</version>
<version>3.0.4</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>devicehive-rest</name>
Expand Down
2 changes: 1 addition & 1 deletion websocket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.github.devicehive</groupId>
<artifactId>devicehive-java</artifactId>
<version>3.0.3</version>
<version>3.0.4</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>devicehive-websocket</name>
Expand Down
Loading

0 comments on commit 4ed57fb

Please sign in to comment.