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 find,
70 * GET, PUT, POST, Observe and automatic verification operations from native layer and propagates
71 * 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 RemoteResourceAttribute.printAttributes(attributeMap);
388 System.out.println("Attributes found: " + (null != attributeMap));
389 System.out.println("No of attributes: " + attributeMap.size());
391 resource.setResourceAttributesMap(attributeMap);
395 private static class ResponseSynchronizerThread implements Runnable {
397 LinkedList<Runnable> responseQueue = new LinkedList<Runnable>();
401 while (!Thread.interrupted()) {
402 synchronized (this) {
404 while (responseQueue.isEmpty()) {
408 } catch (InterruptedException e) {
414 synchronized (this) {
415 thread = responseQueue.pop();
419 } catch (Exception e) {
420 if (e instanceof InterruptedException) {
428 public void addToQueue(Runnable event) {
429 synchronized (this) {
430 responseQueue.add(event);
436 public void addResourceSelectionChangedUIListener(
437 IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
438 synchronized (resourceSelectionChangedUIListeners) {
439 resourceSelectionChangedUIListeners
440 .add(resourceSelectionChangedUIListener);
444 public void addGetUIListener(IGetUIListener getUIListener) {
445 synchronized (getUIListeners) {
446 getUIListeners.add(getUIListener);
450 public void addPutUIListener(IPutUIListener putUIListener) {
451 synchronized (putUIListeners) {
452 putUIListeners.add(putUIListener);
456 public void addPostUIListener(IPostUIListener postUIListener) {
457 synchronized (postUIListeners) {
458 postUIListeners.add(postUIListener);
462 public void addObserveUIListener(IObserveUIListener observeUIListener) {
463 synchronized (observeUIListeners) {
464 observeUIListeners.add(observeUIListener);
468 public void addVerificationUIListener(
469 IVerificationUIListener verificationUIListener) {
470 synchronized (verificationUIListeners) {
471 verificationUIListeners.add(verificationUIListener);
475 public void addConfigUploadUIListener(IConfigurationUpload configListener) {
476 synchronized (configUploadUIListeners) {
477 configUploadUIListeners.add(configListener);
481 public void removeResourceSelectionChangedUIListener(
482 IResourceSelectionChangedUIListener listener) {
483 synchronized (resourceSelectionChangedUIListeners) {
485 && resourceSelectionChangedUIListeners.size() > 0) {
486 resourceSelectionChangedUIListeners.remove(listener);
491 public void removeGetUIListener(IGetUIListener getUIListener) {
492 synchronized (getUIListeners) {
493 getUIListeners.remove(getUIListener);
497 public void removePutUIListener(IPutUIListener putUIListener) {
498 synchronized (putUIListeners) {
499 putUIListeners.remove(putUIListener);
503 public void removePostUIListener(IPostUIListener postUIListener) {
504 synchronized (postUIListeners) {
505 postUIListeners.remove(postUIListener);
509 public void removeObserveUIListener(IObserveUIListener observeUIListener) {
510 synchronized (observeUIListeners) {
511 observeUIListeners.remove(observeUIListener);
515 public void removeVerificationUIListener(
516 IVerificationUIListener verificationUIListener) {
517 synchronized (verificationUIListeners) {
518 verificationUIListeners.remove(verificationUIListener);
522 public void removeConfigUploadUIListener(IConfigurationUpload configListener) {
523 synchronized (configUploadUIListeners) {
524 configUploadUIListeners.remove(configListener);
528 public void addResourcetoFavorites(RemoteResource resource) {
529 if (null == resource) {
532 resource.setFavorite(true);
533 synchronized (favoriteResources) {
534 favoriteResources.add(resource);
535 favoriteURIList.add(resource.getResourceURI());
539 public void removeResourceFromFavorites(RemoteResource resource) {
540 if (null == resource) {
543 resource.setFavorite(false);
544 synchronized (favoriteResources) {
545 favoriteResources.remove(resource);
549 public void removeResourceURIFromFavorites(RemoteResource resource) {
550 if (null == resource) {
553 synchronized (favoriteURIList) {
554 favoriteURIList.remove(resource.getResourceURI());
558 public synchronized RemoteResource getCurrentResourceInSelection() {
559 return currentResourceInSelection;
562 public synchronized void setCurrentResourceInSelection(
563 RemoteResource resource) {
564 this.currentResourceInSelection = resource;
567 private void addResourceDetails(RemoteResource remoteResource) {
568 if (null != remoteResource) {
569 synchronized (resourceMap) {
570 resourceMap.put(remoteResource.getuId(), remoteResource);
575 public void addFindresourceUIListener(IFindResourceUIListener listener) {
576 if (null == listener) {
579 synchronized (findResourceUIListeners) {
580 findResourceUIListeners.add(listener);
584 public void removeFindresourceUIListener(IFindResourceUIListener listener) {
585 if (null == listener) {
588 synchronized (findResourceUIListeners) {
589 findResourceUIListeners.remove(listener);
593 private RemoteResource fetchResourceDetails(
594 SimulatorRemoteResource remoteResourceN) {
595 if (null == remoteResourceN) {
598 RemoteResource remoteResource = new RemoteResource();
599 remoteResource.setuId(remoteResourceN.getId());
600 remoteResource.setResourceURI(remoteResourceN.getUri());
601 remoteResource.setHost(remoteResourceN.getHost());
602 remoteResource.setResourceTypes(remoteResourceN.getResourceTypes());
603 remoteResource.setResourceInterfaces(remoteResourceN
604 .getResourceInterfaces());
605 remoteResource.setConnectivityType(remoteResourceN
606 .getConnectivityType());
607 remoteResource.setObservable(remoteResourceN.getIsObservable());
608 return remoteResource;
611 private boolean isUidExist(String uid) {
613 synchronized (resourceMap) {
614 exist = resourceMap.containsKey(uid);
619 private RemoteResource getResource(String uid) {
623 RemoteResource resource;
624 synchronized (resourceMap) {
625 resource = resourceMap.get(uid);
630 private Map<String, RemoteResourceAttribute> fetchResourceAttributesFromModel(
631 SimulatorResourceModel resourceModelN) {
632 Map<String, RemoteResourceAttribute> resourceAttributeMap = null;
633 if (null != resourceModelN) {
634 Map<String, ResourceAttribute> attributeMapN;
635 attributeMapN = resourceModelN.getAttributes();
636 if (null != attributeMapN) {
637 resourceAttributeMap = new HashMap<String, RemoteResourceAttribute>();
639 Set<String> attNameSet = attributeMapN.keySet();
642 ResourceAttribute attributeN;
643 RemoteResourceAttribute attribute;
644 Iterator<String> attNameItr = attNameSet.iterator();
645 while (attNameItr.hasNext()) {
646 attName = attNameItr.next();
647 attributeN = attributeMapN.get(attName);
648 if (null != attributeN) {
649 attribute = new RemoteResourceAttribute();
650 attribute.setResourceAttribute(attributeN);
651 attribute.setAttributeName(attName);
653 attValueObj = attributeN.getValue();
654 if (null != attValueObj) {
655 attribute.setAttributeValue(attValueObj);
658 // Set the attribute type
659 attribute.setAttValBaseType(attributeN.getBaseType());
660 attribute.setAttValType(attributeN.getType());
662 // Set the range and allowed values
663 Range range = attributeN.getRange();
665 attribute.setMinValue(range.getMin());
666 attribute.setMaxValue(range.getMax());
668 Object[] values = attributeN.getAllowedValues();
669 if (null != values && values.length > 0) {
670 List<Object> valueList = new ArrayList<Object>();
671 for (Object obj : values) {
674 attribute.setAllowedValues(valueList);
677 * Type baseType = attribute.getAttValBaseType();
679 * if(baseType == Type.INT) { //int[] values =
680 * attributeN.getAllowedValues();
681 * attribute.setAllowedValues
682 * (attributeN.getAllowedValues()); } else
683 * if(baseType == Type.DOUBLE) { double[] values =
684 * attributeN.getAllowedValues();
685 * attribute.setAllowedValues
686 * (Utility.converArrayToList(values)); } else
687 * if(baseType == Type.BOOL) { //boolean[] values =
688 * attributeN.getAllowedValues(); List<Object> obj =
689 * new ArrayList<Object>(); obj.add(true);
690 * obj.add(false); attribute.setAllowedValues(obj);
691 * } else if(baseType == Type.STRING) { String[]
692 * values = attributeN.getAllowedValues();
694 * setAllowedValues(Utility.converArrayToList
698 resourceAttributeMap.put(attName, attribute);
703 return resourceAttributeMap;
706 private void newResourceFoundNotification(RemoteResource resource) {
707 synchronized (findResourceUIListeners) {
708 if (findResourceUIListeners.size() > 0) {
709 IFindResourceUIListener listener;
710 Iterator<IFindResourceUIListener> listenerItr = findResourceUIListeners
712 while (listenerItr.hasNext()) {
713 listener = listenerItr.next();
714 if (null != listener) {
715 listener.onNewResourceFound(resource);
722 private void resourceSelectionChangedUINotification(RemoteResource resource) {
723 synchronized (resourceSelectionChangedUIListeners) {
724 if (resourceSelectionChangedUIListeners.size() > 0) {
725 IResourceSelectionChangedUIListener listener;
726 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
728 while (listenerItr.hasNext()) {
729 listener = listenerItr.next();
730 if (null != listener) {
731 listener.onResourceSelectionChange(resource);
738 private void getCompleteNotification(RemoteResource resource) {
739 synchronized (getUIListeners) {
740 if (getUIListeners.size() > 0) {
741 IGetUIListener listener;
742 Iterator<IGetUIListener> listenerItr = getUIListeners
744 while (listenerItr.hasNext()) {
745 listener = listenerItr.next();
746 if (null != listener) {
747 listener.onGetCompleted(resource);
754 private void putCompleteNotification(RemoteResource resource) {
755 synchronized (putUIListeners) {
756 if (putUIListeners.size() > 0) {
757 IPutUIListener listener;
758 Iterator<IPutUIListener> listenerItr = putUIListeners
760 while (listenerItr.hasNext()) {
761 listener = listenerItr.next();
762 if (null != listener) {
763 listener.onPutCompleted(resource);
770 private void postCompleteNotification(RemoteResource resource) {
771 synchronized (postUIListeners) {
772 if (postUIListeners.size() > 0) {
773 IPostUIListener listener;
774 Iterator<IPostUIListener> listenerItr = postUIListeners
776 while (listenerItr.hasNext()) {
777 listener = listenerItr.next();
778 if (null != listener) {
779 listener.onPostCompleted(resource);
786 private void observeCompleteNotification(RemoteResource resource) {
787 synchronized (observeUIListeners) {
788 if (observeUIListeners.size() > 0) {
789 IObserveUIListener listener;
790 Iterator<IObserveUIListener> listenerItr = observeUIListeners
792 while (listenerItr.hasNext()) {
793 listener = listenerItr.next();
794 if (null != listener) {
795 listener.onObserveCompleted(resource);
802 private void verificationStartedNotification(RemoteResource resource,
804 synchronized (verificationUIListeners) {
805 if (verificationUIListeners.size() > 0) {
806 IVerificationUIListener listener;
807 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
809 while (listenerItr.hasNext()) {
810 listener = listenerItr.next();
811 if (null != listener) {
812 listener.onVerificationStarted(resource, autoType);
819 private void verificationAbortedNotification(RemoteResource resource,
821 synchronized (verificationUIListeners) {
822 if (verificationUIListeners.size() > 0) {
823 IVerificationUIListener listener;
824 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
826 while (listenerItr.hasNext()) {
827 listener = listenerItr.next();
828 if (null != listener) {
829 listener.onVerificationAborted(resource, autoType);
836 private void verificationCompletedNotification(RemoteResource resource,
838 synchronized (verificationUIListeners) {
839 if (verificationUIListeners.size() > 0) {
840 IVerificationUIListener listener;
841 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
843 while (listenerItr.hasNext()) {
844 listener = listenerItr.next();
845 if (null != listener) {
846 listener.onVerificationCompleted(resource, autoType);
853 private void configUploadedNotification(RemoteResource resource) {
854 synchronized (configUploadUIListeners) {
855 if (configUploadUIListeners.size() > 0) {
856 IConfigurationUpload listener;
857 Iterator<IConfigurationUpload> listenerItr = configUploadUIListeners
859 while (listenerItr.hasNext()) {
860 listener = listenerItr.next();
861 if (null != listener) {
862 listener.onConfigurationUploaded(resource);
869 // TODO: Temporarily used to display the resource in the UI
870 public List<String> getURIList() {
871 List<String> list = new ArrayList<String>();
872 synchronized (resourceMap) {
874 * Set<String> idSet = resourceMap.keySet(); Iterator<String> idItr
875 * = idSet.iterator(); String sId; RemoteResource resource;
876 * while(idItr.hasNext()) { sId = idItr.next(); resource =
877 * resourceMap.get(sId); if(null == resource) { continue; }
878 * list.add(resource.getResourceURI()); }
880 Set<String> uriSet = resourceMap.keySet();
881 Iterator<String> uriItr = uriSet.iterator();
883 while (uriItr.hasNext()) {
891 Collections.sort(list);
896 public synchronized Set<String> getLastKnownSearchTypes() {
897 return lastKnownSearchTypes;
900 public synchronized void setLastKnownSearchTypes(
901 Set<String> lastKnownSearchTypes) {
902 this.lastKnownSearchTypes = lastKnownSearchTypes;
905 public boolean findResourceRequest(Set<String> searchTypes) {
906 if (null == searchTypes || searchTypes.size() < 1) {
909 boolean result = false;
910 Iterator<String> searchItr = searchTypes.iterator();
912 while (searchItr.hasNext()) {
913 rType = searchItr.next();
915 SimulatorManager.findResources(rType, findResourceListener);
917 } catch (SimulatorException e) {
921 .log(Level.ERROR.ordinal(),
923 "[" + e.getClass().getSimpleName() + "]"
924 + e.code().toString() + "-"
931 public void deleteResources(final Set<String> searchTypes) {
932 if (null == searchTypes || searchTypes.size() < 1) {
937 Iterator<String> typeItr = searchTypes.iterator();
939 while (typeItr.hasNext()) {
940 resType = typeItr.next();
941 deleteResourcesByType(resType);
943 // Change the current resource in selection
944 updateCurrentResourceInSelection(searchTypes);
950 private void updateCurrentResourceInSelection(Set<String> searchTypes) {
951 if (null == searchTypes || searchTypes.size() < 1) {
954 RemoteResource resourceInSelection = getCurrentResourceInSelection();
955 if (null == resourceInSelection) {
958 List<String> typesOfSelection = resourceInSelection.getResourceTypes();
959 if (null == typesOfSelection || typesOfSelection.size() < 1) {
962 Iterator<String> itr = typesOfSelection.iterator();
964 while (itr.hasNext()) {
966 if (searchTypes.contains(type)) {
967 setCurrentResourceInSelection(null);
968 resourceSelectionChangedUINotification(null);
974 private void deleteResourcesByType(String resourceType) {
975 if (null == resourceType) {
978 synchronized (resourceMap) {
979 Set<String> keySet = resourceMap.keySet();
980 if (null == keySet) {
983 Iterator<String> keyItr = keySet.iterator();
985 RemoteResource resource;
988 while (keyItr.hasNext()) {
990 resource = resourceMap.get(uId);
991 if (null == resource) {
994 types = resource.getResourceTypes();
996 exist = types.contains(resourceType);
998 // Remove the resource
1000 removeResourceFromFavorites(resource);
1007 public void resourceSelectionChanged(final RemoteResource resource) {
1011 setCurrentResourceInSelection(resource);
1012 // Notify all observers for resource selection change event
1013 resourceSelectionChangedUINotification(resource);
1018 public List<MetaProperty> getMetaProperties(RemoteResource resource) {
1019 if (null != resource) {
1023 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1025 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1026 propName = Constants.META_PROPERTIES[index];
1027 if (propName.equals(Constants.RESOURCE_URI)) {
1028 propValue = resource.getResourceURI();
1029 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1030 propValue = resource.getConnectivityType().toString();
1031 } else if (propName.equals(Constants.OBSERVABLE)) {
1032 propValue = Utility.getObservableInString(resource
1035 } else if (propName.equals(Constants.RESOURCE_TYPES)) {
1036 List<String> types = resource.getResourceTypes();
1037 if (null != types) {
1038 propValue = types.toString();
1040 propValue = Constants.NOT_AVAILABLE;
1042 } else if (propName.equals(Constants.RESOURCE_INTERFACES)) {
1043 List<String> interfaces = resource.getResourceInterfaces();
1044 if (null != interfaces) {
1045 propValue = interfaces.toString();
1047 propValue = Constants.NOT_AVAILABLE;
1052 if (null != propValue) {
1053 metaPropertyList.add(new MetaProperty(propName, propValue));
1057 return metaPropertyList;
1062 public Map<String, Boolean> getAutomationStatus(RemoteResource resource) {
1063 if (null == resource) {
1066 Map<String, Boolean> autoStatus = new HashMap<String, Boolean>();
1067 autoStatus.put(Constants.GET, resource.isGetAutomtnInProgress());
1068 autoStatus.put(Constants.PUT, resource.isPutAutomtnInProgress());
1069 autoStatus.put(Constants.POST, resource.isPostAutomtnInProgress());
1073 public Map<String, String> getDummyAttributes() {
1074 Map<String, String> attributes = new HashMap<String, String>();
1075 attributes.put("intensity", "1");
1076 attributes.put("power", "off");
1080 public List<RemoteResource> getResourceList() {
1081 List<RemoteResource> resourceList = new ArrayList<RemoteResource>();
1082 synchronized (resourceMap) {
1083 Set<String> idSet = resourceMap.keySet();
1084 Iterator<String> idItr = idSet.iterator();
1085 RemoteResource resource;
1086 while (idItr.hasNext()) {
1087 resource = resourceMap.get(idItr.next());
1088 if (null != resource) {
1089 resourceList.add(resource);
1094 Collections.sort(resourceList, new Comparator<RemoteResource>() {
1095 public int compare(RemoteResource res1, RemoteResource res2) {
1096 String s1 = res1.getResourceURI();
1097 String s2 = res2.getResourceURI();
1099 String s1Part = s1.replaceAll("\\d", "");
1100 String s2Part = s2.replaceAll("\\d", "");
1102 if (s1Part.equalsIgnoreCase(s2Part)) {
1103 return extractInt(s1) - extractInt(s2);
1105 return s1.compareTo(s2);
1108 int extractInt(String s) {
1109 String num = s.replaceAll("\\D", "");
1110 // return 0 if no digits found
1111 return num.isEmpty() ? 0 : Integer.parseInt(num);
1115 return resourceList;
1118 public List<RemoteResource> getFavResourceList() {
1119 List<RemoteResource> resourceList;
1120 synchronized (favoriteResources) {
1121 resourceList = new ArrayList<RemoteResource>(favoriteResources);
1123 return resourceList;
1126 public String getAttributeValue(RemoteResource res, String attName) {
1127 if (null == res || null == attName) {
1130 return res.getAttributeValue(attName);
1133 public void sendGetRequest(RemoteResource resource) {
1134 if (null == resource) {
1137 SimulatorRemoteResource resourceN = resource.getResource();
1138 if (null == resourceN) {
1142 resourceN.get(null, getListener);
1143 } catch (SimulatorException e) {
1147 .log(Level.ERROR.ordinal(),
1149 "[" + e.getClass().getSimpleName() + "]"
1150 + e.code().toString() + "-" + e.message());
1154 public void sendPutRequest(RemoteResource resource,
1155 List<PutPostAttributeModel> putPostModelList) {
1156 System.out.println(putPostModelList);
1157 System.out.println("ResourceManager: sendPutRequest");
1158 if (null == resource) {
1161 System.out.println("ResourceManager: resource not null");
1162 SimulatorRemoteResource resourceN = resource.getResource();
1163 if (null == resourceN) {
1166 System.out.println("ResourceManager: Native resource not null");
1167 Map<String, RemoteResourceAttribute> attMap = resource
1168 .getResourceAttributesMap();
1169 if (null == attMap || attMap.size() < 1) {
1172 System.out.println("ResourceManager: attrubutes obtained");
1173 SimulatorResourceModel resourceModel = getUpdatedResourceModel(attMap,
1175 System.out.println("ResourceModel exist?:" + (resourceModel != null));
1177 resourceN.put(resourceModel, null, putListener);
1178 } catch (SimulatorException e) {
1182 .log(Level.ERROR.ordinal(),
1184 "[" + e.getClass().getSimpleName() + "]"
1185 + e.code().toString() + "-" + e.message());
1187 System.out.println("ResourceManager: called native put");
1190 public void sendPostRequest(RemoteResource resource,
1191 List<PutPostAttributeModel> putPostModelList) {
1192 System.out.println(putPostModelList);
1193 if (null == resource) {
1196 SimulatorRemoteResource resourceN = resource.getResource();
1197 if (null == resourceN) {
1200 Map<String, RemoteResourceAttribute> attMap = resource
1201 .getResourceAttributesMap();
1202 if (null == attMap || attMap.size() < 1) {
1205 // Filter out the attributes whose modification status is true.
1206 Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
1207 PutPostAttributeModel model;
1208 while (itr.hasNext()) {
1210 if (!model.isModified()) {
1214 SimulatorResourceModel resourceModel = getUpdatedResourceModel(attMap,
1217 resourceN.post(resourceModel, null, postListener);
1218 } catch (SimulatorException e) {
1222 .log(Level.ERROR.ordinal(),
1224 "[" + e.getClass().getSimpleName() + "]"
1225 + e.code().toString() + "-" + e.message());
1229 private SimulatorResourceModel getUpdatedResourceModel(
1230 Map<String, RemoteResourceAttribute> attMap,
1231 List<PutPostAttributeModel> putPostModelList) {
1233 SimulatorResourceModel resourceModel = new SimulatorResourceModel();
1234 PutPostAttributeModel model;
1235 RemoteResourceAttribute attribute;
1237 Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
1238 while (itr.hasNext()) {
1240 attName = model.getAttName();
1241 attribute = attMap.get(attName);
1242 attType = attribute.getAttValBaseType();
1243 if (attType == Type.INT) {
1246 attValue = Integer.parseInt(model.getAttValue());
1247 } catch (NumberFormatException e) {
1250 resourceModel.addAttribute(attName, attValue);
1251 } else if (attType == Type.DOUBLE) {
1254 attValue = Double.parseDouble(model.getAttValue());
1255 } catch (NumberFormatException e) {
1258 resourceModel.addAttribute(attName, attValue);
1259 } else if (attType == Type.BOOL) {
1261 attValue = Boolean.parseBoolean(model.getAttValue());
1262 resourceModel.addAttribute(attName, attValue);
1263 } else if (attType == Type.STRING) {
1265 attValue = model.getAttValue();
1266 resourceModel.addAttribute(attName, attValue);
1269 return resourceModel;
1272 public void sendObserveRequest(RemoteResource resource) {
1273 System.out.println("sendObserverRequest() entry");
1274 if (null == resource) {
1277 System.out.println("Resource is null:" + (resource == null));
1278 resource.setObserved(true);
1279 SimulatorRemoteResource resourceN = resource.getResource();
1280 if (null == resourceN) {
1284 resourceN.observe(SimulatorObserveType.OBSERVE, null,
1286 } catch (SimulatorException e) {
1290 .log(Level.ERROR.ordinal(),
1292 "[" + e.getClass().getSimpleName() + "]"
1293 + e.code().toString() + "-" + e.message());
1295 System.out.println("Observer called.");
1298 public void sendCancelObserveRequest(RemoteResource resource) {
1299 if (null == resource) {
1302 resource.setObserved(false);
1303 SimulatorRemoteResource resourceN = resource.getResource();
1304 if (null == resourceN) {
1308 resourceN.cancelObserve();
1309 } catch (SimulatorException e) {
1313 .log(Level.ERROR.ordinal(),
1315 "[" + e.getClass().getSimpleName() + "]"
1316 + e.code().toString() + "-" + e.message());
1320 public void startAutomationRequest(int reqType, RemoteResource resource) {
1321 if (null == resource) {
1324 SimulatorRemoteResource resourceN = resource.getResource();
1325 if (null == resourceN) {
1328 SimulatorVerificationType type = SimulatorVerificationType
1329 .getVerificationType(reqType);
1333 System.out.println("Before calling startVerification: " + reqType);
1336 autoId = resourceN.startVerification(type, verifyListener);
1337 System.out.println("After calling startVerification: " + autoId);
1339 if (reqType == Constants.GET_AUTOMATION_INDEX) {
1340 // resource.setGetAutomtnInProgress(true);
1341 resource.setGetAutomtnId(autoId);
1342 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
1343 // resource.setPutAutomtnInProgress(true);
1344 resource.setPutAutomtnId(autoId);
1345 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1346 // resource.setPostAutomtnInProgress(true);
1347 resource.setPostAutomtnId(autoId);
1350 } catch (SimulatorException e) {
1354 .log(Level.ERROR.ordinal(),
1356 "[" + e.getClass().getSimpleName() + "]"
1357 + e.code().toString() + "-" + e.message());
1361 public void stopAutomationRequest(int reqType, RemoteResource resource) {
1362 if (null == resource) {
1365 SimulatorRemoteResource resourceN = resource.getResource();
1366 if (null == resourceN) {
1370 if (reqType == Constants.GET_AUTOMATION_INDEX) {
1371 resource.setGetAutomtnInProgress(false);
1372 autoId = resource.getGetAutomtnId();
1373 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
1374 resource.setPutAutomtnInProgress(false);
1375 autoId = resource.getPutAutomtnId();
1376 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1377 resource.setPostAutomtnInProgress(false);
1378 autoId = resource.getPostAutomtnId();
1381 resourceN.stopVerification(autoId);
1382 } catch (SimulatorException e) {
1386 .log(Level.ERROR.ordinal(),
1388 "[" + e.getClass().getSimpleName() + "]"
1389 + e.code().toString() + "-" + e.message());
1393 public void setConfigFilePath(RemoteResource resource, String configFilePath) {
1394 if (null == resource) {
1397 SimulatorRemoteResource resourceN = resource.getResource();
1398 if (null == resourceN) {
1402 resourceN.configureRAMLPath(configFilePath);
1403 } catch (SimulatorException e) {
1407 .log(Level.ERROR.ordinal(),
1409 "[" + e.getClass().getSimpleName() + "]"
1410 + e.code().toString() + "-" + e.message());
1413 // Update the status
1414 resource.setConfigUploaded(true);
1416 // Notify the UI listeners
1417 configUploadedNotification(resource);
1420 public Image getImage(String resourceURI) {
1421 if (null == resourceURI) {
1424 URL url = Activator.getDefault().getBundle()
1425 .getEntry(getImageURL(resourceURI));
1429 return ImageDescriptor.createFromURL(url).createImage();
1432 private String getImageURL(String resourceURI) {
1433 // TODO: Hard-coding the image file name temporarily.
1434 // It will be included in a separate class which manages all image
1436 return "/icons/light_16x16.png";
1439 public void shutdown() {
1440 // TODO: To be implemented for clean-up activities.