import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.Vector;
import oic.simulator.serviceprovider.Activator;
-import oic.simulator.serviceprovider.listener.IAutomationUIListener;
-import oic.simulator.serviceprovider.listener.IObserverListChangedUIListener;
-import oic.simulator.serviceprovider.listener.IResourceListChangedUIListener;
-import oic.simulator.serviceprovider.listener.IResourceModelChangedUIListener;
-import oic.simulator.serviceprovider.listener.IResourceSelectionChangedUIListener;
-import oic.simulator.serviceprovider.resource.LocalResourceAttribute;
-import oic.simulator.serviceprovider.resource.MetaProperty;
-import oic.simulator.serviceprovider.resource.ModelChangeNotificationType;
-import oic.simulator.serviceprovider.resource.SimulatorResource;
-import oic.simulator.serviceprovider.resource.StandardConfiguration;
+import oic.simulator.serviceprovider.model.AttributeElement;
+import oic.simulator.serviceprovider.model.MetaProperty;
+import oic.simulator.serviceprovider.model.Resource;
+import oic.simulator.serviceprovider.model.ResourceType;
+import oic.simulator.serviceprovider.model.SingleResource;
import oic.simulator.serviceprovider.utils.Constants;
import oic.simulator.serviceprovider.utils.Utility;
-import org.eclipse.swt.graphics.Image;
-import org.oic.simulator.IAutomation;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.swt.widgets.Display;
+import org.oic.simulator.ArrayProperty;
+import org.oic.simulator.AttributeProperty;
+import org.oic.simulator.AttributeProperty.Type;
+import org.oic.simulator.AttributeValue;
+import org.oic.simulator.AttributeValue.TypeInfo;
+import org.oic.simulator.AttributeValue.ValueType;
+import org.oic.simulator.BooleanProperty;
+import org.oic.simulator.DeviceInfo;
+import org.oic.simulator.DeviceListener;
+import org.oic.simulator.DoubleProperty;
import org.oic.simulator.ILogger.Level;
-import org.oic.simulator.ResourceAttribute;
-import org.oic.simulator.ResourceAttribute.Range;
-import org.oic.simulator.ResourceAttribute.Type;
+import org.oic.simulator.IntegerProperty;
+import org.oic.simulator.PlatformInfo;
import org.oic.simulator.SimulatorException;
import org.oic.simulator.SimulatorManager;
+import org.oic.simulator.SimulatorResourceAttribute;
import org.oic.simulator.SimulatorResourceModel;
-import org.oic.simulator.serviceprovider.AutomationType;
-import org.oic.simulator.serviceprovider.IObserver;
-import org.oic.simulator.serviceprovider.IResourceModelChangedListener;
-import org.oic.simulator.serviceprovider.ObserverInfo;
-import org.oic.simulator.serviceprovider.SimulatorResourceServer;
+import org.oic.simulator.StringProperty;
+import org.oic.simulator.server.Observer;
+import org.oic.simulator.server.SimulatorResource;
+import org.oic.simulator.server.SimulatorResource.AutoUpdateListener;
+import org.oic.simulator.server.SimulatorResource.AutoUpdateType;
+import org.oic.simulator.server.SimulatorResource.ObserverListener;
+import org.oic.simulator.server.SimulatorResource.ResourceModelChangeListener;
+import org.oic.simulator.server.SimulatorSingleResource;
/**
* This class acts as an interface between the simulator java SDK and the
*/
public class ResourceManager {
- private Map<String, Map<String, SimulatorResource>> resourceMap;
+ private Data data;
- private Map<String, ArrayList<String>> orderedResourceUriMap;
+ private Resource currentResourceInSelection;
- private StandardConfiguration stdConfig;
+ private ResourceModelChangeListener resourceModelChangeListener;
- private SimulatorResource currentResourceInSelection;
+ private AutoUpdateListener automationListener;
- private List<IResourceListChangedUIListener> resourceListChangedUIListeners;
+ private ObserverListener observer;
- private List<IResourceSelectionChangedUIListener> resourceSelectionChangedUIListeners;
+ private DeviceListener deviceListener;
- private List<IResourceModelChangedUIListener> resourceModelChangedUIListeners;
+ private NotificationSynchronizerThread synchronizerThread;
- private List<IAutomationUIListener> automationUIListeners;
+ private Thread threadHandle;
- private List<IObserverListChangedUIListener> observerUIListeners;
+ private DeviceInfo deviceInfo;
+ private PlatformInfo platformInfo;
- private IResourceModelChangedListener resourceModelChangeListener;
+ private String deviceName;
- private IAutomation automationListener;
-
- private IObserver observer;
-
- private NotificationSynchronizerThread synchronizerThread;
+ public ResourceManager() {
+ data = new Data();
- private Thread threadHandle;
+ deviceListener = new DeviceListener() {
- public ResourceManager() {
- resourceMap = new HashMap<String, Map<String, SimulatorResource>>();
- orderedResourceUriMap = new HashMap<String, ArrayList<String>>();
- stdConfig = new StandardConfiguration();
+ @Override
+ public void onDeviceFound(final String host,
+ final DeviceInfo deviceInfo) {
+ if (null != ResourceManager.this.deviceInfo
+ || null == deviceInfo || null == host) {
+ return;
+ }
+ synchronizerThread.addToQueue(new Runnable() {
+ @Override
+ public void run() {
+ String rcvdDeviceName = deviceInfo.getName();
+ if (null == rcvdDeviceName) {
+ return;
+ }
+ if (deviceName.equalsIgnoreCase(rcvdDeviceName)) {
+ ResourceManager.this.deviceInfo = deviceInfo;
- resourceListChangedUIListeners = new ArrayList<IResourceListChangedUIListener>();
- resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
- resourceModelChangedUIListeners = new ArrayList<IResourceModelChangedUIListener>();
- automationUIListeners = new ArrayList<IAutomationUIListener>();
- observerUIListeners = new ArrayList<IObserverListChangedUIListener>();
+ // Notify the UI Listeners
+ UiListenerHandler.getInstance()
+ .deviceInfoReceivedNotification();
+ }
+ }
+ });
+ }
+ };
- resourceModelChangeListener = new IResourceModelChangedListener() {
+ resourceModelChangeListener = new ResourceModelChangeListener() {
@Override
public void onResourceModelChanged(final String resourceURI,
if (null == resourceURI || null == resourceModelN) {
return;
}
- SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
- if (null == resource) {
- return;
- }
- ModelChangeNotificationType notificationType;
- // Fetch the resource attributes
- Map<String, LocalResourceAttribute> resourceAttributeMapNew;
- resourceAttributeMapNew = fetchResourceAttributesFromModel(resourceModelN);
- if (null == resourceAttributeMapNew) {
- resource.setResourceAttributesMap(null);
- resourceModelChangedUINotification(
- ModelChangeNotificationType.NO_ATTRIBUTES_IN_MODEL,
- resourceURI, null);
- return;
- }
-
- // Update the resource with new model data
- Map<String, LocalResourceAttribute> resourceAttributeMapOld;
- resourceAttributeMapOld = resource
- .getResourceAttributesMap();
- if (null == resourceAttributeMapOld) {
- resource.setResourceAttributesMap(resourceAttributeMapNew);
- resourceModelChangedUINotification(
- ModelChangeNotificationType.ATTRIBUTE_ADDED,
- resourceURI, null);
- return;
- }
- Set<LocalResourceAttribute> valueChangeSet = new HashSet<LocalResourceAttribute>();
- notificationType = compareAndUpdateLocalAttributes(
- resourceAttributeMapOld,
- resourceAttributeMapNew, valueChangeSet);
- if (notificationType != ModelChangeNotificationType.NONE) {
- // Update the UI listeners
- resourceModelChangedUINotification(
- notificationType, resourceURI,
- valueChangeSet);
- }
+ Display.getDefault().asyncExec(new Runnable() {
+ @Override
+ public void run() {
+ Resource resource = data
+ .getResourceByURI(resourceURI);
+ if (null != resource) {
+ try {
+ resource.updateResourceRepresentation(resourceModelN);
+ } catch (NumberFormatException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(),
+ new Date(),
+ "Error while trying to update the attributes.\n"
+ + Utility
+ .getSimulatorErrorString(
+ e,
+ null));
+ }
+ }
+ }
+ });
}
});
}
};
- automationListener = new IAutomation() {
+ automationListener = new AutoUpdateListener() {
@Override
- public void onAutomationComplete(final String resourceURI,
+ public void onUpdateComplete(final String resourceURI,
final int automationId) {
synchronizerThread.addToQueue(new Runnable() {
@Override
public void run() {
- SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
+ SingleResource resource = data
+ .getSingleResourceByURI(resourceURI);
if (null == resource) {
return;
}
if (resource.isResourceAutomationInProgress()) {
changeResourceLevelAutomationStatus(resource, false);
// Notify the UI listeners
- automationCompleteUINotification(resourceURI, null);
+ UiListenerHandler.getInstance()
+ .automationCompleteUINotification(resource,
+ null);
} else if (resource.isAttributeAutomationInProgress()) {
// Find the attribute with the given automation id
- LocalResourceAttribute attribute;
- attribute = getAttributeWithGivenAutomationId(
+ final AttributeElement attribute = getAttributeWithGivenAutomationId(
resource, automationId);
if (null != attribute) {
- attribute.setAutomationInProgress(false);
+ attribute.setAutoUpdateState(false);
resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
- // Notify the UI listeners
- automationCompleteUINotification(resourceURI,
- attribute.getAttributeName());
+ } else {
+ // Setting the attribute automation status to
+ // false.
+ resource.setAttributeAutomationInProgress(false);
}
- } else {
- // Ignoring the notification as there are no
- // known automation for the current resource.
}
}
});
}
};
- observer = new IObserver() {
+ observer = new ObserverListener() {
- @Override
public void onObserverChanged(final String resourceURI,
- final int status, final ObserverInfo observer) {
+ final int status, final Observer observer) {
new Thread() {
@Override
public void run() {
if (null == resourceURI || null == observer) {
return;
}
- SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
+ Resource resource = data.getResourceByURI(resourceURI);
if (null == resource) {
return;
}
resource.removeObserverInfo(observer);
}
// Notify the UI listeners
- observerListChangedUINotification(resourceURI);
+ UiListenerHandler.getInstance()
+ .observerListChangedUINotification(resource);
}
}.start();
}
+
+ @Override
+ public void onObserverAdded(String resourceURI, Observer observer) {
+ onObserverChanged(resourceURI, 0, observer);
+ }
+
+ @Override
+ public void onObserverRemoved(String resourceURI, Observer observer) {
+ onObserverChanged(resourceURI, 1, observer);
+ }
};
synchronizerThread = new NotificationSynchronizerThread();
threadHandle = new Thread(synchronizerThread);
threadHandle.setName("Simulator service provider event queue");
threadHandle.start();
+
+ // Set the default device name.
+ deviceName = "IoTivity Simulator";
+ try {
+ SimulatorManager.setDeviceInfo(deviceName);
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(),
+ new Date(),
+ "Error while registering the device info.\n"
+ + Utility.getSimulatorErrorString(e, null));
+ }
+
+ // Set the default platform information.
+ platformInfo = new PlatformInfo();
+ platformInfo.setPlatformID("Samsung Platform Identifier");
+ platformInfo.setManufacturerName("Samsung");
+ platformInfo.setManufacturerUrl("www.samsung.com");
+ platformInfo.setModelNumber("Samsung Model Num01");
+ platformInfo.setDateOfManufacture("2015-09-10T11:10:30Z");
+ platformInfo.setPlatformVersion("PlatformVersion01");
+ platformInfo.setOperationSystemVersion("OSVersion01");
+ platformInfo.setHardwareVersion("HardwareVersion01");
+ platformInfo.setFirmwareVersion("FirwareVersion01");
+ platformInfo.setSupportUrl("http://www.samsung.com/support");
+ platformInfo.setSystemTime("2015-09-10T11:10:30Z");
+ try {
+ SimulatorManager.setPlatformInfo(platformInfo);
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(),
+ new Date(),
+ "Error while registering the platform info.\n"
+ + Utility.getSimulatorErrorString(e, null));
+ }
+
+ // Get the device information to show other details of the device in UI.
+ try {
+ SimulatorManager.findDevices("", deviceListener);
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(),
+ new Date(),
+ "Failed to get the local device information.\n"
+ + Utility.getSimulatorErrorString(e, null));
+ }
}
private static class NotificationSynchronizerThread implements Runnable {
}
}
- // This method gives a list of available RAML resource configurations.
- public Map<String, String> getResourceConfigurationList() {
- return stdConfig.getStandardResourceConfigurationList();
- }
-
- public String getConfigFilePath(String fileName) {
- return stdConfig.getFilePath(fileName);
- }
-
- public void addResourceListChangedUIListener(
- IResourceListChangedUIListener resourceListChangedUIListener) {
- synchronized (resourceListChangedUIListeners) {
- resourceListChangedUIListeners.add(resourceListChangedUIListener);
+ public void setDeviceInfo(List<MetaProperty> metaProperties) {
+ if (null == metaProperties || metaProperties.size() < 1) {
+ return;
}
- }
-
- public void addResourceSelectionChangedUIListener(
- IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
- synchronized (resourceSelectionChangedUIListeners) {
- resourceSelectionChangedUIListeners
- .add(resourceSelectionChangedUIListener);
+ Iterator<MetaProperty> itr = metaProperties.iterator();
+ MetaProperty prop;
+ String propName;
+ String propValue;
+ boolean found = false;
+ while (itr.hasNext()) {
+ prop = itr.next();
+ propName = prop.getPropName();
+ propValue = prop.getPropValue();
+ if (propName.equals(Constants.DEVICE_NAME)) {
+ this.deviceName = propValue;
+ found = true;
+ break;
+ }
}
- }
- public void addResourceModelChangedUIListener(
- IResourceModelChangedUIListener resourceModelChangedUIListener) {
- synchronized (resourceModelChangedUIListeners) {
- resourceModelChangedUIListeners.add(resourceModelChangedUIListener);
+ if (!found) {
+ return;
}
- }
- public void addAutomationUIListener(
- IAutomationUIListener automationUIListener) {
- synchronized (automationUIListeners) {
- automationUIListeners.add(automationUIListener);
+ try {
+ SimulatorManager.setDeviceInfo(deviceName);
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(),
+ new Date(),
+ "Error while registering the device info.\n"
+ + Utility.getSimulatorErrorString(e, null));
}
}
- public void addObserverListChangedUIListener(
- IObserverListChangedUIListener observerListChangedUIListener) {
- synchronized (observerUIListeners) {
- observerUIListeners.add(observerListChangedUIListener);
+ public boolean isDeviceInfoValid(List<MetaProperty> metaProperties) {
+ if (null == metaProperties || metaProperties.size() < 1) {
+ return false;
}
- }
- public void removeResourceListChangedUIListener(
- IResourceListChangedUIListener listener) {
- synchronized (resourceListChangedUIListeners) {
- if (null != listener && resourceListChangedUIListeners.size() > 0) {
- resourceListChangedUIListeners.remove(listener);
+ Iterator<MetaProperty> itr = metaProperties.iterator();
+ MetaProperty prop;
+ String propName;
+ String propValue;
+ while (itr.hasNext()) {
+ prop = itr.next();
+ propName = prop.getPropName();
+ propValue = prop.getPropValue();
+ if (propName.equals(Constants.DEVICE_NAME)) {
+ if (null == propValue || propValue.length() < 1) {
+ return false;
+ }
+ break;
}
}
+ return true;
}
- public void removeResourceSelectionChangedUIListener(
- IResourceSelectionChangedUIListener listener) {
- synchronized (resourceSelectionChangedUIListeners) {
- if (null != listener
- && resourceSelectionChangedUIListeners.size() > 0) {
- resourceSelectionChangedUIListeners.remove(listener);
- }
- }
+ public List<MetaProperty> getDeviceInfo() {
+ List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
+ metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, deviceName));
+ if (null != deviceInfo) {
+ metaProperties.add(new MetaProperty(Constants.DEVICE_ID, deviceInfo
+ .getID()));
+ metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
+ deviceInfo.getSpecVersion()));
+ metaProperties.add(new MetaProperty(Constants.DEVICE_DMV,
+ deviceInfo.getDataModelVersion()));
+ }
+ return metaProperties;
}
- public void removeResourceModelChangedUIListener(
- IResourceModelChangedUIListener listener) {
- synchronized (resourceModelChangedUIListeners) {
- if (null != listener && resourceModelChangedUIListeners.size() > 0) {
- resourceModelChangedUIListeners.remove(listener);
- }
- }
+ public List<MetaProperty> getPlatformInfo() {
+ List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
+ metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platformInfo
+ .getPlatformID()));
+ metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_NAME,
+ platformInfo.getManufacturerName()));
+ metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
+ platformInfo.getManufacturerUrl()));
+ metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
+ platformInfo.getModelNumber()));
+ metaProperties.add(new MetaProperty(Constants.PLATFORM_DATE_OF_MANUFAC,
+ platformInfo.getDateOfManufacture()));
+ metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
+ platformInfo.getPlatformVersion()));
+ metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
+ platformInfo.getOperationSystemVersion()));
+ metaProperties.add(new MetaProperty(
+ Constants.PLATFORM_HARDWARE_VERSION, platformInfo
+ .getHardwareVersion()));
+ metaProperties.add(new MetaProperty(
+ Constants.PLATFORM_FIRMWARE_VERSION, platformInfo
+ .getFirmwareVersion()));
+ metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
+ platformInfo.getSupportUrl()));
+ metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
+ platformInfo.getSystemTime()));
+ return metaProperties;
}
- public void removeAutomationUIListener(IAutomationUIListener listener) {
- synchronized (automationUIListeners) {
- if (null != listener && automationUIListeners.size() > 0) {
- automationUIListeners.remove(listener);
+ public void setPlatformInfo(List<MetaProperty> metaProperties) {
+ if (null == metaProperties || metaProperties.size() < 1) {
+ return;
+ }
+ Iterator<MetaProperty> itr = metaProperties.iterator();
+ MetaProperty prop;
+ String propName;
+ String propValue;
+ while (itr.hasNext()) {
+ prop = itr.next();
+ propName = prop.getPropName();
+ propValue = prop.getPropValue();
+ if (propName.equals(Constants.PLATFORM_ID)) {
+ platformInfo.setPlatformID(propValue);
+ } else if (propName.equals(Constants.PLATFORM_MANUFAC_NAME)) {
+ platformInfo.setManufacturerName(propValue);
+ } else if (propName.equals(Constants.PLATFORM_MANUFAC_URL)) {
+ platformInfo.setManufacturerUrl(propValue);
+ } else if (propName.equals(Constants.PLATFORM_MODEL_NO)) {
+ platformInfo.setModelNumber(propValue);
+ } else if (propName.equals(Constants.PLATFORM_DATE_OF_MANUFAC)) {
+ platformInfo.setDateOfManufacture(propValue);
+ } else if (propName.equals(Constants.PLATFORM_VERSION)) {
+ platformInfo.setPlatformVersion(propValue);
+ } else if (propName.equals(Constants.PLATFORM_OS_VERSION)) {
+ platformInfo.setOperationSystemVersion(propValue);
+ } else if (propName.equals(Constants.PLATFORM_HARDWARE_VERSION)) {
+ platformInfo.setHardwareVersion(propValue);
+ } else if (propName.equals(Constants.PLATFORM_FIRMWARE_VERSION)) {
+ platformInfo.setFirmwareVersion(propValue);
+ } else if (propName.equals(Constants.PLATFORM_SUPPORT_URL)) {
+ platformInfo.setSupportUrl(propValue);
+ } else if (propName.equals(Constants.PLATFORM_SYSTEM_TIME)) {
+ platformInfo.setSystemTime(propValue);
}
}
+ try {
+ SimulatorManager.setPlatformInfo(platformInfo);
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(),
+ new Date(),
+ "Error while registering the platform info.\n"
+ + Utility.getSimulatorErrorString(e, null));
+ }
}
- public void removeObserverListChangedUIListener(
- IObserverListChangedUIListener listener) {
- synchronized (observerUIListeners) {
- if (null != listener && observerUIListeners.size() > 0) {
- observerUIListeners.remove(listener);
+ public boolean isPlatformInfoValid(List<MetaProperty> metaProperties) {
+ if (null == metaProperties || metaProperties.size() < 1) {
+ return false;
+ }
+ Iterator<MetaProperty> itr = metaProperties.iterator();
+ MetaProperty prop;
+ String propValue;
+ while (itr.hasNext()) {
+ prop = itr.next();
+ propValue = prop.getPropValue();
+ if (null == propValue || propValue.length() < 1) {
+ return false;
}
}
+ return true;
}
- public synchronized SimulatorResource getCurrentResourceInSelection() {
+ public synchronized Resource getCurrentResourceInSelection() {
return currentResourceInSelection;
}
- public synchronized void setCurrentResourceInSelection(
- SimulatorResource resource) {
+ public synchronized void setCurrentResourceInSelection(Resource resource) {
this.currentResourceInSelection = resource;
}
- private void addResourceUriToOrderedMap(String resourceType,
- String resourceURI) {
- if (null != resourceURI && null != resourceType) {
- synchronized (orderedResourceUriMap) {
- ArrayList<String> uriListForType = orderedResourceUriMap
- .get(resourceType);
- if (null == uriListForType) {
- uriListForType = new ArrayList<String>();
- orderedResourceUriMap.put(resourceType, uriListForType);
- }
- uriListForType.add(resourceURI);
- }
- }
+ public boolean isResourceExist(String resourceURI) {
+ return data.isResourceExist(resourceURI);
}
- private void removeResourceUriFromOrderedMap(String resourceType,
- String resourceURI) {
- synchronized (orderedResourceUriMap) {
- if (null != resourceURI && null != resourceType) {
- ArrayList<String> uriListForType = orderedResourceUriMap
- .get(resourceType);
- if (null != uriListForType) {
- uriListForType.remove(resourceURI);
- if (uriListForType.size() < 1) {
- orderedResourceUriMap.remove(resourceType);
- }
- }
- } else if (null != resourceURI) {
- orderedResourceUriMap.remove(resourceType);
- } else {
- orderedResourceUriMap.clear();
- }
- }
+ public boolean isAnyResourceExist() {
+ return data.isAnyResourceExist();
}
- private void addResourceToMap(SimulatorResource simulatorResource) {
- if (null != simulatorResource) {
- synchronized (resourceMap) {
- Map<String, SimulatorResource> resourceTypeMap;
- resourceTypeMap = resourceMap.get(simulatorResource
- .getResourceType());
- if (null == resourceTypeMap) {
- resourceTypeMap = new HashMap<String, SimulatorResource>();
- resourceMap.put(simulatorResource.getResourceType(),
- resourceTypeMap);
- }
- resourceTypeMap.put(simulatorResource.getResourceURI(),
- simulatorResource);
- }
+ public boolean createSingleResource(SingleResource resource,
+ Map<String, SimulatorResourceAttribute> attributes)
+ throws SimulatorException {
+ if (null == resource) {
+ return false;
}
- }
- private void addResourceToMap(String resourceType,
- Map<String, SimulatorResource> newResourceTypeMap) {
- if (null != resourceType && null != newResourceTypeMap) {
- synchronized (resourceMap) {
- Map<String, SimulatorResource> resourceTypeMap = resourceMap
- .get(resourceType);
- if (null != resourceTypeMap) {
- resourceTypeMap.putAll(newResourceTypeMap);
- } else {
- resourceMap.put(resourceType, newResourceTypeMap);
- }
+ try {
+ // Create the resource.
+ SimulatorResource jSimulatorResource = SimulatorManager
+ .createResource(SimulatorResource.Type.SINGLE,
+ resource.getResourceName(),
+ resource.getResourceURI(),
+ resource.getResourceType());
+ if (null == jSimulatorResource
+ || !(jSimulatorResource instanceof SimulatorSingleResource)) {
+ return false;
}
- }
- }
+ SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) jSimulatorResource;
+ resource.setSimulatorResource(jSimulatorSingleResource);
- private void removeResourceFromMap(String resourceType, String resourceURI) {
- if (null != resourceURI && null != resourceType) {
- synchronized (resourceMap) {
- Map<String, SimulatorResource> resourceTypeMap = resourceMap
- .get(resourceType);
- if (null != resourceTypeMap) {
- resourceTypeMap.remove(resourceURI);
- if (resourceTypeMap.size() < 1) {
- resourceMap.remove(resourceType);
- }
- }
+ // Cancel discoverable property if requested by user.
+ if (!resource.isDiscoverable()) {
+ jSimulatorSingleResource.setDiscoverable(false);
}
- }
- }
- public boolean isResourceExist(String resourceURI) {
- boolean result = false;
- if (null != resourceURI) {
- SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
- if (null != resource) {
- result = true;
+ // Cancel observable property if requested by user.
+ if (!resource.isObservable()) {
+ jSimulatorSingleResource.setObservable(false);
}
- }
- return result;
- }
- public void createResource(final String configFilePath) {
- new Thread() {
- @Override
- public void run() {
- SimulatorResourceServer resourceServerN;
- try {
- resourceServerN = SimulatorManager.createResource(
- configFilePath, resourceModelChangeListener);
- } catch (SimulatorException e) {
- Activator
- .getDefault()
- .getLogManager()
- .log(Level.ERROR.ordinal(),
- new Date(),
- "[" + e.getClass().getSimpleName() + "]"
- + e.code().toString() + "-"
- + e.message());
- return;
- }
- SimulatorResource simulatorResource;
- simulatorResource = fetchResourceData(resourceServerN);
- if (null != simulatorResource) {
- addResourceToMap(simulatorResource);
- addResourceUriToOrderedMap(
- simulatorResource.getResourceType(),
- simulatorResource.getResourceURI());
- resourceCreatedUINotification();
-
- // Set the observer for the created resource
- try {
- resourceServerN.setObserverCallback(observer);
- } catch (SimulatorException e) {
- Activator
- .getDefault()
- .getLogManager()
- .log(Level.ERROR.ordinal(),
- new Date(),
- "[" + e.getClass().getSimpleName()
- + "]" + e.code().toString()
- + "-" + e.message());
- }
+ // Set the model change listener.
+ jSimulatorSingleResource
+ .setResourceModelChangeListener(resourceModelChangeListener);
- // Print the resource data
- simulatorResource.printResourceInfo();
- }
+ // Set the observer listener if the resource is observable.
+ if (resource.isObservable()) {
+ jSimulatorSingleResource.setObserverListener(observer);
}
- }.start();
- }
- public void createResource(final String configFilePath,
- final int noOfInstances) {
- new Thread() {
- @Override
- public void run() {
- Map<String, SimulatorResource> resourceTypeMap;
- SimulatorResourceServer[] simulatorResourceServers = null;
- try {
- simulatorResourceServers = SimulatorManager.createResource(
- configFilePath, noOfInstances,
- resourceModelChangeListener);
- } catch (SimulatorException e) {
- Activator
- .getDefault()
- .getLogManager()
- .log(Level.ERROR.ordinal(),
- new Date(),
- "[" + e.getClass().getSimpleName() + "]"
- + e.code().toString() + "-"
- + e.message());
- return;
- }
- if (null == simulatorResourceServers) {
- return;
- }
- resourceTypeMap = new HashMap<String, SimulatorResource>();
- SimulatorResource resource;
- String uri;
- for (SimulatorResourceServer resourceServerN : simulatorResourceServers) {
- resource = fetchResourceData(resourceServerN);
- if (null != resource) {
- uri = resource.getResourceURI();
- resourceTypeMap.put(uri, resource);
- addResourceUriToOrderedMap(resource.getResourceType(),
- uri);
- }
- // Set the observer for the created resource
- try {
- resourceServerN.setObserverCallback(observer);
- } catch (SimulatorException e) {
- Activator
- .getDefault()
- .getLogManager()
- .log(Level.ERROR.ordinal(),
- new Date(),
- "[" + e.getClass().getSimpleName()
- + "]" + e.code().toString()
- + "-" + e.message());
- }
+ // Add attributes.
+ if (null != attributes && !attributes.isEmpty()) {
+ SimulatorResourceAttribute value;
+ for (Map.Entry<String, SimulatorResourceAttribute> entry : attributes
+ .entrySet()) {
+ value = entry.getValue();
+ if (null != value)
+ jSimulatorSingleResource.addAttribute(value);
}
- // Find the resourceType and add it to the local data
- // structure and notify UI Listeners
- if (resourceTypeMap.size() > 0) {
- String resourceType;
- Set<String> uriSet = resourceTypeMap.keySet();
- Iterator<String> itr = uriSet.iterator();
- if (itr.hasNext()) {
- SimulatorResource simResource = resourceTypeMap.get(itr
- .next());
- if (null != simResource) {
- resourceType = simResource.getResourceType();
-
- addResourceToMap(resourceType, resourceTypeMap);
- resourceCreatedUINotification();
- }
- }
- }
- }
- }.start();
- }
+ // Get the resource model java object reference.
+ resource.setResourceModel(jSimulatorSingleResource
+ .getResourceModel());
- private SimulatorResource fetchResourceData(
- SimulatorResourceServer resourceServerN) {
- SimulatorResource simulatorResource = null;
- if (null != resourceServerN) {
- simulatorResource = new SimulatorResource();
- simulatorResource.setResourceServer(resourceServerN);
- simulatorResource.setResourceURI(resourceServerN.getURI());
- simulatorResource
- .setResourceType(resourceServerN.getResourceType());
- simulatorResource.setResourceName(resourceServerN.getName());
- simulatorResource.setResourceInterface(resourceServerN
- .getInterfaceType());
-
- SimulatorResourceModel resourceModelN;
- try {
- resourceModelN = resourceServerN.getModel();
- } catch (SimulatorException e) {
- Activator
- .getDefault()
- .getLogManager()
- .log(Level.ERROR.ordinal(),
- new Date(),
- "[" + e.getClass().getSimpleName() + "]"
- + e.code().toString() + "-"
- + e.message());
- return null;
- }
- if (null != resourceModelN) {
- simulatorResource.setResourceModel(resourceModelN);
-
- // Fetch the resource attributes
- Map<String, LocalResourceAttribute> resourceAttributeMap;
- resourceAttributeMap = fetchResourceAttributesFromModel(resourceModelN);
- if (null != resourceAttributeMap) {
- simulatorResource
- .setResourceAttributesMap(resourceAttributeMap);
- }
+ resource.createResourceRepresentation(jSimulatorSingleResource
+ .getAttributes());
}
- }
- return simulatorResource;
- }
- private Map<String, LocalResourceAttribute> fetchResourceAttributesFromModel(
- SimulatorResourceModel resourceModelN) {
- Map<String, LocalResourceAttribute> resourceAttributeMap = null;
- if (null != resourceModelN) {
- Map<String, ResourceAttribute> attributeMapN;
- attributeMapN = resourceModelN.getAttributes();
- if (null != attributeMapN) {
- resourceAttributeMap = new HashMap<String, LocalResourceAttribute>();
-
- Set<String> attNameSet = attributeMapN.keySet();
- String attName;
- ResourceAttribute attributeN;
- LocalResourceAttribute attribute;
- Iterator<String> attNameItr = attNameSet.iterator();
- while (attNameItr.hasNext()) {
- attName = attNameItr.next();
- attributeN = attributeMapN.get(attName);
- if (null != attributeN) {
- attribute = new LocalResourceAttribute();
- attribute.setResourceAttribute(attributeN);
-
- // Set the attribute value
- Object valueObj = attributeN.getValue();
- if (null != valueObj) {
- attribute.setAttributeValue(valueObj);
- }
+ // Set the resource interfaces.
+ jSimulatorSingleResource
+ .setInterface(Utility.convertSetToVectorString(resource
+ .getResourceInterfaces()));
- // Set the attribute value list.
- attribute.setAttValues(getValueList(attributeN));
+ // Register the resource with the platform.
+ jSimulatorSingleResource.start();
+ resource.setStarted(true);
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(), new Date(),
+ Utility.getSimulatorErrorString(e, null));
+ throw e;
+ }
- // Initially disabling the automation
- attribute.setAutomationInProgress(false);
+ // Add to local cache.
+ data.addResource(resource);
- // TODO: Temporarily setting the interval to 500.
- // This value should come from the native layer.
- // Native implementation is in progress.
- attribute
- .setAutomationUpdateInterval(Constants.DEFAULT_AUTOMATION_INTERVAL);
+ // Update UI listeners
+ UiListenerHandler.getInstance().resourceCreatedUINotification(
+ ResourceType.SINGLE);
- // Setting the default automation type
- attribute
- .setAutomationType(Constants.DEFAULT_AUTOMATION_TYPE);
+ return true;
+ }
- resourceAttributeMap.put(attName, attribute);
- }
- }
+ public Resource createResourceByRAML(String configFilePath)
+ throws SimulatorException {
+ Resource resource = null;
+ try {
+ // Create the resource
+ SimulatorResource jSimulatorResource = SimulatorManager
+ .createResource(configFilePath);
+ if (null == jSimulatorResource) {
+ return null;
}
- }
- return resourceAttributeMap;
- }
+ if (jSimulatorResource instanceof SimulatorSingleResource) {
+ resource = new SingleResource();
+ } else {
+ return null;
+ }
+ resource.setSimulatorResource(jSimulatorResource);
- // This method gives all known possible values of the attribute in string
- // format. It takes allowed values or range of values whichever is available
- private List<String> getValueList(ResourceAttribute attributeN) {
- Object[] allowedValues = attributeN.getAllowedValues();
- List<String> valueList = new ArrayList<String>();
- if (null != allowedValues && allowedValues.length > 0) {
- for (Object value : allowedValues) {
- if (null != value) {
- valueList.add(String.valueOf(value));
- }
+ // Fetch and locally store the resource name and uri.
+ String uri = jSimulatorResource.getURI();
+ if (null == uri || uri.trim().isEmpty()) {
+ return null;
}
- } else {
- Type valueType = attributeN.getBaseType();
- Range range = attributeN.getRange();
- if (null != range) {
- Object min = range.getMin();
- Object max = range.getMax();
- if (valueType == Type.INT) {
- int minI = (Integer) min;
- int maxI = (Integer) max;
- for (int value = minI; value <= maxI; value++) {
- valueList.add(String.valueOf(value));
- }
- } else if (valueType == Type.DOUBLE) {
- double minD = (Double) min;
- double maxD = (Double) max;
- for (double value = minD; value <= maxD; value++) {
- valueList.add(String.valueOf(value));
- }
- }
+ resource.setResourceURI(uri.trim());
+
+ String name = jSimulatorResource.getName();
+ if (null == name || name.trim().isEmpty()) {
+ return null;
}
+ resource.setResourceName(name.trim());
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(), new Date(),
+ Utility.getSimulatorErrorString(e, null));
+ throw e;
}
- Object attValue = attributeN.getValue();
- if (valueList.size() < 1 && null != attValue) {
- valueList.add(String.valueOf(attValue));
- }
- return valueList;
+ return resource;
}
- public void deleteResourceByURI(final String resourceURI) {
- if (null != resourceURI) {
- new Thread() {
- @Override
- public void run() {
- SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
- if (null != resource) {
- String resourceType = resource.getResourceType();
-
- // Unregister the resource from the platform
- deleteResource(resource);
-
- // Delete from the local data structure
- deleteLocalResourceDetails(resourceType, resourceURI);
-
- // Notify the UI listener for removing this resource
- // from UI
- resourceDeletedUINotification();
-
- if (null != currentResourceInSelection
- && resource == currentResourceInSelection) {
- // Listeners might query the resource being deleted
- // if exists. So set the currently selection to
- // null.
- setCurrentResourceInSelection(null);
-
- // Notify all observers for resource selection
- // change event
- resourceSelectionChangedUINotification();
- }
- }
- }
- }.start();
+ /**
+ * This method can set/change the resource uri and name of an already
+ * created resource which is not yet registered with the platform. This
+ * method registers the model change and observer listeners, registers the
+ * resource, fetches the resource attributes, updates the local cache and
+ * notifies the UI listeners.
+ */
+ public boolean completeSingleResourceCreationByRAML(Resource resource,
+ String uri, String name, boolean multiInstance)
+ throws SimulatorException {
+ if (null == resource || !(resource instanceof SingleResource)) {
+ return false;
}
- }
+ try {
+ SingleResource singleRes = (SingleResource) resource;
- private SimulatorResource getSimulatorResourceByURI(String resourceURI) {
- SimulatorResource resource = null;
- if (null != resourceURI) {
- synchronized (resourceMap) {
- Set<String> typeSet = resourceMap.keySet();
- Iterator<String> typeItr = typeSet.iterator();
- String resourceType;
- Map<String, SimulatorResource> resourceTypeMap;
- while (typeItr.hasNext()) {
- resourceType = typeItr.next();
- resourceTypeMap = resourceMap.get(resourceType);
- if (null != resourceTypeMap) {
- resource = resourceTypeMap.get(resourceURI);
- if (null != resource) {
- break;
- }
- }
+ SimulatorSingleResource jSimulatorSingleResource = (SimulatorSingleResource) resource
+ .getSimulatorResource();
+ if (null == jSimulatorSingleResource) {
+ return false;
+ }
+
+ // Update resource URI and Name if they are changed.
+ String newUri = uri.trim();
+ String newName = name.trim();
+
+ if (multiInstance) {
+ singleRes.setResourceURI(newUri);
+ singleRes.setResourceName(newName);
+ } else {
+ if (!singleRes.getResourceURI().equals(newUri)) {
+ jSimulatorSingleResource.setURI(newUri);
+ singleRes.setResourceURI(newUri);
+ }
+ if (!singleRes.getResourceName().equals(newName)) {
+ jSimulatorSingleResource.setName(newName);
+ singleRes.setResourceName(newName);
}
}
+
+ // Set the model change listener.
+ jSimulatorSingleResource
+ .setResourceModelChangeListener(resourceModelChangeListener);
+
+ // Set the observer listener if the resource is observable.
+ if (jSimulatorSingleResource.isObservable()) {
+ jSimulatorSingleResource.setObserverListener(observer);
+ singleRes.setObservable(true);
+ }
+
+ // Fetch the resource model.
+ SimulatorResourceModel jResModel = jSimulatorSingleResource
+ .getResourceModel();
+ if (null == jResModel) {
+ return false;
+ }
+ singleRes.setResourceModel(jResModel);
+
+ // Fetch the basic details of the resource.
+ singleRes.setResourceType(jSimulatorSingleResource
+ .getResourceType());
+ singleRes
+ .setResourceInterfaces(Utility
+ .convertVectorToSet(jSimulatorSingleResource
+ .getInterface()));
+
+ // Fetch the resource attributes.
+ singleRes.createResourceRepresentation(jSimulatorSingleResource
+ .getAttributes());
+
+ // Register the resource with the platform.
+ jSimulatorSingleResource.start();
+ singleRes.setStarted(true);
+
+ // Add to local cache.
+ data.addResource(singleRes);
+
+ // Update UI listeners for single instance creation
+ if (!multiInstance)
+ UiListenerHandler.getInstance().resourceCreatedUINotification(
+ ResourceType.SINGLE);
+ } catch (Exception e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(), new Date(),
+ Utility.getSimulatorErrorString(e, null));
+ throw e;
}
- return resource;
+ return true;
}
- private void deleteResource(SimulatorResource resource) {
- if (null != resource) {
- SimulatorResourceServer resourceServerN = resource
- .getResourceServer();
- if (null != resourceServerN) {
+ public Set<SingleResource> createSingleResourceMultiInstances(
+ String configFile, int count, IProgressMonitor progressMonitor)
+ throws SimulatorException {
+ Set<SingleResource> resultSet;
+ try {
+ resultSet = new HashSet<SingleResource>();
+ Vector<SimulatorResource> jSimulatorResources = SimulatorManager
+ .createResource(configFile, count);
+ if (null == jSimulatorResources || jSimulatorResources.size() < 1) {
+ return null;
+ }
+ SimulatorSingleResource jResource;
+ SingleResource resource;
+ boolean result;
+ for (SimulatorResource jSimulatorResource : jSimulatorResources) {
+ // If the resource creation progress is canceled, then stop the
+ // creation and stop/delete
+ // the resources created already.
+ if (progressMonitor.isCanceled()) {
+ removeSingleResources(resultSet);
+ return null;
+ }
+ jResource = (SimulatorSingleResource) jSimulatorResource;
+ resource = new SingleResource();
+ resource.setSimulatorResource(jResource);
try {
- SimulatorManager.deleteResource(resourceServerN);
- } catch (SimulatorException e) {
+ result = completeSingleResourceCreationByRAML(resource,
+ jResource.getURI(), jResource.getName(), true);
+ if (result) {
+ resultSet.add(resource);
+ }
+ } catch (SimulatorException eInner) {
Activator
.getDefault()
.getLogManager()
.log(Level.ERROR.ordinal(),
new Date(),
- "[" + e.getClass().getSimpleName() + "]"
- + e.code().toString() + "-"
- + e.message());
+ Utility.getSimulatorErrorString(eInner,
+ null));
}
+ progressMonitor.worked(1);
}
+ } catch (SimulatorException eOuter) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(), new Date(),
+ Utility.getSimulatorErrorString(eOuter, null));
+ throw eOuter;
}
+ return resultSet;
}
- public void deleteResourceByType(final String resourceType) {
- if (null != resourceType) {
- new Thread() {
- @Override
- public void run() {
- // Unregister the resources from the platform
- deleteResource(resourceType);
-
- // Delete from the local data structure
- deleteLocalResourceDetails(resourceType, null);
-
- // Notify the UI listener for removing this resource from UI
- resourceDeletedUINotification();
-
- if (null != currentResourceInSelection
- && resourceType.equals(currentResourceInSelection
- .getResourceType())) {
- // Listeners might query the resource being deleted if
- // exists. So set the currently selection to null.
- setCurrentResourceInSelection(null);
-
- // Notify all observers for resource selection change
- // event
- resourceSelectionChangedUINotification();
- }
- }
- }.start();
+ public List<Resource> getResourceList() {
+ List<Resource> resourceList = data.getResources();
+ if (null == resourceList) {
+ return null;
}
+ // Sort the list
+ Collections.sort(resourceList, Utility.resourceComparator);
+
+ return resourceList;
}
- private void deleteResource(String resourceType) {
- if (null != resourceType) {
- try {
- SimulatorManager.deleteResources(resourceType);
- } catch (SimulatorException e) {
- Activator
- .getDefault()
- .getLogManager()
- .log(Level.ERROR.ordinal(),
- new Date(),
- "[" + e.getClass().getSimpleName() + "]"
- + e.code().toString() + "-"
- + e.message());
+ public List<SingleResource> getSingleResourceList() {
+ List<SingleResource> resourceList = data.getSingleResources();
+ if (null == resourceList) {
+ return null;
+ }
+ // Sort the list
+ Collections.sort(resourceList, Utility.singleResourceComparator);
+
+ return resourceList;
+ }
+
+ public void removeSingleResources(Set<SingleResource> resources)
+ throws SimulatorException {
+ if (null == resources) {
+ return;
+ }
+ Iterator<SingleResource> itr = resources.iterator();
+ while (itr.hasNext()) {
+ removeResource(itr.next());
+ }
+ }
+
+ public void removeResource(Resource res) throws SimulatorException {
+ // Unregister the resource from the platform.
+ SimulatorResource simRes = res.getSimulatorResource();
+ try {
+ simRes.stop();
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(), new Date(),
+ Utility.getSimulatorErrorString(e, null));
+ throw e;
+ }
+
+ // Delete this resource
+ data.deleteResource(res);
+ }
+
+ public boolean isUriUnique(List<MetaProperty> properties) {
+ if (null == properties) {
+ return false;
+ }
+ MetaProperty prop;
+ Iterator<MetaProperty> itr = properties.iterator();
+ while (itr.hasNext()) {
+ prop = itr.next();
+ if (prop.getPropName().equals(Constants.RESOURCE_URI)) {
+ String uri = prop.getPropValue();
+ return !data.isResourceExist(uri);
}
}
+ return false;
}
- public void deleteAllResources() {
+ public void resourceSelectionChanged(final Resource selectedResource) {
new Thread() {
@Override
public void run() {
- // Unregister the resources from the platform
- deleteResource();
-
- // Delete from the local data structure
- deleteLocalResourceDetails(null, null);
+ if (null != selectedResource) {
+ setCurrentResourceInSelection(selectedResource);
+ } else {
+ setCurrentResourceInSelection(null);
+ }
+ // Notify all observers for resource selection change event
+ UiListenerHandler.getInstance()
+ .resourceSelectionChangedUINotification(
+ selectedResource);
+ }
+ }.start();
+ }
- // Notify the UI listener for removing this resource from UI
- resourceDeletedUINotification();
+ public List<MetaProperty> getMetaProperties(Resource resource) {
+ if (null != resource) {
+ String propName;
+ String propValue;
- // Listeners might query the resource being deleted if exists.
- // So set the currently selection to null.
- setCurrentResourceInSelection(null);
+ List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
- // Notify all observers for resource selection change event
- resourceSelectionChangedUINotification();
+ for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
+ propName = Constants.META_PROPERTIES[index];
+ if (propName.equals(Constants.RESOURCE_NAME)) {
+ propValue = resource.getResourceName();
+ } else if (propName.equals(Constants.RESOURCE_URI)) {
+ propValue = resource.getResourceURI();
+ } else if (propName.equals(Constants.RESOURCE_TYPE)) {
+ propValue = resource.getResourceType();
+ } else if (propName.equals(Constants.INTERFACE_TYPES)) {
+ Set<String> ifTypes = resource.getResourceInterfaces();
+ if (null != ifTypes && !ifTypes.isEmpty()) {
+ propValue = "";
+ Iterator<String> itr = ifTypes.iterator();
+ while (itr.hasNext()) {
+ propValue += itr.next();
+ if (itr.hasNext()) {
+ propValue += ", ";
+ }
+ }
+ } else {
+ propValue = null;
+ }
+ } else {
+ propValue = null;
+ }
+ if (null != propValue) {
+ metaPropertyList.add(new MetaProperty(propName, propValue));
+ }
}
- }.start();
+ return metaPropertyList;
+ }
+ return null;
}
- private void deleteResource() {
+ public boolean startResource(Resource resource) throws SimulatorException {
+ if (null == resource) {
+ return false;
+ }
+ SimulatorResource server = resource.getSimulatorResource();
+ if (null == server) {
+ return false;
+ }
try {
- SimulatorManager.deleteResources(null);
+ server.start();
+ resource.setStarted(true);
} catch (SimulatorException e) {
Activator
.getDefault()
.getLogManager()
.log(Level.ERROR.ordinal(),
new Date(),
- "[" + e.getClass().getSimpleName() + "]"
- + e.code().toString() + "-" + e.message());
+ "There is an error while starting the resource.\n"
+ + Utility.getSimulatorErrorString(e, null));
+ throw e;
}
+ return true;
}
- private void deleteLocalResourceDetails(String resourceType,
- String resourceURI) {
- if (null != resourceType && null != resourceURI) {
- removeResourceFromMap(resourceType, resourceURI);
- removeResourceUriFromOrderedMap(resourceType, resourceURI);
- } else {
- synchronized (resourceMap) {
- if (null != resourceType) {
- removeResourceUriFromOrderedMap(resourceType, null);
- resourceMap.remove(resourceType);
- } else {
- resourceMap.clear();
- removeResourceUriFromOrderedMap(null, null);
- }
- }
+ public boolean stopResource(Resource resource) throws SimulatorException {
+ if (null == resource) {
+ return false;
+ }
+ SimulatorResource server = resource.getSimulatorResource();
+ if (null == server) {
+ return false;
}
+ try {
+ server.stop();
+ resource.setStarted(false);
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(),
+ new Date(),
+ "There is an error while stopping the resource.\n"
+ + Utility.getSimulatorErrorString(e, null));
+ throw e;
+ }
+ return true;
}
- private void resourceCreatedUINotification() {
- synchronized (resourceListChangedUIListeners) {
- if (resourceListChangedUIListeners.size() > 0) {
- IResourceListChangedUIListener listener;
- Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
- .iterator();
- while (listenerItr.hasNext()) {
- listener = listenerItr.next();
- if (null != listener) {
- listener.onResourceCreation();
- }
- }
- }
+ public boolean changeResourceName(Resource resource, String newName)
+ throws SimulatorException {
+ if (null == resource || null == newName) {
+ return false;
}
- }
- private void resourceDeletedUINotification() {
- synchronized (resourceListChangedUIListeners) {
- if (resourceListChangedUIListeners.size() > 0) {
- IResourceListChangedUIListener listener;
- Iterator<IResourceListChangedUIListener> listenerItr = resourceListChangedUIListeners
- .iterator();
- while (listenerItr.hasNext()) {
- listener = listenerItr.next();
- if (null != listener) {
- listener.onResourceDeletion();
- }
- }
- }
+ if (!stopResource(resource)) {
+ return false;
}
- }
- private void resourceSelectionChangedUINotification() {
- synchronized (resourceSelectionChangedUIListeners) {
- if (resourceSelectionChangedUIListeners.size() > 0) {
- IResourceSelectionChangedUIListener listener;
- Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
- .iterator();
- while (listenerItr.hasNext()) {
- listener = listenerItr.next();
- if (null != listener) {
- listener.onResourceSelectionChange();
- }
- }
- }
+ SimulatorResource server = resource.getSimulatorResource();
+ try {
+ server.setName(newName);
+ resource.setResourceName(newName);
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(),
+ new Date(),
+ "There is an error while changing the resource name.\n"
+ + Utility.getSimulatorErrorString(e, null));
+ throw e;
}
- }
- private void resourceModelChangedUINotification(
- ModelChangeNotificationType notificationType, String resourceURI,
- Set<LocalResourceAttribute> valueChangeSet) {
- synchronized (resourceModelChangedUIListeners) {
- if (resourceModelChangedUIListeners.size() > 0
- && notificationType != ModelChangeNotificationType.NONE
- && null != resourceURI) {
- IResourceModelChangedUIListener listener;
- Iterator<IResourceModelChangedUIListener> listenerItr = resourceModelChangedUIListeners
- .iterator();
- while (listenerItr.hasNext()) {
- listener = listenerItr.next();
- if (null != listener) {
- listener.onResourceModelChange(notificationType,
- resourceURI, valueChangeSet);
- }
- }
- }
+ if (!startResource(resource)) {
+ return false;
}
+
+ return true;
}
- private void resourceAutomationStartedUINotification(String resourceURI) {
- synchronized (automationUIListeners) {
- if (automationUIListeners.size() > 0 && null != resourceURI) {
- IAutomationUIListener listener;
- Iterator<IAutomationUIListener> listenerItr = automationUIListeners
- .iterator();
- while (listenerItr.hasNext()) {
- listener = listenerItr.next();
- if (null != listener) {
- listener.onResourceAutomationStart(resourceURI);
- }
- }
- }
+ public boolean changeResourceURI(Resource resource, String newURI)
+ throws SimulatorException {
+ if (null == resource || null == newURI) {
+ return false;
}
- }
- private void automationCompleteUINotification(String resourceURI,
- String attName) {
- synchronized (automationUIListeners) {
- if (automationUIListeners.size() > 0 && null != resourceURI) {
- IAutomationUIListener listener;
- Iterator<IAutomationUIListener> listenerItr = automationUIListeners
- .iterator();
- while (listenerItr.hasNext()) {
- listener = listenerItr.next();
- if (null != listener) {
- listener.onAutomationComplete(resourceURI, attName);
- }
- }
- }
+ if (!stopResource(resource)) {
+ return false;
}
- }
- private void observerListChangedUINotification(String resourceURI) {
- synchronized (observerUIListeners) {
- if (observerUIListeners.size() > 0 && null != resourceURI) {
- IObserverListChangedUIListener listener;
- Iterator<IObserverListChangedUIListener> listenerItr = observerUIListeners
- .iterator();
- while (listenerItr.hasNext()) {
- listener = listenerItr.next();
- if (null != listener) {
- listener.onObserverListChanged(resourceURI);
- }
- }
+ String curURI = resource.getResourceURI();
+ setResourceURI(resource, newURI);
+
+ try {
+ if (!startResource(resource)) {
+ return false;
}
+ } catch (SimulatorException e) {
+ setResourceURI(resource, curURI);
}
+
+ return true;
}
- public List<String> getResourceTypeList() {
- List<String> typeList = null;
- synchronized (resourceMap) {
- if (resourceMap.size() > 0) {
- typeList = new ArrayList<String>();
- Set<String> typeSet = resourceMap.keySet();
- Iterator<String> typeItr = typeSet.iterator();
- while (typeItr.hasNext()) {
- typeList.add(typeItr.next());
- }
- }
+ public void setResourceURI(Resource resource, String newURI)
+ throws SimulatorException {
+ String curURI = resource.getResourceURI();
+ SimulatorResource server = resource.getSimulatorResource();
+ try {
+ server.setURI(newURI);
+ data.changeResourceURI(resource, curURI, newURI);
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(),
+ new Date(),
+ "There is an error while changing the resource URI.\n"
+ + Utility.getSimulatorErrorString(e, null));
+ throw e;
}
- return typeList;
}
- public boolean isTypeExist(String resType) {
- synchronized (resourceMap) {
- if (resourceMap.containsKey(resType)) {
- return true;
+ public boolean updateResourceProperties(Resource resource,
+ List<MetaProperty> properties, boolean uriChanged,
+ boolean nameChanged) throws SimulatorException {
+ if (null == resource || null == properties) {
+ return false;
+ }
+
+ // Updating the properties
+ Iterator<MetaProperty> itr = properties.iterator();
+ MetaProperty property;
+ String propName;
+ String propValue;
+ String resName = null;
+ String resURI = null;
+ while (itr.hasNext()) {
+ property = itr.next();
+ if (null == property) {
+ continue;
+ }
+ propName = property.getPropName();
+ propValue = property.getPropValue();
+ if (propName.equals(Constants.RESOURCE_NAME)) {
+ resName = propValue;
+ } else if (propName.equals(Constants.RESOURCE_URI)) {
+ resURI = propValue;
}
}
- return false;
- }
- public List<String> getURIList() {
- List<String> list = null;
- synchronized (orderedResourceUriMap) {
- Set<String> typeSet = orderedResourceUriMap.keySet();
- List<String> typeList = Utility.convertSetToList(typeSet);
- if (null == typeList || typeList.size() < 1) {
- return null;
+ if (nameChanged) {
+ if (!changeResourceName(resource, resName)) {
+ return false;
}
- list = new ArrayList<String>();
- // Sort the types
- Collections.sort(typeList);
+ // Notify UI Listeners
+ UiListenerHandler.getInstance().propertiesChangedUINotification(
+ Resource.class);
+ }
- // Add all URIs to the output list
- Iterator<String> typeItr = typeList.iterator();
- while (typeItr.hasNext()) {
- List<String> l = orderedResourceUriMap.get(typeItr.next());
- if (null != l) {
- list.addAll(l);
- }
+ if (uriChanged) {
+ if (!changeResourceURI(resource, resURI)) {
+ return false;
}
}
- return list;
- }
- public void resourceSelectionChanged(final String selectedItem) {
- new Thread() {
- @Override
- public void run() {
- // Check whether the item selected is a resource or resource
- // category
- if (isTypeExist(selectedItem)) {
- // Given item is a resource Type
- setCurrentResourceInSelection(null);
- } else {
- // Given item is a resource URI
- SimulatorResource resource = getSimulatorResourceByURI(selectedItem);
- if (null != resource) {
- setCurrentResourceInSelection(resource);
- } else {
- setCurrentResourceInSelection(null);
- }
- }
- // Notify all observers for resource selection change event
- resourceSelectionChangedUINotification();
- }
- }.start();
+ return true;
}
- public List<MetaProperty> getMetaProperties(SimulatorResource resource) {
- if (null != resource) {
- String propName;
- String propValue;
-
- List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
-
- for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
- propName = Constants.META_PROPERTIES[index];
- if (propName.equals(Constants.RESOURCE_URI)) {
- propValue = resource.getResourceURI();
- } else if (propName.equals(Constants.RESOURCE_TYPE)) {
- propValue = resource.getResourceType();
- } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
- // TODO: Temporarily ignoring till the implementation.
- propValue = null;
- } else {
- propValue = null;
- }
- if (null != propValue) {
- metaPropertyList.add(new MetaProperty(propName, propValue));
- }
+ public boolean updateResourceInterfaces(Resource resource,
+ Set<String> newIfSet) throws SimulatorException {
+ if (null == resource || null == newIfSet || newIfSet.isEmpty()) {
+ return false;
+ }
+ SimulatorResource jResource = resource.getSimulatorResource();
+ if (null == jResource) {
+ return false;
+ }
+ Set<String> curIfSet = resource.getResourceInterfaces();
+ Iterator<String> itr = curIfSet.iterator();
+ String interfaceType;
+ boolean resourceRestartRequired = false;
+ while (itr.hasNext()) {
+ interfaceType = itr.next();
+ if (!newIfSet.contains(interfaceType)) {
+ resourceRestartRequired = true;
+ break;
}
-
- return metaPropertyList;
}
- return null;
- }
- public List<LocalResourceAttribute> getAttributes(SimulatorResource resource) {
- List<LocalResourceAttribute> attList = null;
- if (null != resource) {
- Map<String, LocalResourceAttribute> attMap = resource
- .getResourceAttributesMap();
- if (null != attMap && attMap.size() > 0) {
- attList = new ArrayList<LocalResourceAttribute>();
- Set<String> attNameSet = attMap.keySet();
- String attName;
- LocalResourceAttribute attribute;
- Iterator<String> attNameItr = attNameSet.iterator();
- while (attNameItr.hasNext()) {
- attName = attNameItr.next();
- attribute = attMap.get(attName);
- if (null != attribute) {
- attList.add(attribute);
+ try {
+ // As there is no support from native layer for interface removal,
+ // supporting it from the simulator requires restarting the resource
+ // if any existing interfaces are removed.
+
+ if (resourceRestartRequired) {
+ stopResource(resource);
+ jResource.setInterface(Utility
+ .convertSetToVectorString(newIfSet));
+ startResource(resource);
+ } else {
+ // Existing interfaces are not removed.
+ itr = newIfSet.iterator();
+ while (itr.hasNext()) {
+ interfaceType = itr.next();
+ if (!curIfSet.contains(interfaceType)) {
+ jResource.addInterface(interfaceType);
}
}
}
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(),
+ new Date(),
+ "There is an error while changing the interface types."
+ + Utility.getSimulatorErrorString(e, null));
+ throw e;
}
- return attList;
+
+ // Set the resource interfaces.
+ resource.setResourceInterfaces(newIfSet);
+
+ return true;
}
- public void attributeValueUpdated(SimulatorResource resource,
- String attributeName, String value) {
+ public boolean attributeValueUpdated(SingleResource resource,
+ String attributeName, AttributeValue value) {
if (null != resource && null != attributeName && null != value) {
- SimulatorResourceServer server = resource.getResourceServer();
- if (null != server) {
- LocalResourceAttribute att = resource
- .getAttribute(attributeName);
- if (null == att) {
- return;
- }
- Type baseType = att.getAttValBaseType();
+ SimulatorSingleResource simRes = (SimulatorSingleResource) resource
+ .getSimulatorResource();
+ if (null != simRes) {
try {
- if (baseType == Type.STRING) {
- server.updateAttributeString(attributeName, value);
- } else if (baseType == Type.INT) {
- int val;
- try {
- val = Integer.parseInt(value);
- } catch (NumberFormatException nfe) {
- return;
- }
- server.updateAttributeInteger(attributeName, val);
- } else if (baseType == Type.DOUBLE) {
- double val;
- try {
- val = Double.parseDouble(value);
- } catch (NumberFormatException nfe) {
- return;
- }
- server.updateAttributeDouble(attributeName, val);
- } else if (baseType == Type.BOOL) {
- boolean val;
- val = Boolean.parseBoolean(value);
- server.updateAttributeBoolean(attributeName, val);
- }
+ simRes.updateAttribute(attributeName, value);
+ return true;
} catch (SimulatorException e) {
Activator
.getDefault()
.getLogManager()
- .log(Level.ERROR.ordinal(),
- new Date(),
- "[" + e.getClass().getSimpleName() + "]"
- + e.code().toString() + "-"
- + e.message());
+ .log(Level.ERROR.ordinal(), new Date(),
+ Utility.getSimulatorErrorString(e, null));
}
}
}
+ return false;
}
- private ModelChangeNotificationType compareAndUpdateLocalAttributes(
- Map<String, LocalResourceAttribute> resourceAttributeMapOld,
- Map<String, LocalResourceAttribute> resourceAttributeMapNew,
- Set<LocalResourceAttribute> valueChangeSet) {
- ModelChangeNotificationType notificationType = ModelChangeNotificationType.NONE;
- if (null != resourceAttributeMapOld && null != resourceAttributeMapNew) {
- Set<String> oldMapKeySet = resourceAttributeMapOld.keySet();
- Iterator<String> attributeMapOldItr = oldMapKeySet.iterator();
- String attName;
- LocalResourceAttribute attributeOld;
- LocalResourceAttribute attributeNew;
- Object attValueOld;
- Object attValueNew;
- String oldValueStr;
- String newValueStr;
- while (attributeMapOldItr.hasNext()) {
- attName = attributeMapOldItr.next();
- if (resourceAttributeMapNew.containsKey(attName)) {
- attributeOld = resourceAttributeMapOld.get(attName);
- attributeNew = resourceAttributeMapNew.get(attName);
- // Copy the attribute value from new to old if the value
- // has been changed
- // Comparing only the attribute's value considering the
- // fact that only the value can be changed
- if (null != attributeOld && null != attributeNew) {
- attValueOld = attributeOld.getAttributeValue();
- attValueNew = attributeNew.getAttributeValue();
-
- oldValueStr = String.valueOf(attValueOld);
- newValueStr = String.valueOf(attValueNew);
-
- if (null != oldValueStr && null != newValueStr) {
- if (!oldValueStr.equals(newValueStr)) {
- attributeOld.setAttributeValue(attValueNew);
- notificationType = ModelChangeNotificationType.ATTRIBUTE_VALUE_CHANGED;
- valueChangeSet.add(attributeOld);
- }
- }
+ public boolean isResourceStarted(Resource resource) {
+ if (null == resource) {
+ return false;
+ }
+ return resource.isStarted();
+ }
+
+ public boolean isPropertyValueInvalid(Resource resource,
+ List<MetaProperty> properties, String propName) {
+ if (null == resource || null == properties || null == propName) {
+ return false;
+ }
+ boolean invalid = false;
+ MetaProperty prop;
+ Iterator<MetaProperty> itr = properties.iterator();
+ while (itr.hasNext()) {
+ prop = itr.next();
+ if (prop.getPropName().equals(propName)) {
+ String value = prop.getPropValue();
+ if (propName.equals(Constants.RESOURCE_URI)) {
+ if (!Utility.isUriValid(value)) {
+ invalid = true;
}
- resourceAttributeMapNew.remove(attName);
} else {
- // Attribute doesn't exist in the new model. Hence
- // removing it from the model.
- resourceAttributeMapOld.remove(attName);
- notificationType = ModelChangeNotificationType.ATTRIBUTE_REMOVED;
+ if (null == value || value.trim().isEmpty()) {
+ invalid = true;
+ }
}
}
- // Check for new attributes in the new model
- if (resourceAttributeMapNew.size() > 0) {
- Set<String> remainingAttSet = resourceAttributeMapNew.keySet();
- Iterator<String> remainingAttItr = remainingAttSet.iterator();
- LocalResourceAttribute attribute;
- while (remainingAttItr.hasNext()) {
- attName = remainingAttItr.next();
- if (null != attName) {
- attribute = resourceAttributeMapNew.get(attName);
- if (null != attribute) {
- resourceAttributeMapOld.put(attName, attribute);
- }
- }
+ }
+ return invalid;
+ }
+
+ public boolean isPropValueChanged(Resource resource,
+ List<MetaProperty> properties, String propName) {
+ if (null == resource || null == properties || null == propName) {
+ return false;
+ }
+ boolean changed = false;
+ MetaProperty prop;
+ String oldValue;
+ Iterator<MetaProperty> itr = properties.iterator();
+ while (itr.hasNext()) {
+ prop = itr.next();
+ if (prop.getPropName().equals(propName)) {
+ oldValue = getPropertyValueFromResource(resource, propName);
+ if (null != oldValue && !prop.getPropValue().equals(oldValue)) {
+ changed = true;
}
- notificationType = ModelChangeNotificationType.ATTRIBUTE_ADDED;
+ break;
}
}
- return notificationType;
+ return changed;
}
- public int startAutomation(SimulatorResource resource,
- LocalResourceAttribute attribute, AutomationType autoType,
+ private String getPropertyValueFromResource(Resource resource,
+ String propName) {
+ if (null == resource || null == propName) {
+ return null;
+ }
+ if (propName.equals(Constants.RESOURCE_URI)) {
+ return resource.getResourceURI();
+ } else if (propName.equals(Constants.RESOURCE_NAME)) {
+ return resource.getResourceName();
+ } else if (propName.equals(Constants.RESOURCE_TYPE)) {
+ return resource.getResourceType();
+ } else {
+ return null;
+ }
+ }
+
+ public boolean isAttHasRangeOrAllowedValues(SimulatorResourceAttribute att) {
+ if (null == att) {
+ return false;
+ }
+ AttributeProperty prop = att.property();
+ if (null == prop) {
+ return false;
+ }
+
+ if (prop.getType() == Type.INTEGER) {
+ IntegerProperty intProperty = prop.asInteger();
+ if (null != intProperty) {
+ return (intProperty.hasRange() || intProperty.hasValues());
+ } else {
+ return false;
+ }
+ } else if (prop.getType() == Type.DOUBLE) {
+ DoubleProperty dblProperty = prop.asDouble();
+ if (null != dblProperty) {
+ return (dblProperty.hasRange() || dblProperty.hasValues());
+ } else {
+ return false;
+ }
+ } else if (prop.getType() == Type.STRING) {
+ StringProperty stringProperty = prop.asString();
+ if (null != stringProperty) {
+ return stringProperty.hasValues();
+ } else {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public int startAutomation(SingleResource resource,
+ AttributeElement attribute, AutoUpdateType autoType,
int autoUpdateInterval) {
int autoId = -1;
if (null != resource && null != attribute) {
- SimulatorResourceServer resourceServerN = resource
- .getResourceServer();
- if (null != resourceServerN) {
- String attrName = attribute.getAttributeName();
+ SimulatorSingleResource server = (SimulatorSingleResource) resource
+ .getSimulatorResource();
+ if (null != server) {
+ String attrName = attribute.getSimulatorResourceAttribute()
+ .name();
try {
- autoId = resourceServerN.startAttributeAutomation(attrName,
- autoType, automationListener);
+ autoId = server.startAttributeUpdation(attrName, autoType,
+ autoUpdateInterval, automationListener);
} catch (SimulatorException e) {
Activator
.getDefault()
return -1;
}
if (-1 != autoId) {
- attribute.setAutomationId(autoId);
- attribute.setAutomationType(autoType);
- attribute.setAutomationUpdateInterval(autoUpdateInterval);
- attribute.setAutomationInProgress(true);
+ attribute.setAutoUpdateId(autoId);
+ attribute.setAutoUpdateType(autoType);
+ attribute.setAutoUpdateInterval(autoUpdateInterval);
+ attribute.setAutoUpdateState(true);
resource.setAttributeAutomationInProgress(true);
- } else {
- attribute.setAutomationInProgress(false);
}
}
}
return autoId;
}
- public void stopAutomation(SimulatorResource resource,
- LocalResourceAttribute att, int autoId) {
+ public void stopAutomation(SingleResource resource, AttributeElement att,
+ int autoId) {
if (null != resource) {
- SimulatorResourceServer resourceServerN = resource
- .getResourceServer();
- if (null != resourceServerN) {
+ SimulatorSingleResource server = (SimulatorSingleResource) resource
+ .getSimulatorResource();
+ if (null != server) {
try {
- resourceServerN.stopAutomation(autoId);
+ server.stopUpdation(autoId);
} catch (SimulatorException e) {
Activator
.getDefault()
return;
}
// Change the automation status
- att.setAutomationInProgress(false);
+ att.setAutoUpdateState(false);
resource.setAttributeAutomationInProgress(isAnyAttributeInAutomation(resource));
}
}
}
- private boolean isAnyAttributeInAutomation(SimulatorResource resource) {
+ public boolean startResourceAutomationUIRequest(AutoUpdateType autoType,
+ int autoUpdateInterval, final SingleResource resource) {
if (null == resource) {
return false;
}
- Map<String, LocalResourceAttribute> attMap = resource
- .getResourceAttributesMap();
- if (null == attMap) {
- return false;
- }
boolean status = false;
- Set<String> keySet = attMap.keySet();
- Iterator<String> attItr = keySet.iterator();
- while (attItr.hasNext()) {
- LocalResourceAttribute attribute = attMap.get(attItr.next());
- if (attribute.isAutomationInProgress()) {
- status = true;
- break;
+ // Invoke the native automation method
+ SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
+ .getSimulatorResource();
+ if (null != resourceServer) {
+ int autoId = -1;
+ try {
+ autoId = resourceServer.startResourceUpdation(autoType,
+ autoUpdateInterval, automationListener);
+ } catch (SimulatorException e) {
+ Activator
+ .getDefault()
+ .getLogManager()
+ .log(Level.ERROR.ordinal(), new Date(),
+ Utility.getSimulatorErrorString(e, null));
+ autoId = -1;
}
- }
- return status;
- }
+ if (-1 != autoId) {
+ // Automation request accepted.
+ changeResourceLevelAutomationStatus(resource, true);
- private LocalResourceAttribute getAttributeWithGivenAutomationId(
- SimulatorResource resource, int automationId) {
- LocalResourceAttribute targetAttribute = null;
- if (null != resource) {
- Map<String, LocalResourceAttribute> attributeMap = resource
- .getResourceAttributesMap();
- if (null != attributeMap) {
- Set<String> attNameSet = attributeMap.keySet();
- Iterator<String> attNameItr = attNameSet.iterator();
- String attName;
- LocalResourceAttribute attribute;
- while (attNameItr.hasNext()) {
- attName = attNameItr.next();
- if (null != attName) {
- attribute = attributeMap.get(attName);
- if (null != attribute) {
- if (attribute.isAutomationInProgress()
- && (attribute.getAutomationId() == automationId)) {
- targetAttribute = attribute;
- break;
- }
- }
- }
- }
- }
- }
- return targetAttribute;
- }
+ resource.setAutomationId(autoId);
- public boolean startResourceAutomationUIRequest(final String resourceURI) {
- if (null == resourceURI) {
- return false;
- }
- boolean status = false;
- SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
- if (null != resource) {
- changeResourceLevelAutomationStatus(resource, true);
-
- // Invoke the native automation method
- SimulatorResourceServer resourceServer = resource
- .getResourceServer();
- if (null != resourceServer) {
- // TODO: Temporarily handling the normal one-time automation for
- // resources
- int autoId = -1;
- try {
- autoId = resourceServer.startResourceAutomation(
- AutomationType.NORMAL, automationListener);
- } catch (SimulatorException e) {
- Activator
- .getDefault()
- .getLogManager()
- .log(Level.ERROR.ordinal(),
- new Date(),
- "[" + e.getClass().getSimpleName() + "]"
- + e.code().toString() + "-"
- + e.message());
- autoId = -1;
- }
- if (-1 == autoId) {
- // Automation request failed and hence status is being
- // rolled back
- changeResourceLevelAutomationStatus(resource, false);
- } else {
- // Automation request accepted.
- resource.setAutomationId(autoId);
-
- // Notify the UI listeners in a different thread.
- Thread notifyThread = new Thread() {
- public void run() {
- resourceAutomationStartedUINotification(resourceURI);
- };
+ // Notify the UI listeners in a different thread.
+ Thread notifyThread = new Thread() {
+ public void run() {
+ UiListenerHandler.getInstance()
+ .resourceAutomationStartedUINotification(
+ resource);
};
- notifyThread.setPriority(Thread.MAX_PRIORITY);
- notifyThread.start();
+ };
+ notifyThread.setPriority(Thread.MAX_PRIORITY);
+ notifyThread.start();
- status = true;
- }
+ status = true;
}
}
return status;
}
- public boolean stopResourceAutomationUIRequest(final String resourceURI) {
- SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
+ public boolean stopResourceAutomationUIRequest(final SingleResource resource) {
if (null == resource) {
return false;
}
if (-1 == autoId) {
return false;
}
- SimulatorResourceServer resourceServer = resource.getResourceServer();
+ SimulatorSingleResource resourceServer = (SimulatorSingleResource) resource
+ .getSimulatorResource();
if (null == resourceServer) {
return false;
}
// Call native method
try {
- resourceServer.stopAutomation(autoId);
+ resourceServer.stopUpdation(autoId);
} catch (SimulatorException e) {
Activator
.getDefault()
.getLogManager()
- .log(Level.ERROR.ordinal(),
- new Date(),
- "[" + e.getClass().getSimpleName() + "]"
- + e.code().toString() + "-" + e.message());
+ .log(Level.ERROR.ordinal(), new Date(),
+ Utility.getSimulatorErrorString(e, null));
return false;
}
// Notify the UI Listeners. Invoke the automation complete callback.
Thread stopThread = new Thread() {
public void run() {
- automationListener.onAutomationComplete(resourceURI, autoId);
+ automationListener.onUpdateComplete(resource.getResourceURI(),
+ autoId);
}
};
stopThread.start();
return true;
}
+ private boolean isAnyAttributeInAutomation(SingleResource resource) {
+ if (null == resource || null == resource.getResourceRepresentation()) {
+ return false;
+ }
+
+ Map<String, AttributeElement> attributes = resource
+ .getResourceRepresentation().getAttributes();
+ if (null == attributes || 0 == attributes.size())
+ return false;
+
+ for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
+ if (entry.getValue().isAutoUpdateInProgress())
+ return true;
+ }
+
+ return false;
+ }
+
// Changes the automation state of the resource and its attributes
- private void changeResourceLevelAutomationStatus(
- SimulatorResource resource, boolean status) {
-
- Map<String, LocalResourceAttribute> attributeMap = resource
- .getResourceAttributesMap();
- if (null != attributeMap) {
- Set<String> attrNameSet = attributeMap.keySet();
- Iterator<String> attrNameItr = attrNameSet.iterator();
- String attrName;
- LocalResourceAttribute attribute;
- while (attrNameItr.hasNext()) {
- attrName = attrNameItr.next();
- attribute = attributeMap.get(attrName);
- if (null != attribute) {
- attribute.setAutomationInProgress(status);
- }
- }
+ private void changeResourceLevelAutomationStatus(SingleResource resource,
+ boolean status) {
+
+ if (null == resource || null == resource.getResourceRepresentation()) {
+ return;
}
+
+ Map<String, AttributeElement> attributes = resource
+ .getResourceRepresentation().getAttributes();
+ if (null == attributes || 0 == attributes.size())
+ return;
+
+ for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
+ entry.getValue().setAutoUpdateState(status);
+ }
+
resource.setResourceAutomationInProgress(status);
}
- public boolean isResourceAutomationStarted(String resourceURI) {
- boolean status = false;
- if (null == resourceURI) {
- return status;
+ private AttributeElement getAttributeWithGivenAutomationId(
+ SingleResource resource, int automationId) {
+ if (null == resource || null == resource.getResourceRepresentation()) {
+ return null;
}
- SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
- if (null != resource) {
- status = resource.isResourceAutomationInProgress();
+ Map<String, AttributeElement> attributes = resource
+ .getResourceRepresentation().getAttributes();
+ if (null == attributes || 0 == attributes.size())
+ return null;
+
+ for (Map.Entry<String, AttributeElement> entry : attributes.entrySet()) {
+ if (automationId == entry.getValue().getAutoUpdateId())
+ return entry.getValue();
}
- return status;
+
+ return null;
}
- public boolean isAttributeAutomationStarted(String resourceURI) {
+ public boolean isResourceAutomationStarted(SingleResource resource) {
boolean status = false;
- if (null == resourceURI) {
- return status;
- }
- SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
if (null != resource) {
- status = resource.isAttributeAutomationInProgress();
+ status = resource.isResourceAutomationInProgress();
}
return status;
}
- public LocalResourceAttribute getAttributeByResourceURI(String uri,
- String attName) {
- if (null == uri || null == attName) {
- return null;
- }
- SimulatorResource resource = getSimulatorResourceByURI(uri);
+ public boolean isAttributeAutomationStarted(SingleResource resource) {
if (null == resource) {
- return null;
- }
- Map<String, LocalResourceAttribute> attMap = resource
- .getResourceAttributesMap();
- if (null == attMap) {
- return null;
+ return false;
}
- return attMap.get(attName);
+ return resource.isAttributeAutomationInProgress();
}
- public void notifyObserverRequest(SimulatorResource res, int observerId) {
- if (null == res) {
+ public void notifyObserverRequest(Resource resource, int observerId) {
+ if (null == resource) {
return;
}
- SimulatorResourceServer server = res.getResourceServer();
- if (null == server) {
+ SimulatorResource simulatorResource = resource.getSimulatorResource();
+ if (null == simulatorResource) {
return;
}
try {
- server.notifyObserver(observerId);
+ simulatorResource.notifyObserver(observerId);
} catch (SimulatorException e) {
Activator
.getDefault()
.getLogManager()
- .log(Level.ERROR.ordinal(),
- new Date(),
- "[" + e.getClass().getSimpleName() + "]"
- + e.code().toString() + "-" + e.message());
+ .log(Level.ERROR.ordinal(), new Date(),
+ Utility.getSimulatorErrorString(e, null));
}
}
- public Image getImage(String resourceURI) {
- if (null == resourceURI) {
+ public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
+ if (null == att) {
return null;
}
- SimulatorResource resource = getSimulatorResourceByURI(resourceURI);
- if (null == resource) {
+
+ AttributeValue val = att.value();
+ if (null == val) {
+ return null;
+ }
+
+ TypeInfo type = val.typeInfo();
+
+ AttributeProperty prop = att.property();
+ if (null == prop) {
return null;
}
- return Activator.getDefault().getImageRegistry()
- .get(resource.getResourceType());
+
+ List<String> values = new ArrayList<String>();
+
+ if (type.mType != ValueType.RESOURCEMODEL) {
+ if (type.mType == ValueType.ARRAY) {
+ if (type.mDepth == 1) {
+ ArrayProperty arrayProperty = prop.asArray();
+ if (null != arrayProperty) {
+ AttributeProperty childProp = arrayProperty
+ .getElementProperty();
+ switch (childProp.getType()) {
+ case INTEGER:
+ IntegerProperty intProperty = childProp
+ .asInteger();
+ if (null != intProperty) {
+ values.addAll(getAllValues(intProperty,
+ Type.INTEGER));
+ }
+ break;
+ case DOUBLE:
+ DoubleProperty dblProperty = childProp
+ .asDouble();
+ if (null != dblProperty) {
+ values.addAll(getAllValues(dblProperty,
+ Type.DOUBLE));
+ }
+ break;
+ case BOOLEAN:
+ BooleanProperty boolProperty = childProp
+ .asBoolean();
+ if (null != boolProperty) {
+ values.addAll(getAllValues(boolProperty,
+ Type.BOOLEAN));
+ }
+ break;
+ case STRING:
+ StringProperty stringProperty = childProp
+ .asString();
+ if (null != stringProperty) {
+ values.addAll(getAllValues(stringProperty,
+ Type.STRING));
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ } else {
+ switch (prop.getType()) {
+ case INTEGER:
+ IntegerProperty intProperty = prop.asInteger();
+ if (null != intProperty) {
+ values.addAll(getAllValues(intProperty,
+ Type.INTEGER));
+ }
+ break;
+ case DOUBLE:
+ DoubleProperty dblProperty = prop.asDouble();
+ if (null != dblProperty) {
+ values.addAll(getAllValues(dblProperty, Type.DOUBLE));
+ }
+ break;
+ case BOOLEAN:
+ BooleanProperty boolProperty = prop.asBoolean();
+ if (null != boolProperty) {
+ values.addAll(getAllValues(boolProperty,
+ Type.BOOLEAN));
+ }
+ break;
+ case STRING:
+ StringProperty stringProperty = prop.asString();
+ if (null != stringProperty) {
+ values.addAll(getAllValues(stringProperty,
+ Type.STRING));
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ }
+
+ return values;
+ }
+
+ public List<String> getAllValues(IntegerProperty intProperty,
+ AttributeProperty.Type type) {
+ List<String> values = new ArrayList<String>();
+
+ if (intProperty.hasRange()) {
+ int min = (int) intProperty.min();
+ int max = (int) intProperty.max();
+ for (int iVal = min; iVal <= max; iVal++) {
+ values.add(String.valueOf(iVal));
+ }
+ } else if (intProperty.hasValues()) {
+ for (Integer val : intProperty.getValues()) {
+ values.add(String.valueOf(val));
+ }
+ } else {
+ // Adding the default value.
+ values.add(String.valueOf(intProperty.getDefaultValue()));
+ }
+ return values;
+ }
+
+ public List<String> getAllValues(DoubleProperty dblProperty,
+ AttributeProperty.Type type) {
+ List<String> values = new ArrayList<String>();
+
+ if (dblProperty.hasRange()) {
+ double min = (double) dblProperty.min();
+ double max = (double) dblProperty.max();
+ for (double iVal = min; iVal <= max; iVal = iVal + 1) {
+ values.add(String.valueOf(iVal));
+ }
+ } else if (dblProperty.hasValues()) {
+ for (Double val : dblProperty.getValues()) {
+ values.add(String.valueOf(val));
+ }
+ } else {
+ // Adding the default value.
+ values.add(String.valueOf(dblProperty.getDefaultValue()));
+ }
+ return values;
+ }
+
+ public List<String> getAllValues(BooleanProperty boolProperty,
+ AttributeProperty.Type type) {
+ List<String> values = new ArrayList<String>();
+ values.add("true");
+ values.add("false");
+ return values;
+ }
+
+ public List<String> getAllValues(StringProperty stringProperty,
+ AttributeProperty.Type type) {
+ List<String> values = new ArrayList<String>();
+
+ if (stringProperty.hasValues()) {
+ for (String val : stringProperty.getValues()) {
+ values.add(String.valueOf(val));
+ }
+ } else {
+ // Adding the default value.
+ values.add(String.valueOf(stringProperty.getDefaultValue()));
+ }
+ return values;
+ }
+
+ public int getResourceCount() {
+ return data.getResourceCount();
}
public void shutdown() {