Work item 2: Add cloud functionality

parent 53594012
...@@ -30,7 +30,7 @@ android { ...@@ -30,7 +30,7 @@ android {
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 28 targetSdkVersion 28
versionCode 13 versionCode 13
versionName "2.13.0" versionName "3.0.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
compileOptions { compileOptions {
......
...@@ -27,6 +27,7 @@ import org.iotivity.CborEncoder; ...@@ -27,6 +27,7 @@ import org.iotivity.CborEncoder;
import org.iotivity.OCBufferSettings; import org.iotivity.OCBufferSettings;
import org.iotivity.OCClientResponse; import org.iotivity.OCClientResponse;
import org.iotivity.OCCloud; import org.iotivity.OCCloud;
import org.iotivity.OCCloudContext;
import org.iotivity.OCCoreRes; import org.iotivity.OCCoreRes;
import org.iotivity.OCDiscoveryAllHandler; import org.iotivity.OCDiscoveryAllHandler;
import org.iotivity.OCDiscoveryFlags; import org.iotivity.OCDiscoveryFlags;
...@@ -354,9 +355,9 @@ public class IotivityRepository { ...@@ -354,9 +355,9 @@ public class IotivityRepository {
public Single<String> getNonSecureEndpoint(Device device) { public Single<String> getNonSecureEndpoint(Device device) {
return Single.create(emitter -> { return Single.create(emitter -> {
String endpoint = device.getIpv6Host(); String endpoint = device.getIpv6Host() != null ? device.getIpv6Host() : device.getIpv6TcpHost();
if (endpoint == null) { if (endpoint == null) {
endpoint = device.getIpv4Host(); endpoint = device.getIpv4Host() != null ? device.getIpv4Host() : device.getIpv4TcpHost();
} }
emitter.onSuccess(endpoint); emitter.onSuccess(endpoint);
}); });
...@@ -364,9 +365,9 @@ public class IotivityRepository { ...@@ -364,9 +365,9 @@ public class IotivityRepository {
public Single<String> getSecureEndpoint(Device device) { public Single<String> getSecureEndpoint(Device device) {
return Single.create(emitter -> { return Single.create(emitter -> {
String endpoint = device.getIpv6SecureHost(); String endpoint = device.getIpv6SecureHost() != null ? device.getIpv6SecureHost() : device.getIpv6TcpSecureHost();
if (endpoint == null) { if (endpoint == null) {
endpoint = device.getIpv4SecureHost(); endpoint = device.getIpv4SecureHost() != null ? device.getIpv4SecureHost() : device.getIpv4TcpSecureHost();
} }
emitter.onSuccess(endpoint); emitter.onSuccess(endpoint);
}); });
...@@ -777,6 +778,12 @@ public class IotivityRepository { ...@@ -777,6 +778,12 @@ public class IotivityRepository {
public void close() { public void close() {
Timber.d("Calling OCMain.mainShutdown()"); Timber.d("Calling OCMain.mainShutdown()");
OCCloudContext ctx = OCCloud.getContext(0);
if (ctx != null) {
OCCloud.managerStop(ctx);
}
OCMain.mainShutdown(); OCMain.mainShutdown();
OCObt.shutdown(); OCObt.shutdown();
} }
......
...@@ -44,9 +44,13 @@ public class Device implements Comparable<Device>, Serializable { ...@@ -44,9 +44,13 @@ public class Device implements Comparable<Device>, Serializable {
private String deviceId; private String deviceId;
private transient OcDeviceInfo deviceInfo; private transient OcDeviceInfo deviceInfo;
private String ipv6SecureHost; private String ipv6SecureHost;
private String ipv6TcpSecureHost;
private String ipv6Host; private String ipv6Host;
private String ipv6TcpHost;
private String ipv4SecureHost; private String ipv4SecureHost;
private String ipv4TcpSecureHost;
private String ipv4Host; private String ipv4Host;
private String ipv4TcpHost;
private int permits; private int permits;
public Device() {} public Device() {}
...@@ -61,14 +65,26 @@ public class Device implements Comparable<Device>, Serializable { ...@@ -61,14 +65,26 @@ public class Device implements Comparable<Device>, Serializable {
while(endpoints != null) { while(endpoints != null) {
String endpointStr = OCEndpointUtil.toString(endpoints); String endpointStr = OCEndpointUtil.toString(endpoints);
if (endpointStr.startsWith("coaps://") && endpointStr.contains(".")) { if (endpointStr.contains(".")) {
ipv4SecureHost = endpointStr; if (endpointStr.startsWith("coaps://")) {
} else if (endpointStr.startsWith("coaps://")) { ipv4SecureHost = endpointStr;
ipv6SecureHost = endpointStr; } else if (endpointStr.startsWith("coaps+tcp://")) {
} else if (endpointStr.startsWith("coap://") && endpointStr.contains(".")) { ipv4TcpSecureHost = endpointStr;
ipv4Host = endpointStr; } else if (endpointStr.startsWith("coap://")) {
} else if (endpointStr.startsWith("coap://")){ ipv4Host = endpointStr;
ipv6Host = endpointStr; } else if (endpointStr.startsWith("coap+tcp://")) {
ipv4TcpHost = endpointStr;
}
} else {
if (endpointStr.startsWith("coaps://")) {
ipv6SecureHost = endpointStr;
} else if (endpointStr.startsWith("coaps+tcp://")) {
ipv6TcpSecureHost = endpointStr;
} else if (endpointStr.startsWith("coap://")) {
ipv6Host = endpointStr;
} else if (endpointStr.startsWith("coap+tcp://")) {
ipv6TcpHost = endpointStr;
}
} }
endpoints = endpoints.getNext(); endpoints = endpoints.getNext();
...@@ -83,14 +99,26 @@ public class Device implements Comparable<Device>, Serializable { ...@@ -83,14 +99,26 @@ public class Device implements Comparable<Device>, Serializable {
this.permits = permits; this.permits = permits;
for (String endpoint : endpoints) { for (String endpoint : endpoints) {
if (endpoint.startsWith("coaps://") && endpoint.contains(".")) { if (endpoint.contains(".")) {
ipv4SecureHost = endpoint; if (endpoint.startsWith("coaps://")) {
} else if (endpoint.startsWith("coaps://")) { ipv4SecureHost = endpoint;
ipv6SecureHost = endpoint; } else if (endpoint.startsWith("coaps+tcp://")) {
} else if (endpoint.startsWith("coap://") && endpoint.contains(".")) { ipv4TcpSecureHost = endpoint;
ipv4Host = endpoint; } else if (endpoint.startsWith("coap://")) {
} else if (endpoint.startsWith("coap://")) { ipv4Host = endpoint;
ipv6Host = endpoint; } else if (endpoint.startsWith("coap+tcp://")) {
ipv4TcpHost = endpoint;
}
} else {
if (endpoint.startsWith("coaps://")) {
ipv6SecureHost = endpoint;
} else if (endpoint.startsWith("coaps+tcp://")) {
ipv6TcpSecureHost = endpoint;
} else if (endpoint.startsWith("coap://")) {
ipv6Host = endpoint;
} else if (endpoint.startsWith("coap+tcp://")) {
ipv6TcpHost = endpoint;
}
} }
} }
} }
...@@ -135,6 +163,14 @@ public class Device implements Comparable<Device>, Serializable { ...@@ -135,6 +163,14 @@ public class Device implements Comparable<Device>, Serializable {
this.ipv6Host = host; this.ipv6Host = host;
} }
public String getIpv6TcpHost() {
return this.ipv6TcpHost;
}
public void setIpv6TcpHost(String host) {
this.ipv6TcpHost = host;
}
public String getIpv6SecureHost() { public String getIpv6SecureHost() {
return this.ipv6SecureHost; return this.ipv6SecureHost;
} }
...@@ -143,6 +179,14 @@ public class Device implements Comparable<Device>, Serializable { ...@@ -143,6 +179,14 @@ public class Device implements Comparable<Device>, Serializable {
this.ipv6SecureHost = host; this.ipv6SecureHost = host;
} }
public String getIpv6TcpSecureHost() {
return this.ipv6TcpSecureHost;
}
public void setIpv6TcpSecureHost(String host) {
this.ipv6TcpSecureHost = host;
}
public String getIpv4Host() { public String getIpv4Host() {
return this.ipv4Host; return this.ipv4Host;
} }
...@@ -151,6 +195,14 @@ public class Device implements Comparable<Device>, Serializable { ...@@ -151,6 +195,14 @@ public class Device implements Comparable<Device>, Serializable {
this.ipv4Host = host; this.ipv4Host = host;
} }
public String getIpv4TcpHost() {
return this.ipv4TcpHost;
}
public void setIpv4TcpHost(String host) {
this.ipv4TcpHost = host;
}
public String getIpv4SecureHost() { public String getIpv4SecureHost() {
return this.ipv4SecureHost; return this.ipv4SecureHost;
} }
...@@ -159,27 +211,55 @@ public class Device implements Comparable<Device>, Serializable { ...@@ -159,27 +211,55 @@ public class Device implements Comparable<Device>, Serializable {
this.ipv4SecureHost = host; this.ipv4SecureHost = host;
} }
public String getIpv4TcpSecureHost() {
return this.ipv4SecureHost;
}
public void setIpv4TcpSecureHost(String host) {
this.ipv4SecureHost = host;
}
public boolean equalsHosts(Device device) { public boolean equalsHosts(Device device) {
if (this.getIpv6Host() != null && device.getIpv6Host() != null if (this.getIpv6Host() != null && device.getIpv6Host() != null
&& !this.getIpv6Host().equals(device.getIpv6Host())) { && !this.getIpv6Host().equals(device.getIpv6Host())) {
return false; return false;
} }
if (this.getIpv6TcpHost() != null && device.getIpv6TcpHost() != null
&& !this.getIpv6TcpHost().equals(device.getIpv6TcpHost())) {
return false;
}
if (this.getIpv6SecureHost() != null && device.getIpv6SecureHost() != null if (this.getIpv6SecureHost() != null && device.getIpv6SecureHost() != null
&& !this.getIpv6SecureHost().equals(device.getIpv6SecureHost())) { && !this.getIpv6SecureHost().equals(device.getIpv6SecureHost())) {
return false; return false;
} }
if (this.getIpv6TcpSecureHost() != null && device.getIpv6TcpSecureHost() != null
&& !this.getIpv6TcpSecureHost().equals(device.getIpv6TcpSecureHost())) {
return false;
}
if (this.getIpv4Host() != null && device.getIpv4Host() != null if (this.getIpv4Host() != null && device.getIpv4Host() != null
&& !this.getIpv4Host().equals(device.getIpv4Host())) { && !this.getIpv4Host().equals(device.getIpv4Host())) {
return false; return false;
} }
if (this.getIpv4TcpHost() != null && device.getIpv4TcpHost() != null
&& !this.getIpv4TcpHost().equals(device.getIpv4TcpHost())) {
return false;
}
if (this.getIpv4SecureHost() != null && device.getIpv4SecureHost() != null if (this.getIpv4SecureHost() != null && device.getIpv4SecureHost() != null
&& !this.getIpv4SecureHost().equals(device.getIpv4SecureHost())) { && !this.getIpv4SecureHost().equals(device.getIpv4SecureHost())) {
return false; return false;
} }
if (this.getIpv4TcpSecureHost() != null && device.getIpv4TcpSecureHost() != null
&& !this.getIpv4TcpSecureHost().equals(device.getIpv4TcpSecureHost())) {
return false;
}
return true; return true;
} }
......
...@@ -25,5 +25,6 @@ public enum DeviceType { ...@@ -25,5 +25,6 @@ public enum DeviceType {
UNOWNED, UNOWNED,
OWNED_BY_SELF, OWNED_BY_SELF,
OWNED_BY_OTHER, OWNED_BY_OTHER,
OWNED_BY_OTHER_WITH_PERMITS OWNED_BY_OTHER_WITH_PERMITS,
CLOUD
} }
package org.openconnectivity.otgc.domain.usecase.cloud;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import javax.inject.Inject;
import io.reactivex.Single;
public class CloudDeregisterUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudDeregisterUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<Integer> execute() {
return cloudRepository.deregister()
.andThen(cloudRepository.retrieveState());
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import javax.inject.Inject;
import io.reactivex.Observable;
public class CloudDiscoverDevicesUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudDiscoverDevicesUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Observable<Device> execute() {
return cloudRepository.discoverDevices();
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.client.SerializableResource;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import org.openconnectivity.otgc.domain.model.resource.virtual.res.OcResource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import io.reactivex.Completable;
import io.reactivex.Single;
public class CloudDiscoverResourcesUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudDiscoverResourcesUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<List<SerializableResource>> execute(Device device) {
return
Completable.complete()
.delay(1, TimeUnit.SECONDS)
.andThen(cloudRepository.discoverVerticalResources(device.getDeviceId())
.map(ocResources -> {
List<SerializableResource> serializableResources = new ArrayList<>();
for (OcResource resource : ocResources) {
SerializableResource serializableResource = new SerializableResource();
serializableResource.setUri(resource.getHref());
serializableResource.setResourceTypes(resource.getResourceTypes());
serializableResource.setResourceInterfaces(resource.getInterfaces());
serializableResource.setObservable(false);
serializableResources.add(serializableResource);
}
Collections.sort(serializableResources,
(r1, r2) -> r1.getUri().compareTo(r2.getUri()));
return serializableResources;
}));
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.client.SerializableResource;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import javax.inject.Inject;
public class CloudGetResourceUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudGetResourceUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<SerializableResource> execute(Device device, SerializableResource resource) {
return cloudRepository.retrieveEndpoint()
.flatMap(endpoint -> cloudRepository.get(endpoint, resource.getUri(), device.getDeviceId()))
.map(ocRepresentation -> {
resource.setProperties(ocRepresentation);
return resource;
});
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import javax.inject.Inject;
import io.reactivex.Single;
public class CloudLoginUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudLoginUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<Integer> execute() {
return cloudRepository.login()
.andThen(cloudRepository.retrieveState());
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import javax.inject.Inject;
import io.reactivex.Single;
public class CloudLogoutUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudLogoutUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<Integer> execute() {
return cloudRepository.logout()
.andThen(cloudRepository.retrieveState());
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Completable;
import org.iotivity.OCRepresentation;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.client.SerializableResource;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import javax.inject.Inject;
import java.util.Map;
public class CloudPostResourceUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudPostResourceUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Completable execute(Device device, SerializableResource resource, OCRepresentation rep, Object valueArray) {
return cloudRepository.retrieveEndpoint()
.flatMapCompletable(endpoint -> cloudRepository.post(endpoint, resource.getUri(), device.getDeviceId(), rep, valueArray));
}
public Completable execute(Device device, SerializableResource resource, Map<String, Object> values) {
return cloudRepository.retrieveEndpoint()
.flatMapCompletable(endpoint -> cloudRepository.post(endpoint, resource.getUri(), device.getDeviceId(), values));
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import javax.inject.Inject;
import io.reactivex.Single;
public class CloudRefreshTokenUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudRefreshTokenUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<Integer> execute() {
return cloudRepository.refreshToken()
.andThen(cloudRepository.retrieveState());
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import javax.inject.Inject;
import io.reactivex.Single;
public class CloudRegisterUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudRegisterUseCase(CloudRepository cloudRepository) {
this.cloudRepository = cloudRepository;
}
public Single<Integer> execute() {
return cloudRepository.register()
.andThen(cloudRepository.retrieveState());
}
}
package org.openconnectivity.otgc.domain.usecase.cloud;
import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.CloudRepository;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import org.openconnectivity.otgc.domain.model.resource.virtual.d.OcDeviceInfo;
import org.openconnectivity.otgc.utils.constant.OcfResourceUri;
import javax.inject.Inject;
public class CloudRetrieveDeviceInfoUseCase {
private final CloudRepository cloudRepository;
@Inject
public CloudRetrieveDeviceInfoUseCase(CloudRepository cloudRepository) {