|
@@ -1,5 +1,6 @@
|
|
|
package org.iotivity.bridge.homeserver;
|
|
|
|
|
|
+import android.provider.Settings;
|
|
|
import android.util.Log;
|
|
|
|
|
|
import org.iotivity.OCBridge;
|
|
@@ -43,6 +44,8 @@ import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
import java.util.Properties;
|
|
|
|
|
|
public class BridgeServer {
|
|
@@ -63,7 +66,7 @@ public class BridgeServer {
|
|
|
private Bridge bridge = null ;
|
|
|
private Light light = null ;
|
|
|
|
|
|
- private ArrayList<OcRemoteDevice> pairwised_devices = new ArrayList<OcRemoteDevice>();
|
|
|
+ private HashMap<OcRemoteDevice, Long> pairwised_devices = new HashMap<OcRemoteDevice, Long>();
|
|
|
private ArrayList<OCUuid> pairwised_credential_uuids = new ArrayList<OCUuid>();
|
|
|
|
|
|
public BridgeServer(BridgeService service) {
|
|
@@ -279,7 +282,9 @@ public class BridgeServer {
|
|
|
public VirtualOCFDevice addVirtualOCFDevice(String device_name, String virtual_di, String device_type, String eco_name) {
|
|
|
|
|
|
// Add Virtual Device for Light
|
|
|
+ Log.d(TAG, "virtual_di = " + virtual_di);
|
|
|
OCUuid uuid = OCUuidUtil.stringToUuid(virtual_di);
|
|
|
+ Log.d(TAG, "virtual_di = " + OCUuidUtil.uuidToString(uuid));
|
|
|
int vod_index = OCBridge.addVirtualDevice(uuid.getId(),
|
|
|
eco_name, "/oic/d", device_type, // "oic.d.virtual" automatically added by internal api implements
|
|
|
device_name, "ocf.2.2.0", "ocf.res.1.3.0,ocf.sh.1.3.0");
|
|
@@ -570,7 +575,12 @@ public class BridgeServer {
|
|
|
|
|
|
/* These APIs are related to online pairwised devices */
|
|
|
public ArrayList<OcRemoteDevice> getPairwisedDevices() {
|
|
|
- return pairwised_devices;
|
|
|
+ ArrayList<OcRemoteDevice> pDevices = new ArrayList<>();
|
|
|
+ Iterator<OcRemoteDevice> it = pairwised_devices.keySet().iterator();
|
|
|
+ while(it.hasNext()){
|
|
|
+ pDevices.add(it.next());
|
|
|
+ }
|
|
|
+ return pDevices.size() > 0 ? pDevices : null;
|
|
|
}
|
|
|
|
|
|
public void clearPairwisedDevices() {
|
|
@@ -578,18 +588,32 @@ public class BridgeServer {
|
|
|
}
|
|
|
|
|
|
public boolean addOrReplacePairwisedDevice(OcRemoteDevice new_dev) {
|
|
|
- for(OcRemoteDevice dev : pairwised_devices) {
|
|
|
+
|
|
|
+ Iterator<OcRemoteDevice> it = pairwised_devices.keySet().iterator();
|
|
|
+ while(it.hasNext()){
|
|
|
+ OcRemoteDevice dev = it.next();
|
|
|
if(OCUuidUtil.isEqual(dev.getDeviceId(), new_dev.getDeviceId())) {
|
|
|
- return true ;
|
|
|
+ removePairwisedDevices(dev);
|
|
|
+ return this.pairwised_devices.put(new_dev, Long.valueOf(System.currentTimeMillis())) == null ? false : true ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return this.pairwised_devices.put(new_dev, Long.valueOf(System.currentTimeMillis())) == null ? false : true ;
|
|
|
+
|
|
|
+ /*ArrayList<OcRemoteDevice> clones = (ArrayList<OcRemoteDevice>)pairwised_devices.clone();
|
|
|
+ for(OcRemoteDevice dev : clones) {
|
|
|
+ if(OCUuidUtil.isEqual(dev.getDeviceId(), new_dev.getDeviceId())) {
|
|
|
+ removePairwisedDevices(dev);
|
|
|
+ return this.pairwised_devices.add(new_dev) ;
|
|
|
}
|
|
|
}
|
|
|
- return this.pairwised_devices.add(new_dev);
|
|
|
+ return this.pairwised_devices.add(new_dev);*/
|
|
|
}
|
|
|
|
|
|
public boolean removePairwisedDevices(OcRemoteDevice dev) {
|
|
|
- return this.pairwised_devices.remove(dev);
|
|
|
+ return this.pairwised_devices.remove(dev) > 0;
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
public boolean removePairwisedDevices(int index) {
|
|
|
return pairwised_devices.remove(pairwised_devices.get(index));
|
|
|
}
|
|
@@ -597,17 +621,48 @@ public class BridgeServer {
|
|
|
public OcRemoteDevice getPairwisedDevice(int index) {
|
|
|
return pairwised_devices.get(index);
|
|
|
}
|
|
|
+*/
|
|
|
|
|
|
public OcRemoteDevice getPairwisedDevice(String device_uuid) {
|
|
|
OCUuid uuid = OCUuidUtil.stringToUuid(device_uuid);
|
|
|
- for(OcRemoteDevice device : pairwised_devices) {
|
|
|
+ Iterator<OcRemoteDevice> it = pairwised_devices.keySet().iterator();
|
|
|
+ while(it.hasNext()){
|
|
|
+ OcRemoteDevice dev = it.next();
|
|
|
+ if(OCUuidUtil.isEqual(dev.getDeviceId(), uuid)) {
|
|
|
+ return dev ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /*for(OcRemoteDevice device : pairwised_devices) {
|
|
|
if(OCUuidUtil.isEqual(device.getDeviceId(), uuid)) {
|
|
|
return device ;
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
return null ;
|
|
|
}
|
|
|
|
|
|
+ public long getPairwisedDeviceTimeStamp(String device_uuid) {
|
|
|
+ OCUuid uuid = OCUuidUtil.stringToUuid(device_uuid);
|
|
|
+ Iterator<OcRemoteDevice> it = pairwised_devices.keySet().iterator();
|
|
|
+ while(it.hasNext()){
|
|
|
+ OcRemoteDevice dev = it.next();
|
|
|
+ if(OCUuidUtil.isEqual(dev.getDeviceId(), uuid)) {
|
|
|
+ return pairwised_devices.get(dev) ;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0 ;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isPairwisedDeviceOnlineStatus(String device_uuid) {
|
|
|
+ long current_time = System.currentTimeMillis() ;
|
|
|
+ long timestamp = getPairwisedDeviceTimeStamp(device_uuid);
|
|
|
+ Log.d(TAG, "isPairwisedDeviceOnlineStatus() : System.currentTimeMillis() = " + current_time + ", latest discovered time = " + timestamp);
|
|
|
+ if((current_time - timestamp)
|
|
|
+ < (BridgeService.PAIRWISED_DISCOVERY_PEROID * 1.8) * 1000L) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
public OcRemoteResource getPairwisedResource(String device_uuid, String resource_uri) {
|
|
|
OcRemoteDevice device = getPairwisedDevice(device_uuid);
|
|
|
for(OcRemoteResource resource : device.getResources()) {
|