import oic.simulator.clientcontroller.listener.IResourceSelectionChangedUIListener;
import oic.simulator.clientcontroller.listener.IVerificationUIListener;
import oic.simulator.clientcontroller.remoteresource.MetaProperty;
-import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
import oic.simulator.clientcontroller.remoteresource.RemoteResource;
-import oic.simulator.clientcontroller.remoteresource.RemoteResourceAttribute;
import oic.simulator.clientcontroller.utils.Constants;
import oic.simulator.clientcontroller.utils.Utility;
// Get the device and platform information
try {
- SimulatorManager.findDevices(deviceListener);
- SimulatorManager
- .getPlatformInformation(platformListener);
+ SimulatorManager.findDevices(resource
+ .getRemoteResourceRef().getHost(),
+ deviceListener);
+ SimulatorManager.getPlatformInformation(resource
+ .getRemoteResourceRef().getHost(),
+ platformListener);
} catch (SimulatorException e) {
Activator
.getDefault()
return null;
}
- resource.setResourceModelRef(resourceModelN);
- Map<String, RemoteResourceAttribute> attributeMap = fetchResourceAttributesFromModel(resourceModelN);
- if (resource.isConfigUploaded()) {
- updateResourceAttributesFromResponse(resource, attributeMap);
+ // if(!resource.isConfigUploaded() || null ==
+ // resource.getResourceModelRef())
+ SimulatorResourceModel resourceModel = resource.getResourceModelRef();
+ if (null == resourceModel) {
+ resource.setResourceModelRef(resourceModelN);
} else {
- resource.setResourceAttributesMap(attributeMap);
+ resourceModel.update(resourceModelN);
}
- // TODO: Printing the values for debugging
- if (null != attributeMap) {
- RemoteResourceAttribute.printAttributes(attributeMap);
- }
- return resource;
- }
- private void updateResourceAttributesFromResponse(RemoteResource res,
- Map<String, RemoteResourceAttribute> newAttributeMap) {
- if (null == res || null == newAttributeMap) {
- return;
- }
- Map<String, RemoteResourceAttribute> oldAttributeMap = res
- .getResourceAttributesMap();
- if (null == oldAttributeMap) {
- return;
- }
- Iterator<String> itr = oldAttributeMap.keySet().iterator();
- String attName;
- RemoteResourceAttribute newAtt;
- RemoteResourceAttribute oldAtt;
- while (itr.hasNext()) {
- attName = itr.next();
- newAtt = newAttributeMap.get(attName);
- if (null == newAtt) {
- // Attribute does not exist in the received model. Hence
- // removing it from local model.
- itr.remove();
- } else {
- oldAtt = oldAttributeMap.get(attName);
- if (null != oldAtt) {
- oldAtt.setAttributeValue(newAtt.getAttributeValue());
- } else {
- itr.remove();
- }
- newAttributeMap.remove(attName);
- }
- }
- // Adding new attributes in the received model.
- itr = newAttributeMap.keySet().iterator();
- while (itr.hasNext()) {
- attName = itr.next();
- oldAttributeMap.put(attName, newAttributeMap.get(attName));
- }
+ resource.setResourceRepresentation(resourceModelN, false);
+
+ return resource;
}
public synchronized DeviceInfo getDeviceInfo() {
public void addObservedResourceURI(String resourceURI) {
synchronized (observedResourceURIList) {
- observedResourceURIList.add(resourceURI);
+ if (!observedResourceURIList.contains(resourceURI))
+ observedResourceURIList.add(resourceURI);
}
}
return resource;
}
- private Map<String, RemoteResourceAttribute> fetchResourceAttributesFromModel(
- SimulatorResourceModel resourceModelN) {
- Map<String, RemoteResourceAttribute> resourceAttributeMap = null;
- if (null != resourceModelN) {
- Map<String, SimulatorResourceAttribute> attributeMapN;
- attributeMapN = resourceModelN.getAttributes();
- if (null != attributeMapN) {
- resourceAttributeMap = new HashMap<String, RemoteResourceAttribute>();
-
- Set<String> attNameSet = attributeMapN.keySet();
- String attName;
- Object attValueObj;
- AttributeValue attValueN;
- AttributeProperty attPropN;
- TypeInfo typeInfo;
- Type valuesType;
- SimulatorResourceAttribute attributeN;
- RemoteResourceAttribute attribute;
- Iterator<String> attNameItr = attNameSet.iterator();
- while (attNameItr.hasNext()) {
- attName = attNameItr.next();
- attributeN = attributeMapN.get(attName);
- if (null != attributeN) {
- attribute = new RemoteResourceAttribute();
- attribute.setResourceAttributeRef(attributeN);
- attribute.setAttributeName(attName);
-
- attValueN = attributeN.value();
- if (null != attValueN) {
- attValueObj = attValueN.get();
- if (null != attValueObj) {
- attribute.setAttributeValue(attValueObj);
- }
-
- // Set the attribute type
- typeInfo = attValueN.typeInfo();
- if (null != typeInfo) {
- attribute.setAttValBaseType(typeInfo.mBaseType);
- attribute.setAttValType(typeInfo.mType);
- attribute.setDepth(typeInfo.mDepth);
- }
-
- }
-
- // Set the range and allowed values
- attPropN = attributeN.property();
- valuesType = attPropN.type();
- attribute.setValuesType(valuesType);
- if (valuesType == Type.RANGE) {
- attribute.setMinValue(attPropN.min());
- attribute.setMaxValue(attPropN.max());
- } else if (valuesType == Type.VALUESET) {
- Object[] values = attPropN.valueSet();
- if (null != values && values.length > 0) {
- List<Object> valueList = new ArrayList<Object>();
- for (Object obj : values) {
- valueList.add(((AttributeValue) obj).get());
- }
- attribute.setAllowedValues(valueList);
- }
- }
- resourceAttributeMap.put(attName, attribute);
- }
- }
- }
- }
- return resourceAttributeMap;
- }
-
private void newResourceFoundNotification(RemoteResource resource) {
synchronized (findResourceUIListeners) {
if (findResourceUIListeners.size() > 0) {
return resourceList;
}
- public String getAttributeValue(RemoteResource res, String attName) {
- if (null == res || null == attName) {
+ public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
+ if (null == att) {
+ return null;
+ }
+
+ AttributeValue val = att.value();
+ if (null == val) {
+ return null;
+ }
+
+ List<String> values = new ArrayList<String>();
+
+ TypeInfo type = val.typeInfo();
+
+ AttributeProperty prop = att.property();
+ if (null == prop || prop.type().ordinal() == Type.UNKNOWN.ordinal()) {
+ values.add(Utility.getAttributeValueAsString(val));
+ return values;
+ }
+
+ Type valuesType = prop.type();
+
+ if (type.mType != ValueType.RESOURCEMODEL) {
+ if (type.mType == ValueType.ARRAY) {
+ if (type.mDepth == 1) {
+ AttributeProperty childProp = prop.getChildProperty();
+ if (null != childProp) {
+ valuesType = childProp.type();
+ if (valuesType.ordinal() == Type.RANGE.ordinal()) {
+ List<String> list = getRangeForPrimitiveNonArrayAttributes(
+ childProp, type.mBaseType);
+ if (null != list) {
+ values.addAll(list);
+ }
+ } else if (valuesType.ordinal() == Type.VALUESET
+ .ordinal()) {
+ List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
+ childProp.valueSet(), type.mBaseType);
+ if (null != list) {
+ values.addAll(list);
+ }
+ }
+ }
+ }
+ } else {
+ if (valuesType.ordinal() == Type.RANGE.ordinal()) {
+ List<String> list = getRangeForPrimitiveNonArrayAttributes(
+ prop, type.mType);
+ if (null != list) {
+ values.addAll(list);
+ }
+ } else if (valuesType.ordinal() == Type.VALUESET.ordinal()) {
+ List<String> list = getAllowedValuesForPrimitiveNonArrayAttributes(
+ prop.valueSet(), type.mType);
+ if (null != list) {
+ values.addAll(list);
+ }
+ }
+ }
+ }
+
+ if (values.isEmpty()) {
+ values.add(Utility.getAttributeValueAsString(val));
+ }
+
+ return values;
+ }
+
+ public List<String> getRangeForPrimitiveNonArrayAttributes(
+ AttributeProperty prop, ValueType type) {
+ if (null == prop) {
+ return null;
+ }
+
+ if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
+ return null;
+ }
+
+ List<String> values = new ArrayList<String>();
+ switch (type) {
+ case INTEGER:
+ int min = (int) prop.min();
+ int max = (int) prop.max();
+ for (int iVal = min; iVal <= max; iVal++) {
+ values.add(String.valueOf(iVal));
+ }
+ break;
+ case DOUBLE:
+ double minD = (double) prop.min();
+ double maxD = (double) prop.max();
+ for (double iVal = minD; iVal <= maxD; iVal = iVal + 1.0) {
+ values.add(String.valueOf(iVal));
+ }
+ break;
+ default:
+ }
+ return values;
+ }
+
+ public List<String> getAllowedValuesForPrimitiveNonArrayAttributes(
+ AttributeValue[] attValues, ValueType type) {
+ if (null == attValues || attValues.length < 1) {
+ return null;
+ }
+
+ if (type == ValueType.ARRAY || type == ValueType.RESOURCEMODEL) {
return null;
}
- return res.getAttributeValue(attName);
+
+ Object obj;
+ List<String> values = new ArrayList<String>();
+ for (AttributeValue val : attValues) {
+ if (null == val) {
+ continue;
+ }
+ obj = val.get();
+ if (null == obj) {
+ continue;
+ }
+ values.add(String.valueOf(obj));
+ }
+ return values;
}
+ /*
+ * public String getAttributeValue(RemoteResource res, String attName) { if
+ * (null == res || null == attName) { return null; } return
+ * res.getAttributeValue(attName); }
+ */
public void sendGetRequest(RemoteResource resource) {
if (null == resource) {
return;
}
public void sendPutRequest(RemoteResource resource,
- List<PutPostAttributeModel> putPostModelList) {
- if (null == resource) {
+ SimulatorResourceModel model) {
+ if (null == resource || null == model) {
return;
}
SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
if (null == resourceN) {
return;
}
- Map<String, RemoteResourceAttribute> attMap = resource
- .getResourceAttributesMap();
- if (null == attMap || attMap.size() < 1) {
- return;
- }
try {
- SimulatorResourceModel resourceModel = getUpdatedResourceModel(
- attMap, putPostModelList);
- resourceN.put(null, resourceModel, putListener);
+ resourceN.put(null, model, putListener);
} catch (Exception e) {
String addlInfo;
addlInfo = "Invalid Attribute Value. Cannot send PUT request.";
}
public void sendPostRequest(RemoteResource resource,
- List<PutPostAttributeModel> putPostModelList) {
- if (null == resource) {
+ SimulatorResourceModel model) {
+ if (null == resource || null == model) {
return;
}
SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
if (null == resourceN) {
return;
}
- Map<String, RemoteResourceAttribute> attMap = resource
- .getResourceAttributesMap();
- if (null == attMap || attMap.size() < 1) {
- return;
- }
- // Filter out the attributes whose modification status is true.
- Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
- PutPostAttributeModel model;
- while (itr.hasNext()) {
- model = itr.next();
- if (!model.isModified()) {
- itr.remove();
- }
- }
try {
- SimulatorResourceModel resourceModel = getUpdatedResourceModel(
- attMap, putPostModelList);
- resourceN.post(null, resourceModel, postListener);
+ resourceN.post(null, model, postListener);
} catch (Exception e) {
String addlInfo;
addlInfo = "Invalid Attribute Value. Cannot send POST request.";
}
}
- private SimulatorResourceModel getUpdatedResourceModel(
- Map<String, RemoteResourceAttribute> attMap,
- List<PutPostAttributeModel> putPostModelList) throws Exception {
- String attName;
- SimulatorResourceModel resourceModel = new SimulatorResourceModel();
- PutPostAttributeModel model;
- RemoteResourceAttribute attribute;
- ValueType attType;
- Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
- while (itr.hasNext()) {
- model = itr.next();
- attName = model.getAttName();
- attribute = attMap.get(attName);
- if (null == attribute) {
- continue;
- }
- attType = attribute.getAttValBaseType();
- if (attType == ValueType.INTEGER) {
- int attValue;
- attValue = Integer.parseInt(model.getAttValue());
- resourceModel.addAttribute(attName,
- new AttributeValue(attValue));
- } else if (attType == ValueType.DOUBLE) {
- double attValue;
- attValue = Double.parseDouble(model.getAttValue());
- resourceModel.addAttribute(attName,
- new AttributeValue(attValue));
- } else if (attType == ValueType.BOOLEAN) {
- String attValue = model.getAttValue();
- if (null != attValue && attValue.length() > 0) {
- attValue = attValue.toLowerCase();
- if (!(attValue.equals("true") || attValue.equals("false"))) {
- throw new Exception("Invalid attribute value");
- }
- }
- resourceModel.addAttribute(attName,
- new AttributeValue(attValue));
- } else if (attType == ValueType.STRING) {
- String attValue;
- attValue = model.getAttValue();
- resourceModel.addAttribute(attName,
- new AttributeValue(attValue));
- }
- }
- return resourceModel;
- }
-
public boolean sendObserveRequest(RemoteResource resource) {
if (null == resource) {
return false;
return false;
}
try {
- SimulatorResourceModel resourceModel;
- resourceModel = resourceN.setConfigInfo(configFilePath);
- if (null == resourceModel) {
+ SimulatorResourceModel configuredResourceModel;
+ configuredResourceModel = resourceN.setConfigInfo(configFilePath);
+ if (null == configuredResourceModel) {
return false;
}
+
// Store the resource model in the local cache
- resource.setResourceModelRef(resourceModel);
-
- // Fetching the allowed values and range for all the attributes
- Map<String, RemoteResourceAttribute> attributeMap = fetchResourceAttributesFromModel(resourceModel);
- resource.setResourceAttributesMap(attributeMap);
- // TODO: Printing the values for debugging
- if (null != attributeMap) {
- RemoteResourceAttribute.printAttributes(attributeMap);
+ SimulatorResourceModel resourceModel = resource
+ .getResourceModelRef();
+ if (null != resourceModel) {
+ configuredResourceModel.update(resourceModel);
}
+ resource.setResourceModelRef(configuredResourceModel);
} catch (SimulatorException e) {
Activator
.getDefault()