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.InvalidArgsException;
51 import org.oic.simulator.ResourceAttribute;
52 import org.oic.simulator.ResourceAttribute.Range;
53 import org.oic.simulator.ResourceAttribute.Type;
54 import org.oic.simulator.SimulatorException;
55 import org.oic.simulator.SimulatorManager;
56 import org.oic.simulator.SimulatorResourceModel;
57 import org.oic.simulator.clientcontroller.IFindResourceListener;
58 import org.oic.simulator.clientcontroller.IGetListener;
59 import org.oic.simulator.clientcontroller.IObserveListener;
60 import org.oic.simulator.clientcontroller.IPostListener;
61 import org.oic.simulator.clientcontroller.IPutListener;
62 import org.oic.simulator.clientcontroller.IVerificationListener;
63 import org.oic.simulator.clientcontroller.SimulatorObserveType;
64 import org.oic.simulator.clientcontroller.SimulatorRemoteResource;
65 import org.oic.simulator.clientcontroller.SimulatorVerificationType;
68 * This class acts as an interface between the simulator java SDK and the
69 * various UI modules. It maintains all the details of resources and provides
70 * other UI modules with the information required. It also handles responses for
71 * find, GET, PUT, POST, Observe and automatic verification operations from
72 * native layer and propagates those events to the registered UI listeners.
74 public class ResourceManager {
76 private Set<String> lastKnownSearchTypes;
78 private RemoteResource currentResourceInSelection;
80 private IFindResourceListener findResourceListener;
81 private IGetListener getListener;
82 private IPutListener putListener;
83 private IPostListener postListener;
84 private IObserveListener observeListener;
85 private IVerificationListener verifyListener;
87 private ResponseSynchronizerThread synchronizerThread;
89 private Thread threadHandle;
91 private List<IFindResourceUIListener> findResourceUIListeners;
92 private List<IResourceSelectionChangedUIListener> resourceSelectionChangedUIListeners;
93 private List<IGetUIListener> getUIListeners;
94 private List<IPutUIListener> putUIListeners;
95 private List<IPostUIListener> postUIListeners;
96 private List<IObserveUIListener> observeUIListeners;
97 private List<IVerificationUIListener> verificationUIListeners;
98 private List<IConfigurationUpload> configUploadUIListeners;
100 // Map with Server ID as key and the complete object as the value
101 private Map<String, RemoteResource> resourceMap;
102 private List<RemoteResource> favoriteResources;
103 // Maintaining a list of resource URIs for favorite resources feature.
104 private List<String> favoriteURIList;
106 // Maintaining a list of observed resource URIs.
107 private List<String> observedResourceURIList;
109 public ResourceManager() {
110 resourceMap = new HashMap<String, RemoteResource>();
111 favoriteResources = new ArrayList<RemoteResource>();
112 favoriteURIList = new ArrayList<String>();
113 observedResourceURIList = new ArrayList<String>();
114 findResourceUIListeners = new ArrayList<IFindResourceUIListener>();
115 resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
116 getUIListeners = new ArrayList<IGetUIListener>();
117 putUIListeners = new ArrayList<IPutUIListener>();
118 postUIListeners = new ArrayList<IPostUIListener>();
119 observeUIListeners = new ArrayList<IObserveUIListener>();
120 verificationUIListeners = new ArrayList<IVerificationUIListener>();
121 configUploadUIListeners = new ArrayList<IConfigurationUpload>();
123 findResourceListener = new IFindResourceListener() {
126 public void onResourceCallback(
127 final SimulatorRemoteResource resourceN) {
128 synchronizerThread.addToQueue(new Runnable() {
131 System.out.println("onResourceCallback() entry");
132 if (null == resourceN) {
135 // If resource already exist, then ignore it.
136 String uid = resourceN.getId();
140 boolean exist = isUidExist(uid);
142 System.out.println("Duplicate resource found: ["
147 // Fetch the resource data
148 RemoteResource resource = fetchResourceDetails(resourceN);
149 if (null == resource) {
153 resource.setResource(resourceN);
155 String uri = resource.getResourceURI();
157 // Add resource to favorite list if it was in
158 // favorites list during find/refresh operation.
159 if (favoriteURIList.contains(uri)) {
160 addResourcetoFavorites(resource);
162 // Add resource to observed resources list if it was
163 // in observe list during find/refresh operation.
164 if (observedResourceURIList.contains(uri)) {
165 resource.setObserved(true);
169 // Add the resource in local data structure
170 addResourceDetails(resource);
172 // Notify the UI listener
173 newResourceFoundNotification(resource);
175 // Send an initial GET request to get the resource
178 resourceN.get(null, getListener);
179 } catch (SimulatorException e) {
183 .log(Level.ERROR.ordinal(),
185 "[" + e.getClass().getSimpleName()
186 + "]" + e.code().toString()
187 + "-" + e.message());
194 getListener = new IGetListener() {
196 public void onGetCompleted(final String uid,
197 final SimulatorResourceModel resourceModelN) {
198 synchronizerThread.addToQueue(new Runnable() {
201 // Handling the response which includes retrieving the
202 // attributes and updating the local model.
203 RemoteResource resource = handleResponse(uid,
205 if (null != resource) {
206 // Notify the UI listeners
207 getCompleteNotification(resource);
214 public void onGetFailed(Throwable th) {
215 synchronizerThread.addToQueue(new Runnable() {
223 putListener = new IPutListener() {
226 public void onPutCompleted(final String uid,
227 final SimulatorResourceModel resourceModelN) {
228 synchronizerThread.addToQueue(new Thread() {
231 // Handling the response which includes retrieving the
232 // attributes and updating the local model.
233 RemoteResource resource = handleResponse(uid,
235 if (null != resource) {
236 // Notify the UI listeners
237 putCompleteNotification(resource);
244 public void onPutFailed(Throwable th) {
245 synchronizerThread.addToQueue(new Runnable() {
253 postListener = new IPostListener() {
255 public void onPostCompleted(final String uid,
256 final SimulatorResourceModel resourceModelN) {
257 synchronizerThread.addToQueue(new Runnable() {
260 // Handling the response which includes retrieving the
261 // attributes and updating the local model.
262 RemoteResource resource = handleResponse(uid,
264 if (null != resource) {
265 // Notify the UI listeners
266 postCompleteNotification(resource);
273 public void onPostFailed(Throwable th) {
274 synchronizerThread.addToQueue(new Runnable() {
282 observeListener = new IObserveListener() {
285 public void onObserveCompleted(final String uid,
286 final SimulatorResourceModel resourceModelN, final int seq) {
287 System.out.println("ResourceManager: onObserveCallback()");
288 synchronizerThread.addToQueue(new Runnable() {
291 // Handling the response which includes retrieving the
292 // attributes and updating the local model.
293 RemoteResource resource = handleResponse(uid,
295 if (null != resource) {
296 // Notify the UI listeners
297 observeCompleteNotification(resource);
304 public void onObserveFailed(Throwable th) {
305 // TODO Auto-generated method stub
309 verifyListener = new IVerificationListener() {
312 public void onVerificationStarted(final String uid, final int autoId) {
313 System.out.println("onVefificationStarted: " + autoId);
314 synchronizerThread.addToQueue(new Runnable() {
317 RemoteResource resource = getResource(uid);
318 if (null == resource) {
321 // Update the automation status.
322 resource.updateAutomationStatus(autoId, true);
324 int autoType = resource.getAutomationtype(autoId);
326 // Notify the listeners.
327 verificationStartedNotification(resource, autoType);
333 public void onVerificationCompleted(final String uid,
335 System.out.println("onVefificationCompleted: " + autoId);
336 synchronizerThread.addToQueue(new Runnable() {
339 RemoteResource resource = getResource(uid);
340 if (null == resource) {
343 // Update the automation status.
344 resource.updateAutomationStatus(autoId, false);
346 int autoType = resource.getAutomationtype(autoId);
348 // Notify the listeners.
349 verificationCompletedNotification(resource, autoType);
355 public void onVerificationAborted(final String uid, final int autoId) {
356 System.out.println("onVefificationAborted: " + autoId);
357 synchronizerThread.addToQueue(new Runnable() {
360 RemoteResource resource = getResource(uid);
361 if (null == resource) {
364 // Update the automation status.
365 resource.updateAutomationStatus(autoId, false);
367 int autoType = resource.getAutomationtype(autoId);
369 // Notify the listeners.
370 verificationAbortedNotification(resource, autoType);
376 synchronizerThread = new ResponseSynchronizerThread();
377 threadHandle = new Thread(synchronizerThread);
378 threadHandle.setName("Simulator Client Controller Event Queue");
379 threadHandle.start();
382 private RemoteResource handleResponse(String uid,
383 SimulatorResourceModel resourceModelN) {
384 if (null == uid || null == resourceModelN) {
388 // Update the local model
389 RemoteResource resource;
390 resource = getResource(uid);
391 if (null == resource) {
395 resource.setResourceModel(resourceModelN);
396 Map<String, RemoteResourceAttribute> attributeMap = fetchResourceAttributesFromModel(resourceModelN);
398 // TODO: For debugging
399 if (null != attributeMap) {
400 RemoteResourceAttribute.printAttributes(attributeMap);
401 System.out.println("Attributes found: ");
402 System.out.println("No of attributes: " + attributeMap.size());
404 resource.setResourceAttributesMap(attributeMap);
409 private static class ResponseSynchronizerThread implements Runnable {
411 LinkedList<Runnable> responseQueue = new LinkedList<Runnable>();
415 while (!Thread.interrupted()) {
416 synchronized (this) {
418 while (responseQueue.isEmpty()) {
422 } catch (InterruptedException e) {
428 synchronized (this) {
429 thread = responseQueue.pop();
433 } catch (Exception e) {
434 if (e instanceof InterruptedException) {
442 public void addToQueue(Runnable event) {
443 synchronized (this) {
444 responseQueue.add(event);
450 public void addResourceSelectionChangedUIListener(
451 IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
452 synchronized (resourceSelectionChangedUIListeners) {
453 resourceSelectionChangedUIListeners
454 .add(resourceSelectionChangedUIListener);
458 public void addGetUIListener(IGetUIListener getUIListener) {
459 synchronized (getUIListeners) {
460 getUIListeners.add(getUIListener);
464 public void addPutUIListener(IPutUIListener putUIListener) {
465 synchronized (putUIListeners) {
466 putUIListeners.add(putUIListener);
470 public void addPostUIListener(IPostUIListener postUIListener) {
471 synchronized (postUIListeners) {
472 postUIListeners.add(postUIListener);
476 public void addObserveUIListener(IObserveUIListener observeUIListener) {
477 synchronized (observeUIListeners) {
478 observeUIListeners.add(observeUIListener);
482 public void addVerificationUIListener(
483 IVerificationUIListener verificationUIListener) {
484 synchronized (verificationUIListeners) {
485 verificationUIListeners.add(verificationUIListener);
489 public void addConfigUploadUIListener(IConfigurationUpload configListener) {
490 synchronized (configUploadUIListeners) {
491 configUploadUIListeners.add(configListener);
495 public void removeResourceSelectionChangedUIListener(
496 IResourceSelectionChangedUIListener listener) {
497 synchronized (resourceSelectionChangedUIListeners) {
499 && resourceSelectionChangedUIListeners.size() > 0) {
500 resourceSelectionChangedUIListeners.remove(listener);
505 public void removeGetUIListener(IGetUIListener getUIListener) {
506 synchronized (getUIListeners) {
507 getUIListeners.remove(getUIListener);
511 public void removePutUIListener(IPutUIListener putUIListener) {
512 synchronized (putUIListeners) {
513 putUIListeners.remove(putUIListener);
517 public void removePostUIListener(IPostUIListener postUIListener) {
518 synchronized (postUIListeners) {
519 postUIListeners.remove(postUIListener);
523 public void removeObserveUIListener(IObserveUIListener observeUIListener) {
524 synchronized (observeUIListeners) {
525 observeUIListeners.remove(observeUIListener);
529 public void removeVerificationUIListener(
530 IVerificationUIListener verificationUIListener) {
531 synchronized (verificationUIListeners) {
532 verificationUIListeners.remove(verificationUIListener);
536 public void removeConfigUploadUIListener(IConfigurationUpload configListener) {
537 synchronized (configUploadUIListeners) {
538 configUploadUIListeners.remove(configListener);
542 public void addResourcetoFavorites(RemoteResource resource) {
543 if (null == resource) {
546 resource.setFavorite(true);
547 synchronized (favoriteResources) {
548 favoriteResources.add(resource);
549 favoriteURIList.add(resource.getResourceURI());
553 public void removeResourceFromFavorites(RemoteResource resource) {
554 if (null == resource) {
557 resource.setFavorite(false);
558 synchronized (favoriteResources) {
559 favoriteResources.remove(resource);
563 public void removeResourceURIFromFavorites(RemoteResource resource) {
564 if (null == resource) {
567 synchronized (favoriteURIList) {
568 favoriteURIList.remove(resource.getResourceURI());
572 public void addObservedResourceURI(String resourceURI) {
573 synchronized (observedResourceURIList) {
574 observedResourceURIList.add(resourceURI);
578 public void removeObservedResourceURI(String resourceURI) {
579 synchronized (observedResourceURIList) {
580 observedResourceURIList.remove(resourceURI);
584 public boolean isResourceObserved(String resourceURI) {
586 synchronized (observedResourceURIList) {
587 observed = observedResourceURIList.contains(resourceURI);
592 public synchronized RemoteResource getCurrentResourceInSelection() {
593 return currentResourceInSelection;
596 public synchronized void setCurrentResourceInSelection(
597 RemoteResource resource) {
598 this.currentResourceInSelection = resource;
601 private void addResourceDetails(RemoteResource remoteResource) {
602 if (null != remoteResource) {
603 synchronized (resourceMap) {
604 resourceMap.put(remoteResource.getuId(), remoteResource);
609 public void addFindresourceUIListener(IFindResourceUIListener listener) {
610 if (null == listener) {
613 synchronized (findResourceUIListeners) {
614 findResourceUIListeners.add(listener);
618 public void removeFindresourceUIListener(IFindResourceUIListener listener) {
619 if (null == listener) {
622 synchronized (findResourceUIListeners) {
623 findResourceUIListeners.remove(listener);
627 private RemoteResource fetchResourceDetails(
628 SimulatorRemoteResource remoteResourceN) {
629 if (null == remoteResourceN) {
632 RemoteResource remoteResource = new RemoteResource();
633 remoteResource.setuId(remoteResourceN.getId());
634 remoteResource.setResourceURI(remoteResourceN.getUri());
635 remoteResource.setHost(remoteResourceN.getHost());
636 remoteResource.setResourceTypes(remoteResourceN.getResourceTypes());
637 remoteResource.setResourceInterfaces(remoteResourceN
638 .getResourceInterfaces());
639 remoteResource.setConnectivityType(remoteResourceN
640 .getConnectivityType());
641 remoteResource.setObservable(remoteResourceN.getIsObservable());
642 return remoteResource;
645 private boolean isUidExist(String uid) {
647 synchronized (resourceMap) {
648 exist = resourceMap.containsKey(uid);
653 private RemoteResource getResource(String uid) {
657 RemoteResource resource;
658 synchronized (resourceMap) {
659 resource = resourceMap.get(uid);
664 private Map<String, RemoteResourceAttribute> fetchResourceAttributesFromModel(
665 SimulatorResourceModel resourceModelN) {
666 Map<String, RemoteResourceAttribute> resourceAttributeMap = null;
667 if (null != resourceModelN) {
668 Map<String, ResourceAttribute> attributeMapN;
670 attributeMapN = resourceModelN.getAttributes();
671 } catch (SimulatorException e) {
675 .log(Level.ERROR.ordinal(),
677 "[" + e.getClass().getSimpleName() + "]"
678 + e.code().toString() + "-"
682 if (null != attributeMapN) {
683 resourceAttributeMap = new HashMap<String, RemoteResourceAttribute>();
685 Set<String> attNameSet = attributeMapN.keySet();
688 ResourceAttribute attributeN;
689 RemoteResourceAttribute attribute;
690 Iterator<String> attNameItr = attNameSet.iterator();
691 while (attNameItr.hasNext()) {
692 attName = attNameItr.next();
693 attributeN = attributeMapN.get(attName);
694 if (null != attributeN) {
695 attribute = new RemoteResourceAttribute();
696 attribute.setResourceAttribute(attributeN);
697 attribute.setAttributeName(attName);
699 attValueObj = attributeN.getValue();
700 if (null != attValueObj) {
701 attribute.setAttributeValue(attValueObj);
704 // Set the attribute type
705 attribute.setAttValBaseType(attributeN.getBaseType());
706 attribute.setAttValType(attributeN.getType());
708 // Set the range and allowed values
709 Range range = attributeN.getRange();
711 attribute.setMinValue(range.getMin());
712 attribute.setMaxValue(range.getMax());
714 Object[] values = attributeN.getAllowedValues();
715 if (null != values && values.length > 0) {
716 List<Object> valueList = new ArrayList<Object>();
717 for (Object obj : values) {
720 attribute.setAllowedValues(valueList);
723 * Type baseType = attribute.getAttValBaseType();
725 * if(baseType == Type.INT) { //int[] values =
726 * attributeN.getAllowedValues();
727 * attribute.setAllowedValues
728 * (attributeN.getAllowedValues()); } else
729 * if(baseType == Type.DOUBLE) { double[] values =
730 * attributeN.getAllowedValues();
731 * attribute.setAllowedValues
732 * (Utility.converArrayToList(values)); } else
733 * if(baseType == Type.BOOL) { //boolean[] values =
734 * attributeN.getAllowedValues(); List<Object> obj =
735 * new ArrayList<Object>(); obj.add(true);
736 * obj.add(false); attribute.setAllowedValues(obj);
737 * } else if(baseType == Type.STRING) { String[]
738 * values = attributeN.getAllowedValues();
740 * setAllowedValues(Utility.converArrayToList
744 resourceAttributeMap.put(attName, attribute);
749 return resourceAttributeMap;
752 private void newResourceFoundNotification(RemoteResource resource) {
753 synchronized (findResourceUIListeners) {
754 if (findResourceUIListeners.size() > 0) {
755 IFindResourceUIListener listener;
756 Iterator<IFindResourceUIListener> listenerItr = findResourceUIListeners
758 while (listenerItr.hasNext()) {
759 listener = listenerItr.next();
760 if (null != listener) {
761 listener.onNewResourceFound(resource);
768 private void resourceSelectionChangedUINotification(RemoteResource resource) {
769 synchronized (resourceSelectionChangedUIListeners) {
770 if (resourceSelectionChangedUIListeners.size() > 0) {
771 IResourceSelectionChangedUIListener listener;
772 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
774 while (listenerItr.hasNext()) {
775 listener = listenerItr.next();
776 if (null != listener) {
777 listener.onResourceSelectionChange(resource);
784 private void getCompleteNotification(RemoteResource resource) {
785 synchronized (getUIListeners) {
786 if (getUIListeners.size() > 0) {
787 IGetUIListener listener;
788 Iterator<IGetUIListener> listenerItr = getUIListeners
790 while (listenerItr.hasNext()) {
791 listener = listenerItr.next();
792 if (null != listener) {
793 listener.onGetCompleted(resource);
800 private void putCompleteNotification(RemoteResource resource) {
801 synchronized (putUIListeners) {
802 if (putUIListeners.size() > 0) {
803 IPutUIListener listener;
804 Iterator<IPutUIListener> listenerItr = putUIListeners
806 while (listenerItr.hasNext()) {
807 listener = listenerItr.next();
808 if (null != listener) {
809 listener.onPutCompleted(resource);
816 private void postCompleteNotification(RemoteResource resource) {
817 synchronized (postUIListeners) {
818 if (postUIListeners.size() > 0) {
819 IPostUIListener listener;
820 Iterator<IPostUIListener> listenerItr = postUIListeners
822 while (listenerItr.hasNext()) {
823 listener = listenerItr.next();
824 if (null != listener) {
825 listener.onPostCompleted(resource);
832 private void observeCompleteNotification(RemoteResource resource) {
833 synchronized (observeUIListeners) {
834 if (observeUIListeners.size() > 0) {
835 IObserveUIListener listener;
836 Iterator<IObserveUIListener> listenerItr = observeUIListeners
838 while (listenerItr.hasNext()) {
839 listener = listenerItr.next();
840 if (null != listener) {
841 listener.onObserveCompleted(resource);
848 private void verificationStartedNotification(RemoteResource resource,
850 synchronized (verificationUIListeners) {
851 if (verificationUIListeners.size() > 0) {
852 IVerificationUIListener listener;
853 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
855 while (listenerItr.hasNext()) {
856 listener = listenerItr.next();
857 if (null != listener) {
858 listener.onVerificationStarted(resource, autoType);
865 private void verificationAbortedNotification(RemoteResource resource,
867 synchronized (verificationUIListeners) {
868 if (verificationUIListeners.size() > 0) {
869 IVerificationUIListener listener;
870 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
872 while (listenerItr.hasNext()) {
873 listener = listenerItr.next();
874 if (null != listener) {
875 listener.onVerificationAborted(resource, autoType);
882 private void verificationCompletedNotification(RemoteResource resource,
884 synchronized (verificationUIListeners) {
885 if (verificationUIListeners.size() > 0) {
886 IVerificationUIListener listener;
887 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
889 while (listenerItr.hasNext()) {
890 listener = listenerItr.next();
891 if (null != listener) {
892 listener.onVerificationCompleted(resource, autoType);
899 private void configUploadedNotification(RemoteResource resource) {
900 synchronized (configUploadUIListeners) {
901 if (configUploadUIListeners.size() > 0) {
902 IConfigurationUpload listener;
903 Iterator<IConfigurationUpload> listenerItr = configUploadUIListeners
905 while (listenerItr.hasNext()) {
906 listener = listenerItr.next();
907 if (null != listener) {
908 listener.onConfigurationUploaded(resource);
915 // TODO: Temporarily used to display the resource in the UI
916 public List<String> getURIList() {
917 List<String> list = new ArrayList<String>();
918 synchronized (resourceMap) {
920 * Set<String> idSet = resourceMap.keySet(); Iterator<String> idItr
921 * = idSet.iterator(); String sId; RemoteResource resource;
922 * while(idItr.hasNext()) { sId = idItr.next(); resource =
923 * resourceMap.get(sId); if(null == resource) { continue; }
924 * list.add(resource.getResourceURI()); }
926 Set<String> uriSet = resourceMap.keySet();
927 Iterator<String> uriItr = uriSet.iterator();
929 while (uriItr.hasNext()) {
937 Collections.sort(list);
942 public synchronized Set<String> getLastKnownSearchTypes() {
943 return lastKnownSearchTypes;
946 public synchronized void setLastKnownSearchTypes(
947 Set<String> lastKnownSearchTypes) {
948 this.lastKnownSearchTypes = lastKnownSearchTypes;
951 public boolean findResourceRequest(Set<String> searchTypes) {
952 if (null == searchTypes || searchTypes.size() < 1) {
955 boolean result = false;
956 Iterator<String> searchItr = searchTypes.iterator();
958 while (searchItr.hasNext()) {
959 rType = searchItr.next();
961 SimulatorManager.findResource(rType, findResourceListener);
963 } catch (SimulatorException e) {
967 .log(Level.ERROR.ordinal(),
969 "[" + e.getClass().getSimpleName() + "]"
970 + e.code().toString() + "-"
977 public void deleteResources(final Set<String> searchTypes) {
978 if (null == searchTypes || searchTypes.size() < 1) {
983 Iterator<String> typeItr = searchTypes.iterator();
985 while (typeItr.hasNext()) {
986 resType = typeItr.next();
987 deleteResourcesByType(resType);
989 // Change the current resource in selection
990 updateCurrentResourceInSelection(searchTypes);
996 private void updateCurrentResourceInSelection(Set<String> searchTypes) {
997 if (null == searchTypes || searchTypes.size() < 1) {
1000 RemoteResource resourceInSelection = getCurrentResourceInSelection();
1001 if (null == resourceInSelection) {
1004 List<String> typesOfSelection = resourceInSelection.getResourceTypes();
1005 if (null == typesOfSelection || typesOfSelection.size() < 1) {
1008 Iterator<String> itr = typesOfSelection.iterator();
1010 while (itr.hasNext()) {
1012 if (searchTypes.contains(type)) {
1013 setCurrentResourceInSelection(null);
1014 resourceSelectionChangedUINotification(null);
1020 private void deleteResourcesByType(String resourceType) {
1021 if (null == resourceType) {
1024 synchronized (resourceMap) {
1025 Set<String> keySet = resourceMap.keySet();
1026 if (null == keySet) {
1029 Iterator<String> keyItr = keySet.iterator();
1031 RemoteResource resource;
1034 while (keyItr.hasNext()) {
1035 uId = keyItr.next();
1036 resource = resourceMap.get(uId);
1037 if (null == resource) {
1040 types = resource.getResourceTypes();
1041 if (null != types) {
1042 exist = types.contains(resourceType);
1044 // Remove the resource
1046 // Remove the resource from favorites list.
1047 removeResourceFromFavorites(resource);
1054 public void resourceSelectionChanged(final RemoteResource resource) {
1058 setCurrentResourceInSelection(resource);
1059 // Notify all observers for resource selection change event
1060 resourceSelectionChangedUINotification(resource);
1065 public List<MetaProperty> getMetaProperties(RemoteResource resource) {
1066 if (null != resource) {
1070 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1072 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1073 propName = Constants.META_PROPERTIES[index];
1074 if (propName.equals(Constants.RESOURCE_URI)) {
1075 propValue = resource.getResourceURI();
1076 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1077 propValue = resource.getConnectivityType().toString();
1078 } else if (propName.equals(Constants.OBSERVABLE)) {
1079 propValue = Utility.getObservableInString(resource
1082 } else if (propName.equals(Constants.RESOURCE_TYPES)) {
1083 List<String> types = resource.getResourceTypes();
1084 if (null != types) {
1085 propValue = types.toString();
1087 propValue = Constants.NOT_AVAILABLE;
1089 } else if (propName.equals(Constants.RESOURCE_INTERFACES)) {
1090 List<String> interfaces = resource.getResourceInterfaces();
1091 if (null != interfaces) {
1092 propValue = interfaces.toString();
1094 propValue = Constants.NOT_AVAILABLE;
1099 if (null != propValue) {
1100 metaPropertyList.add(new MetaProperty(propName, propValue));
1104 return metaPropertyList;
1109 public Map<String, Boolean> getAutomationStatus(RemoteResource resource) {
1110 if (null == resource) {
1113 Map<String, Boolean> autoStatus = new HashMap<String, Boolean>();
1114 autoStatus.put(Constants.GET, resource.isGetAutomtnInProgress());
1115 autoStatus.put(Constants.PUT, resource.isPutAutomtnInProgress());
1116 autoStatus.put(Constants.POST, resource.isPostAutomtnInProgress());
1120 public Map<String, String> getDummyAttributes() {
1121 Map<String, String> attributes = new HashMap<String, String>();
1122 attributes.put("intensity", "1");
1123 attributes.put("power", "off");
1127 public List<RemoteResource> getResourceList() {
1128 List<RemoteResource> resourceList = new ArrayList<RemoteResource>();
1129 synchronized (resourceMap) {
1130 Set<String> idSet = resourceMap.keySet();
1131 Iterator<String> idItr = idSet.iterator();
1132 RemoteResource resource;
1133 while (idItr.hasNext()) {
1134 resource = resourceMap.get(idItr.next());
1135 if (null != resource) {
1136 resourceList.add(resource);
1141 Collections.sort(resourceList, new Comparator<RemoteResource>() {
1142 public int compare(RemoteResource res1, RemoteResource res2) {
1143 String s1 = res1.getResourceURI();
1144 String s2 = res2.getResourceURI();
1146 String s1Part = s1.replaceAll("\\d", "");
1147 String s2Part = s2.replaceAll("\\d", "");
1149 if (s1Part.equalsIgnoreCase(s2Part)) {
1150 return extractInt(s1) - extractInt(s2);
1152 return s1.compareTo(s2);
1155 int extractInt(String s) {
1156 String num = s.replaceAll("\\D", "");
1157 // return 0 if no digits found
1158 return num.isEmpty() ? 0 : Integer.parseInt(num);
1162 return resourceList;
1165 public List<RemoteResource> getFavResourceList() {
1166 List<RemoteResource> resourceList;
1167 synchronized (favoriteResources) {
1168 resourceList = new ArrayList<RemoteResource>(favoriteResources);
1170 return resourceList;
1173 public String getAttributeValue(RemoteResource res, String attName) {
1174 if (null == res || null == attName) {
1177 return res.getAttributeValue(attName);
1180 public void sendGetRequest(RemoteResource resource) {
1181 if (null == resource) {
1184 SimulatorRemoteResource resourceN = resource.getResource();
1185 if (null == resourceN) {
1189 resourceN.get(null, getListener);
1190 } catch (SimulatorException e) {
1194 .log(Level.ERROR.ordinal(),
1196 "[" + e.getClass().getSimpleName() + "]"
1197 + e.code().toString() + "-" + e.message());
1201 public void sendPutRequest(RemoteResource resource,
1202 List<PutPostAttributeModel> putPostModelList) {
1203 System.out.println(putPostModelList);
1204 System.out.println("ResourceManager: sendPutRequest");
1205 if (null == resource) {
1208 System.out.println("ResourceManager: resource not null");
1209 SimulatorRemoteResource resourceN = resource.getResource();
1210 if (null == resourceN) {
1213 System.out.println("ResourceManager: Native resource not null");
1214 Map<String, RemoteResourceAttribute> attMap = resource
1215 .getResourceAttributesMap();
1216 if (null == attMap || attMap.size() < 1) {
1219 System.out.println("ResourceManager: attrubutes obtained");
1220 SimulatorResourceModel resourceModel = getUpdatedResourceModel(attMap,
1222 System.out.println("ResourceModel exist?:" + (resourceModel != null));
1224 resourceN.put(resourceModel, null, putListener);
1225 } catch (SimulatorException e) {
1229 .log(Level.ERROR.ordinal(),
1231 "[" + e.getClass().getSimpleName() + "]"
1232 + e.code().toString() + "-" + e.message());
1234 System.out.println("ResourceManager: called native put");
1237 public void sendPostRequest(RemoteResource resource,
1238 List<PutPostAttributeModel> putPostModelList) {
1239 System.out.println(putPostModelList);
1240 if (null == resource) {
1243 SimulatorRemoteResource resourceN = resource.getResource();
1244 if (null == resourceN) {
1247 Map<String, RemoteResourceAttribute> attMap = resource
1248 .getResourceAttributesMap();
1249 if (null == attMap || attMap.size() < 1) {
1252 // Filter out the attributes whose modification status is true.
1253 Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
1254 PutPostAttributeModel model;
1255 while (itr.hasNext()) {
1257 if (!model.isModified()) {
1261 SimulatorResourceModel resourceModel = getUpdatedResourceModel(attMap,
1264 resourceN.post(resourceModel, null, postListener);
1265 } catch (SimulatorException e) {
1269 .log(Level.ERROR.ordinal(),
1271 "[" + e.getClass().getSimpleName() + "]"
1272 + e.code().toString() + "-" + e.message());
1276 private SimulatorResourceModel getUpdatedResourceModel(
1277 Map<String, RemoteResourceAttribute> attMap,
1278 List<PutPostAttributeModel> putPostModelList) {
1280 SimulatorResourceModel resourceModel = new SimulatorResourceModel();
1281 PutPostAttributeModel model;
1282 RemoteResourceAttribute attribute;
1284 Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
1285 while (itr.hasNext()) {
1287 attName = model.getAttName();
1288 attribute = attMap.get(attName);
1289 if (null == attribute) {
1292 attType = attribute.getAttValBaseType();
1293 if (attType == Type.INT) {
1296 attValue = Integer.parseInt(model.getAttValue());
1297 resourceModel.addAttributeInt(attName, attValue);
1298 } catch (NumberFormatException e) {
1302 .log(Level.ERROR.ordinal(), new Date(),
1304 } catch (SimulatorException e) {
1308 .log(Level.ERROR.ordinal(),
1310 "[" + e.getClass().getSimpleName() + "]"
1311 + e.code().toString() + "-"
1314 } else if (attType == Type.DOUBLE) {
1317 attValue = Double.parseDouble(model.getAttValue());
1318 resourceModel.addAttributeDouble(attName, attValue);
1319 } catch (NumberFormatException e) {
1323 .log(Level.ERROR.ordinal(), new Date(),
1325 } catch (SimulatorException e) {
1329 .log(Level.ERROR.ordinal(),
1331 "[" + e.getClass().getSimpleName() + "]"
1332 + e.code().toString() + "-"
1335 } else if (attType == Type.BOOL) {
1337 attValue = Boolean.parseBoolean(model.getAttValue());
1339 resourceModel.addAttributeBoolean(attName, attValue);
1340 } catch (SimulatorException e) {
1344 .log(Level.ERROR.ordinal(),
1346 "[" + e.getClass().getSimpleName() + "]"
1347 + e.code().toString() + "-"
1350 } else if (attType == Type.STRING) {
1352 attValue = model.getAttValue();
1354 resourceModel.addAttributeString(attName, attValue);
1355 } catch (SimulatorException e) {
1359 .log(Level.ERROR.ordinal(),
1361 "[" + e.getClass().getSimpleName() + "]"
1362 + e.code().toString() + "-"
1367 return resourceModel;
1370 public void sendObserveRequest(RemoteResource resource) {
1371 System.out.println("sendObserverRequest() entry");
1372 if (null == resource) {
1375 System.out.println("Resource is null:" + (resource == null));
1376 resource.setObserved(true);
1377 SimulatorRemoteResource resourceN = resource.getResource();
1378 if (null == resourceN) {
1382 resourceN.startObserve(SimulatorObserveType.OBSERVE, null,
1384 // Add observed resource URI to show the proper status after every
1385 // find/refresh operations.
1386 addObservedResourceURI(resource.getResourceURI());
1387 } catch (SimulatorException e) {
1391 .log(Level.ERROR.ordinal(),
1393 "[" + e.getClass().getSimpleName() + "]"
1394 + e.code().toString() + "-" + e.message());
1396 System.out.println("Observer called.");
1399 public void sendCancelObserveRequest(RemoteResource resource) {
1400 if (null == resource) {
1403 resource.setObserved(false);
1404 SimulatorRemoteResource resourceN = resource.getResource();
1405 if (null == resourceN) {
1409 resourceN.stopObserve();
1410 // Remove observed resource URI to show the proper status after
1411 // every find/refresh operations.
1412 removeObservedResourceURI(resource.getResourceURI());
1413 } catch (SimulatorException e) {
1417 .log(Level.ERROR.ordinal(),
1419 "[" + e.getClass().getSimpleName() + "]"
1420 + e.code().toString() + "-" + e.message());
1424 public void startAutomationRequest(int reqType, RemoteResource resource) {
1425 if (null == resource) {
1428 SimulatorRemoteResource resourceN = resource.getResource();
1429 if (null == resourceN) {
1432 SimulatorVerificationType type = SimulatorVerificationType
1433 .getVerificationType(reqType);
1437 System.out.println("Before calling startVerification: " + reqType);
1440 autoId = resourceN.startVerification(type, verifyListener);
1441 System.out.println("After calling startVerification: " + autoId);
1443 if (reqType == Constants.GET_AUTOMATION_INDEX) {
1444 // resource.setGetAutomtnInProgress(true);
1445 resource.setGetAutomtnId(autoId);
1446 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
1447 // resource.setPutAutomtnInProgress(true);
1448 resource.setPutAutomtnId(autoId);
1449 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1450 // resource.setPostAutomtnInProgress(true);
1451 resource.setPostAutomtnId(autoId);
1454 } catch (SimulatorException e) {
1458 .log(Level.ERROR.ordinal(),
1460 "[" + e.getClass().getSimpleName() + "]"
1461 + e.code().toString() + "-" + e.message());
1465 public void stopAutomationRequest(int reqType, RemoteResource resource) {
1466 if (null == resource) {
1469 SimulatorRemoteResource resourceN = resource.getResource();
1470 if (null == resourceN) {
1474 if (reqType == Constants.GET_AUTOMATION_INDEX) {
1475 resource.setGetAutomtnInProgress(false);
1476 autoId = resource.getGetAutomtnId();
1477 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
1478 resource.setPutAutomtnInProgress(false);
1479 autoId = resource.getPutAutomtnId();
1480 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1481 resource.setPostAutomtnInProgress(false);
1482 autoId = resource.getPostAutomtnId();
1485 resourceN.stopVerification(autoId);
1486 } catch (InvalidArgsException e) {
1490 .log(Level.ERROR.ordinal(),
1492 "[" + e.getClass().getSimpleName() + "]"
1493 + e.code().toString() + "-"
1496 } catch (SimulatorException e) {
1500 .log(Level.ERROR.ordinal(),
1502 "[" + e.getClass().getSimpleName() + "]"
1503 + e.code().toString() + "-" + e.message());
1507 public void setConfigFilePath(RemoteResource resource, String configFilePath) {
1508 if (null == resource) {
1511 SimulatorRemoteResource resourceN = resource.getResource();
1512 if (null == resourceN) {
1516 resourceN.setConfigInfo(configFilePath);
1517 } catch (SimulatorException e) {
1521 .log(Level.ERROR.ordinal(),
1523 "[" + e.getClass().getSimpleName() + "]"
1524 + e.code().toString() + "-" + e.message());
1527 // Update the status
1528 resource.setConfigUploaded(true);
1530 // Notify the UI listeners
1531 configUploadedNotification(resource);
1534 public Image getImage(String resourceURI) {
1535 if (null == resourceURI) {
1538 URL url = Activator.getDefault().getBundle()
1539 .getEntry(getImageURL(resourceURI));
1543 return ImageDescriptor.createFromURL(url).createImage();
1546 private String getImageURL(String resourceURI) {
1547 // TODO: Hard-coding the image file name temporarily.
1548 // It will be included in a separate class which manages all image
1550 return "/icons/light_16x16.png";
1553 public void shutdown() {
1554 // TODO: To be implemented for clean-up activities.