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 // TODO: Temporarily maintaining a list of favorite resource URIs.
103 private List<String> favoriteURIList;
105 public ResourceManager() {
106 resourceMap = new HashMap<String, RemoteResource>();
107 favoriteResources = new ArrayList<RemoteResource>();
108 favoriteURIList = new ArrayList<String>();
109 findResourceUIListeners = new ArrayList<IFindResourceUIListener>();
110 resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
111 getUIListeners = new ArrayList<IGetUIListener>();
112 putUIListeners = new ArrayList<IPutUIListener>();
113 postUIListeners = new ArrayList<IPostUIListener>();
114 observeUIListeners = new ArrayList<IObserveUIListener>();
115 verificationUIListeners = new ArrayList<IVerificationUIListener>();
116 configUploadUIListeners = new ArrayList<IConfigurationUpload>();
118 findResourceListener = new IFindResourceListener() {
121 public void onResourceCallback(
122 final SimulatorRemoteResource resourceN) {
123 synchronizerThread.addToQueue(new Runnable() {
126 System.out.println("onResourceCallback() entry");
127 if (null == resourceN) {
130 // If resource already exist, then ignore it.
131 String uid = resourceN.getId();
135 boolean exist = isUidExist(uid);
137 System.out.println("Duplicate resource found: ["
142 // Fetch the resource data
143 RemoteResource resource = fetchResourceDetails(resourceN);
144 if (null == resource) {
148 resource.setResource(resourceN);
150 // Add the resource in local data structure
151 addResourceDetails(resource);
153 // Add resource to favorite list
154 String uri = resource.getResourceURI();
156 if (favoriteURIList.contains(uri)) {
157 addResourcetoFavorites(resource);
160 // Notify the UI listener
161 newResourceFoundNotification(resource);
163 // Send an initial GET request to get the resource
166 resourceN.get(null, getListener);
167 } catch (SimulatorException e) {
171 .log(Level.ERROR.ordinal(),
173 "[" + e.getClass().getSimpleName()
174 + "]" + e.code().toString()
175 + "-" + e.message());
182 getListener = new IGetListener() {
184 public void onGetCompleted(final String uid,
185 final SimulatorResourceModel resourceModelN) {
186 synchronizerThread.addToQueue(new Runnable() {
189 // Handling the response which includes retrieving the
190 // attributes and updating the local model.
191 RemoteResource resource = handleResponse(uid,
193 if (null != resource) {
194 // Notify the UI listeners
195 getCompleteNotification(resource);
202 public void onGetFailed(Throwable th) {
203 synchronizerThread.addToQueue(new Runnable() {
211 putListener = new IPutListener() {
214 public void onPutCompleted(final String uid,
215 final SimulatorResourceModel resourceModelN) {
216 synchronizerThread.addToQueue(new Thread() {
219 // Handling the response which includes retrieving the
220 // attributes and updating the local model.
221 RemoteResource resource = handleResponse(uid,
223 if (null != resource) {
224 // Notify the UI listeners
225 putCompleteNotification(resource);
232 public void onPutFailed(Throwable th) {
233 synchronizerThread.addToQueue(new Runnable() {
241 postListener = new IPostListener() {
243 public void onPostCompleted(final String uid,
244 final SimulatorResourceModel resourceModelN) {
245 synchronizerThread.addToQueue(new Runnable() {
248 // Handling the response which includes retrieving the
249 // attributes and updating the local model.
250 RemoteResource resource = handleResponse(uid,
252 if (null != resource) {
253 // Notify the UI listeners
254 postCompleteNotification(resource);
261 public void onPostFailed(Throwable th) {
262 synchronizerThread.addToQueue(new Runnable() {
270 observeListener = new IObserveListener() {
273 public void onObserveCompleted(final String uid,
274 final SimulatorResourceModel resourceModelN, final int seq) {
275 System.out.println("ResourceManager: onObserveCallback()");
276 synchronizerThread.addToQueue(new Runnable() {
279 // Handling the response which includes retrieving the
280 // attributes and updating the local model.
281 RemoteResource resource = handleResponse(uid,
283 if (null != resource) {
284 // Notify the UI listeners
285 observeCompleteNotification(resource);
292 public void onObserveFailed(Throwable th) {
293 // TODO Auto-generated method stub
297 verifyListener = new IVerificationListener() {
300 public void onVerificationStarted(final String uid, final int autoId) {
301 System.out.println("onVefificationStarted: " + autoId);
302 synchronizerThread.addToQueue(new Runnable() {
305 RemoteResource resource = getResource(uid);
306 if (null == resource) {
309 // Update the automation status.
310 resource.updateAutomationStatus(autoId, true);
312 int autoType = resource.getAutomationtype(autoId);
314 // Notify the listeners.
315 verificationStartedNotification(resource, autoType);
321 public void onVerificationCompleted(final String uid,
323 System.out.println("onVefificationCompleted: " + autoId);
324 synchronizerThread.addToQueue(new Runnable() {
327 RemoteResource resource = getResource(uid);
328 if (null == resource) {
331 // Update the automation status.
332 resource.updateAutomationStatus(autoId, false);
334 int autoType = resource.getAutomationtype(autoId);
336 // Notify the listeners.
337 verificationCompletedNotification(resource, autoType);
343 public void onVerificationAborted(final String uid, final int autoId) {
344 System.out.println("onVefificationAborted: " + autoId);
345 synchronizerThread.addToQueue(new Runnable() {
348 RemoteResource resource = getResource(uid);
349 if (null == resource) {
352 // Update the automation status.
353 resource.updateAutomationStatus(autoId, false);
355 int autoType = resource.getAutomationtype(autoId);
357 // Notify the listeners.
358 verificationAbortedNotification(resource, autoType);
364 synchronizerThread = new ResponseSynchronizerThread();
365 threadHandle = new Thread(synchronizerThread);
366 threadHandle.setName("Simulator Client Controller Event Queue");
367 threadHandle.start();
370 private RemoteResource handleResponse(String uid,
371 SimulatorResourceModel resourceModelN) {
372 if (null == uid || null == resourceModelN) {
376 // Update the local model
377 RemoteResource resource;
378 resource = getResource(uid);
379 if (null == resource) {
383 resource.setResourceModel(resourceModelN);
384 Map<String, RemoteResourceAttribute> attributeMap = fetchResourceAttributesFromModel(resourceModelN);
386 // TODO: For debugging
387 if (null != attributeMap) {
388 RemoteResourceAttribute.printAttributes(attributeMap);
389 System.out.println("Attributes found: " + (null != attributeMap));
390 System.out.println("No of attributes: " + attributeMap.size());
392 resource.setResourceAttributesMap(attributeMap);
397 private static class ResponseSynchronizerThread implements Runnable {
399 LinkedList<Runnable> responseQueue = new LinkedList<Runnable>();
403 while (!Thread.interrupted()) {
404 synchronized (this) {
406 while (responseQueue.isEmpty()) {
410 } catch (InterruptedException e) {
416 synchronized (this) {
417 thread = responseQueue.pop();
421 } catch (Exception e) {
422 if (e instanceof InterruptedException) {
430 public void addToQueue(Runnable event) {
431 synchronized (this) {
432 responseQueue.add(event);
438 public void addResourceSelectionChangedUIListener(
439 IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
440 synchronized (resourceSelectionChangedUIListeners) {
441 resourceSelectionChangedUIListeners
442 .add(resourceSelectionChangedUIListener);
446 public void addGetUIListener(IGetUIListener getUIListener) {
447 synchronized (getUIListeners) {
448 getUIListeners.add(getUIListener);
452 public void addPutUIListener(IPutUIListener putUIListener) {
453 synchronized (putUIListeners) {
454 putUIListeners.add(putUIListener);
458 public void addPostUIListener(IPostUIListener postUIListener) {
459 synchronized (postUIListeners) {
460 postUIListeners.add(postUIListener);
464 public void addObserveUIListener(IObserveUIListener observeUIListener) {
465 synchronized (observeUIListeners) {
466 observeUIListeners.add(observeUIListener);
470 public void addVerificationUIListener(
471 IVerificationUIListener verificationUIListener) {
472 synchronized (verificationUIListeners) {
473 verificationUIListeners.add(verificationUIListener);
477 public void addConfigUploadUIListener(IConfigurationUpload configListener) {
478 synchronized (configUploadUIListeners) {
479 configUploadUIListeners.add(configListener);
483 public void removeResourceSelectionChangedUIListener(
484 IResourceSelectionChangedUIListener listener) {
485 synchronized (resourceSelectionChangedUIListeners) {
487 && resourceSelectionChangedUIListeners.size() > 0) {
488 resourceSelectionChangedUIListeners.remove(listener);
493 public void removeGetUIListener(IGetUIListener getUIListener) {
494 synchronized (getUIListeners) {
495 getUIListeners.remove(getUIListener);
499 public void removePutUIListener(IPutUIListener putUIListener) {
500 synchronized (putUIListeners) {
501 putUIListeners.remove(putUIListener);
505 public void removePostUIListener(IPostUIListener postUIListener) {
506 synchronized (postUIListeners) {
507 postUIListeners.remove(postUIListener);
511 public void removeObserveUIListener(IObserveUIListener observeUIListener) {
512 synchronized (observeUIListeners) {
513 observeUIListeners.remove(observeUIListener);
517 public void removeVerificationUIListener(
518 IVerificationUIListener verificationUIListener) {
519 synchronized (verificationUIListeners) {
520 verificationUIListeners.remove(verificationUIListener);
524 public void removeConfigUploadUIListener(IConfigurationUpload configListener) {
525 synchronized (configUploadUIListeners) {
526 configUploadUIListeners.remove(configListener);
530 public void addResourcetoFavorites(RemoteResource resource) {
531 if (null == resource) {
534 resource.setFavorite(true);
535 synchronized (favoriteResources) {
536 favoriteResources.add(resource);
537 favoriteURIList.add(resource.getResourceURI());
541 public void removeResourceFromFavorites(RemoteResource resource) {
542 if (null == resource) {
545 resource.setFavorite(false);
546 synchronized (favoriteResources) {
547 favoriteResources.remove(resource);
551 public void removeResourceURIFromFavorites(RemoteResource resource) {
552 if (null == resource) {
555 synchronized (favoriteURIList) {
556 favoriteURIList.remove(resource.getResourceURI());
560 public synchronized RemoteResource getCurrentResourceInSelection() {
561 return currentResourceInSelection;
564 public synchronized void setCurrentResourceInSelection(
565 RemoteResource resource) {
566 this.currentResourceInSelection = resource;
569 private void addResourceDetails(RemoteResource remoteResource) {
570 if (null != remoteResource) {
571 synchronized (resourceMap) {
572 resourceMap.put(remoteResource.getuId(), remoteResource);
577 public void addFindresourceUIListener(IFindResourceUIListener listener) {
578 if (null == listener) {
581 synchronized (findResourceUIListeners) {
582 findResourceUIListeners.add(listener);
586 public void removeFindresourceUIListener(IFindResourceUIListener listener) {
587 if (null == listener) {
590 synchronized (findResourceUIListeners) {
591 findResourceUIListeners.remove(listener);
595 private RemoteResource fetchResourceDetails(
596 SimulatorRemoteResource remoteResourceN) {
597 if (null == remoteResourceN) {
600 RemoteResource remoteResource = new RemoteResource();
601 remoteResource.setuId(remoteResourceN.getId());
602 remoteResource.setResourceURI(remoteResourceN.getUri());
603 remoteResource.setHost(remoteResourceN.getHost());
604 remoteResource.setResourceTypes(remoteResourceN.getResourceTypes());
605 remoteResource.setResourceInterfaces(remoteResourceN
606 .getResourceInterfaces());
607 remoteResource.setConnectivityType(remoteResourceN
608 .getConnectivityType());
609 remoteResource.setObservable(remoteResourceN.getIsObservable());
610 return remoteResource;
613 private boolean isUidExist(String uid) {
615 synchronized (resourceMap) {
616 exist = resourceMap.containsKey(uid);
621 private RemoteResource getResource(String uid) {
625 RemoteResource resource;
626 synchronized (resourceMap) {
627 resource = resourceMap.get(uid);
632 private Map<String, RemoteResourceAttribute> fetchResourceAttributesFromModel(
633 SimulatorResourceModel resourceModelN) {
634 Map<String, RemoteResourceAttribute> resourceAttributeMap = null;
635 if (null != resourceModelN) {
636 Map<String, ResourceAttribute> attributeMapN;
638 attributeMapN = resourceModelN.getAttributes();
639 } catch (SimulatorException e) {
643 .log(Level.ERROR.ordinal(),
645 "[" + e.getClass().getSimpleName() + "]"
646 + e.code().toString() + "-"
650 if (null != attributeMapN) {
651 resourceAttributeMap = new HashMap<String, RemoteResourceAttribute>();
653 Set<String> attNameSet = attributeMapN.keySet();
656 ResourceAttribute attributeN;
657 RemoteResourceAttribute attribute;
658 Iterator<String> attNameItr = attNameSet.iterator();
659 while (attNameItr.hasNext()) {
660 attName = attNameItr.next();
661 attributeN = attributeMapN.get(attName);
662 if (null != attributeN) {
663 attribute = new RemoteResourceAttribute();
664 attribute.setResourceAttribute(attributeN);
665 attribute.setAttributeName(attName);
667 attValueObj = attributeN.getValue();
668 if (null != attValueObj) {
669 attribute.setAttributeValue(attValueObj);
672 // Set the attribute type
673 attribute.setAttValBaseType(attributeN.getBaseType());
674 attribute.setAttValType(attributeN.getType());
676 // Set the range and allowed values
677 Range range = attributeN.getRange();
679 attribute.setMinValue(range.getMin());
680 attribute.setMaxValue(range.getMax());
682 Object[] values = attributeN.getAllowedValues();
683 if (null != values && values.length > 0) {
684 List<Object> valueList = new ArrayList<Object>();
685 for (Object obj : values) {
688 attribute.setAllowedValues(valueList);
691 * Type baseType = attribute.getAttValBaseType();
693 * if(baseType == Type.INT) { //int[] values =
694 * attributeN.getAllowedValues();
695 * attribute.setAllowedValues
696 * (attributeN.getAllowedValues()); } else
697 * if(baseType == Type.DOUBLE) { double[] values =
698 * attributeN.getAllowedValues();
699 * attribute.setAllowedValues
700 * (Utility.converArrayToList(values)); } else
701 * if(baseType == Type.BOOL) { //boolean[] values =
702 * attributeN.getAllowedValues(); List<Object> obj =
703 * new ArrayList<Object>(); obj.add(true);
704 * obj.add(false); attribute.setAllowedValues(obj);
705 * } else if(baseType == Type.STRING) { String[]
706 * values = attributeN.getAllowedValues();
708 * setAllowedValues(Utility.converArrayToList
712 resourceAttributeMap.put(attName, attribute);
717 return resourceAttributeMap;
720 private void newResourceFoundNotification(RemoteResource resource) {
721 synchronized (findResourceUIListeners) {
722 if (findResourceUIListeners.size() > 0) {
723 IFindResourceUIListener listener;
724 Iterator<IFindResourceUIListener> listenerItr = findResourceUIListeners
726 while (listenerItr.hasNext()) {
727 listener = listenerItr.next();
728 if (null != listener) {
729 listener.onNewResourceFound(resource);
736 private void resourceSelectionChangedUINotification(RemoteResource resource) {
737 synchronized (resourceSelectionChangedUIListeners) {
738 if (resourceSelectionChangedUIListeners.size() > 0) {
739 IResourceSelectionChangedUIListener listener;
740 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
742 while (listenerItr.hasNext()) {
743 listener = listenerItr.next();
744 if (null != listener) {
745 listener.onResourceSelectionChange(resource);
752 private void getCompleteNotification(RemoteResource resource) {
753 synchronized (getUIListeners) {
754 if (getUIListeners.size() > 0) {
755 IGetUIListener listener;
756 Iterator<IGetUIListener> listenerItr = getUIListeners
758 while (listenerItr.hasNext()) {
759 listener = listenerItr.next();
760 if (null != listener) {
761 listener.onGetCompleted(resource);
768 private void putCompleteNotification(RemoteResource resource) {
769 synchronized (putUIListeners) {
770 if (putUIListeners.size() > 0) {
771 IPutUIListener listener;
772 Iterator<IPutUIListener> listenerItr = putUIListeners
774 while (listenerItr.hasNext()) {
775 listener = listenerItr.next();
776 if (null != listener) {
777 listener.onPutCompleted(resource);
784 private void postCompleteNotification(RemoteResource resource) {
785 synchronized (postUIListeners) {
786 if (postUIListeners.size() > 0) {
787 IPostUIListener listener;
788 Iterator<IPostUIListener> listenerItr = postUIListeners
790 while (listenerItr.hasNext()) {
791 listener = listenerItr.next();
792 if (null != listener) {
793 listener.onPostCompleted(resource);
800 private void observeCompleteNotification(RemoteResource resource) {
801 synchronized (observeUIListeners) {
802 if (observeUIListeners.size() > 0) {
803 IObserveUIListener listener;
804 Iterator<IObserveUIListener> listenerItr = observeUIListeners
806 while (listenerItr.hasNext()) {
807 listener = listenerItr.next();
808 if (null != listener) {
809 listener.onObserveCompleted(resource);
816 private void verificationStartedNotification(RemoteResource resource,
818 synchronized (verificationUIListeners) {
819 if (verificationUIListeners.size() > 0) {
820 IVerificationUIListener listener;
821 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
823 while (listenerItr.hasNext()) {
824 listener = listenerItr.next();
825 if (null != listener) {
826 listener.onVerificationStarted(resource, autoType);
833 private void verificationAbortedNotification(RemoteResource resource,
835 synchronized (verificationUIListeners) {
836 if (verificationUIListeners.size() > 0) {
837 IVerificationUIListener listener;
838 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
840 while (listenerItr.hasNext()) {
841 listener = listenerItr.next();
842 if (null != listener) {
843 listener.onVerificationAborted(resource, autoType);
850 private void verificationCompletedNotification(RemoteResource resource,
852 synchronized (verificationUIListeners) {
853 if (verificationUIListeners.size() > 0) {
854 IVerificationUIListener listener;
855 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
857 while (listenerItr.hasNext()) {
858 listener = listenerItr.next();
859 if (null != listener) {
860 listener.onVerificationCompleted(resource, autoType);
867 private void configUploadedNotification(RemoteResource resource) {
868 synchronized (configUploadUIListeners) {
869 if (configUploadUIListeners.size() > 0) {
870 IConfigurationUpload listener;
871 Iterator<IConfigurationUpload> listenerItr = configUploadUIListeners
873 while (listenerItr.hasNext()) {
874 listener = listenerItr.next();
875 if (null != listener) {
876 listener.onConfigurationUploaded(resource);
883 // TODO: Temporarily used to display the resource in the UI
884 public List<String> getURIList() {
885 List<String> list = new ArrayList<String>();
886 synchronized (resourceMap) {
888 * Set<String> idSet = resourceMap.keySet(); Iterator<String> idItr
889 * = idSet.iterator(); String sId; RemoteResource resource;
890 * while(idItr.hasNext()) { sId = idItr.next(); resource =
891 * resourceMap.get(sId); if(null == resource) { continue; }
892 * list.add(resource.getResourceURI()); }
894 Set<String> uriSet = resourceMap.keySet();
895 Iterator<String> uriItr = uriSet.iterator();
897 while (uriItr.hasNext()) {
905 Collections.sort(list);
910 public synchronized Set<String> getLastKnownSearchTypes() {
911 return lastKnownSearchTypes;
914 public synchronized void setLastKnownSearchTypes(
915 Set<String> lastKnownSearchTypes) {
916 this.lastKnownSearchTypes = lastKnownSearchTypes;
919 public boolean findResourceRequest(Set<String> searchTypes) {
920 if (null == searchTypes || searchTypes.size() < 1) {
923 boolean result = false;
924 Iterator<String> searchItr = searchTypes.iterator();
926 while (searchItr.hasNext()) {
927 rType = searchItr.next();
929 SimulatorManager.findResources(rType, findResourceListener);
931 } catch (SimulatorException e) {
935 .log(Level.ERROR.ordinal(),
937 "[" + e.getClass().getSimpleName() + "]"
938 + e.code().toString() + "-"
945 public void deleteResources(final Set<String> searchTypes) {
946 if (null == searchTypes || searchTypes.size() < 1) {
951 Iterator<String> typeItr = searchTypes.iterator();
953 while (typeItr.hasNext()) {
954 resType = typeItr.next();
955 deleteResourcesByType(resType);
957 // Change the current resource in selection
958 updateCurrentResourceInSelection(searchTypes);
964 private void updateCurrentResourceInSelection(Set<String> searchTypes) {
965 if (null == searchTypes || searchTypes.size() < 1) {
968 RemoteResource resourceInSelection = getCurrentResourceInSelection();
969 if (null == resourceInSelection) {
972 List<String> typesOfSelection = resourceInSelection.getResourceTypes();
973 if (null == typesOfSelection || typesOfSelection.size() < 1) {
976 Iterator<String> itr = typesOfSelection.iterator();
978 while (itr.hasNext()) {
980 if (searchTypes.contains(type)) {
981 setCurrentResourceInSelection(null);
982 resourceSelectionChangedUINotification(null);
988 private void deleteResourcesByType(String resourceType) {
989 if (null == resourceType) {
992 synchronized (resourceMap) {
993 Set<String> keySet = resourceMap.keySet();
994 if (null == keySet) {
997 Iterator<String> keyItr = keySet.iterator();
999 RemoteResource resource;
1002 while (keyItr.hasNext()) {
1003 uId = keyItr.next();
1004 resource = resourceMap.get(uId);
1005 if (null == resource) {
1008 types = resource.getResourceTypes();
1009 if (null != types) {
1010 exist = types.contains(resourceType);
1012 // Remove the resource
1014 removeResourceFromFavorites(resource);
1021 public void resourceSelectionChanged(final RemoteResource resource) {
1025 setCurrentResourceInSelection(resource);
1026 // Notify all observers for resource selection change event
1027 resourceSelectionChangedUINotification(resource);
1032 public List<MetaProperty> getMetaProperties(RemoteResource resource) {
1033 if (null != resource) {
1037 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1039 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1040 propName = Constants.META_PROPERTIES[index];
1041 if (propName.equals(Constants.RESOURCE_URI)) {
1042 propValue = resource.getResourceURI();
1043 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1044 propValue = resource.getConnectivityType().toString();
1045 } else if (propName.equals(Constants.OBSERVABLE)) {
1046 propValue = Utility.getObservableInString(resource
1049 } else if (propName.equals(Constants.RESOURCE_TYPES)) {
1050 List<String> types = resource.getResourceTypes();
1051 if (null != types) {
1052 propValue = types.toString();
1054 propValue = Constants.NOT_AVAILABLE;
1056 } else if (propName.equals(Constants.RESOURCE_INTERFACES)) {
1057 List<String> interfaces = resource.getResourceInterfaces();
1058 if (null != interfaces) {
1059 propValue = interfaces.toString();
1061 propValue = Constants.NOT_AVAILABLE;
1066 if (null != propValue) {
1067 metaPropertyList.add(new MetaProperty(propName, propValue));
1071 return metaPropertyList;
1076 public Map<String, Boolean> getAutomationStatus(RemoteResource resource) {
1077 if (null == resource) {
1080 Map<String, Boolean> autoStatus = new HashMap<String, Boolean>();
1081 autoStatus.put(Constants.GET, resource.isGetAutomtnInProgress());
1082 autoStatus.put(Constants.PUT, resource.isPutAutomtnInProgress());
1083 autoStatus.put(Constants.POST, resource.isPostAutomtnInProgress());
1087 public Map<String, String> getDummyAttributes() {
1088 Map<String, String> attributes = new HashMap<String, String>();
1089 attributes.put("intensity", "1");
1090 attributes.put("power", "off");
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(),
1163 "[" + e.getClass().getSimpleName() + "]"
1164 + e.code().toString() + "-" + e.message());
1168 public void sendPutRequest(RemoteResource resource,
1169 List<PutPostAttributeModel> putPostModelList) {
1170 System.out.println(putPostModelList);
1171 System.out.println("ResourceManager: sendPutRequest");
1172 if (null == resource) {
1175 System.out.println("ResourceManager: resource not null");
1176 SimulatorRemoteResource resourceN = resource.getResource();
1177 if (null == resourceN) {
1180 System.out.println("ResourceManager: Native resource not null");
1181 Map<String, RemoteResourceAttribute> attMap = resource
1182 .getResourceAttributesMap();
1183 if (null == attMap || attMap.size() < 1) {
1186 System.out.println("ResourceManager: attrubutes obtained");
1187 SimulatorResourceModel resourceModel = getUpdatedResourceModel(attMap,
1189 System.out.println("ResourceModel exist?:" + (resourceModel != null));
1191 resourceN.put(resourceModel, null, putListener);
1192 } catch (SimulatorException e) {
1196 .log(Level.ERROR.ordinal(),
1198 "[" + e.getClass().getSimpleName() + "]"
1199 + e.code().toString() + "-" + e.message());
1201 System.out.println("ResourceManager: called native put");
1204 public void sendPostRequest(RemoteResource resource,
1205 List<PutPostAttributeModel> putPostModelList) {
1206 System.out.println(putPostModelList);
1207 if (null == resource) {
1210 SimulatorRemoteResource resourceN = resource.getResource();
1211 if (null == resourceN) {
1214 Map<String, RemoteResourceAttribute> attMap = resource
1215 .getResourceAttributesMap();
1216 if (null == attMap || attMap.size() < 1) {
1219 // Filter out the attributes whose modification status is true.
1220 Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
1221 PutPostAttributeModel model;
1222 while (itr.hasNext()) {
1224 if (!model.isModified()) {
1228 SimulatorResourceModel resourceModel = getUpdatedResourceModel(attMap,
1231 resourceN.post(resourceModel, null, postListener);
1232 } catch (SimulatorException e) {
1236 .log(Level.ERROR.ordinal(),
1238 "[" + e.getClass().getSimpleName() + "]"
1239 + e.code().toString() + "-" + e.message());
1243 private SimulatorResourceModel getUpdatedResourceModel(
1244 Map<String, RemoteResourceAttribute> attMap,
1245 List<PutPostAttributeModel> putPostModelList) {
1247 SimulatorResourceModel resourceModel = new SimulatorResourceModel();
1248 PutPostAttributeModel model;
1249 RemoteResourceAttribute attribute;
1251 Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
1252 while (itr.hasNext()) {
1254 attName = model.getAttName();
1255 attribute = attMap.get(attName);
1256 if (null == attribute) {
1259 attType = attribute.getAttValBaseType();
1260 if (attType == Type.INT) {
1263 attValue = Integer.parseInt(model.getAttValue());
1264 resourceModel.addAttributeInt(attName, attValue);
1265 } catch (NumberFormatException e) {
1269 .log(Level.ERROR.ordinal(), new Date(),
1271 } catch (SimulatorException e) {
1275 .log(Level.ERROR.ordinal(),
1277 "[" + e.getClass().getSimpleName() + "]"
1278 + e.code().toString() + "-"
1281 } else if (attType == Type.DOUBLE) {
1284 attValue = Double.parseDouble(model.getAttValue());
1285 resourceModel.addAttributeDouble(attName, attValue);
1286 } catch (NumberFormatException e) {
1290 .log(Level.ERROR.ordinal(), new Date(),
1292 } catch (SimulatorException e) {
1296 .log(Level.ERROR.ordinal(),
1298 "[" + e.getClass().getSimpleName() + "]"
1299 + e.code().toString() + "-"
1302 } else if (attType == Type.BOOL) {
1304 attValue = Boolean.parseBoolean(model.getAttValue());
1306 resourceModel.addAttributeBoolean(attName, attValue);
1307 } catch (SimulatorException e) {
1311 .log(Level.ERROR.ordinal(),
1313 "[" + e.getClass().getSimpleName() + "]"
1314 + e.code().toString() + "-"
1317 } else if (attType == Type.STRING) {
1319 attValue = model.getAttValue();
1321 resourceModel.addAttributeString(attName, attValue);
1322 } catch (SimulatorException e) {
1326 .log(Level.ERROR.ordinal(),
1328 "[" + e.getClass().getSimpleName() + "]"
1329 + e.code().toString() + "-"
1334 return resourceModel;
1337 public void sendObserveRequest(RemoteResource resource) {
1338 System.out.println("sendObserverRequest() entry");
1339 if (null == resource) {
1342 System.out.println("Resource is null:" + (resource == null));
1343 resource.setObserved(true);
1344 SimulatorRemoteResource resourceN = resource.getResource();
1345 if (null == resourceN) {
1349 resourceN.observe(SimulatorObserveType.OBSERVE, null,
1351 } catch (SimulatorException e) {
1355 .log(Level.ERROR.ordinal(),
1357 "[" + e.getClass().getSimpleName() + "]"
1358 + e.code().toString() + "-" + e.message());
1360 System.out.println("Observer called.");
1363 public void sendCancelObserveRequest(RemoteResource resource) {
1364 if (null == resource) {
1367 resource.setObserved(false);
1368 SimulatorRemoteResource resourceN = resource.getResource();
1369 if (null == resourceN) {
1373 resourceN.cancelObserve();
1374 } catch (SimulatorException e) {
1378 .log(Level.ERROR.ordinal(),
1380 "[" + e.getClass().getSimpleName() + "]"
1381 + e.code().toString() + "-" + e.message());
1385 public void startAutomationRequest(int reqType, RemoteResource resource) {
1386 if (null == resource) {
1389 SimulatorRemoteResource resourceN = resource.getResource();
1390 if (null == resourceN) {
1393 SimulatorVerificationType type = SimulatorVerificationType
1394 .getVerificationType(reqType);
1398 System.out.println("Before calling startVerification: " + reqType);
1401 autoId = resourceN.startVerification(type, verifyListener);
1402 System.out.println("After calling startVerification: " + autoId);
1404 if (reqType == Constants.GET_AUTOMATION_INDEX) {
1405 // resource.setGetAutomtnInProgress(true);
1406 resource.setGetAutomtnId(autoId);
1407 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
1408 // resource.setPutAutomtnInProgress(true);
1409 resource.setPutAutomtnId(autoId);
1410 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1411 // resource.setPostAutomtnInProgress(true);
1412 resource.setPostAutomtnId(autoId);
1415 } catch (SimulatorException e) {
1419 .log(Level.ERROR.ordinal(),
1421 "[" + e.getClass().getSimpleName() + "]"
1422 + e.code().toString() + "-" + e.message());
1426 public void stopAutomationRequest(int reqType, RemoteResource resource) {
1427 if (null == resource) {
1430 SimulatorRemoteResource resourceN = resource.getResource();
1431 if (null == resourceN) {
1435 if (reqType == Constants.GET_AUTOMATION_INDEX) {
1436 resource.setGetAutomtnInProgress(false);
1437 autoId = resource.getGetAutomtnId();
1438 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
1439 resource.setPutAutomtnInProgress(false);
1440 autoId = resource.getPutAutomtnId();
1441 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1442 resource.setPostAutomtnInProgress(false);
1443 autoId = resource.getPostAutomtnId();
1446 resourceN.stopVerification(autoId);
1447 } catch (SimulatorException e) {
1451 .log(Level.ERROR.ordinal(),
1453 "[" + e.getClass().getSimpleName() + "]"
1454 + e.code().toString() + "-" + e.message());
1458 public void setConfigFilePath(RemoteResource resource, String configFilePath) {
1459 if (null == resource) {
1462 SimulatorRemoteResource resourceN = resource.getResource();
1463 if (null == resourceN) {
1467 resourceN.configureRAMLPath(configFilePath);
1468 } catch (SimulatorException e) {
1472 .log(Level.ERROR.ordinal(),
1474 "[" + e.getClass().getSimpleName() + "]"
1475 + e.code().toString() + "-" + e.message());
1478 // Update the status
1479 resource.setConfigUploaded(true);
1481 // Notify the UI listeners
1482 configUploadedNotification(resource);
1485 public Image getImage(String resourceURI) {
1486 if (null == resourceURI) {
1489 URL url = Activator.getDefault().getBundle()
1490 .getEntry(getImageURL(resourceURI));
1494 return ImageDescriptor.createFromURL(url).createImage();
1497 private String getImageURL(String resourceURI) {
1498 // TODO: Hard-coding the image file name temporarily.
1499 // It will be included in a separate class which manages all image
1501 return "/icons/light_16x16.png";
1504 public void shutdown() {
1505 // TODO: To be implemented for clean-up activities.