Merge test folder of test branch
[iotivity.git] / test / src / iBtestapp / modules / re / android / src / org / iotivity / test / re / app / RETestAppAction.java
1 //******************************************************************\r
2 //\r
3 // Copyright 2016 Samsung Electronics All Rights Reserved.\r
4 //\r
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
6 //\r
7 // Licensed under the Apache License, Version 2.0 (the "License");\r
8 // you may not use this file except in compliance with the License.\r
9 // You may obtain a copy of the License at\r
10 //\r
11 //      http://www.apache.org/licenses/LICENSE-2.0\r
12 //\r
13 // Unless required by applicable law or agreed to in writing, software\r
14 // distributed under the License is distributed on an "AS IS" BASIS,\r
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16 // See the License for the specific language governing permissions and\r
17 // limitations under the License.\r
18 //\r
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
20 \r
21 package org.iotivity.test.re.app;\r
22 \r
23 import java.util.ArrayList;\r
24 \r
25 import org.iotivity.service.RcsException;\r
26 import org.iotivity.service.RcsResourceAttributes;\r
27 import org.iotivity.service.RcsValue;\r
28 import org.iotivity.service.client.RcsRemoteResourceObject;\r
29 import org.iotivity.service.server.RcsResourceObject;\r
30 import org.iotivity.service.server.RcsResourceObject.AutoNotifyPolicy;\r
31 \r
32 import static org.iotivity.test.re.app.REUtility.*;\r
33 \r
34 import org.iotivity.test.re.app.REAppHelper.LogLevel;\r
35 import org.iotivity.test.re.app.REAppHelper.ResourceFounderTask;\r
36 import org.iotivity.service.testapp.framework.Base;\r
37 \r
38 public class RETestAppAction extends Base {\r
39     private RcsRemoteResourceObject m_Resource = null;\r
40     private REAppHelper m_REAppHelper = null;\r
41     private Boolean m_IsResourceCreated = false;\r
42     private RcsResourceObject m_TempActuatorRes = null;\r
43     private static int m_Temperature = 0;\r
44 \r
45     public RETestAppAction() {\r
46         m_REAppHelper = REAppHelper.getInstance();\r
47         m_IsResourceCreated = false;\r
48     }\r
49 \r
50     public void createResource() {\r
51 \r
52         if (!m_IsResourceCreated) {\r
53             m_REAppHelper.showLog(LogLevel.INFO, "Creating Resource...", true);\r
54 \r
55             try {\r
56                 RcsResourceObject.Builder builderSwitch = new RcsResourceObject.Builder(\r
57                         BINARY_SWITCH_URI, BINARY_SWITCH_TYPE,\r
58                         DEFAULT_ACTUATOR_INTERFACE);\r
59 \r
60                 builderSwitch.setDiscoverable(true);\r
61                 builderSwitch.setObservable(true);\r
62 \r
63                 builderSwitch.build();\r
64 \r
65                 RcsResourceObject.Builder builderTempS = new RcsResourceObject.Builder(\r
66                         TEMPERATURE_SENSOR_URI, TEMPERATURE_RES_TYPE,\r
67                         DEFAULT_SENSOR_INTERFACE);\r
68 \r
69                 builderTempS.setDiscoverable(false);\r
70                 builderTempS.setObservable(false);\r
71 \r
72                 builderTempS.build();\r
73 \r
74                 RcsResourceObject.Builder builderTempA = new RcsResourceObject.Builder(\r
75                         TEMPERATURE_ACTUATOR_URI, TEMPERATURE_RES_TYPE,\r
76                         DEFAULT_SENSOR_INTERFACE);\r
77 \r
78                 builderTempA.setDiscoverable(true);\r
79                 builderTempA.setObservable(true);\r
80 \r
81                 m_TempActuatorRes = builderTempA.build();\r
82 \r
83                 m_TempActuatorRes.setAttribute(TEMPERATURE_UNITS_KEY,\r
84                         new RcsValue("C"));\r
85                 m_TempActuatorRes.setAttribute(TEMPERATURE_RANGE_KEY,\r
86                         new RcsValue("0,30"));\r
87                 m_TempActuatorRes.setAttribute(TEMPERATURE_TEMP_KEY,\r
88                         new RcsValue(m_Temperature));\r
89 \r
90                 m_TempActuatorRes\r
91                         .setGetRequestHandler(m_REAppHelper.m_GetRequestHandler);\r
92                 m_TempActuatorRes\r
93                         .setSetRequestHandler(m_REAppHelper.m_SetRequestHandler);\r
94                 m_TempActuatorRes.setAutoNotifyPolicy(AutoNotifyPolicy.UPDATED);\r
95 \r
96                 m_REAppHelper.waitInSecond(WAIT_TIME_DEFAULT);\r
97 \r
98                 m_IsResourceCreated = true;\r
99 \r
100                 m_REAppHelper.showLog(LogLevel.INFO,\r
101                         "Resource created successfully.", true);\r
102             } catch (Exception e) {\r
103                 m_REAppHelper.showLog(\r
104                         LogLevel.ERROR,\r
105                         "Resource creation failed, due to "\r
106                                 + e.getLocalizedMessage(), true);\r
107             }\r
108         } else {\r
109             m_REAppHelper.showLog(LogLevel.INFO, "Resource Already Created...",\r
110                     true);\r
111         }\r
112     }\r
113 \r
114     public void setAttributes() {\r
115         m_REAppHelper.showLog(LogLevel.INFO, "Setting Attributes...", true);\r
116 \r
117         if (m_IsResourceCreated && m_TempActuatorRes != null) {\r
118             if (++m_Temperature > 30)\r
119                 m_Temperature = 0;\r
120 \r
121             RcsValue tempValue = new RcsValue(m_Temperature);\r
122 \r
123             try {\r
124                 m_TempActuatorRes.setAttribute(TEMPERATURE_TEMP_KEY, tempValue);\r
125 \r
126                 m_REAppHelper.showLog(LogLevel.INFO,\r
127                         "Set Attributes Successfully.", true);\r
128             } catch (Exception e) {\r
129                 m_REAppHelper.showLog(\r
130                         LogLevel.ERROR,\r
131                         "Set Attributes failed, due to "\r
132                                 + e.getLocalizedMessage(), true);\r
133             }\r
134         } else {\r
135             m_REAppHelper.showLog(LogLevel.ERROR, "Create Resource First", true);\r
136         }\r
137     }\r
138 \r
139     public void getAttributes() {\r
140         m_REAppHelper.showLog(LogLevel.INFO, "Getting Attributes...", true);\r
141 \r
142         if (m_IsResourceCreated && m_TempActuatorRes != null) {\r
143             try {\r
144                 RcsResourceAttributes attrs = m_TempActuatorRes.getAttributes();\r
145 \r
146                 m_REAppHelper.printAttributes(attrs);\r
147             } catch (RcsException e) {\r
148                 m_REAppHelper.showLog(\r
149                         LogLevel.ERROR,\r
150                         "No Attribute found, because of "\r
151                                 + e.getLocalizedMessage(), true);\r
152             }\r
153         } else {\r
154             m_REAppHelper.showLog(LogLevel.ERROR, "Create Resource First", true);\r
155         }\r
156     }\r
157 \r
158     public void discoverResource() {\r
159         if (m_REAppHelper.g_IsDiscovering) {\r
160             m_REAppHelper.showLog(LogLevel.INFO,\r
161                     "Resource is already Discovering...", false);\r
162         } else {\r
163             m_REAppHelper\r
164                     .showLog(LogLevel.INFO, "Discovering Resource...", true);\r
165 \r
166             m_Resource = null;\r
167 \r
168             ArrayList<String> uris = new ArrayList<String>();\r
169             uris.add(BINARY_SWITCH_URI);\r
170             uris.add(TEMPERATURE_ACTUATOR_URI);\r
171 \r
172             m_REAppHelper.discoverAllResource(uris);\r
173 \r
174             try {\r
175                 ResourceFounderTask resFounder = REAppHelper.getInstance().new ResourceFounderTask();\r
176                 resFounder.execute(String.valueOf(WAIT_TIME_MAX));\r
177 \r
178             } catch (Exception e) {\r
179                 m_REAppHelper.showLog(LogLevel.ERROR,\r
180                         "Exception occurred while finding resource, exception is: "\r
181                                 + e.getLocalizedMessage(), true);\r
182             }\r
183         }\r
184     }\r
185 \r
186     public void discoverResourceByType() {\r
187         if (m_REAppHelper.g_IsDiscovering) {\r
188             m_REAppHelper.showLog(LogLevel.INFO,\r
189                     "Resource is already Discovering...", false);\r
190         } else {\r
191             m_REAppHelper\r
192                     .showLog(LogLevel.INFO, "Discovering Resource...", true);\r
193 \r
194             m_Resource = null;\r
195 \r
196             ArrayList<String> uris = new ArrayList<String>();\r
197             uris.add(TEMPERATURE_ACTUATOR_URI);\r
198 \r
199             m_REAppHelper.discoverResourceWithQuery(uris);\r
200 \r
201             try {\r
202                 ResourceFounderTask resFounder = REAppHelper.getInstance().new ResourceFounderTask();\r
203                 resFounder.execute(String.valueOf(WAIT_TIME_MAX));\r
204 \r
205             } catch (Exception e) {\r
206                 m_REAppHelper.showLog(LogLevel.ERROR,\r
207                         "Exception occurred while finding resource, exception is: "\r
208                                 + e.getLocalizedMessage(), true);\r
209             }\r
210         }\r
211     }\r
212 \r
213     public void discoverNonDiscoverableResource() {\r
214         if (m_REAppHelper.g_IsDiscovering) {\r
215             m_REAppHelper.showLog(LogLevel.INFO,\r
216                     "Resource is already Discovering...", false);\r
217         } else {\r
218             m_REAppHelper\r
219                     .showLog(LogLevel.INFO, "Discovering Resource...", true);\r
220 \r
221             m_Resource = null;\r
222 \r
223             ArrayList<String> uris = new ArrayList<String>();\r
224             uris.add(TEMPERATURE_SENSOR_URI);\r
225 \r
226             m_REAppHelper.discoverAllResource(uris);\r
227 \r
228             try {\r
229                 ResourceFounderTask resFounder = REAppHelper.getInstance().new ResourceFounderTask();\r
230                 resFounder.execute(String.valueOf(WAIT_TIME_MAX));\r
231 \r
232             } catch (Exception e) {\r
233                 m_REAppHelper.showLog(LogLevel.ERROR,\r
234                         "Exception occurred while finding resource, exception is: "\r
235                                 + e.getLocalizedMessage(), true);\r
236             }\r
237         }\r
238     }\r
239 \r
240     public void startMonitoring() {\r
241         m_REAppHelper.showLog(LogLevel.INFO, "Starting Resource Monitoring...",\r
242                 true);\r
243 \r
244         m_Resource = m_REAppHelper\r
245                 .getResourceFromSelection(TEMPERATURE_ACTUATOR_URI);\r
246 \r
247         if (m_REAppHelper.isResourceAvailable(m_Resource)) {\r
248             try {\r
249                 if (m_Resource.isMonitoring()) {\r
250                     m_REAppHelper.showLog(LogLevel.INFO,\r
251                             "Monitoring already started.", true);\r
252                 } else {\r
253                     m_Resource\r
254                             .startMonitoring(m_REAppHelper.m_OnStateChangedListener);\r
255 \r
256                     m_REAppHelper.waitInSecond(WAIT_TIME_MIN);\r
257 \r
258                     if (m_Resource.isMonitoring()) {\r
259                         m_REAppHelper.showLog(LogLevel.INFO,\r
260                                 "Monitoring started successfully.", false);\r
261                     } else {\r
262                         m_REAppHelper.showLog(LogLevel.INFO,\r
263                                 "Monitoring is not started yet.", false);\r
264                     }\r
265                 }\r
266             } catch (RcsException e) {\r
267                 m_REAppHelper.showLog(LogLevel.ERROR,\r
268                         "Exception occured in startMonitoring. Exception is "\r
269                                 + e.getLocalizedMessage(), false);\r
270             }\r
271         }\r
272     }\r
273 \r
274     public void stopMonitoring() {\r
275         m_REAppHelper.showLog(LogLevel.INFO, "Stopping Resource Monitoring...",\r
276                 true);\r
277 \r
278         m_Resource = m_REAppHelper\r
279                 .getResourceFromSelection(TEMPERATURE_ACTUATOR_URI);\r
280 \r
281         if (m_REAppHelper.isResourceAvailable(m_Resource)) {\r
282             try {\r
283                 if (m_Resource.isMonitoring()) {\r
284                     m_Resource.stopMonitoring();\r
285 \r
286                     m_REAppHelper.waitInSecond(WAIT_TIME_MIN);\r
287 \r
288                     if (!m_Resource.isMonitoring()) {\r
289                         m_REAppHelper.showLog(LogLevel.INFO,\r
290                                 "Monitoring stopped successfully.", false);\r
291                     } else {\r
292                         m_REAppHelper.showLog(LogLevel.INFO,\r
293                                 "Monitoring is not stopped yet.", false);\r
294                     }\r
295                 } else {\r
296                     m_REAppHelper.showLog(LogLevel.INFO,\r
297                             "Monitoring is not started yet.", false);\r
298                 }\r
299             } catch (RcsException e) {\r
300                 m_REAppHelper.showLog(LogLevel.ERROR,\r
301                         "Exception occured in stopMonitoring. Exception is "\r
302                                 + e.getLocalizedMessage(), false);\r
303             }\r
304         }\r
305     }\r
306 \r
307     public void getState() {\r
308         m_REAppHelper.showLog(LogLevel.INFO, "Getting Resource State...", true);\r
309 \r
310         m_Resource = m_REAppHelper\r
311                 .getResourceFromSelection(TEMPERATURE_ACTUATOR_URI);\r
312 \r
313         if (m_REAppHelper.isResourceAvailable(m_Resource)) {\r
314             try {\r
315                 m_REAppHelper.showLog(\r
316                         LogLevel.INFO,\r
317                         "Remote Resource State : "\r
318                                 + m_REAppHelper.getStringResourceState(m_Resource\r
319                                 .getState()), false);\r
320             } catch (RcsException e) {\r
321                 m_REAppHelper.showLog(\r
322                         LogLevel.ERROR,\r
323                         "Exception occured in getState. Exception is "\r
324                                 + e.getLocalizedMessage(), false);\r
325             }\r
326         }\r
327     }\r
328 \r
329     public void setRemoteAttributes() {\r
330         m_REAppHelper.showLog(LogLevel.INFO, "Setting Remote Attributes...",\r
331                 true);\r
332 \r
333         m_Resource = m_REAppHelper\r
334                 .getResourceFromSelection(TEMPERATURE_ACTUATOR_URI);\r
335 \r
336         if (m_REAppHelper.isResourceAvailable(m_Resource)) {\r
337             try {\r
338                 if (++m_Temperature > 30)\r
339                     m_Temperature = 0;\r
340 \r
341                 RcsValue tempValue = new RcsValue(m_Temperature);\r
342 \r
343                 RcsResourceAttributes attrs = new RcsResourceAttributes();\r
344                 attrs.put(TEMPERATURE_TEMP_KEY, tempValue);\r
345 \r
346                 m_Resource.setRemoteAttributes(attrs,\r
347                         m_REAppHelper.m_OnRemoteAttributesReceivedListener);\r
348 \r
349                 m_REAppHelper.waitInSecond(WAIT_TIME_DEFAULT);\r
350             } catch (RcsException e) {\r
351                 m_REAppHelper.showLog(LogLevel.ERROR,\r
352                         "Exception occured in setRemoteAttributes. Exception is "\r
353                                 + e.getLocalizedMessage(), false);\r
354             }\r
355         }\r
356     }\r
357 \r
358     public void getRemoteAttributes() {\r
359         m_REAppHelper.showLog(LogLevel.INFO, "Getting Remote Attributes", true);\r
360 \r
361         m_Resource = m_REAppHelper\r
362                 .getResourceFromSelection(TEMPERATURE_ACTUATOR_URI);\r
363 \r
364         if (m_REAppHelper.isResourceAvailable(m_Resource)) {\r
365             try {\r
366                 m_Resource\r
367                         .getRemoteAttributes(m_REAppHelper.m_OnRemoteAttributesReceivedListener);\r
368 \r
369                 m_REAppHelper.waitInSecond(WAIT_TIME_DEFAULT);\r
370             } catch (RcsException e) {\r
371                 m_REAppHelper.showLog(LogLevel.ERROR,\r
372                         "Exception occured in getRemoteAttributes. Exception is "\r
373                                 + e.getLocalizedMessage(), false);\r
374             }\r
375         }\r
376     }\r
377 \r
378     public void startCaching() {\r
379         m_REAppHelper.showLog(LogLevel.INFO, "Starting Resource Caching...",\r
380                 true);\r
381 \r
382         m_Resource = m_REAppHelper\r
383                 .getResourceFromSelection(TEMPERATURE_ACTUATOR_URI);\r
384 \r
385         if (m_REAppHelper.isResourceAvailable(m_Resource)) {\r
386             try {\r
387                 if (m_Resource.isCaching()) {\r
388                     m_REAppHelper.showLog(LogLevel.INFO,\r
389                             "Caching already started.", true);\r
390                 } else {\r
391                     m_Resource\r
392                             .startCaching(m_REAppHelper.m_OnCacheUpdatedListener);\r
393 \r
394                     m_REAppHelper.waitInSecond(WAIT_TIME_MIN);\r
395 \r
396                     if (m_Resource.isCaching()) {\r
397                         m_REAppHelper.showLog(LogLevel.INFO,\r
398                                 "Caching started successfully.", false);\r
399                     } else {\r
400                         m_REAppHelper.showLog(LogLevel.INFO,\r
401                                 "Caching is not started yet.", false);\r
402                     }\r
403                 }\r
404             } catch (RcsException e) {\r
405                 m_REAppHelper.showLog(\r
406                         LogLevel.ERROR,\r
407                         "Exception occured in startCaching. Exception is "\r
408                                 + e.getLocalizedMessage(), true);\r
409             }\r
410         }\r
411     }\r
412 \r
413     public void stopCaching() {\r
414         m_REAppHelper.showLog(LogLevel.INFO, "Stopping Resource Caching...",\r
415                 true);\r
416 \r
417         m_Resource = m_REAppHelper\r
418                 .getResourceFromSelection(TEMPERATURE_ACTUATOR_URI);\r
419 \r
420         if (m_REAppHelper.isResourceAvailable(m_Resource)) {\r
421             try {\r
422                 if (m_Resource.isCaching()) {\r
423                     m_Resource.stopCaching();\r
424 \r
425                     m_REAppHelper.waitInSecond(WAIT_TIME_MIN);\r
426 \r
427                     if (!m_Resource.isCaching()) {\r
428                         m_REAppHelper.showLog(LogLevel.INFO,\r
429                                 "Caching stopped successfully.", false);\r
430                     } else {\r
431                         m_REAppHelper.showLog(LogLevel.INFO,\r
432                                 "Caching is not stopped yet.", false);\r
433                     }\r
434                 } else {\r
435                     m_REAppHelper.showLog(LogLevel.INFO,\r
436                             "Caching is not started yet.", false);\r
437                 }\r
438             } catch (RcsException e) {\r
439                 m_REAppHelper.showLog(\r
440                         LogLevel.ERROR,\r
441                         "Exception occured in stopCaching. Exception is "\r
442                                 + e.getLocalizedMessage(), false);\r
443             }\r
444         }\r
445     }\r
446 \r
447     public void getCachedAttributes() {\r
448         m_REAppHelper.showLog(LogLevel.INFO, "Getting Cached Attributes", true);\r
449 \r
450         m_Resource = m_REAppHelper\r
451                 .getResourceFromSelection(TEMPERATURE_ACTUATOR_URI);\r
452 \r
453         if (m_REAppHelper.isResourceAvailable(m_Resource)) {\r
454             try {\r
455                 if (m_Resource.isCaching()) {\r
456                     RcsResourceAttributes attrs = m_Resource\r
457                             .getCachedAttributes();\r
458 \r
459                     m_REAppHelper.waitInSecond(WAIT_TIME_MIN);\r
460 \r
461                     m_REAppHelper.printAttributes(attrs);\r
462                 } else {\r
463                     m_REAppHelper.showLog(LogLevel.INFO,\r
464                             "Caching is not started yet.", false);\r
465                 }\r
466             } catch (RcsException e) {\r
467                 m_REAppHelper.showLog(LogLevel.ERROR,\r
468                         "Exception occured in getCachedAttributes. Exception is "\r
469                                 + e.getLocalizedMessage(), false);\r
470             }\r
471         }\r
472     }\r
473 \r
474     public void getCachedState() {\r
475         m_REAppHelper.showLog(LogLevel.INFO, "Getting Cached State", true);\r
476 \r
477         m_Resource = m_REAppHelper\r
478                 .getResourceFromSelection(TEMPERATURE_ACTUATOR_URI);\r
479 \r
480         if (m_REAppHelper.isResourceAvailable(m_Resource)) {\r
481             try {\r
482                 m_REAppHelper.showLog(\r
483                         LogLevel.INFO,\r
484                         "Cached State : "\r
485                                 + m_REAppHelper.getStringCacheState(m_Resource\r
486                                 .getCacheState()), false);\r
487             } catch (RcsException e) {\r
488                 m_REAppHelper.showLog(LogLevel.ERROR,\r
489                         "Exception occured in getCachedState. Exception is "\r
490                                 + e.getLocalizedMessage(), false);\r
491             }\r
492         }\r
493     }\r
494 \r
495     public void exitApplication() {\r
496         m_REAppHelper.showLog(LogLevel.INFO, "Quit from RETestApp...", true);\r
497 \r
498         m_REAppHelper.waitInSecond(WAIT_TIME_MIN);\r
499 \r
500         m_TempActuatorRes = null;\r
501 \r
502         m_Resource = null;\r
503         m_REAppHelper = null;\r
504         m_IsResourceCreated = false;\r
505 \r
506         android.os.Process.killProcess(android.os.Process.myPid());\r
507     }\r
508 }