2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package oic.simulator.clientcontroller.manager;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.LinkedList;
26 import java.util.List;
29 import java.util.Vector;
31 import oic.simulator.clientcontroller.Activator;
32 import oic.simulator.clientcontroller.remoteresource.DeviceAndPlatformInfo;
33 import oic.simulator.clientcontroller.remoteresource.MetaProperty;
34 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
35 import oic.simulator.clientcontroller.utils.AttributeValueStringConverter;
36 import oic.simulator.clientcontroller.utils.Constants;
37 import oic.simulator.clientcontroller.utils.Utility;
39 import org.oic.simulator.ArrayProperty;
40 import org.oic.simulator.AttributeProperty;
41 import org.oic.simulator.AttributeProperty.Type;
42 import org.oic.simulator.AttributeValue;
43 import org.oic.simulator.AttributeValue.TypeInfo;
44 import org.oic.simulator.AttributeValue.ValueType;
45 import org.oic.simulator.BooleanProperty;
46 import org.oic.simulator.DeviceInfo;
47 import org.oic.simulator.DeviceListener;
48 import org.oic.simulator.DoubleProperty;
49 import org.oic.simulator.ILogger.Level;
50 import org.oic.simulator.IntegerProperty;
51 import org.oic.simulator.PlatformInfo;
52 import org.oic.simulator.PlatformListener;
53 import org.oic.simulator.SimulatorException;
54 import org.oic.simulator.SimulatorManager;
55 import org.oic.simulator.SimulatorResourceAttribute;
56 import org.oic.simulator.SimulatorResourceModel;
57 import org.oic.simulator.SimulatorResult;
58 import org.oic.simulator.StringProperty;
59 import org.oic.simulator.client.FindResourceListener;
60 import org.oic.simulator.client.SimulatorRemoteResource;
61 import org.oic.simulator.client.SimulatorRemoteResource.GetResponseListener;
62 import org.oic.simulator.client.SimulatorRemoteResource.ObserveNotificationListener;
63 import org.oic.simulator.client.SimulatorRemoteResource.PostResponseListener;
64 import org.oic.simulator.client.SimulatorRemoteResource.PutResponseListener;
65 import org.oic.simulator.client.SimulatorRemoteResource.VerificationListener;
66 import org.oic.simulator.client.SimulatorRemoteResource.VerificationType;
69 * This class acts as an interface between the simulator java SDK and the
70 * various UI modules. It maintains all the details of resources and provides
71 * other UI modules with the information required. It also handles responses for
72 * find, GET, PUT, POST, Observe and automatic verification operations from
73 * native layer and propagates those events to the registered UI listeners.
75 public class ResourceManager {
77 private Set<String> lastKnownSearchTypes;
79 private RemoteResource currentResourceInSelection;
81 private FindResourceListener findResourceListener;
82 private GetResponseListener getListener;
83 private PutResponseListener putListener;
84 private PostResponseListener postListener;
85 private ObserveNotificationListener observeListener;
86 private VerificationListener verifyListener;
87 private DeviceListener deviceListener;
88 private PlatformListener platformListener;
90 private ResponseSynchronizerThread synchronizerThread;
92 private Thread threadHandle;
94 // Map with Server ID as key and the complete object as the value
95 private Map<String, RemoteResource> resourceMap;
96 private List<RemoteResource> favoriteResources;
97 // Maintaining a list of resource URIs for favorite resources feature.
98 private List<String> favoriteURIList;
100 // Maintaining a list of observed resource URIs.
101 private List<String> observedResourceURIList;
103 private Map<String, DeviceAndPlatformInfo> hostDeviceAndPlatformMap;
105 public ResourceManager() {
106 resourceMap = new HashMap<String, RemoteResource>();
107 favoriteResources = new ArrayList<RemoteResource>();
108 favoriteURIList = new ArrayList<String>();
109 observedResourceURIList = new ArrayList<String>();
110 hostDeviceAndPlatformMap = new HashMap<String, DeviceAndPlatformInfo>();
112 findResourceListener = new FindResourceListener() {
115 public void onResourceFound(final SimulatorRemoteResource resourceN) {
116 synchronizerThread.addToQueue(new Runnable() {
119 if (null == resourceN) {
123 // Ignore the response if the resource is a device or
125 Vector<String> resTypes = resourceN.getResourceTypes();
126 if (null != resTypes && resTypes.contains("oic.wk.d")
127 || resTypes.contains("oic.wk.p")) {
131 // If id is not available, then it cannot be added to
132 // the local map as null value should not be allowed as
134 String uid = resourceN.getId();
139 // If resource already exist, then ignore it.
140 boolean exist = isUidExist(uid);
145 RemoteResource resource = new RemoteResource();
146 resource.setRemoteResourceRef(resourceN);
148 String uri = resourceN.getURI();
149 if (null != uri && uri.trim().length() > 0) {
150 // Add resource to favorite list if it was in
151 // favorites list during find/refresh operation.
152 if (favoriteURIList.contains(uri)) {
153 addResourcetoFavorites(resource);
155 // Add resource to observed resources list if it was
156 // in observe list during find/refresh operation.
157 if (observedResourceURIList.contains(uri)) {
158 sendObserveRequest(resource);
164 .log(Level.INFO.ordinal(), new Date(),
165 "Found a resource without URI. Ignoring it.");
169 // Add the resource in local data structure
170 addResourceDetails(resource);
172 // Notify the UI listener
173 UiListenerHandler.getInstance()
174 .newResourceFoundNotification(resource);
179 .log(Level.INFO.ordinal(),
181 "Resource Found [" + resourceN.getURI()
184 // Send an initial GET request to get the resource
185 // attributes on an interface supported by the resource.
187 String ifType = null;
188 Vector<String> resInterfaces = resourceN
189 .getResourceInterfaces();
190 if (null != resInterfaces) {
191 ifType = resInterfaces.get(0);
193 resourceN.get(formQueryParameters(ifType, null),
195 } catch (SimulatorException e) {
199 .log(Level.ERROR.ordinal(),
201 Utility.getSimulatorErrorString(e,
205 // Get the device information
206 if (!isDeviceInfoExist(resourceN.getHost())) {
208 SimulatorManager.findDevices(resource
209 .getRemoteResourceRef().getHost(),
211 } catch (SimulatorException e) {
215 .log(Level.ERROR.ordinal(),
217 Utility.getSimulatorErrorString(
222 // Get the platform information
223 if (!isPlatformInfoExist(resourceN.getHost())) {
225 SimulatorManager.getPlatformInformation(
226 resource.getRemoteResourceRef()
227 .getHost(), platformListener);
228 } catch (SimulatorException e) {
232 .log(Level.ERROR.ordinal(),
234 Utility.getSimulatorErrorString(
243 // Listeners for device and platform information.
244 deviceListener = new DeviceListener() {
247 public void onDeviceFound(final String host,
248 final DeviceInfo deviceInfo) {
249 if (null == deviceInfo || null == host) {
252 synchronizerThread.addToQueue(new Runnable() {
255 synchronized (hostDeviceAndPlatformMap) {
256 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap
259 info = new DeviceAndPlatformInfo();
261 hostDeviceAndPlatformMap.put(host, info);
263 info.setDeviceInfo(deviceInfo);
266 // Notify UI listeners
267 UiListenerHandler.getInstance()
268 .deviceInfoReceivedNotification();
274 platformListener = new PlatformListener() {
277 public void onPlatformFound(final String host,
278 final PlatformInfo platformInfo) {
279 if (null == platformInfo || null == host) {
282 synchronizerThread.addToQueue(new Runnable() {
285 synchronized (hostDeviceAndPlatformMap) {
286 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap
289 info = new DeviceAndPlatformInfo();
291 hostDeviceAndPlatformMap.put(host, info);
293 info.setPlatformInfo(platformInfo);
296 // Notify UI listeners
297 UiListenerHandler.getInstance()
298 .platformInfoReceivedNotification();
304 getListener = new GetResponseListener() {
306 public void onGetResponse(final String uid,
307 final SimulatorResult result,
308 final SimulatorResourceModel resourceModelN) {
309 if (result != SimulatorResult.SIMULATOR_OK) {
313 .log(Level.ERROR.ordinal(),
317 + "] Received error response for GET request.");
320 synchronizerThread.addToQueue(new Runnable() {
323 // Handling the response which includes retrieving the
324 // attributes and updating the local model.
325 RemoteResource resource = handleResponse(uid,
327 if (null != resource) {
328 // Notify the UI listeners
329 UiListenerHandler.getInstance()
330 .getCompleteNotification(resource);
337 putListener = new PutResponseListener() {
340 public void onPutResponse(final String uid,
341 final SimulatorResult result,
342 final SimulatorResourceModel resourceModelN) {
343 if (result != SimulatorResult.SIMULATOR_OK) {
347 .log(Level.ERROR.ordinal(),
351 + "] Received error response for PUT request.");
354 synchronizerThread.addToQueue(new Thread() {
357 // Handling the response which includes retrieving the
358 // attributes and updating the local model.
359 RemoteResource resource = handleResponse(uid,
361 if (null != resource) {
362 // Notify the UI listeners
363 UiListenerHandler.getInstance()
364 .putCompleteNotification(resource);
371 postListener = new PostResponseListener() {
373 public void onPostResponse(final String uid,
374 final SimulatorResult result,
375 final SimulatorResourceModel resourceModelN) {
376 if (result != SimulatorResult.SIMULATOR_OK) {
380 .log(Level.ERROR.ordinal(),
384 + "] Received error response for POST request.");
387 synchronizerThread.addToQueue(new Runnable() {
390 // Handling the response which includes retrieving the
391 // attributes and updating the local model.
392 RemoteResource resource = handleResponse(uid,
394 if (null != resource) {
395 // Notify the UI listeners
396 UiListenerHandler.getInstance()
397 .postCompleteNotification(resource);
404 observeListener = new ObserveNotificationListener() {
407 public void onObserveNotification(final String uid,
408 final SimulatorResourceModel resourceModelN, final int seq) {
409 synchronizerThread.addToQueue(new Runnable() {
412 // Handling the response which includes retrieving the
413 // attributes and updating the local model.
414 RemoteResource resource = handleResponse(uid,
416 if (null != resource) {
417 // Notify the UI listeners
418 UiListenerHandler.getInstance()
419 .observeCompleteNotification(resource);
426 verifyListener = new VerificationListener() {
429 public void onVerificationStarted(final String uid, final int autoId) {
430 synchronizerThread.addToQueue(new Runnable() {
433 RemoteResource resource = getResource(uid);
434 if (null == resource) {
437 // Update the automation status.
438 resource.updateAutomationStatus(autoId, true);
440 int autoType = resource.getAutomationtype(autoId);
442 // Notify the listeners.
443 UiListenerHandler.getInstance()
444 .verificationStartedNotification(resource,
451 public void onVerificationCompleted(final String uid,
453 synchronizerThread.addToQueue(new Runnable() {
456 RemoteResource resource = getResource(uid);
457 if (null == resource) {
460 // Update the automation status.
461 resource.updateAutomationStatus(autoId, false);
463 int autoType = resource.getAutomationtype(autoId);
465 // Notify the listeners.
466 UiListenerHandler.getInstance()
467 .verificationCompletedNotification(resource,
474 public void onVerificationAborted(final String uid, final int autoId) {
475 synchronizerThread.addToQueue(new Runnable() {
478 RemoteResource resource = getResource(uid);
479 if (null == resource) {
482 // Update the automation status.
483 resource.updateAutomationStatus(autoId, false);
485 int autoType = resource.getAutomationtype(autoId);
487 // Notify the listeners.
488 UiListenerHandler.getInstance()
489 .verificationAbortedNotification(resource,
496 synchronizerThread = new ResponseSynchronizerThread();
497 threadHandle = new Thread(synchronizerThread);
498 threadHandle.setName("Simulator Client Controller Event Queue");
499 threadHandle.start();
502 private RemoteResource handleResponse(String uid,
503 SimulatorResourceModel resourceModelN) {
504 if (null == uid || null == resourceModelN) {
508 // Update the local model
509 RemoteResource resource;
510 resource = getResource(uid);
511 if (null == resource) {
515 resource.setResourceModelRef(resourceModelN);
516 resource.setResourceRepresentation(resourceModelN, false);
521 public synchronized boolean isDeviceInfoExist(String host) {
522 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap.get(host);
526 if (null == info.getDeviceInfo()) {
532 public synchronized boolean isPlatformInfoExist(String host) {
533 DeviceAndPlatformInfo info = hostDeviceAndPlatformMap.get(host);
537 if (null == info.getPlatformInfo()) {
543 private static class ResponseSynchronizerThread implements Runnable {
545 LinkedList<Runnable> responseQueue = new LinkedList<Runnable>();
549 while (!Thread.interrupted()) {
550 synchronized (this) {
552 while (responseQueue.isEmpty()) {
556 } catch (InterruptedException e) {
562 synchronized (this) {
563 thread = responseQueue.pop();
567 } catch (Exception e) {
568 if (e instanceof InterruptedException) {
576 public void addToQueue(Runnable event) {
577 synchronized (this) {
578 responseQueue.add(event);
584 public void addResourcetoFavorites(RemoteResource resource) {
585 if (null == resource) {
588 resource.setFavorite(true);
589 synchronized (favoriteResources) {
590 favoriteResources.add(resource);
594 public void removeResourceFromFavorites(RemoteResource resource) {
595 if (null == resource) {
598 resource.setFavorite(false);
599 synchronized (favoriteResources) {
600 favoriteResources.remove(resource);
604 public void addResourceURItoFavorites(RemoteResource resource) {
605 if (null == resource) {
608 synchronized (favoriteURIList) {
609 favoriteURIList.add(resource.getRemoteResourceRef().getURI());
613 public void removeResourceURIFromFavorites(RemoteResource resource) {
614 if (null == resource) {
617 synchronized (favoriteURIList) {
618 favoriteURIList.remove(resource.getRemoteResourceRef().getURI());
622 public void addObservedResourceURI(String resourceURI) {
623 synchronized (observedResourceURIList) {
624 if (!observedResourceURIList.contains(resourceURI))
625 observedResourceURIList.add(resourceURI);
629 public void removeObservedResourceURI(String resourceURI) {
630 synchronized (observedResourceURIList) {
631 observedResourceURIList.remove(resourceURI);
635 public boolean isResourceObserved(String resourceURI) {
636 boolean observed = false;
637 synchronized (observedResourceURIList) {
638 observed = observedResourceURIList.contains(resourceURI);
643 public synchronized RemoteResource getCurrentResourceInSelection() {
644 return currentResourceInSelection;
647 public synchronized void setCurrentResourceInSelection(
648 RemoteResource resource) {
649 this.currentResourceInSelection = resource;
652 private void addResourceDetails(RemoteResource remoteResource) {
653 if (null != remoteResource) {
654 synchronized (resourceMap) {
655 resourceMap.put(remoteResource.getRemoteResourceRef().getId(),
661 private boolean isUidExist(String uid) {
663 synchronized (resourceMap) {
664 exist = resourceMap.containsKey(uid);
669 private RemoteResource getResource(String uid) {
673 RemoteResource resource;
674 synchronized (resourceMap) {
675 resource = resourceMap.get(uid);
680 public synchronized Set<String> getLastKnownSearchTypes() {
681 return lastKnownSearchTypes;
684 public synchronized void setLastKnownSearchTypes(
685 Set<String> lastKnownSearchTypes) {
686 this.lastKnownSearchTypes = lastKnownSearchTypes;
689 public boolean findResourceRequest(Set<String> searchTypes) {
690 boolean result = false;
691 if (null == searchTypes || searchTypes.size() < 1) {
693 SimulatorManager.findResource(findResourceListener);
695 } catch (SimulatorException e) {
699 .log(Level.ERROR.ordinal(), new Date(),
700 Utility.getSimulatorErrorString(e, null));
703 Iterator<String> searchItr = searchTypes.iterator();
705 while (searchItr.hasNext()) {
706 rType = searchItr.next();
708 SimulatorManager.findResource(rType, findResourceListener);
710 } catch (SimulatorException e) {
714 .log(Level.ERROR.ordinal(), new Date(),
715 Utility.getSimulatorErrorString(e, null));
722 public void deleteResources(final Set<String> searchTypes) {
723 synchronized (resourceMap) {
724 if (null == resourceMap && resourceMap.isEmpty()) {
730 if (null == searchTypes || searchTypes.size() < 1) {
731 synchronized (resourceMap) {
732 // Stop observing all the resources
733 Iterator<String> itr = resourceMap.keySet().iterator();
734 while (itr.hasNext()) {
735 sendCancelObserveRequest(
736 resourceMap.get(itr.next()), false);
738 // Delete all cached details of resources
740 favoriteResources.clear();
742 // Clearing the device and platform information
743 hostDeviceAndPlatformMap.clear();
745 // Change the current resource in selection
746 setCurrentResourceInSelection(null);
747 UiListenerHandler.getInstance()
748 .resourceSelectionChangedUINotification(null);
750 Iterator<String> typeItr = searchTypes.iterator();
752 while (typeItr.hasNext()) {
753 resType = typeItr.next();
754 deleteResourcesByType(resType);
756 // Change the current resource in selection
757 updateCurrentResourceInSelection(searchTypes);
764 private void updateCurrentResourceInSelection(Set<String> searchTypes) {
765 if (null == searchTypes || searchTypes.size() < 1) {
768 RemoteResource resourceInSelection = getCurrentResourceInSelection();
769 if (null == resourceInSelection) {
772 List<String> typesOfSelection = resourceInSelection
773 .getRemoteResourceRef().getResourceTypes();
774 if (null == typesOfSelection || typesOfSelection.size() < 1) {
777 Iterator<String> itr = typesOfSelection.iterator();
779 while (itr.hasNext()) {
781 if (searchTypes.contains(type)) {
782 setCurrentResourceInSelection(null);
783 UiListenerHandler.getInstance()
784 .resourceSelectionChangedUINotification(null);
790 private void deleteResourcesByType(String resourceType) {
791 if (null == resourceType) {
794 synchronized (resourceMap) {
795 Set<String> keySet = resourceMap.keySet();
796 if (null == keySet) {
799 Iterator<String> keyItr = keySet.iterator();
801 RemoteResource resource;
804 while (keyItr.hasNext()) {
806 resource = resourceMap.get(uId);
807 if (null == resource) {
810 types = resource.getRemoteResourceRef().getResourceTypes();
812 exist = types.contains(resourceType);
814 // Cancel observing the resource.
815 sendCancelObserveRequest(resource, false);
816 // Remove the resource from favorites list.
817 removeResourceFromFavorites(resource);
818 // Remove the resource
820 // Remove the device and platform information
821 synchronized (hostDeviceAndPlatformMap) {
822 hostDeviceAndPlatformMap.remove(resource
823 .getRemoteResourceRef().getHost());
831 public void resourceSelectionChanged(final RemoteResource resource) {
835 setCurrentResourceInSelection(resource);
836 // Notify all observers for resource selection change event
837 UiListenerHandler.getInstance()
838 .resourceSelectionChangedUINotification(resource);
843 public List<MetaProperty> getDefaultProperties(RemoteResource resource) {
844 if (null != resource) {
848 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
850 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
851 propName = Constants.META_PROPERTIES[index];
852 if (propName.equals(Constants.RESOURCE_URI)) {
853 propValue = resource.getRemoteResourceRef().getURI();
854 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
855 propValue = resource.getRemoteResourceRef()
856 .getConnectivityType().toString();
857 } else if (propName.equals(Constants.ADDRESS)) {
858 propValue = resource.getRemoteResourceRef().getHost();
859 } else if (propName.equals(Constants.OBSERVABLE)) {
860 propValue = Utility.getObservableInString(resource
861 .getRemoteResourceRef().isObservable());
862 } else if (propName.equals(Constants.RESOURCE_TYPES)) {
863 Vector<String> resTypes = resource.getRemoteResourceRef()
865 if (null != resTypes && !resTypes.isEmpty()) {
867 Iterator<String> itr = resTypes.iterator();
868 while (itr.hasNext()) {
869 propValue += itr.next();
875 propValue = Constants.NOT_AVAILABLE;
877 } else if (propName.equals(Constants.RESOURCE_INTERFACES)) {
878 Vector<String> interfaces = resource.getRemoteResourceRef()
879 .getResourceInterfaces();
880 if (null != interfaces && !interfaces.isEmpty()) {
882 Iterator<String> itr = interfaces.iterator();
883 while (itr.hasNext()) {
884 propValue += itr.next();
890 propValue = Constants.NOT_AVAILABLE;
895 if (null != propValue) {
896 metaPropertyList.add(new MetaProperty(propName, propValue));
900 return metaPropertyList;
905 public List<MetaProperty> getDeviceProperties() {
906 if (null == currentResourceInSelection) {
910 SimulatorRemoteResource remoteResource = currentResourceInSelection
911 .getRemoteResourceRef();
912 if (null == remoteResource) {
916 String host = remoteResource.getHost();
921 if (!isDeviceInfoExist(host)) {
922 // Device Information
924 SimulatorManager.findDevices(host, deviceListener);
925 } catch (SimulatorException e) {
929 .log(Level.ERROR.ordinal(), new Date(),
930 Utility.getSimulatorErrorString(e, null));
935 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
936 synchronized (hostDeviceAndPlatformMap) {
937 DeviceInfo devInfo = hostDeviceAndPlatformMap.get(host)
939 metaProperties.add(new MetaProperty(Constants.DEVICE_ID, devInfo
941 metaProperties.add(new MetaProperty(Constants.DEVICE_NAME, devInfo
943 metaProperties.add(new MetaProperty(Constants.DEVICE_SPEC_VERSION,
944 devInfo.getSpecVersion()));
945 metaProperties.add(new MetaProperty(Constants.DEVICE_DMV_VERSION,
946 devInfo.getDataModelVersion()));
949 return metaProperties;
952 public List<MetaProperty> getPlatformProperties() {
953 if (null == currentResourceInSelection) {
957 SimulatorRemoteResource remoteResource = currentResourceInSelection
958 .getRemoteResourceRef();
959 if (null == remoteResource) {
963 String host = remoteResource.getHost();
968 if (!isPlatformInfoExist(host)) {
969 // Platform Information
971 SimulatorManager.getPlatformInformation(host, platformListener);
972 } catch (SimulatorException e) {
976 .log(Level.ERROR.ordinal(), new Date(),
977 Utility.getSimulatorErrorString(e, null));
982 List<MetaProperty> metaProperties = new ArrayList<MetaProperty>();
983 synchronized (hostDeviceAndPlatformMap) {
984 PlatformInfo platInfo = hostDeviceAndPlatformMap.get(host)
986 metaProperties.add(new MetaProperty(Constants.PLATFORM_ID, platInfo
988 metaProperties.add(new MetaProperty(
989 Constants.PLATFORM_MANUFAC_NAME, platInfo
990 .getManufacturerName()));
991 metaProperties.add(new MetaProperty(Constants.PLATFORM_MANUFAC_URL,
992 platInfo.getManufacturerUrl()));
993 metaProperties.add(new MetaProperty(Constants.PLATFORM_MODEL_NO,
994 platInfo.getModelNumber()));
995 metaProperties.add(new MetaProperty(
996 Constants.PLATFORM_DATE_OF_MANUFAC, platInfo
997 .getDateOfManufacture()));
998 metaProperties.add(new MetaProperty(Constants.PLATFORM_VERSION,
999 platInfo.getPlatformVersion()));
1000 metaProperties.add(new MetaProperty(Constants.PLATFORM_OS_VERSION,
1001 platInfo.getOperationSystemVersion()));
1002 metaProperties.add(new MetaProperty(
1003 Constants.PLATFORM_HARDWARE_VERSION, platInfo
1004 .getHardwareVersion()));
1005 metaProperties.add(new MetaProperty(
1006 Constants.PLATFORM_FIRMWARE_VERSION, platInfo
1007 .getFirmwareVersion()));
1008 metaProperties.add(new MetaProperty(Constants.PLATFORM_SUPPORT_URL,
1009 platInfo.getSupportUrl()));
1010 metaProperties.add(new MetaProperty(Constants.PLATFORM_SYSTEM_TIME,
1011 platInfo.getSystemTime()));
1013 return metaProperties;
1016 public Map<String, Boolean> getAutomationStatus(RemoteResource resource) {
1017 if (null == resource) {
1020 Map<String, Boolean> autoStatus = new HashMap<String, Boolean>();
1021 autoStatus.put(Constants.GET, resource.isGetAutomtnInProgress());
1022 autoStatus.put(Constants.PUT, resource.isPutAutomtnInProgress());
1023 autoStatus.put(Constants.POST, resource.isPostAutomtnInProgress());
1027 public List<RemoteResource> getResourceList() {
1028 List<RemoteResource> resourceList = new ArrayList<RemoteResource>();
1029 synchronized (resourceMap) {
1030 Set<String> idSet = resourceMap.keySet();
1031 Iterator<String> idItr = idSet.iterator();
1032 RemoteResource resource;
1033 while (idItr.hasNext()) {
1034 resource = resourceMap.get(idItr.next());
1035 if (null != resource) {
1036 resourceList.add(resource);
1041 Collections.sort(resourceList, new Comparator<RemoteResource>() {
1042 public int compare(RemoteResource res1, RemoteResource res2) {
1043 String s1 = res1.getRemoteResourceRef().getURI();
1044 String s2 = res2.getRemoteResourceRef().getURI();
1046 String s1Part = s1.replaceAll("\\d", "");
1047 String s2Part = s2.replaceAll("\\d", "");
1049 if (s1Part.equalsIgnoreCase(s2Part)) {
1050 return extractInt(s1) - extractInt(s2);
1052 return s1.compareTo(s2);
1055 int extractInt(String s) {
1056 String num = s.replaceAll("\\D", "");
1057 // return 0 if no digits found
1058 return num.isEmpty() ? 0 : Integer.parseInt(num);
1062 return resourceList;
1065 public List<RemoteResource> getFavResourceList() {
1066 List<RemoteResource> resourceList;
1067 synchronized (favoriteResources) {
1068 resourceList = new ArrayList<RemoteResource>(favoriteResources);
1070 return resourceList;
1073 public List<String> getAllValuesOfAttribute(SimulatorResourceAttribute att) {
1078 AttributeValue val = att.value();
1079 if (null == val || null == val.get()) {
1083 TypeInfo type = val.typeInfo();
1085 if (type.mType == ValueType.RESOURCEMODEL
1086 || type.mBaseType == ValueType.RESOURCEMODEL) {
1090 List<String> values = new ArrayList<String>();
1092 AttributeProperty prop = att.property();
1094 values.add(new AttributeValueStringConverter(val).toString());
1098 if (type.mType == ValueType.ARRAY) {
1099 if (type.mDepth == 1) {
1100 ArrayProperty arrayProperty = prop.asArray();
1101 if (null != arrayProperty) {
1102 AttributeProperty childProp = arrayProperty
1103 .getElementProperty();
1104 switch (childProp.getType()) {
1106 IntegerProperty intProperty = childProp.asInteger();
1107 if (null != intProperty) {
1108 values.addAll(getAllValues(intProperty,
1113 DoubleProperty dblProperty = childProp.asDouble();
1114 if (null != dblProperty) {
1115 values.addAll(getAllValues(dblProperty,
1120 BooleanProperty boolProperty = childProp
1122 if (null != boolProperty) {
1123 values.addAll(getAllValues(boolProperty,
1128 StringProperty stringProperty = childProp
1130 if (null != stringProperty) {
1131 values.addAll(getAllValues(stringProperty,
1141 switch (prop.getType()) {
1143 IntegerProperty intProperty = prop.asInteger();
1144 if (null != intProperty) {
1145 values.addAll(getAllValues(intProperty, Type.INTEGER));
1149 DoubleProperty dblProperty = prop.asDouble();
1150 if (null != dblProperty) {
1151 values.addAll(getAllValues(dblProperty, Type.DOUBLE));
1155 BooleanProperty boolProperty = prop.asBoolean();
1156 if (null != boolProperty) {
1157 values.addAll(getAllValues(boolProperty, Type.BOOLEAN));
1161 StringProperty stringProperty = prop.asString();
1162 if (null != stringProperty) {
1163 values.addAll(getAllValues(stringProperty, Type.STRING));
1174 public List<String> getAllValues(IntegerProperty intProperty,
1175 AttributeProperty.Type type) {
1176 List<String> values = new ArrayList<String>();
1178 if (intProperty.hasRange()) {
1179 int min = (int) intProperty.min();
1180 int max = (int) intProperty.max();
1181 for (int iVal = min; iVal <= max; iVal++) {
1182 values.add(String.valueOf(iVal));
1184 } else if (intProperty.hasValues()) {
1185 for (Integer val : intProperty.getValues()) {
1186 values.add(String.valueOf(val));
1189 // Adding the default value.
1190 values.add(String.valueOf(intProperty.getDefaultValue()));
1195 public List<String> getAllValues(DoubleProperty dblProperty,
1196 AttributeProperty.Type type) {
1197 List<String> values = new ArrayList<String>();
1199 if (dblProperty.hasRange()) {
1200 double min = (double) dblProperty.min();
1201 double max = (double) dblProperty.max();
1202 for (double iVal = min; iVal <= max; iVal = iVal + 1) {
1203 values.add(String.valueOf(iVal));
1205 } else if (dblProperty.hasValues()) {
1206 for (Double val : dblProperty.getValues()) {
1207 values.add(String.valueOf(val));
1210 // Adding the default value.
1211 values.add(String.valueOf(dblProperty.getDefaultValue()));
1216 public List<String> getAllValues(BooleanProperty boolProperty,
1217 AttributeProperty.Type type) {
1218 List<String> values = new ArrayList<String>();
1220 values.add("false");
1224 public List<String> getAllValues(StringProperty stringProperty,
1225 AttributeProperty.Type type) {
1226 List<String> values = new ArrayList<String>();
1228 if (stringProperty.hasValues()) {
1229 for (String val : stringProperty.getValues()) {
1230 values.add(String.valueOf(val));
1233 // Adding the default value.
1234 values.add(String.valueOf(stringProperty.getDefaultValue()));
1239 public void sendGetRequest(String ifType, String query,
1240 RemoteResource resource) {
1241 if (null == resource) {
1244 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1245 if (null == resourceN) {
1249 Map<String, String> queryParams = formQueryParameters(ifType, query);
1251 resourceN.get(queryParams, getListener);
1252 } catch (SimulatorException e) {
1256 .log(Level.ERROR.ordinal(), new Date(),
1257 Utility.getSimulatorErrorString(e, null));
1261 public void sendPutRequest(String ifType, RemoteResource resource,
1262 SimulatorResourceModel model) {
1263 if (null == resource || null == model) {
1266 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1267 if (null == resourceN) {
1270 Map<String, String> queryParams = formQueryParameters(ifType, null);
1272 resourceN.put(queryParams, model, putListener);
1273 } catch (Exception e) {
1275 addlInfo = "Invalid Attribute Value. Cannot send PUT request.";
1279 .log(Level.ERROR.ordinal(), new Date(),
1280 Utility.getSimulatorErrorString(e, addlInfo));
1284 public void sendPostRequest(String ifType, RemoteResource resource,
1285 SimulatorResourceModel model) {
1286 if (null == resource || null == model) {
1289 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1290 if (null == resourceN) {
1293 Map<String, String> queryParams = formQueryParameters(ifType, null);
1295 resourceN.post(queryParams, model, postListener);
1296 } catch (Exception e) {
1298 addlInfo = "Invalid Attribute Value. Cannot send POST request.";
1302 .log(Level.ERROR.ordinal(), new Date(),
1303 Utility.getSimulatorErrorString(e, addlInfo));
1307 public boolean sendObserveRequest(RemoteResource resource) {
1308 if (null == resource) {
1311 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1312 if (null == resourceN) {
1316 resourceN.observe(observeListener);
1317 resource.setObserved(true);
1318 // Add observed resource URI to show the proper status after every
1319 // find/refresh operations.
1320 addObservedResourceURI(resource.getRemoteResourceRef().getURI());
1321 } catch (SimulatorException e) {
1325 .log(Level.ERROR.ordinal(), new Date(),
1326 Utility.getSimulatorErrorString(e, null));
1332 private Map<String, String> formQueryParameters(String ifType, String query) {
1333 Map<String, String> queryParams = new HashMap<String, String>();
1335 // Including the interface type, if given,
1336 if (null != ifType) {
1337 ifType = ifType.trim();
1338 if (ifType.length() > 0)
1339 queryParams.put("if", ifType);
1342 // Including other queries, if given.
1343 if (null != query) {
1344 query = query.trim();
1345 if (query.length() > 0) {
1346 // Parse the query parameters and fill the map.
1347 String queries[] = query.split(";");
1348 if (queries.length > 0) {
1349 for (String pair : queries) {
1350 String tok[] = pair.split("=");
1351 if (null != tok && tok.length == 2) {
1352 queryParams.put(tok[0].trim(), tok[1].trim());
1361 public boolean sendCancelObserveRequest(RemoteResource resource,
1362 boolean removeEntry) {
1363 if (null == resource || !resource.isObserved()) {
1366 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1367 if (null == resourceN) {
1371 resourceN.stopObserve();
1372 resource.setObserved(false);
1373 // Remove observed resource URI to show the proper status after
1374 // every find/refresh operations.
1376 removeObservedResourceURI(resource.getRemoteResourceRef()
1378 } catch (SimulatorException e) {
1382 .log(Level.ERROR.ordinal(), new Date(),
1383 Utility.getSimulatorErrorString(e, null));
1389 public void startAutomationRequest(VerificationType reqType,
1390 RemoteResource resource) {
1391 if (null == resource) {
1394 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1395 if (null == resourceN) {
1398 if (null == reqType) {
1403 autoId = resourceN.startVerification(reqType, verifyListener);
1405 if (reqType == VerificationType.GET) {
1406 resource.setGetAutomtnId(autoId);
1407 } else if (reqType == VerificationType.PUT) {
1408 resource.setPutAutomtnId(autoId);
1410 resource.setPostAutomtnId(autoId);
1416 .log(Level.INFO.ordinal(),
1418 "[" + reqType.toString()
1419 + "] Verification Started for \""
1420 + resourceN.getURI() + "\".");
1421 } catch (SimulatorException e) {
1425 .log(Level.ERROR.ordinal(), new Date(),
1426 Utility.getSimulatorErrorString(e, null));
1430 public void stopAutomationRequest(VerificationType reqType,
1431 RemoteResource resource) {
1432 if (null == resource) {
1435 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1436 if (null == resourceN) {
1440 if (reqType == VerificationType.GET) {
1441 resource.setGetAutomtnInProgress(false);
1442 autoId = resource.getGetAutomtnId();
1443 } else if (reqType == VerificationType.PUT) {
1444 resource.setPutAutomtnInProgress(false);
1445 autoId = resource.getPutAutomtnId();
1447 resource.setPostAutomtnInProgress(false);
1448 autoId = resource.getPostAutomtnId();
1451 resourceN.stopVerification(autoId);
1452 } catch (SimulatorException e) {
1456 .log(Level.ERROR.ordinal(), new Date(),
1457 Utility.getSimulatorErrorString(e, null));
1461 public boolean setConfigFilePath(RemoteResource resource,
1462 String configFilePath) throws SimulatorException {
1463 if (null == resource) {
1466 SimulatorRemoteResource resourceN = resource.getRemoteResourceRef();
1467 if (null == resourceN) {
1471 SimulatorResourceModel configuredResourceModel;
1472 configuredResourceModel = resourceN.setConfigInfo(configFilePath);
1473 if (null == configuredResourceModel) {
1477 // Store the resource model in the local cache
1479 * SimulatorResourceModel resourceModel = resource
1480 * .getResourceModelRef(); if (null != resourceModel) {
1481 * configuredResourceModel.update(resourceModel); }
1482 * resource.setResourceModelRef(configuredResourceModel);
1484 } catch (SimulatorException e) {
1488 .log(Level.ERROR.ordinal(), new Date(),
1489 Utility.getSimulatorErrorString(e, null));
1492 // Update the status
1493 resource.setConfigUploaded(true);
1495 // Notify the UI listeners
1496 UiListenerHandler.getInstance().configUploadedNotification(resource);
1501 public void shutdown() {