2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package oic.simulator.clientcontroller.manager;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.List;
31 import oic.simulator.clientcontroller.Activator;
32 import oic.simulator.clientcontroller.listener.IConfigurationUpload;
33 import oic.simulator.clientcontroller.listener.IFindResourceUIListener;
34 import oic.simulator.clientcontroller.listener.IGetUIListener;
35 import oic.simulator.clientcontroller.listener.IObserveUIListener;
36 import oic.simulator.clientcontroller.listener.IPostUIListener;
37 import oic.simulator.clientcontroller.listener.IPutUIListener;
38 import oic.simulator.clientcontroller.listener.IResourceSelectionChangedUIListener;
39 import oic.simulator.clientcontroller.listener.IVerificationUIListener;
40 import oic.simulator.clientcontroller.remoteresource.MetaProperty;
41 import oic.simulator.clientcontroller.remoteresource.PutPostAttributeModel;
42 import oic.simulator.clientcontroller.remoteresource.RemoteResource;
43 import oic.simulator.clientcontroller.remoteresource.RemoteResourceAttribute;
44 import oic.simulator.clientcontroller.utils.Constants;
45 import oic.simulator.clientcontroller.utils.Utility;
47 import org.eclipse.jface.resource.ImageDescriptor;
48 import org.eclipse.swt.graphics.Image;
49 import org.oic.simulator.ILogger.Level;
50 import org.oic.simulator.ResourceAttribute;
51 import org.oic.simulator.ResourceAttribute.Range;
52 import org.oic.simulator.ResourceAttribute.Type;
53 import org.oic.simulator.SimulatorException;
54 import org.oic.simulator.SimulatorManager;
55 import org.oic.simulator.SimulatorResourceModel;
56 import org.oic.simulator.clientcontroller.IFindResourceListener;
57 import org.oic.simulator.clientcontroller.IGetListener;
58 import org.oic.simulator.clientcontroller.IObserveListener;
59 import org.oic.simulator.clientcontroller.IPostListener;
60 import org.oic.simulator.clientcontroller.IPutListener;
61 import org.oic.simulator.clientcontroller.IVerificationListener;
62 import org.oic.simulator.clientcontroller.SimulatorObserveType;
63 import org.oic.simulator.clientcontroller.SimulatorRemoteResource;
64 import org.oic.simulator.clientcontroller.SimulatorVerificationType;
67 * This class acts as an interface between the simulator java SDK and the
68 * various UI modules. It maintains all the details of resources and provides
69 * other UI modules with the information required. It also handles responses for
70 * find, GET, PUT, POST, Observe and automatic verification operations from
71 * native layer and propagates those events to the registered UI listeners.
73 public class ResourceManager {
75 private Set<String> lastKnownSearchTypes;
77 private RemoteResource currentResourceInSelection;
79 private IFindResourceListener findResourceListener;
80 private IGetListener getListener;
81 private IPutListener putListener;
82 private IPostListener postListener;
83 private IObserveListener observeListener;
84 private IVerificationListener verifyListener;
86 private ResponseSynchronizerThread synchronizerThread;
88 private Thread threadHandle;
90 private List<IFindResourceUIListener> findResourceUIListeners;
91 private List<IResourceSelectionChangedUIListener> resourceSelectionChangedUIListeners;
92 private List<IGetUIListener> getUIListeners;
93 private List<IPutUIListener> putUIListeners;
94 private List<IPostUIListener> postUIListeners;
95 private List<IObserveUIListener> observeUIListeners;
96 private List<IVerificationUIListener> verificationUIListeners;
97 private List<IConfigurationUpload> configUploadUIListeners;
99 // Map with Server ID as key and the complete object as the value
100 private Map<String, RemoteResource> resourceMap;
101 private List<RemoteResource> favoriteResources;
102 // Maintaining a list of resource URIs for favorite resources feature.
103 private List<String> favoriteURIList;
105 // Maintaining a list of observed resource URIs.
106 private List<String> observedResourceURIList;
108 public ResourceManager() {
109 resourceMap = new HashMap<String, RemoteResource>();
110 favoriteResources = new ArrayList<RemoteResource>();
111 favoriteURIList = new ArrayList<String>();
112 observedResourceURIList = new ArrayList<String>();
113 findResourceUIListeners = new ArrayList<IFindResourceUIListener>();
114 resourceSelectionChangedUIListeners = new ArrayList<IResourceSelectionChangedUIListener>();
115 getUIListeners = new ArrayList<IGetUIListener>();
116 putUIListeners = new ArrayList<IPutUIListener>();
117 postUIListeners = new ArrayList<IPostUIListener>();
118 observeUIListeners = new ArrayList<IObserveUIListener>();
119 verificationUIListeners = new ArrayList<IVerificationUIListener>();
120 configUploadUIListeners = new ArrayList<IConfigurationUpload>();
122 findResourceListener = new IFindResourceListener() {
125 public void onResourceCallback(
126 final SimulatorRemoteResource resourceN) {
127 synchronizerThread.addToQueue(new Runnable() {
130 System.out.println("onResourceCallback() entry");
131 if (null == resourceN) {
134 // If resource already exist, then ignore it.
135 String uid = resourceN.getId();
139 boolean exist = isUidExist(uid);
141 System.out.println("Duplicate resource found: ["
146 // Fetch the resource data
147 RemoteResource resource = fetchResourceDetails(resourceN);
148 if (null == resource) {
152 resource.setResource(resourceN);
154 String uri = resource.getResourceURI();
156 // Add resource to favorite list if it was in
157 // favorites list during find/refresh operation.
158 if (favoriteURIList.contains(uri)) {
159 addResourcetoFavorites(resource);
161 // Add resource to observed resources list if it was
162 // in observe list during find/refresh operation.
163 if (observedResourceURIList.contains(uri)) {
164 resource.setObserved(true);
168 // Add the resource in local data structure
169 addResourceDetails(resource);
171 // Notify the UI listener
172 newResourceFoundNotification(resource);
174 // Send an initial GET request to get the resource
177 resourceN.get(null, getListener);
178 } catch (SimulatorException e) {
182 .log(Level.ERROR.ordinal(),
184 "[" + e.getClass().getSimpleName()
185 + "]" + e.code().toString()
186 + "-" + e.message());
193 getListener = new IGetListener() {
195 public void onGetCompleted(final String uid,
196 final SimulatorResourceModel resourceModelN) {
197 synchronizerThread.addToQueue(new Runnable() {
200 // Handling the response which includes retrieving the
201 // attributes and updating the local model.
202 RemoteResource resource = handleResponse(uid,
204 if (null != resource) {
205 // Notify the UI listeners
206 getCompleteNotification(resource);
213 public void onGetFailed(Throwable th) {
214 synchronizerThread.addToQueue(new Runnable() {
222 putListener = new IPutListener() {
225 public void onPutCompleted(final String uid,
226 final SimulatorResourceModel resourceModelN) {
227 synchronizerThread.addToQueue(new Thread() {
230 // Handling the response which includes retrieving the
231 // attributes and updating the local model.
232 RemoteResource resource = handleResponse(uid,
234 if (null != resource) {
235 // Notify the UI listeners
236 putCompleteNotification(resource);
243 public void onPutFailed(Throwable th) {
244 synchronizerThread.addToQueue(new Runnable() {
252 postListener = new IPostListener() {
254 public void onPostCompleted(final String uid,
255 final SimulatorResourceModel resourceModelN) {
256 synchronizerThread.addToQueue(new Runnable() {
259 // Handling the response which includes retrieving the
260 // attributes and updating the local model.
261 RemoteResource resource = handleResponse(uid,
263 if (null != resource) {
264 // Notify the UI listeners
265 postCompleteNotification(resource);
272 public void onPostFailed(Throwable th) {
273 synchronizerThread.addToQueue(new Runnable() {
281 observeListener = new IObserveListener() {
284 public void onObserveCompleted(final String uid,
285 final SimulatorResourceModel resourceModelN, final int seq) {
286 System.out.println("ResourceManager: onObserveCallback()");
287 synchronizerThread.addToQueue(new Runnable() {
290 // Handling the response which includes retrieving the
291 // attributes and updating the local model.
292 RemoteResource resource = handleResponse(uid,
294 if (null != resource) {
295 // Notify the UI listeners
296 observeCompleteNotification(resource);
303 public void onObserveFailed(Throwable th) {
304 // TODO Auto-generated method stub
308 verifyListener = new IVerificationListener() {
311 public void onVerificationStarted(final String uid, final int autoId) {
312 System.out.println("onVefificationStarted: " + autoId);
313 synchronizerThread.addToQueue(new Runnable() {
316 RemoteResource resource = getResource(uid);
317 if (null == resource) {
320 // Update the automation status.
321 resource.updateAutomationStatus(autoId, true);
323 int autoType = resource.getAutomationtype(autoId);
325 // Notify the listeners.
326 verificationStartedNotification(resource, autoType);
332 public void onVerificationCompleted(final String uid,
334 System.out.println("onVefificationCompleted: " + autoId);
335 synchronizerThread.addToQueue(new Runnable() {
338 RemoteResource resource = getResource(uid);
339 if (null == resource) {
342 // Update the automation status.
343 resource.updateAutomationStatus(autoId, false);
345 int autoType = resource.getAutomationtype(autoId);
347 // Notify the listeners.
348 verificationCompletedNotification(resource, autoType);
354 public void onVerificationAborted(final String uid, final int autoId) {
355 System.out.println("onVefificationAborted: " + autoId);
356 synchronizerThread.addToQueue(new Runnable() {
359 RemoteResource resource = getResource(uid);
360 if (null == resource) {
363 // Update the automation status.
364 resource.updateAutomationStatus(autoId, false);
366 int autoType = resource.getAutomationtype(autoId);
368 // Notify the listeners.
369 verificationAbortedNotification(resource, autoType);
375 synchronizerThread = new ResponseSynchronizerThread();
376 threadHandle = new Thread(synchronizerThread);
377 threadHandle.setName("Simulator Client Controller Event Queue");
378 threadHandle.start();
381 private RemoteResource handleResponse(String uid,
382 SimulatorResourceModel resourceModelN) {
383 if (null == uid || null == resourceModelN) {
387 // Update the local model
388 RemoteResource resource;
389 resource = getResource(uid);
390 if (null == resource) {
394 resource.setResourceModel(resourceModelN);
395 Map<String, RemoteResourceAttribute> attributeMap = fetchResourceAttributesFromModel(resourceModelN);
397 // TODO: For debugging
398 if (null != attributeMap) {
399 RemoteResourceAttribute.printAttributes(attributeMap);
400 System.out.println("Attributes found: " + (null != attributeMap));
401 System.out.println("No of attributes: " + attributeMap.size());
403 resource.setResourceAttributesMap(attributeMap);
408 private static class ResponseSynchronizerThread implements Runnable {
410 LinkedList<Runnable> responseQueue = new LinkedList<Runnable>();
414 while (!Thread.interrupted()) {
415 synchronized (this) {
417 while (responseQueue.isEmpty()) {
421 } catch (InterruptedException e) {
427 synchronized (this) {
428 thread = responseQueue.pop();
432 } catch (Exception e) {
433 if (e instanceof InterruptedException) {
441 public void addToQueue(Runnable event) {
442 synchronized (this) {
443 responseQueue.add(event);
449 public void addResourceSelectionChangedUIListener(
450 IResourceSelectionChangedUIListener resourceSelectionChangedUIListener) {
451 synchronized (resourceSelectionChangedUIListeners) {
452 resourceSelectionChangedUIListeners
453 .add(resourceSelectionChangedUIListener);
457 public void addGetUIListener(IGetUIListener getUIListener) {
458 synchronized (getUIListeners) {
459 getUIListeners.add(getUIListener);
463 public void addPutUIListener(IPutUIListener putUIListener) {
464 synchronized (putUIListeners) {
465 putUIListeners.add(putUIListener);
469 public void addPostUIListener(IPostUIListener postUIListener) {
470 synchronized (postUIListeners) {
471 postUIListeners.add(postUIListener);
475 public void addObserveUIListener(IObserveUIListener observeUIListener) {
476 synchronized (observeUIListeners) {
477 observeUIListeners.add(observeUIListener);
481 public void addVerificationUIListener(
482 IVerificationUIListener verificationUIListener) {
483 synchronized (verificationUIListeners) {
484 verificationUIListeners.add(verificationUIListener);
488 public void addConfigUploadUIListener(IConfigurationUpload configListener) {
489 synchronized (configUploadUIListeners) {
490 configUploadUIListeners.add(configListener);
494 public void removeResourceSelectionChangedUIListener(
495 IResourceSelectionChangedUIListener listener) {
496 synchronized (resourceSelectionChangedUIListeners) {
498 && resourceSelectionChangedUIListeners.size() > 0) {
499 resourceSelectionChangedUIListeners.remove(listener);
504 public void removeGetUIListener(IGetUIListener getUIListener) {
505 synchronized (getUIListeners) {
506 getUIListeners.remove(getUIListener);
510 public void removePutUIListener(IPutUIListener putUIListener) {
511 synchronized (putUIListeners) {
512 putUIListeners.remove(putUIListener);
516 public void removePostUIListener(IPostUIListener postUIListener) {
517 synchronized (postUIListeners) {
518 postUIListeners.remove(postUIListener);
522 public void removeObserveUIListener(IObserveUIListener observeUIListener) {
523 synchronized (observeUIListeners) {
524 observeUIListeners.remove(observeUIListener);
528 public void removeVerificationUIListener(
529 IVerificationUIListener verificationUIListener) {
530 synchronized (verificationUIListeners) {
531 verificationUIListeners.remove(verificationUIListener);
535 public void removeConfigUploadUIListener(IConfigurationUpload configListener) {
536 synchronized (configUploadUIListeners) {
537 configUploadUIListeners.remove(configListener);
541 public void addResourcetoFavorites(RemoteResource resource) {
542 if (null == resource) {
545 resource.setFavorite(true);
546 synchronized (favoriteResources) {
547 favoriteResources.add(resource);
548 favoriteURIList.add(resource.getResourceURI());
552 public void removeResourceFromFavorites(RemoteResource resource) {
553 if (null == resource) {
556 resource.setFavorite(false);
557 synchronized (favoriteResources) {
558 favoriteResources.remove(resource);
562 public void removeResourceURIFromFavorites(RemoteResource resource) {
563 if (null == resource) {
566 synchronized (favoriteURIList) {
567 favoriteURIList.remove(resource.getResourceURI());
571 public void addObservedResourceURI(String resourceURI) {
572 synchronized (observedResourceURIList) {
573 observedResourceURIList.add(resourceURI);
577 public void removeObservedResourceURI(String resourceURI) {
578 synchronized (observedResourceURIList) {
579 observedResourceURIList.remove(resourceURI);
583 public boolean isResourceObserved(String resourceURI) {
584 boolean observed = false;
585 synchronized (observedResourceURIList) {
586 observed = observedResourceURIList.contains(resourceURI);
591 public synchronized RemoteResource getCurrentResourceInSelection() {
592 return currentResourceInSelection;
595 public synchronized void setCurrentResourceInSelection(
596 RemoteResource resource) {
597 this.currentResourceInSelection = resource;
600 private void addResourceDetails(RemoteResource remoteResource) {
601 if (null != remoteResource) {
602 synchronized (resourceMap) {
603 resourceMap.put(remoteResource.getuId(), remoteResource);
608 public void addFindresourceUIListener(IFindResourceUIListener listener) {
609 if (null == listener) {
612 synchronized (findResourceUIListeners) {
613 findResourceUIListeners.add(listener);
617 public void removeFindresourceUIListener(IFindResourceUIListener listener) {
618 if (null == listener) {
621 synchronized (findResourceUIListeners) {
622 findResourceUIListeners.remove(listener);
626 private RemoteResource fetchResourceDetails(
627 SimulatorRemoteResource remoteResourceN) {
628 if (null == remoteResourceN) {
631 RemoteResource remoteResource = new RemoteResource();
632 remoteResource.setuId(remoteResourceN.getId());
633 remoteResource.setResourceURI(remoteResourceN.getUri());
634 remoteResource.setHost(remoteResourceN.getHost());
635 remoteResource.setResourceTypes(remoteResourceN.getResourceTypes());
636 remoteResource.setResourceInterfaces(remoteResourceN
637 .getResourceInterfaces());
638 remoteResource.setConnectivityType(remoteResourceN
639 .getConnectivityType());
640 remoteResource.setObservable(remoteResourceN.getIsObservable());
641 return remoteResource;
644 private boolean isUidExist(String uid) {
646 synchronized (resourceMap) {
647 exist = resourceMap.containsKey(uid);
652 private RemoteResource getResource(String uid) {
656 RemoteResource resource;
657 synchronized (resourceMap) {
658 resource = resourceMap.get(uid);
663 private Map<String, RemoteResourceAttribute> fetchResourceAttributesFromModel(
664 SimulatorResourceModel resourceModelN) {
665 Map<String, RemoteResourceAttribute> resourceAttributeMap = null;
666 if (null != resourceModelN) {
667 Map<String, ResourceAttribute> attributeMapN;
669 attributeMapN = resourceModelN.getAttributes();
670 } catch (SimulatorException e) {
674 .log(Level.ERROR.ordinal(),
676 "[" + e.getClass().getSimpleName() + "]"
677 + e.code().toString() + "-"
681 if (null != attributeMapN) {
682 resourceAttributeMap = new HashMap<String, RemoteResourceAttribute>();
684 Set<String> attNameSet = attributeMapN.keySet();
687 ResourceAttribute attributeN;
688 RemoteResourceAttribute attribute;
689 Iterator<String> attNameItr = attNameSet.iterator();
690 while (attNameItr.hasNext()) {
691 attName = attNameItr.next();
692 attributeN = attributeMapN.get(attName);
693 if (null != attributeN) {
694 attribute = new RemoteResourceAttribute();
695 attribute.setResourceAttribute(attributeN);
696 attribute.setAttributeName(attName);
698 attValueObj = attributeN.getValue();
699 if (null != attValueObj) {
700 attribute.setAttributeValue(attValueObj);
703 // Set the attribute type
704 attribute.setAttValBaseType(attributeN.getBaseType());
705 attribute.setAttValType(attributeN.getType());
707 // Set the range and allowed values
708 Range range = attributeN.getRange();
710 attribute.setMinValue(range.getMin());
711 attribute.setMaxValue(range.getMax());
713 Object[] values = attributeN.getAllowedValues();
714 if (null != values && values.length > 0) {
715 List<Object> valueList = new ArrayList<Object>();
716 for (Object obj : values) {
719 attribute.setAllowedValues(valueList);
722 * Type baseType = attribute.getAttValBaseType();
724 * if(baseType == Type.INT) { //int[] values =
725 * attributeN.getAllowedValues();
726 * attribute.setAllowedValues
727 * (attributeN.getAllowedValues()); } else
728 * if(baseType == Type.DOUBLE) { double[] values =
729 * attributeN.getAllowedValues();
730 * attribute.setAllowedValues
731 * (Utility.converArrayToList(values)); } else
732 * if(baseType == Type.BOOL) { //boolean[] values =
733 * attributeN.getAllowedValues(); List<Object> obj =
734 * new ArrayList<Object>(); obj.add(true);
735 * obj.add(false); attribute.setAllowedValues(obj);
736 * } else if(baseType == Type.STRING) { String[]
737 * values = attributeN.getAllowedValues();
739 * setAllowedValues(Utility.converArrayToList
743 resourceAttributeMap.put(attName, attribute);
748 return resourceAttributeMap;
751 private void newResourceFoundNotification(RemoteResource resource) {
752 synchronized (findResourceUIListeners) {
753 if (findResourceUIListeners.size() > 0) {
754 IFindResourceUIListener listener;
755 Iterator<IFindResourceUIListener> listenerItr = findResourceUIListeners
757 while (listenerItr.hasNext()) {
758 listener = listenerItr.next();
759 if (null != listener) {
760 listener.onNewResourceFound(resource);
767 private void resourceSelectionChangedUINotification(RemoteResource resource) {
768 synchronized (resourceSelectionChangedUIListeners) {
769 if (resourceSelectionChangedUIListeners.size() > 0) {
770 IResourceSelectionChangedUIListener listener;
771 Iterator<IResourceSelectionChangedUIListener> listenerItr = resourceSelectionChangedUIListeners
773 while (listenerItr.hasNext()) {
774 listener = listenerItr.next();
775 if (null != listener) {
776 listener.onResourceSelectionChange(resource);
783 private void getCompleteNotification(RemoteResource resource) {
784 synchronized (getUIListeners) {
785 if (getUIListeners.size() > 0) {
786 IGetUIListener listener;
787 Iterator<IGetUIListener> listenerItr = getUIListeners
789 while (listenerItr.hasNext()) {
790 listener = listenerItr.next();
791 if (null != listener) {
792 listener.onGetCompleted(resource);
799 private void putCompleteNotification(RemoteResource resource) {
800 synchronized (putUIListeners) {
801 if (putUIListeners.size() > 0) {
802 IPutUIListener listener;
803 Iterator<IPutUIListener> listenerItr = putUIListeners
805 while (listenerItr.hasNext()) {
806 listener = listenerItr.next();
807 if (null != listener) {
808 listener.onPutCompleted(resource);
815 private void postCompleteNotification(RemoteResource resource) {
816 synchronized (postUIListeners) {
817 if (postUIListeners.size() > 0) {
818 IPostUIListener listener;
819 Iterator<IPostUIListener> listenerItr = postUIListeners
821 while (listenerItr.hasNext()) {
822 listener = listenerItr.next();
823 if (null != listener) {
824 listener.onPostCompleted(resource);
831 private void observeCompleteNotification(RemoteResource resource) {
832 synchronized (observeUIListeners) {
833 if (observeUIListeners.size() > 0) {
834 IObserveUIListener listener;
835 Iterator<IObserveUIListener> listenerItr = observeUIListeners
837 while (listenerItr.hasNext()) {
838 listener = listenerItr.next();
839 if (null != listener) {
840 listener.onObserveCompleted(resource);
847 private void verificationStartedNotification(RemoteResource resource,
849 synchronized (verificationUIListeners) {
850 if (verificationUIListeners.size() > 0) {
851 IVerificationUIListener listener;
852 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
854 while (listenerItr.hasNext()) {
855 listener = listenerItr.next();
856 if (null != listener) {
857 listener.onVerificationStarted(resource, autoType);
864 private void verificationAbortedNotification(RemoteResource resource,
866 synchronized (verificationUIListeners) {
867 if (verificationUIListeners.size() > 0) {
868 IVerificationUIListener listener;
869 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
871 while (listenerItr.hasNext()) {
872 listener = listenerItr.next();
873 if (null != listener) {
874 listener.onVerificationAborted(resource, autoType);
881 private void verificationCompletedNotification(RemoteResource resource,
883 synchronized (verificationUIListeners) {
884 if (verificationUIListeners.size() > 0) {
885 IVerificationUIListener listener;
886 Iterator<IVerificationUIListener> listenerItr = verificationUIListeners
888 while (listenerItr.hasNext()) {
889 listener = listenerItr.next();
890 if (null != listener) {
891 listener.onVerificationCompleted(resource, autoType);
898 private void configUploadedNotification(RemoteResource resource) {
899 synchronized (configUploadUIListeners) {
900 if (configUploadUIListeners.size() > 0) {
901 IConfigurationUpload listener;
902 Iterator<IConfigurationUpload> listenerItr = configUploadUIListeners
904 while (listenerItr.hasNext()) {
905 listener = listenerItr.next();
906 if (null != listener) {
907 listener.onConfigurationUploaded(resource);
914 // TODO: Temporarily used to display the resource in the UI
915 public List<String> getURIList() {
916 List<String> list = new ArrayList<String>();
917 synchronized (resourceMap) {
919 * Set<String> idSet = resourceMap.keySet(); Iterator<String> idItr
920 * = idSet.iterator(); String sId; RemoteResource resource;
921 * while(idItr.hasNext()) { sId = idItr.next(); resource =
922 * resourceMap.get(sId); if(null == resource) { continue; }
923 * list.add(resource.getResourceURI()); }
925 Set<String> uriSet = resourceMap.keySet();
926 Iterator<String> uriItr = uriSet.iterator();
928 while (uriItr.hasNext()) {
936 Collections.sort(list);
941 public synchronized Set<String> getLastKnownSearchTypes() {
942 return lastKnownSearchTypes;
945 public synchronized void setLastKnownSearchTypes(
946 Set<String> lastKnownSearchTypes) {
947 this.lastKnownSearchTypes = lastKnownSearchTypes;
950 public boolean findResourceRequest(Set<String> searchTypes) {
951 if (null == searchTypes || searchTypes.size() < 1) {
954 boolean result = false;
955 Iterator<String> searchItr = searchTypes.iterator();
957 while (searchItr.hasNext()) {
958 rType = searchItr.next();
960 SimulatorManager.findResource(rType, findResourceListener);
962 } catch (SimulatorException e) {
966 .log(Level.ERROR.ordinal(),
968 "[" + e.getClass().getSimpleName() + "]"
969 + e.code().toString() + "-"
976 public void deleteResources(final Set<String> searchTypes) {
977 if (null == searchTypes || searchTypes.size() < 1) {
982 Iterator<String> typeItr = searchTypes.iterator();
984 while (typeItr.hasNext()) {
985 resType = typeItr.next();
986 deleteResourcesByType(resType);
988 // Change the current resource in selection
989 updateCurrentResourceInSelection(searchTypes);
995 private void updateCurrentResourceInSelection(Set<String> searchTypes) {
996 if (null == searchTypes || searchTypes.size() < 1) {
999 RemoteResource resourceInSelection = getCurrentResourceInSelection();
1000 if (null == resourceInSelection) {
1003 List<String> typesOfSelection = resourceInSelection.getResourceTypes();
1004 if (null == typesOfSelection || typesOfSelection.size() < 1) {
1007 Iterator<String> itr = typesOfSelection.iterator();
1009 while (itr.hasNext()) {
1011 if (searchTypes.contains(type)) {
1012 setCurrentResourceInSelection(null);
1013 resourceSelectionChangedUINotification(null);
1019 private void deleteResourcesByType(String resourceType) {
1020 if (null == resourceType) {
1023 synchronized (resourceMap) {
1024 Set<String> keySet = resourceMap.keySet();
1025 if (null == keySet) {
1028 Iterator<String> keyItr = keySet.iterator();
1030 RemoteResource resource;
1033 while (keyItr.hasNext()) {
1034 uId = keyItr.next();
1035 resource = resourceMap.get(uId);
1036 if (null == resource) {
1039 types = resource.getResourceTypes();
1040 if (null != types) {
1041 exist = types.contains(resourceType);
1043 // Remove the resource
1045 // Remove the resource from favorites list.
1046 removeResourceFromFavorites(resource);
1053 public void resourceSelectionChanged(final RemoteResource resource) {
1057 setCurrentResourceInSelection(resource);
1058 // Notify all observers for resource selection change event
1059 resourceSelectionChangedUINotification(resource);
1064 public List<MetaProperty> getMetaProperties(RemoteResource resource) {
1065 if (null != resource) {
1069 List<MetaProperty> metaPropertyList = new ArrayList<MetaProperty>();
1071 for (int index = 0; index < Constants.META_PROPERTY_COUNT; index++) {
1072 propName = Constants.META_PROPERTIES[index];
1073 if (propName.equals(Constants.RESOURCE_URI)) {
1074 propValue = resource.getResourceURI();
1075 } else if (propName.equals(Constants.CONNECTIVITY_TYPE)) {
1076 propValue = resource.getConnectivityType().toString();
1077 } else if (propName.equals(Constants.OBSERVABLE)) {
1078 propValue = Utility.getObservableInString(resource
1081 } else if (propName.equals(Constants.RESOURCE_TYPES)) {
1082 List<String> types = resource.getResourceTypes();
1083 if (null != types) {
1084 propValue = types.toString();
1086 propValue = Constants.NOT_AVAILABLE;
1088 } else if (propName.equals(Constants.RESOURCE_INTERFACES)) {
1089 List<String> interfaces = resource.getResourceInterfaces();
1090 if (null != interfaces) {
1091 propValue = interfaces.toString();
1093 propValue = Constants.NOT_AVAILABLE;
1098 if (null != propValue) {
1099 metaPropertyList.add(new MetaProperty(propName, propValue));
1103 return metaPropertyList;
1108 public Map<String, Boolean> getAutomationStatus(RemoteResource resource) {
1109 if (null == resource) {
1112 Map<String, Boolean> autoStatus = new HashMap<String, Boolean>();
1113 autoStatus.put(Constants.GET, resource.isGetAutomtnInProgress());
1114 autoStatus.put(Constants.PUT, resource.isPutAutomtnInProgress());
1115 autoStatus.put(Constants.POST, resource.isPostAutomtnInProgress());
1119 public Map<String, String> getDummyAttributes() {
1120 Map<String, String> attributes = new HashMap<String, String>();
1121 attributes.put("intensity", "1");
1122 attributes.put("power", "off");
1126 public List<RemoteResource> getResourceList() {
1127 List<RemoteResource> resourceList = new ArrayList<RemoteResource>();
1128 synchronized (resourceMap) {
1129 Set<String> idSet = resourceMap.keySet();
1130 Iterator<String> idItr = idSet.iterator();
1131 RemoteResource resource;
1132 while (idItr.hasNext()) {
1133 resource = resourceMap.get(idItr.next());
1134 if (null != resource) {
1135 resourceList.add(resource);
1140 Collections.sort(resourceList, new Comparator<RemoteResource>() {
1141 public int compare(RemoteResource res1, RemoteResource res2) {
1142 String s1 = res1.getResourceURI();
1143 String s2 = res2.getResourceURI();
1145 String s1Part = s1.replaceAll("\\d", "");
1146 String s2Part = s2.replaceAll("\\d", "");
1148 if (s1Part.equalsIgnoreCase(s2Part)) {
1149 return extractInt(s1) - extractInt(s2);
1151 return s1.compareTo(s2);
1154 int extractInt(String s) {
1155 String num = s.replaceAll("\\D", "");
1156 // return 0 if no digits found
1157 return num.isEmpty() ? 0 : Integer.parseInt(num);
1161 return resourceList;
1164 public List<RemoteResource> getFavResourceList() {
1165 List<RemoteResource> resourceList;
1166 synchronized (favoriteResources) {
1167 resourceList = new ArrayList<RemoteResource>(favoriteResources);
1169 return resourceList;
1172 public String getAttributeValue(RemoteResource res, String attName) {
1173 if (null == res || null == attName) {
1176 return res.getAttributeValue(attName);
1179 public void sendGetRequest(RemoteResource resource) {
1180 if (null == resource) {
1183 SimulatorRemoteResource resourceN = resource.getResource();
1184 if (null == resourceN) {
1188 resourceN.get(null, getListener);
1189 } catch (SimulatorException e) {
1193 .log(Level.ERROR.ordinal(),
1195 "[" + e.getClass().getSimpleName() + "]"
1196 + e.code().toString() + "-" + e.message());
1200 public void sendPutRequest(RemoteResource resource,
1201 List<PutPostAttributeModel> putPostModelList) {
1202 System.out.println(putPostModelList);
1203 System.out.println("ResourceManager: sendPutRequest");
1204 if (null == resource) {
1207 System.out.println("ResourceManager: resource not null");
1208 SimulatorRemoteResource resourceN = resource.getResource();
1209 if (null == resourceN) {
1212 System.out.println("ResourceManager: Native resource not null");
1213 Map<String, RemoteResourceAttribute> attMap = resource
1214 .getResourceAttributesMap();
1215 if (null == attMap || attMap.size() < 1) {
1218 System.out.println("ResourceManager: attrubutes obtained");
1219 SimulatorResourceModel resourceModel = getUpdatedResourceModel(attMap,
1221 System.out.println("ResourceModel exist?:" + (resourceModel != null));
1223 resourceN.put(resourceModel, null, putListener);
1224 } catch (SimulatorException e) {
1228 .log(Level.ERROR.ordinal(),
1230 "[" + e.getClass().getSimpleName() + "]"
1231 + e.code().toString() + "-" + e.message());
1233 System.out.println("ResourceManager: called native put");
1236 public void sendPostRequest(RemoteResource resource,
1237 List<PutPostAttributeModel> putPostModelList) {
1238 System.out.println(putPostModelList);
1239 if (null == resource) {
1242 SimulatorRemoteResource resourceN = resource.getResource();
1243 if (null == resourceN) {
1246 Map<String, RemoteResourceAttribute> attMap = resource
1247 .getResourceAttributesMap();
1248 if (null == attMap || attMap.size() < 1) {
1251 // Filter out the attributes whose modification status is true.
1252 Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
1253 PutPostAttributeModel model;
1254 while (itr.hasNext()) {
1256 if (!model.isModified()) {
1260 SimulatorResourceModel resourceModel = getUpdatedResourceModel(attMap,
1263 resourceN.post(resourceModel, null, postListener);
1264 } catch (SimulatorException e) {
1268 .log(Level.ERROR.ordinal(),
1270 "[" + e.getClass().getSimpleName() + "]"
1271 + e.code().toString() + "-" + e.message());
1275 private SimulatorResourceModel getUpdatedResourceModel(
1276 Map<String, RemoteResourceAttribute> attMap,
1277 List<PutPostAttributeModel> putPostModelList) {
1279 SimulatorResourceModel resourceModel = new SimulatorResourceModel();
1280 PutPostAttributeModel model;
1281 RemoteResourceAttribute attribute;
1283 Iterator<PutPostAttributeModel> itr = putPostModelList.iterator();
1284 while (itr.hasNext()) {
1286 attName = model.getAttName();
1287 attribute = attMap.get(attName);
1288 if (null == attribute) {
1291 attType = attribute.getAttValBaseType();
1292 if (attType == Type.INT) {
1295 attValue = Integer.parseInt(model.getAttValue());
1296 resourceModel.addAttributeInt(attName, attValue);
1297 } catch (NumberFormatException e) {
1301 .log(Level.ERROR.ordinal(), new Date(),
1303 } catch (SimulatorException e) {
1307 .log(Level.ERROR.ordinal(),
1309 "[" + e.getClass().getSimpleName() + "]"
1310 + e.code().toString() + "-"
1313 } else if (attType == Type.DOUBLE) {
1316 attValue = Double.parseDouble(model.getAttValue());
1317 resourceModel.addAttributeDouble(attName, attValue);
1318 } catch (NumberFormatException e) {
1322 .log(Level.ERROR.ordinal(), new Date(),
1324 } catch (SimulatorException e) {
1328 .log(Level.ERROR.ordinal(),
1330 "[" + e.getClass().getSimpleName() + "]"
1331 + e.code().toString() + "-"
1334 } else if (attType == Type.BOOL) {
1336 attValue = Boolean.parseBoolean(model.getAttValue());
1338 resourceModel.addAttributeBoolean(attName, attValue);
1339 } catch (SimulatorException e) {
1343 .log(Level.ERROR.ordinal(),
1345 "[" + e.getClass().getSimpleName() + "]"
1346 + e.code().toString() + "-"
1349 } else if (attType == Type.STRING) {
1351 attValue = model.getAttValue();
1353 resourceModel.addAttributeString(attName, attValue);
1354 } catch (SimulatorException e) {
1358 .log(Level.ERROR.ordinal(),
1360 "[" + e.getClass().getSimpleName() + "]"
1361 + e.code().toString() + "-"
1366 return resourceModel;
1369 public void sendObserveRequest(RemoteResource resource) {
1370 System.out.println("sendObserverRequest() entry");
1371 if (null == resource) {
1374 System.out.println("Resource is null:" + (resource == null));
1375 resource.setObserved(true);
1376 SimulatorRemoteResource resourceN = resource.getResource();
1377 if (null == resourceN) {
1381 resourceN.startObserve(SimulatorObserveType.OBSERVE, null,
1383 // Add observed resource URI to show the proper status after every
1384 // find/refresh operations.
1385 addObservedResourceURI(resource.getResourceURI());
1386 } catch (SimulatorException e) {
1390 .log(Level.ERROR.ordinal(),
1392 "[" + e.getClass().getSimpleName() + "]"
1393 + e.code().toString() + "-" + e.message());
1395 System.out.println("Observer called.");
1398 public void sendCancelObserveRequest(RemoteResource resource) {
1399 if (null == resource) {
1402 resource.setObserved(false);
1403 SimulatorRemoteResource resourceN = resource.getResource();
1404 if (null == resourceN) {
1408 resourceN.stopObserve();
1409 // Remove observed resource URI to show the proper status after
1410 // every find/refresh operations.
1411 removeObservedResourceURI(resource.getResourceURI());
1412 } catch (SimulatorException e) {
1416 .log(Level.ERROR.ordinal(),
1418 "[" + e.getClass().getSimpleName() + "]"
1419 + e.code().toString() + "-" + e.message());
1423 public void startAutomationRequest(int reqType, RemoteResource resource) {
1424 if (null == resource) {
1427 SimulatorRemoteResource resourceN = resource.getResource();
1428 if (null == resourceN) {
1431 SimulatorVerificationType type = SimulatorVerificationType
1432 .getVerificationType(reqType);
1436 System.out.println("Before calling startVerification: " + reqType);
1439 autoId = resourceN.startVerification(type, verifyListener);
1440 System.out.println("After calling startVerification: " + autoId);
1442 if (reqType == Constants.GET_AUTOMATION_INDEX) {
1443 // resource.setGetAutomtnInProgress(true);
1444 resource.setGetAutomtnId(autoId);
1445 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
1446 // resource.setPutAutomtnInProgress(true);
1447 resource.setPutAutomtnId(autoId);
1448 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1449 // resource.setPostAutomtnInProgress(true);
1450 resource.setPostAutomtnId(autoId);
1453 } catch (SimulatorException e) {
1457 .log(Level.ERROR.ordinal(),
1459 "[" + e.getClass().getSimpleName() + "]"
1460 + e.code().toString() + "-" + e.message());
1464 public void stopAutomationRequest(int reqType, RemoteResource resource) {
1465 if (null == resource) {
1468 SimulatorRemoteResource resourceN = resource.getResource();
1469 if (null == resourceN) {
1473 if (reqType == Constants.GET_AUTOMATION_INDEX) {
1474 resource.setGetAutomtnInProgress(false);
1475 autoId = resource.getGetAutomtnId();
1476 } else if (reqType == Constants.PUT_AUTOMATION_INDEX) {
1477 resource.setPutAutomtnInProgress(false);
1478 autoId = resource.getPutAutomtnId();
1479 } else {// if(reqType == Constants.POST_AUTOMATION_INDEX) {
1480 resource.setPostAutomtnInProgress(false);
1481 autoId = resource.getPostAutomtnId();
1484 resourceN.stopVerification(autoId);
1485 } catch (SimulatorException e) {
1489 .log(Level.ERROR.ordinal(),
1491 "[" + e.getClass().getSimpleName() + "]"
1492 + e.code().toString() + "-" + e.message());
1496 public void setConfigFilePath(RemoteResource resource, String configFilePath) {
1497 if (null == resource) {
1500 SimulatorRemoteResource resourceN = resource.getResource();
1501 if (null == resourceN) {
1505 resourceN.setConfigInfo(configFilePath);
1506 } catch (SimulatorException e) {
1510 .log(Level.ERROR.ordinal(),
1512 "[" + e.getClass().getSimpleName() + "]"
1513 + e.code().toString() + "-" + e.message());
1516 // Update the status
1517 resource.setConfigUploaded(true);
1519 // Notify the UI listeners
1520 configUploadedNotification(resource);
1523 public Image getImage(String resourceURI) {
1524 if (null == resourceURI) {
1527 URL url = Activator.getDefault().getBundle()
1528 .getEntry(getImageURL(resourceURI));
1532 return ImageDescriptor.createFromURL(url).createImage();
1535 private String getImageURL(String resourceURI) {
1536 // TODO: Hard-coding the image file name temporarily.
1537 // It will be included in a separate class which manages all image
1539 return "/icons/light_16x16.png";
1542 public void shutdown() {
1543 // TODO: To be implemented for clean-up activities.