Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #17 from openconnectivity/develop
Browse files Browse the repository at this point in the history
Merge develop into master
  • Loading branch information
javiguerra committed Dec 13, 2019
2 parents a54fbb1 + 2db78a8 commit 4763d79
Show file tree
Hide file tree
Showing 25 changed files with 1,119 additions and 305 deletions.
2 changes: 1 addition & 1 deletion build/debian/otgc_native.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# Constants
PROJECT_NAME="otgc"
VERSION="2.7.0"
VERSION="2.8.0"

program=$0

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>otgc</groupId>
<artifactId>otgc</artifactId>
<version>2.7.0</version>
<version>2.8.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ public Completable doOwnershipTransfer(String deviceId, OcfOxmType oxm) {
} else if (oxm == OcfOxmType.OC_OXMTYPE_RDP) {
ret = OCObt.requestRandomPin(uuid, (OCUuid ocUuid, int status) -> {
if (status >= 0) {
LOG.debug("Successfully request Random PIN " + OCUuidUtil.uuidToString(ocUuid));
String pin = randomPinHandler.handler();
String id = OCUuidUtil.uuidToString(ocUuid);
LOG.debug("Successfully request Random PIN " + id);
String pin = randomPinHandler.handler(id);
if (OCObt.performRandomPinOtm(uuid, pin, handler) != -1){
emitter.onComplete();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ public Observable<Device> scanUnownedDevices() {
deviceDao.insert(new DeviceEntity(deviceId, device.getName(), endpoints, DeviceType.UNOWNED, Device.NOTHING_PERMITS));
}

unownedDevices.add(new Device(DeviceType.UNOWNED, deviceId, new OcDeviceInfo(), endpoints, Device.NOTHING_PERMITS));
Device device1 = new Device(DeviceType.UNOWNED, deviceId, new OcDeviceInfo(), endpoints, Device.NOTHING_PERMITS);
if (!unownedDevices.contains(device1)) {
unownedDevices.add(device1);
}
};

int ret;
Expand Down Expand Up @@ -202,7 +205,10 @@ public Observable<Device> scanOwnedDevices() {
deviceDao.insert(new DeviceEntity(deviceId, device.getName(), endpoints, DeviceType.OWNED_BY_SELF, Device.FULL_PERMITS));
}

ownedDevices.add(new Device(DeviceType.OWNED_BY_SELF, deviceId, new OcDeviceInfo(), endpoints, Device.FULL_PERMITS));
Device device1 = new Device(DeviceType.OWNED_BY_SELF, deviceId, new OcDeviceInfo(), endpoints, Device.FULL_PERMITS);
if (!ownedDevices.contains(device1)) {
ownedDevices.add(device1);
}
};

int ret;
Expand Down Expand Up @@ -568,6 +574,38 @@ public Completable post(String host, String uri, String deviceId, OCRepresentati
});
}

public Completable post(String host, String uri, String deviceId, Map<String, Object> values) {
return Completable.create(emitter -> {
OCEndpoint ep = OCEndpointUtil.stringToEndpoint(host, new String[1]);
OCUuid uuid = OCUuidUtil.stringToUuid(deviceId);
OCEndpointUtil.setDi(ep, uuid);

OCResponseHandler handler = (OCClientResponse response) -> {
OCStatus code = response.getCode();
if (code == OCStatus.OC_STATUS_OK
|| code == OCStatus.OC_STATUS_CHANGED) {
emitter.onComplete();
} else {
emitter.onError(new Exception("POST " + uri + " error - code: " + code));
}
};

if (OCMain.initPost(uri, ep, null, handler, OCQos.HIGH_QOS)) {
CborEncoder root = OCRep.beginRootObject();
parseOCRepresentionToCbor(root, values);
OCRep.endRootObject();

if (!OCMain.doPost()) {
emitter.onError(new Exception("Do POST " + uri + " error"));
}
} else {
emitter.onError(new Exception("Init POST " + uri + " error"));
}

OCEndpointUtil.freeEndpoint(ep);
});
}

private void parseOCRepresentionToCbor(CborEncoder parent, OCRepresentation rep, Object valueArray) {
while (rep != null) {
switch (rep.getType()) {
Expand All @@ -592,6 +630,9 @@ private void parseOCRepresentionToCbor(CborEncoder parent, OCRepresentation rep,
case OC_REP_STRING_ARRAY:
OCRep.setStringArray(parent, rep.getName(), (String[])valueArray);
break;
case OC_REP_BOOL_ARRAY:
OCRep.setBooleanArray(parent, rep.getName(), (boolean[])valueArray);
break;
default:
break;
}
Expand All @@ -600,6 +641,46 @@ private void parseOCRepresentionToCbor(CborEncoder parent, OCRepresentation rep,
}
}

private void parseOCRepresentionToCbor(CborEncoder parent, Map<String, Object> values) {
for (String key : values.keySet()) {
if (values.get(key) instanceof Boolean) {
OCRep.setBoolean(parent, key, (boolean)values.get(key));
} else if (values.get(key) instanceof Integer) {
OCRep.setLong(parent, key, (Integer)values.get(key));
} else if (values.get(key) instanceof Double) {
OCRep.setDouble(parent, key, (Double)values.get(key));
} else if (values.get(key) instanceof String) {
OCRep.setTextString(parent, key, (String)values.get(key));
} else if (values.get(key) instanceof List) {
if (((List) values.get(key)).get(0) instanceof String) {
String[] ret = new String[((List<String>)values.get(key)).size()];
for (int i=0; i< ((List<String>)values.get(key)).size(); i++) {
ret[i] = ((List<String>)values.get(key)).get(i);
}
OCRep.setStringArray(parent, key, ret);
} else if (((List) values.get(key)).get(0) instanceof Integer) {
long[] ret = new long[((List<Integer>)values.get(key)).size()];
for (int i=0; i< ((List<Integer>)values.get(key)).size(); i++) {
ret[i] = ((List<Integer>)values.get(key)).get(i);
}
OCRep.setLongArray(parent, key, ret);
} else if (((List) values.get(key)).get(0) instanceof Double) {
double[] ret = new double[((List<Double>)values.get(key)).size()];
for (int i=0; i< ((List<Double>)values.get(key)).size(); i++) {
ret[i] = ((List<Double>)values.get(key)).get(i);
}
OCRep.setDoubleArray(parent, key, ret);
} else if (((List) values.get(key)).get(0) instanceof Boolean) {
boolean[] ret = new boolean[((List<Boolean>)values.get(key)).size()];
for (int i=0; i< ((List<Boolean>)values.get(key)).size(); i++) {
ret[i] = ((List<Boolean>)values.get(key)).get(i);
}
OCRep.setBooleanArray(parent, key, ret);
}
}
}
}

public void close() {
LOG.debug("Calling OCMain.mainShutdown()");
OCMain.mainShutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class SerializableResource implements Serializable {
private List<String> types;
private List<String> interfaces;
Map<String, Object> properties = new HashMap<>();
Map<String, Boolean> propertiesAccess = new HashMap<>();

private boolean observing = false;
private boolean observable = true;
Expand Down Expand Up @@ -95,6 +96,9 @@ public void setProperties(OCRepresentation ocRepresentation) {
case OC_REP_BOOL:
properties.put(ocRepresentation.getName(), ocRepresentation.getValue().getBool());
break;
case OC_REP_BOOL_ARRAY:
properties.put(ocRepresentation.getName(), OCRep.ocArrayToBooleanArray(ocRepresentation.getValue().getArray()));
break;
case OC_REP_STRING:
properties.put(ocRepresentation.getName(), ocRepresentation.getValue().getString());
break;
Expand All @@ -121,6 +125,16 @@ public void setProperties(OCRepresentation ocRepresentation) {
}
}

public Map<String, Boolean> getPropertiesAccess() {
return this.propertiesAccess;
}

public void setPropertiesAccess(List<DynamicUiProperty> properties) {
for (DynamicUiProperty property : properties) {
propertiesAccess.put(property.getName(), property.isReadOnly());
}
}

public boolean isObserving() {
return this.observing;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,14 @@ public int compareTo(Device device) {

return res;
}

@Override
public boolean equals(Object device) {
boolean same = false;

if (device != null && device instanceof Device) {
same = this.deviceId.equals(((Device)device).getDeviceId());
}
return same;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.openconnectivity.otgc.domain.usecase;

import io.reactivex.Single;
import org.openconnectivity.otgc.data.repository.DoxsRepository;
import org.openconnectivity.otgc.data.repository.IotivityRepository;
import org.openconnectivity.otgc.data.repository.SettingRepository;
import org.openconnectivity.otgc.domain.model.devicelist.Device;
import org.openconnectivity.otgc.utils.constant.OcfOxmType;
import org.openconnectivity.otgc.utils.rx.SchedulersFacade;

import javax.inject.Inject;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class OnboardDevicesUseCase {

/* Repositories */
private final IotivityRepository iotivityRepository;
private final DoxsRepository doxsRepository;
private final SettingRepository settingRepository;
/* Scheduler */
private final SchedulersFacade schedulersFacade;

@Inject
public OnboardDevicesUseCase(IotivityRepository iotivityRepository,
DoxsRepository doxsRepository,
SettingRepository settingRepository,
SchedulersFacade schedulersFacade) {
this.iotivityRepository = iotivityRepository;
this.doxsRepository = doxsRepository;
this.settingRepository = settingRepository;

this.schedulersFacade = schedulersFacade;
}

public Single<Device> execute(Device device, List<OcfOxmType> oxms) {
int it1 = oxms.size() - 1;
if (it1 < 0) {
return null;
}

return executeOnboard(device, oxms.get(it1))
.onErrorResumeNext(error1 -> {
int it2 = it1 - 1;
if (it2 < 0) {
return null;
}
return iotivityRepository.scanUnownedDevices()
.filter(device1 -> device.getDeviceId().equals(device1.getDeviceId()))
.firstOrError()
.flatMap(device1 ->
executeOnboard(device1, oxms.get(it2))
.onErrorResumeNext(error -> {
int it3 = it1 - 2;
if (it3 < 0) {
return null;
}
return iotivityRepository.scanUnownedDevices()
.filter(device2 -> device.getDeviceId().equals(device2.getDeviceId()))
.firstOrError()
.flatMap(device2 -> executeOnboard(device2, oxms.get(it3)));
})
);
});
}

private Single<Device> executeOnboard(Device deviceToOnboard, OcfOxmType oxm) {
int delay = Integer.parseInt(settingRepository.get(SettingRepository.REQUESTS_DELAY_KEY, SettingRepository.REQUESTS_DELAY_DEFAULT_VALUE));

final Single<Device> getUpdatedOcSecureResource = iotivityRepository.scanOwnedDevices()
.filter(device -> deviceToOnboard.getDeviceId().equals(device.getDeviceId())
|| deviceToOnboard.equalsHosts(device))
.singleOrError();

return doxsRepository.doOwnershipTransfer(deviceToOnboard.getDeviceId(), oxm)
.delay(2 * delay, TimeUnit.SECONDS, schedulersFacade.ui())
.andThen(getUpdatedOcSecureResource
.onErrorResumeNext(error -> getUpdatedOcSecureResource
.retry(2)
.onErrorResumeNext(Single.error(error)))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openconnectivity.otgc.domain.model.devicelist.Device;

import javax.inject.Inject;
import java.util.Map;

public class PostRequestUseCase {
private final IotivityRepository iotivityRepository;
Expand All @@ -39,4 +40,9 @@ public Completable execute(Device device, SerializableResource resource, OCRepre
return iotivityRepository.getSecureEndpoint(device)
.flatMapCompletable(endpoint -> iotivityRepository.post(endpoint, resource.getUri(), device.getDeviceId(), rep, valueArray));
}

public Completable execute(Device device, SerializableResource resource, Map<String, Object> values) {
return iotivityRepository.getSecureEndpoint(device)
.flatMapCompletable(endpoint -> iotivityRepository.post(endpoint, resource.getUri(), device.getDeviceId(), values));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private List<DynamicUiElement> jsonSwaggerToUi(@NonNull JSONObject jsonSwagger)
uiElement.setPath(pathEntry.getKey());
String definitionName = getSchemaTitle(pathEntry.getValue());
if (definitionName != null && !definitionName.isEmpty()) {
uiElement.setResourceTypes(getResourceTypes(jsonSwagger, definitionName));
uiElement.setResourceTypes(getResourceTypes(swagger, definitionName));
uiElement.setInterfaces(getInterfaces(swagger, definitionName));
uiElement.setProperties(getProperties(swagger, definitionName));
uiElement.setSupportedOperations(getSupportedOperations(pathEntry.getValue()));
Expand All @@ -94,20 +94,11 @@ private String getSchemaTitle(Path path) {
return schemaTitle;
}

private List<String> getResourceTypes(JSONObject jsonSwagger, String definitionName) throws JSONException {
List<String> resourceTypes = new ArrayList<>();
private List<String> getResourceTypes(Swagger swagger, String definitionName) throws JSONException {
ArrayProperty interfaces = (ArrayProperty) swagger.getDefinitions().get(definitionName).getProperties().get("rt");
StringProperty rtItems = (StringProperty) interfaces.getItems();

JSONArray jsonRt = jsonSwagger.getJSONObject("definitions")
.getJSONObject(definitionName)
.getJSONObject("properties")
.getJSONObject("rt")
.getJSONArray("default");

for (int i = 0; i < jsonRt.length(); i++) {
resourceTypes.add(jsonRt.getString(i));
}

return resourceTypes;
return rtItems.getEnum();
}

private List<String> getInterfaces(Swagger swagger, String definitionName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.openconnectivity.otgc.utils.handler;

public interface OCSetRandomPinHandler {
public String handler();
public String handler(String deviceId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import javafx.beans.property.*;
import org.openconnectivity.otgc.domain.model.devicelist.Device;

import java.util.List;

public class DeviceListToolbarDetailScope implements Scope {

private final ObjectProperty<Device> selectedDevice = new SimpleObjectProperty<>(this, "selectedDevice");
public ObjectProperty<Device> selectedDeviceProperty() {
private final ObjectProperty<List<Device>> selectedDevice = new SimpleObjectProperty<>(this, "selectedDevice");
public ObjectProperty<List<Device>> selectedDeviceProperty() {
return this.selectedDevice;
}

Expand All @@ -43,7 +45,7 @@ public StringProperty selectedTabProperty() {
return this.selectedTab;
}

public final Device getSelectedDevice() {
/*public final Device getSelectedDevice() {
return this.selectedDeviceProperty().get();
}
public final void setSelectedDevice(final Device selectedDevice) {
Expand All @@ -55,5 +57,5 @@ public final int getPositionSelectedDevice() {
}
public final void setPositionSelectedDevice(final int position) {
this.positionSelectedDeviceProperty().set(position);
}
}*/
}
Loading

0 comments on commit 4763d79

Please sign in to comment.