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;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.List;
31 import oic.simulator.clientcontroller.Activator;
32 import oic.simulator.clientcontroller.listener.IConfigurationUpload;
33 import oic.simulator.clientcontroller.listener.IFindResourceUIListener;
34 import oic.simulator.clientcontroller.listener.IGetUIListener;
35 import oic.simulator.clientcontroller.listener.IObserveUIListener;
36 import oic.simulator.clientcontroller.listener.IPostUIListener;
37 import oic.simulator.clientcontroller.listener.IPutUIListener;
38 import oic.simulator.clientcontroller.listener.IResourceSelectionChangedUIListener;
39 import oic.simulator.clientcontroller.listener.IVerificationUIListener;
40 import oic.simulator.clientcontroller.remoteresource.MetaProperty;
41 import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
42 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
43 import oic.simulator.clientcontroller.remoteresource.RemoteResourceAttribute;
44 import oic.simulator.clientcontroller.utils.Constants;
45 import oic.simulator.clientcontroller.utils.Utility;
47 import org.eclipse.jface.resource.ImageDescriptor;
48 import org.eclipse.swt.graphics.Image;
49 import org.oic.simulator.ILogger.Level;
50 import org.oic.simulator.ResourceAttribute;
51 import org.oic.simulator.ResourceAttribute.Range;
52 import org.oic.simulator.ResourceAttribute.Type;
53 import org.oic.simulator.SimulatorException;
54 import org.oic.simulator.SimulatorManager;
55 import org.oic.simulator.SimulatorResourceModel;
56 import org.oic.simulator.clientcontroller.IFindResourceListener;
57 import org.oic.simulator.clientcontroller.IGetListener;
58 import org.oic.simulator.clientcontroller.IObserveListener;
59 import org.oic.simulator.clientcontroller.IPostListener;
60 import org.oic.simulator.clientcontroller.IPutListener;
61 import org.oic.simulator.clientcontroller.IVerificationListener;
62 import org.oic.simulator.clientcontroller.SimulatorObserveType;
63 import org.oic.simulator.clientcontroller.SimulatorRemoteResource;
64 import org.oic.simulator.clientcontroller.SimulatorVerificationType;
67 * This class acts as an interface between the simulator java SDK and the
68 * various UI modules. It maintains all the details of resources and provides
69 * other UI modules with the information required. It also handles responses for
70 * find, GET, PUT, POST, Observe and automatic verification operations from
71 * native layer and propagates those events to the registered UI listeners.
73 public class ResourceManager {
75 private Set<String> lastKnownSearchTypes;
77 private RemoteResource currentResourceInSelection;
79 private IFindResourceListener findResourceListener;
80 private IGetListener getListener;
81 private IPutListener putListener;
82 private IPostListener postListener;
83 private IObserveListener observeListener;
84 private IVerificationListener verifyListener;
86 private ResponseSynchronizerThread synchronizerThread;
88 private Thread threadHandle;
90 private List<IFindResourceUIListener> findResourceUIListeners;
91 private List<IResourceSelectionChangedUIListener> resourceSelectionChangedUIListeners;
92 private List<IGetUIListener> getUIListeners;
93 private List<IPutUIListener> putUIListeners;
94 private List<IPostUIListener> postUIListeners;
95 private List<IObserveUIListener> observeUIListeners;
96 private List<IVerificationUIListener> verificationUIListeners;
97 private List<IConfigurationUpload> configUploadUIListeners;
99 // Map with Server ID as key and the complete object as the value
100 private Map<String, RemoteResource> resourceMap;
101 private List<RemoteResource> favoriteResources;
102 // Maintaining a list of resource URIs for favorite resources feature.
103 private List<String> favoriteURIList;
105 // Maintaining a list of observed resource URIs.
106 private List<String> observedResourceURIList;
108 public ResourceManager() {
109 resourceMap = new HashMap<String, RemoteResource>();
110 favoriteResources = new ArrayList<RemoteResource>();
111 favoriteURIList = new ArrayList<String>();
112 observedResourceURIList = new ArrayList<String>();
113 findResourceUIListeners = new ArrayList<IFindResourceUIListener>();
114 resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
115 getUIListeners = new ArrayList<IGetUIListener>();
116 putUIListeners = new ArrayList<IPutUIListener>();
117 postUIListeners = new ArrayList<IPostUIListener>();
118 observeUIListeners = new ArrayList<IObserveUIListener>();
119 verificationUIListeners = new ArrayList<IVerificationUIListener>();
120 configUploadUIListeners = new ArrayList<IConfigurationUpload>();
122 findResourceListener = new IFindResourceListener() {
125 public void onResourceCallback(
126 final SimulatorRemoteResource resourceN) {
127 synchronizerThread.addToQueue(new Runnable() {
130 System.out.println("onResourceCallback() entry");
131 if (null == resourceN) {
134 // If resource already exist, then ignore it.
135 String uid = resourceN.getId();
139 boolean exist = isUidExist(uid);
141 System.out.println("Duplicate resource found: ["
146 // Fetch the resource data
147 RemoteResource resource = fetchResourceDetails(resourceN);
148 if (null == resource) {
152 resource.setResource(resourceN);
154 String uri = resource.getResourceURI();
156 // Add resource to favorite list if it was in
157 // favorites list during find/refresh operation.
158 if (favoriteURIList.contains(uri)) {
159 addResourcetoFavorites(resource);
161 // Add resource to observed resources list if it was
162 // in observe list during find/refresh operation.
163 if (observedResourceURIList.contains(uri)) {
164 resource.setObserved(true);
168 // Add the resource in local data structure
169 addResourceDetails(resource);
171 // Notify the UI listener
172 newResourceFoundNotification(resource);
177 .log(Level.INFO.ordinal(),
180 + resource.getResourceURI()
183 // Send an initial GET request to get the resource
186 resourceN.get(null, getListener);
187 } catch (SimulatorException e) {
191 .log(Level.ERROR.ordinal(),
193 Utility.getSimulatorErrorString(e,
201 getListener = new IGetListener() {
203 public void onGetCompleted(final String uid,
204 final SimulatorResourceModel resourceModelN) {
205 synchronizerThread.addToQueue(new Runnable() {
208 // Handling the response which includes retrieving the
209 // attributes and updating the local model.
210 RemoteResource resource = handleResponse(uid,
212 if (null != resource) {
213 // Notify the UI listeners
214 getCompleteNotification(resource);
221 public void onGetFailed(Throwable th) {
222 synchronizerThread.addToQueue(new Runnable() {
230 putListener = new IPutListener() {
233 public void onPutCompleted(final String uid,
234 final SimulatorResourceModel resourceModelN) {
235 synchronizerThread.addToQueue(new Thread() {
238 // Handling the response which includes retrieving the
239 // attributes and updating the local model.
240 RemoteResource resource = handleResponse(uid,
242 if (null != resource) {
243 // Notify the UI listeners
244 putCompleteNotification(resource);
251 public void onPutFailed(Throwable th) {
252 synchronizerThread.addToQueue(new Runnable() {
260 postListener = new IPostListener() {
262 public void onPostCompleted(final String uid,
263 final SimulatorResourceModel resourceModelN) {
264 synchronizerThread.addToQueue(new Runnable() {
267 // Handling the response which includes retrieving the
268 // attributes and updating the local model.
269 RemoteResource resource = handleResponse(uid,
271 if (null != resource) {
272 // Notify the UI listeners
273 postCompleteNotification(resource);
280 public void onPostFailed(Throwable th) {
281 synchronizerThread.addToQueue(new Runnable() {
289 observeListener = new IObserveListener() {
292 public void onObserveCompleted(final String uid,
293 final SimulatorResourceModel resourceModelN, final int seq) {
294 System.out.println("ResourceManager: onObserveCallback()");
295 synchronizerThread.addToQueue(new Runnable() {
298 // Handling the response which includes retrieving the
299 // attributes and updating the local model.
300 RemoteResource resource = handleResponse(uid,
302 if (null != resource) {
303 // Notify the UI listeners
304 observeCompleteNotification(resource);
311 public void onObserveFailed(Throwable th) {
312 // TODO Auto-generated method stub
316 verifyListener = new IVerificationListener() {
319 public void onVerificationStarted(final String uid, final int autoId) {
320 System.out.println("onVefificationStarted: " + autoId);
321 synchronizerThread.addToQueue(new Runnable() {
324 RemoteResource resource = getResource(uid);
325 if (null == resource) {
328 // Update the automation status.
329 resource.updateAutomationStatus(autoId, true);
331 int autoType = resource.getAutomationtype(autoId);
333 // Notify the listeners.
334 verificationStartedNotification(resource, autoType);
340 public void onVerificationCompleted(final String uid,
342 System.out.println("onVefificationCompleted: " + autoId);
343 synchronizerThread.addToQueue(new Runnable() {
346 RemoteResource resource = getResource(uid);
347 if (null == resource) {
350 // Update the automation status.
351 resource.updateAutomationStatus(autoId, false);
353 int autoType = resource.getAutomationtype(autoId);
355 // Notify the listeners.
356 verificationCompletedNotification(resource, autoType);
362 public void onVerificationAborted(final String uid, final int autoId) {
363 System.out.println("onVefificationAborted: " + autoId);
364 synchronizerThread.addToQueue(new Runnable() {
367 RemoteResource resource = getResource(uid);
368 if (null == resource) {
371 // Update the automation status.
372 resource.updateAutomationStatus(autoId, false);
374 int autoType = resource.getAutomationtype(autoId);
376 // Notify the listeners.
377 verificationAbortedNotification(resource, autoType);
383 synchronizerThread = new ResponseSynchronizerThread();
384 threadHandle = new Thread(synchronizerThread);
385 threadHandle.setName("Simulator Client Controller Event Queue");
386 threadHandle.start();
389 private RemoteResource handleResponse(String uid,
390 SimulatorResourceModel resourceModelN) {
391 if (null == uid || null == resourceModelN) {
395 // Update the local model
396 RemoteResource resource;
397 resource = getResource(uid);
398 if (null == resource) {
402 resource.setResourceModel(resourceModelN);
403 Map<String, RemoteResourceAttribute> attributeMap = fetchResourceAttributesFromModel(resourceModelN);
404 resource.setResourceAttributesMap(attributeMap);
405 // TODO: Printing the values for debugging
406 if (null != attributeMap) {
407 RemoteResourceAttribute.printAttributes(attributeMap);
412 private static class ResponseSynchronizerThread implements Runnable {
414 LinkedList<Runnable> responseQueue = new LinkedList<Runnable>();
418 while (!Thread.interrupted()) {
419 synchronized (this) {
421 while (responseQueue.isEmpty()) {
425 } catch (InterruptedException e) {
431 synchronized (this) {
432 thread = responseQueue.pop();
436 } catch (Exception e) {
437 if (e instanceof InterruptedException) {
445 public void addToQueue(Runnable event) {
446 synchronized (this) {
447 responseQueue.add(event);
453 public void addResourceSelectionChangedUIListener(
454 IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
455 synchronized (resourceSelectionChangedUIListeners) {
456 resourceSelectionChangedUIListeners
457 .add(resourceSelectionChangedUIListener);
461 public void addGetUIListener(IGetUIListener getUIListener) {
462 synchronized (getUIListeners) {
463 getUIListeners.add(getUIListener);
467 public void addPutUIListener(IPutUIListener putUIListener) {
468 synchronized (putUIListeners) {
469 putUIListeners.add(putUIListener);
473 public void addPostUIListener(IPostUIListener postUIListener) {
474 synchronized (postUIListeners) {
475 postUIListeners.add(postUIListener);
479 public void addObserveUIListener(IObserveUIListener observeUIListener) {
480 synchronized (observeUIListeners) {
481 observeUIListeners.add(observeUIListener);
485 public void addVerificationUIListener(
486 IVerificationUIListener verificationUIListener) {
487 synchronized (verificationUIListeners) {
488 verificationUIListeners.add(verificationUIListener);
492 public void addConfigUploadUIListener(IConfigurationUpload configListener) {
493 synchronized (configUploadUIListeners) {
494 configUploadUIListeners.add(configListener);
498 public void removeResourceSelectionChangedUIListener(
499 IResourceSelectionChangedUIListener listener) {
500 synchronized (resourceSelectionChangedUIListeners) {
502 && resourceSelectionChangedUIListeners.size() > 0) {
503 resourceSelectionChangedUIListeners.remove(listener);
508 public void removeGetUIListener(IGetUIListener getUIListener) {
509 synchronized (getUIListeners) {
510 getUIListeners.remove(getUIListener);
514 public void removePutUIListener(IPutUIListener putUIListener) {
515 synchronized (putUIListeners) {
516 putUIListeners.remove(putUIListener);
520 public void removePostUIListener(IPostUIListener postUIListener) {
521 synchronized (postUIListeners) {
522 postUIListeners.remove(postUIListener);
526 public void removeObserveUIListener(IObserveUIListener observeUIListener) {
527 synchronized (observeUIListeners) {
528 observeUIListeners.remove(observeUIListener);
532 public void removeVerificationUIListener(
533 IVerificationUIListener verificationUIListener) {
534 synchronized (verificationUIListeners) {
535 verificationUIListeners.remove(verificationUIListener);
539 public void removeConfigUploadUIListener(IConfigurationUpload configListener) {
540 synchronized (configUploadUIListeners) {
541 configUploadUIListeners.remove(configListener);
545 public void addResourcetoFavorites(RemoteResource resource) {
546 if (null == resource) {
549 resource.setFavorite(true);
550 synchronized (favoriteResources) {
551 favoriteResources.add(resource);
555 public void removeResourceFromFavorites(RemoteResource resource) {
556 if (null == resource) {
559 resource.setFavorite(false);
560 synchronized (favoriteResources) {
561 favoriteResources.remove(resource);
565 public void addResourceURItoFavorites(RemoteResource resource) {
566 if (null == resource) {
569 synchronized (favoriteURIList) {
570 favoriteURIList.add(resource.getResourceURI());
574 public void removeResourceURIFromFavorites(RemoteResource resource) {
575 if (null == resource) {
578 synchronized (favoriteURIList) {
579 favoriteURIList.remove(resource.getResourceURI());
583 public void addObservedResourceURI(String resourceURI) {
584 synchronized (observedResourceURIList) {
585 observedResourceURIList.add(resourceURI);
589 public void removeObservedResourceURI(String resourceURI) {
590 synchronized (observedResourceURIList) {
591 observedResourceURIList.remove(resourceURI);
595 public boolean isResourceObserved(String resourceURI) {
596 boolean observed = false;
597 synchronized (observedResourceURIList) {
598 observed = observedResourceURIList.contains(resourceURI);
603 public synchronized RemoteResource getCurrentResourceInSelection() {
604 return currentResourceInSelection;
607 public synchronized void setCurrentResourceInSelection(
608 RemoteResource resource) {
609 this.currentResourceInSelection = resource;
612 private void addResourceDetails(RemoteResource remoteResource) {
613 if (null != remoteResource) {
614 synchronized (resourceMap) {
615 resourceMap.put(remoteResource.getuId(), remoteResource);
620 public void addFindresourceUIListener(IFindResourceUIListener listener) {
621 if (null == listener) {
624 synchronized (findResourceUIListeners) {
625 findResourceUIListeners.add(listener);
629 public void removeFindresourceUIListener(IFindResourceUIListener listener) {
630 if (null == listener) {
633 synchronized (findResourceUIListeners) {
634 findResourceUIListeners.remove(listener);
638 private RemoteResource fetchResourceDetails(
639 SimulatorRemoteResource remoteResourceN) {
640 if (null == remoteResourceN) {
643 RemoteResource remoteResource = new RemoteResource();
644 remoteResource.setuId(remoteResourceN.getId());
645 remoteResource.setResourceURI(remoteResourceN.getUri());
646 remoteResource.setHost(remoteResourceN.getHost());
647 remoteResource.setResourceTypes(remoteResourceN.getResourceTypes());
648 remoteResource.setResourceInterfaces(remoteResourceN
649 .getResourceInterfaces());
650 remoteResource.setConnectivityType(remoteResourceN
651 .getConnectivityType());
652 remoteResource.setObservable(remoteResourceN.getIsObservable());
653 return remoteResource;
656 private boolean isUidExist(String uid) {
658 synchronized (resourceMap) {
659 exist = resourceMap.containsKey(uid);
664 private RemoteResource getResource(String uid) {
668 RemoteResource resource;
669 synchronized (resourceMap) {
670 resource = resourceMap.get(uid);
675 private Map<String, RemoteResourceAttribute> fetchResourceAttributesFromModel(
676 SimulatorResourceModel resourceModelN) {
677 Map<String, RemoteResourceAttribute> resourceAttributeMap = null;
678 if (null != resourceModelN) {
679 Map<String, ResourceAttribute> attributeMapN;
681 attributeMapN = resourceModelN.getAttributes();
682 } catch (SimulatorException e) {
686 .log(Level.ERROR.ordinal(), new Date(),
687 Utility.getSimulatorErrorString(e, null));
690 if (null != attributeMapN) {
691 resourceAttributeMap = new HashMap<String, RemoteResourceAttribute>();
693 Set<String> attNameSet = attributeMapN.keySet();
696 ResourceAttribute attributeN;
697 RemoteResourceAttribute attribute;
698 Iterator<String> attNameItr = attNameSet.iterator();
699 while (attNameItr.hasNext()) {
700 attName = attNameItr.next();
701 attributeN = attributeMapN.get(attName);
702 if (null != attributeN) {
703 attribute = new RemoteResourceAttribute();
704 attribute.setResourceAttribute(attributeN);
705 attribute.setAttributeName(attName);
707 attValueObj = attributeN.getValue();
708 if (null != attValueObj) {
709 attribute.setAttributeValue(attValueObj);
712 // Set the attribute type
713 attribute.setAttValBaseType(attributeN.getBaseType());
714 attribute.setAttValType(attributeN.getType());
716 // Set the range and allowed values
717 Range range = attributeN.getRange();
719 attribute.setMinValue(range.getMin());
720 attribute.setMaxValue(range.getMax());
722 Object[] values = attributeN.getAllowedValues();
723 if (null != values && values.length > 0) {
724 List<Object> valueList = new ArrayList<Object>();
725 for (Object obj : values) {
728 attribute.setAllowedValues(valueList);
731 resourceAttributeMap.put(attName, attribute);
736 return resourceAttributeMap;
739 private void newResourceFoundNotification(RemoteResource resource) {
740 synchronized (findResourceUIListeners) {
741 if (findResourceUIListeners.size() > 0) {
742 IFindResourceUIListener listener;
743 Iterator<IFindResourceUIListener> listenerItr = findResourceUIListeners
745 while (listenerItr.hasNext()) {
746 listener = listenerItr.next();
747 if (null != listener) {
748 listener.onNewResourceFound(resource);
755 private void resourceSelectionChangedUINotification(RemoteResource resource) {
756 synchronized (resourceSelectionChangedUIListeners) {
757 if (resourceSelectionChangedUIListeners.size() > 0) {
758 IResourceSelectionChangedUIListener listener;
759 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
761 while (listenerItr.hasNext()) {
762 listener = listenerItr.next();
763 if (null != listener) {
764 listener.onResourceSelectionChange(resource);
771 private void getCompleteNotification(RemoteResource resource) {
772 synchronized (getUIListeners) {
773 if (getUIListeners.size() > 0) {
774 IGetUIListener listener;
775 Iterator<IGetUIListener> listenerItr = getUIListeners
777 while (listenerItr.hasNext()) {
778 listener = listenerItr.next();
779 if (null != listener) {
780 listener.onGetCompleted(resource);
787 private void putCompleteNotification(RemoteResource resource) {
788 synchronized (putUIListeners) {
789 if (putUIListeners.size() > 0) {
790 IPutUIListener listener;
791 Iterator<IPutUIListener> listenerItr = putUIListeners
793 while (listenerItr.hasNext()) {
794 listener = listenerItr.next();
795 if (null != listener) {
796 listener.onPutCompleted(resource);
803 private void postCompleteNotification(RemoteResource resource) {
804 synchronized (postUIListeners) {
805 if (postUIListeners.size() > 0) {
806 IPostUIListener listener;
807 Iterator<IPostUIListener> listenerItr = postUIListeners
809 while (listenerItr.hasNext()) {
810 listener = listenerItr.next();
811 if (null != listener) {
812 listener.onPostCompleted(resource);
819 private void observeCompleteNotification(RemoteResource resource) {
820 synchronized (observeUIListeners) {
821 if (observeUIListeners.size() > 0) {
822 IObserveUIListener listener;
823 Iterator<IObserveUIListener> listenerItr = observeUIListeners
825 while (listenerItr.hasNext()) {
826 listener = listenerItr.next();
827 if (null != listener) {
828 listener.onObserveCompleted(resource);
835 private void verificationStartedNotification(RemoteResource resource,
837 synchronized (verificationUIListeners) {
838 if (verificationUIListeners.size() > 0) {
839 IVerificationUIListener listener;
840 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
842 while (listenerItr.hasNext()) {
843 listener = listenerItr.next();
844 if (null != listener) {
845 listener.onVerificationStarted(resource, autoType);
852 private void verificationAbortedNotification(RemoteResource resource,
854 synchronized (verificationUIListeners) {
855 if (verificationUIListeners.size() > 0) {
856 IVerificationUIListener listener;
857 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
859 while (listenerItr.hasNext()) {
860 listener = listenerItr.next();
861 if (null != listener) {
862 listener.onVerificationAborted(resource, autoType);
869 private void verificationCompletedNotification(RemoteResource resource,
871 synchronized (verificationUIListeners) {
872 if (verificationUIListeners.size() > 0) {
873 IVerificationUIListener listener;
874 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
876 while (listenerItr.hasNext()) {
877 listener = listenerItr.next();
878 if (null != listener) {
879 listener.onVerificationCompleted(resource, autoType);
886 private void configUploadedNotification(RemoteResource resource) {
887 synchronized (configUploadUIListeners) {
888 if (configUploadUIListeners.size() > 0) {
889 IConfigurationUpload listener;
890 Iterator<IConfigurationUpload> listenerItr = configUploadUIListeners
892 while (listenerItr.hasNext()) {
893 listener = listenerItr.next();
894 if (null != listener) {
895 listener.onConfigurationUploaded(resource);
902 public synchronized Set<String> getLastKnownSearchTypes() {
903 return lastKnownSearchTypes;
906 public synchronized void setLastKnownSearchTypes(
907 Set<String> lastKnownSearchTypes) {
908 this.lastKnownSearchTypes = lastKnownSearchTypes;
911 public boolean findResourceRequest(Set<String> searchTypes) {
912 boolean result = false;
913 if (null == searchTypes || searchTypes.size() < 1) {
915 SimulatorManager.findResource(findResourceListener);
917 } catch (SimulatorException e) {
921 .log(Level.ERROR.ordinal(), new Date(),
922 Utility.getSimulatorErrorString(e, null));
925 Iterator<String> searchItr = searchTypes.iterator();
927 while (searchItr.hasNext()) {
928 rType = searchItr.next();
930 SimulatorManager.findResource(rType, findResourceListener);
932 } catch (SimulatorException e) {
936 .log(Level.ERROR.ordinal(), new Date(),
937 Utility.getSimulatorErrorString(e, null));
944 public void deleteResources(final Set<String> searchTypes) {
947 if (null == searchTypes || searchTypes.size() < 1) {
948 // Delete all cached details of resources
950 favoriteResources.clear();
952 // Change the current resource in selection
953 setCurrentResourceInSelection(null);
954 resourceSelectionChangedUINotification(null);
956 Iterator<String> typeItr = searchTypes.iterator();
958 while (typeItr.hasNext()) {
959 resType = typeItr.next();
960 deleteResourcesByType(resType);
962 // Change the current resource in selection
963 updateCurrentResourceInSelection(searchTypes);
970 private void updateCurrentResourceInSelection(Set<String> searchTypes) {
971 if (null == searchTypes || searchTypes.size() < 1) {
974 RemoteResource resourceInSelection = getCurrentResourceInSelection();
975 if (null == resourceInSelection) {
978 List<String> typesOfSelection = resourceInSelection.getResourceTypes();
979 if (null == typesOfSelection || typesOfSelection.size() < 1) {
982 Iterator<String> itr = typesOfSelection.iterator();
984 while (itr.hasNext()) {
986 if (searchTypes.contains(type)) {
987 setCurrentResourceInSelection(null);
988 resourceSelectionChangedUINotification(null);
994 private void deleteResourcesByType(String resourceType) {
995 if (null == resourceType) {
998 synchronized (resourceMap) {
999 Set<String> keySet = resourceMap.keySet();
1000 if (null == keySet) {
1003 Iterator<String> keyItr = keySet.iterator();
1005 RemoteResource resource;
1008 while (keyItr.hasNext()) {
1009 uId = keyItr.next();
1010 resource = resourceMap.get(uId);
1011 if (null == resource) {
1014 types = resource.getResourceTypes();
1015 if (null != types) {
1016 exist = types.contains(resourceType);
1018 // Remove the resource from favorites list.
1019 removeResourceFromFavorites(resource);
1020 // Remove the resource
1028 public void resourceSelectionChanged(final RemoteResource resource) {
1032 setCurrentResourceInSelection(resource);
1033 // Notify all observers for resource selection change event
1034 resourceSelectionChangedUINotification(resource);
1039 public List<MetaProperty> getMetaProperties(RemoteResource resource) {
1040 if (null != resource) {
1044 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1046 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1047 propName = Constants.META_PROPERTIES[index];
1048 if (propName.equals(Constants.RESOURCE_URI)) {
1049 propValue = resource.getResourceURI();
1050 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1051 propValue = resource.getConnectivityType().toString();
1052 } else if (propName.equals(Constants.OBSERVABLE)) {
1053 propValue = Utility.getObservableInString(resource
1056 } else if (propName.equals(Constants.RESOURCE_TYPES)) {
1057 List<String> types = resource.getResourceTypes();
1058 if (null != types) {
1059 propValue = types.toString();
1061 propValue = Constants.NOT_AVAILABLE;
1063 } else if (propName.equals(Constants.RESOURCE_INTERFACES)) {
1064 List<String> interfaces = resource.getResourceInterfaces();
1065 if (null != interfaces) {
1066 propValue = interfaces.toString();
1068 propValue = Constants.NOT_AVAILABLE;
1073 if (null != propValue) {
1074 metaPropertyList.add(new MetaProperty(propName, propValue));
1078 return metaPropertyList;
1083 public Map<String, Boolean> getAutomationStatus(RemoteResource resource) {
1084 if (null == resource) {
1087 Map<String, Boolean> autoStatus = new HashMap<String, Boolean>();
1088 autoStatus.put(Constants.GET, resource.isGetAutomtnInProgress());
1089 autoStatus.put(Constants.PUT, resource.isPutAutomtnInProgress());
1090 autoStatus.put(Constants.POST, resource.isPostAutomtnInProgress());
1094 public List<RemoteResource> getResourceList() {
1095 List<RemoteResource> resourceList = new ArrayList<RemoteResource>();
1096 synchronized (resourceMap) {
1097 Set<String> idSet = resourceMap.keySet();
1098 Iterator<String> idItr = idSet.iterator();
1099 RemoteResource resource;
1100 while (idItr.hasNext()) {
1101 resource = resourceMap.get(idItr.next());
1102 if (null != resource) {
1103 resourceList.add(resource);
1108 Collections.sort(resourceList, new Comparator<RemoteResource>() {
1109 public int compare(RemoteResource res1, RemoteResource res2) {
1110 String s1 = res1.getResourceURI();
1111 String s2 = res2.getResourceURI();
1113 String s1Part = s1.replaceAll("\\d", "");
1114 String s2Part = s2.replaceAll("\\d", "");
1116 if (s1Part.equalsIgnoreCase(s2Part)) {
1117 return extractInt(s1) - extractInt(s2);
1119 return s1.compareTo(s2);
1122 int extractInt(String s) {
1123 String num = s.replaceAll("\\D", "");
1124 // return 0 if no digits found
1125 return num.isEmpty() ? 0 : Integer.parseInt(num);
1129 return resourceList;
1132 public List<RemoteResource> getFavResourceList() {
1133 List<RemoteResource> resourceList;
1134 synchronized (favoriteResources) {
1135 resourceList = new ArrayList<RemoteResource>(favoriteResources);
1137 return resourceList;
1140 public String getAttributeValue(RemoteResource res, String attName) {
1141 if (null == res || null == attName) {
1144 return res.getAttributeValue(attName);
1147 public void sendGetRequest(RemoteResource resource) {
1148 if (null == resource) {
1151 SimulatorRemoteResource resourceN = resource.getResource();
1152 if (null == resourceN) {
1156 resourceN.get(null, getListener);
1157 } catch (SimulatorException e) {
1161 .log(Level.ERROR.ordinal(), new Date(),
1162 Utility.getSimulatorErrorString(e, null));
1166 public void sendPutRequest(RemoteResource resource,
1167 List<PutPostAttributeModel> putPostModelList) {
1168 System.out.println(putPostModelList);
1169 System.out.println("ResourceManager: sendPutRequest");
1170 if (null == resource) {
1173 System.out.println("ResourceManager: resource not null");
1174 SimulatorRemoteResource resourceN = resource.getResource();
1175 if (null == resourceN) {
1178 System.out.println("ResourceManager: Native resource not null");
1179 Map<String, RemoteResourceAttribute> attMap = resource
1180 .getResourceAttributesMap();
1181 if (null == attMap || attMap.size() < 1) {
1184 System.out.println("ResourceManager: attrubutes obtained");
1185 SimulatorResourceModel resourceModel = getUpdatedResourceModel(attMap,
1187 System.out.println("ResourceModel exist?:" + (resourceModel != null));
1189 resourceN.put(resourceModel, null, putListener);
1190 } catch (SimulatorException e) {
1194 .log(Level.ERROR.ordinal(), new Date(),
1195 Utility.getSimulatorErrorString(e, null));
1197 System.out.println("ResourceManager: called native put");
1200 public void sendPostRequest(RemoteResource resource,
1201 List<PutPostAttributeModel> putPostModelList) {
1202 System.out.println(putPostModelList);
1203 if (null == resource) {
1206 SimulatorRemoteResource resourceN = resource.getResource();
1207 if (null == resourceN) {
1210 Map<String, RemoteResourceAttribute> attMap = resource
1211 .getResourceAttributesMap();
1212 if (null == attMap || attMap.size() < 1) {
1215 // Filter out the attributes whose modification status is true.
1216 Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
1217 PutPostAttributeModel model;
1218 while (itr.hasNext()) {
1220 if (!model.isModified()) {
1224 SimulatorResourceModel resourceModel = getUpdatedResourceModel(attMap,
1227 resourceN.post(resourceModel, null, postListener);
1228 } catch (SimulatorException e) {
1232 .log(Level.ERROR.ordinal(), new Date(),
1233 Utility.getSimulatorErrorString(e, null));
1237 private SimulatorResourceModel getUpdatedResourceModel(
1238 Map<String, RemoteResourceAttribute> attMap,
1239 List<PutPostAttributeModel> putPostModelList) {
1241 SimulatorResourceModel resourceModel = new SimulatorResourceModel();
1242 PutPostAttributeModel model;
1243 RemoteResourceAttribute attribute;
1245 Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
1246 while (itr.hasNext()) {
1248 attName = model.getAttName();
1249 attribute = attMap.get(attName);
1250 if (null == attribute) {
1253 attType = attribute.getAttValBaseType();
1254 if (attType == Type.INT) {
1257 attValue = Integer.parseInt(model.getAttValue());
1258 resourceModel.addAttributeInt(attName, attValue);
1259 } catch (NumberFormatException e) {
1263 .log(Level.ERROR.ordinal(),
1265 Utility.getSimulatorErrorString(e,
1266 "Failed to convert the attribute value."));
1267 } catch (SimulatorException e) {
1271 .log(Level.ERROR.ordinal(), new Date(),
1272 Utility.getSimulatorErrorString(e, null));
1274 } else if (attType == Type.DOUBLE) {
1277 attValue = Double.parseDouble(model.getAttValue());
1278 resourceModel.addAttributeDouble(attName, attValue);
1279 } catch (NumberFormatException e) {
1283 .log(Level.ERROR.ordinal(),
1285 Utility.getSimulatorErrorString(e,
1286 "Failed to convert the attribute value."));
1287 } catch (SimulatorException e) {
1291 .log(Level.ERROR.ordinal(), new Date(),
1292 Utility.getSimulatorErrorString(e, null));
1294 } else if (attType == Type.BOOL) {
1296 attValue = Boolean.parseBoolean(model.getAttValue());
1298 resourceModel.addAttributeBoolean(attName, attValue);
1299 } catch (SimulatorException e) {
1303 .log(Level.ERROR.ordinal(), new Date(),
1304 Utility.getSimulatorErrorString(e, null));
1306 } else if (attType == Type.STRING) {
1308 attValue = model.getAttValue();
1310 resourceModel.addAttributeString(attName, attValue);
1311 } catch (SimulatorException e) {
1315 .log(Level.ERROR.ordinal(), new Date(),
1316 Utility.getSimulatorErrorString(e, null));
1320 return resourceModel;
1323 public void sendObserveRequest(RemoteResource resource) {
1324 System.out.println("sendObserverRequest() entry");
1325 if (null == resource) {
1328 System.out.println("Resource is null:" + (resource == null));
1329 resource.setObserved(true);
1330 SimulatorRemoteResource resourceN = resource.getResource();
1331 if (null == resourceN) {
1335 resourceN.startObserve(SimulatorObserveType.OBSERVE, null,
1337 // Add observed resource URI to show the proper status after every
1338 // find/refresh operations.
1339 addObservedResourceURI(resource.getResourceURI());
1340 } catch (SimulatorException e) {
1344 .log(Level.ERROR.ordinal(), new Date(),
1345 Utility.getSimulatorErrorString(e, null));
1347 System.out.println("Observer called.");
1350 public void sendCancelObserveRequest(RemoteResource resource) {
1351 if (null == resource) {
1354 resource.setObserved(false);
1355 SimulatorRemoteResource resourceN = resource.getResource();
1356 if (null == resourceN) {
1360 resourceN.stopObserve();
1361 // Remove observed resource URI to show the proper status after
1362 // every find/refresh operations.
1363 removeObservedResourceURI(resource.getResourceURI());
1364 } catch (SimulatorException e) {
1368 .log(Level.ERROR.ordinal(), new Date(),
1369 Utility.getSimulatorErrorString(e, null));
1373 public void startAutomationRequest(int reqType, RemoteResource resource) {
1374 if (null == resource) {
1377 SimulatorRemoteResource resourceN = resource.getResource();
1378 if (null == resourceN) {
1381 SimulatorVerificationType type = SimulatorVerificationType
1382 .getVerificationType(reqType);
1386 System.out.println("Before calling startVerification: " + reqType);
1389 autoId = resourceN.startVerification(type, verifyListener);
1390 System.out.println("After calling startVerification: " + autoId);
1392 if (reqType == Constants.GET_AUTOMATION_INDEX) {
1393 // resource.setGetAutomtnInProgress(true);
1394 resource.setGetAutomtnId(autoId);
1395 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
1396 // resource.setPutAutomtnInProgress(true);
1397 resource.setPutAutomtnId(autoId);
1398 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1399 // resource.setPostAutomtnInProgress(true);
1400 resource.setPostAutomtnId(autoId);
1403 } catch (SimulatorException e) {
1407 .log(Level.ERROR.ordinal(), new Date(),
1408 Utility.getSimulatorErrorString(e, null));
1412 public void stopAutomationRequest(int reqType, RemoteResource resource) {
1413 if (null == resource) {
1416 SimulatorRemoteResource resourceN = resource.getResource();
1417 if (null == resourceN) {
1421 if (reqType == Constants.GET_AUTOMATION_INDEX) {
1422 resource.setGetAutomtnInProgress(false);
1423 autoId = resource.getGetAutomtnId();
1424 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
1425 resource.setPutAutomtnInProgress(false);
1426 autoId = resource.getPutAutomtnId();
1427 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1428 resource.setPostAutomtnInProgress(false);
1429 autoId = resource.getPostAutomtnId();
1432 resourceN.stopVerification(autoId);
1433 } catch (SimulatorException e) {
1437 .log(Level.ERROR.ordinal(), new Date(),
1438 Utility.getSimulatorErrorString(e, null));
1442 public void setConfigFilePath(RemoteResource resource, String configFilePath) {
1443 if (null == resource) {
1446 SimulatorRemoteResource resourceN = resource.getResource();
1447 if (null == resourceN) {
1451 resourceN.setConfigInfo(configFilePath);
1452 } catch (SimulatorException e) {
1456 .log(Level.ERROR.ordinal(), new Date(),
1457 Utility.getSimulatorErrorString(e, null));
1460 // Update the status
1461 resource.setConfigUploaded(true);
1463 // Notify the UI listeners
1464 configUploadedNotification(resource);
1467 public Image getImage(String resourceURI) {
1468 if (null == resourceURI) {
1471 URL url = Activator.getDefault().getBundle()
1472 .getEntry(getImageURL(resourceURI));
1476 return ImageDescriptor.createFromURL(url).createImage();
1479 private String getImageURL(String resourceURI) {
1480 // TODO: Hard-coding the image file name temporarily.
1481 // It will be included in a separate class which manages all image
1483 return "/icons/light_16x16.png";
1486 public void shutdown() {
1487 // TODO: To be implemented for clean-up activities.