Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / notification-manager / SampleApp / android / SampleConsumer / src / com / example / sample / consumer / SampleConsumer.java
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 package com.example.sample.consumer;
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.iotivity.base.ModeType;
27 import org.iotivity.base.ObserveType;
28 import org.iotivity.base.OcConnectivityType;
29 import org.iotivity.base.OcException;
30 import org.iotivity.base.OcHeaderOption;
31 import org.iotivity.base.OcPlatform;
32 import org.iotivity.base.OcRepresentation;
33 import org.iotivity.base.OcResource;
34 import org.iotivity.base.OcResource.OnDeleteListener;
35 import org.iotivity.base.OcResource.OnGetListener;
36 import org.iotivity.base.OcResource.OnObserveListener;
37 import org.iotivity.base.OcResource.OnPutListener;
38 import org.iotivity.base.PlatformConfig;
39 import org.iotivity.base.QualityOfService;
40 import org.iotivity.base.ServiceType;
41
42 import com.sec.android.iot.sampleconsumer.R;
43
44 import android.app.Activity;
45 import android.content.BroadcastReceiver;
46 import android.content.Context;
47 import android.content.Intent;
48 import android.os.Bundle;
49 import android.util.Log;
50 import android.view.View;
51 import android.widget.Button;
52 import android.widget.TextView;
53 import android.widget.Toast;
54
55 public class SampleConsumer extends Activity implements View.OnClickListener,
56     OcPlatform.OnResourceFoundListener, OnGetListener, OnDeleteListener,
57     OnObserveListener, OnPutListener
58 {
59         private final String TAG = "NMConsumer : " + this.getClass().getSimpleName();
60
61         public static final int OC_STACK_OK = 0;
62         public static final String OBSERVE = "Observe";
63         public static final String GET = "Get";
64         public static final String PUT = "Put";
65         public static final String DELETE = "Delete";
66         public static final String POST = "Post";
67         public static final String MESSAGE = "message";
68         public static String outputString = "";
69
70         private Button btn_observe;
71         private Button btn_get;
72         private Button btn_put;
73         private Button btn_post;
74         private Button btn_delete;
75         private Button btn_clean;
76
77         public TextView tv_found_uri;
78         public TextView tv_select_method_type;
79         public TextView tv_receive_result;
80         public TextView tv_current_log_result;
81
82         public String current_log_result = "";
83         public String found_uri = "";
84         public String select_method_type = "";
85         public String receive_result = "";
86
87         public static OcResource curResource;
88         public OcPlatform.OnResourceFoundListener OnResourceFoundListener;
89
90         public static int oc = 0;
91         /*
92          * To initialize UI Function Setting
93          * To execute initOICStack for running find resource
94          */
95         @Override
96         public void onCreate(Bundle savedInstanceState)
97         {
98             super.onCreate(savedInstanceState);
99
100             setContentView(R.layout.sampleconsumer_layout);
101
102             btn_observe = (Button) findViewById(R.id.btn_observe);
103             btn_get = (Button) findViewById(R.id.btn_get);
104             btn_put = (Button) findViewById(R.id.btn_put);
105             btn_post = (Button) findViewById(R.id.btn_post);
106             btn_delete = (Button) findViewById(R.id.btn_delete);
107             btn_clean = (Button) findViewById(R.id.btn_clean);
108             tv_found_uri = (TextView) findViewById(R.id.tv_found_resource_result);
109             tv_select_method_type = (TextView) findViewById(R.id.tv_selected_method_type);
110             tv_receive_result = (TextView) findViewById(R.id.tv_receive_result);
111             tv_current_log_result = (TextView) findViewById(R.id.tv_current_log_result);
112             btn_observe.setOnClickListener(this);
113             btn_get.setOnClickListener(this);
114             btn_put.setOnClickListener(this);
115             btn_post.setOnClickListener(this);
116             btn_delete.setOnClickListener(this);
117             btn_clean.setOnClickListener(this);
118
119             initOICStack();
120         }
121
122         @Override
123         protected void onDestroy()
124         {
125             super.onDestroy();
126             onStop();
127         }
128         /*
129          * To reset text String is null for cleaning log
130          *
131          */
132         @Override
133         protected void onStop()
134         {
135             super.onStop();
136
137             tv_found_uri = null;
138             tv_select_method_type = null;
139             tv_receive_result = null;
140             tv_current_log_result = null;
141
142             current_log_result = "";
143             found_uri = "";
144             select_method_type = "";
145             receive_result = "";
146             btn_observe.setClickable(true);
147             btn_get.setClickable(true);
148             btn_put.setClickable(true);
149             btn_delete.setClickable(true);
150         }
151
152         public void cleanLogString()
153         {
154             current_log_result = "";
155             found_uri = "";
156             select_method_type = "";
157             receive_result = "";
158
159             tv_current_log_result.setText(current_log_result);
160             tv_found_uri.setText(found_uri);
161             tv_select_method_type.setText(select_method_type);
162             tv_receive_result.setText(receive_result);
163         }
164
165         public void initOICStack()
166         {
167             PlatformConfig cfg = new PlatformConfig(this,ServiceType.IN_PROC,
168                                                     ModeType.CLIENT, "0.0.0.0", 0, QualityOfService.LOW);
169             OcPlatform.Configure(cfg);
170             current_log_result += "Created Platform...\n";
171             tv_current_log_result.setText(current_log_result);
172             Log.i(TAG, current_log_result);
173             findResourceCandidate();
174             PRINT();
175         }
176
177         public void findResourceCandidate()
178         {
179             nmfindResource("", "/oc/core?rt=Resource.Hosting");
180             current_log_result += "Finding Resource... \n";
181             tv_current_log_result.setText(current_log_result);
182             Log.i(TAG, current_log_result);
183         }
184
185         public void nmfindResource(String host, String resourceName)
186         {
187             try
188             {
189                 OcPlatform.findResource(host, resourceName, OcConnectivityType.ALL,this);
190
191             }
192             catch (OcException e)
193             {
194                 e.printStackTrace();
195                 current_log_result += e.getMessage() + "\n";
196                 tv_current_log_result.setText(current_log_result);
197                 Log.i(TAG, current_log_result);
198             }
199         }
200
201         /*
202          * No Use until Yet
203          */
204         public void getRepresentation(OcResource resource)
205         {
206             if (resource != null)
207             {
208                 current_log_result += "Getting Light Representation...\n";
209                 tv_current_log_result.setText(current_log_result);
210                 Log.i(TAG, current_log_result);
211             }
212         }
213
214         /*
215          * No Use until Yet
216          */
217         public void getLightRepresentation(OcResource resource)
218         {
219             if (resource != null)
220             {
221                 current_log_result += "Getting Light Representation...\n";
222                 tv_current_log_result.setText(current_log_result);
223                 Log.i(TAG, current_log_result);
224
225                 try
226                 {
227                     Map<String, String> queryParamsMap = new HashMap<String, String>();
228                     resource.get(queryParamsMap, this);
229                 }
230                 catch (OcException e)
231                 {
232                     e.printStackTrace();
233                     current_log_result += e.getMessage() + "\n";
234                     tv_current_log_result.setText(current_log_result);
235                     Log.i(TAG, current_log_result);
236                 }
237             }
238         }
239
240         public void PRINT()
241         {
242             current_log_result += "********************************************\n";
243             current_log_result += "*  method Type : 1 - Observe               *\n";
244             current_log_result += "*  method Type : 2 - Get                   *\n";
245             current_log_result += "*  method Type : 3 - Put                   *\n";
246             current_log_result += "*  method Type : 4 - Delete                *\n";
247             current_log_result += "********************************************\n";
248         }
249
250         public void startObserve(OcResource resource)
251         {
252             if (resource != null)
253             {
254                 Map<String, String> queryParamsMap = new HashMap<String, String>();
255                 current_log_result += "startObserve\n";
256                 tv_current_log_result.setText(current_log_result);
257                 Log.i(TAG, current_log_result);
258
259                 try
260                 {
261                     resource.observe(ObserveType.OBSERVE, queryParamsMap, this);
262                 }
263                 catch (OcException e)
264                 {
265                     e.printStackTrace();
266                     current_log_result += e.getMessage() + "\n";
267                     tv_current_log_result.setText(current_log_result);
268                     Log.i(TAG, current_log_result);
269                 }
270             }
271         }
272
273         public void startGet(OcResource resource)
274         {
275             if (resource != null)
276             {
277                 Map<String, String> queryParamsMap = new HashMap<String, String>();
278                 current_log_result += "startGet\n";
279                 tv_current_log_result.setText(current_log_result);
280                 Log.i(TAG, current_log_result);
281
282                 try
283                 {
284                     resource.get(queryParamsMap, this);
285                 }
286                 catch (OcException e)
287                 {
288                     e.printStackTrace();
289                     current_log_result += e.getMessage() + "\n";
290                     tv_current_log_result.setText(current_log_result);
291                     Log.i(TAG, current_log_result);
292                 }
293             }
294         }
295
296         public void startPut(OcResource resource)
297         {
298             if (resource != null)
299             {
300                 curResource = resource;
301                 OcRepresentation rep = new OcRepresentation();
302                 rep.setValueInt("temperature", 25);
303                 rep.setValueInt("humidity", 10);
304                 Map<String, String> queryParamsMap = new HashMap<String, String>();
305                 current_log_result += "startPut\n";
306                 tv_current_log_result.setText(current_log_result);
307                 Log.i(TAG, current_log_result);
308
309                 try
310                 {
311                     resource.put(rep, queryParamsMap, this);
312                 }
313                 catch (OcException e)
314                 {
315                     e.printStackTrace();
316                     current_log_result += e.getMessage() + "\n";
317                     tv_current_log_result.setText(current_log_result);
318                     Log.i(TAG, current_log_result);
319                 }
320             }
321         }
322
323         public void startDelete(OcResource resource) throws OcException
324         {
325             curResource = resource;
326             if (resource != null)
327             {
328                 resource.deleteResource(this);
329             }
330         }
331
332         @Override
333         public void onClick(View v)
334         {
335             switch (v.getId())
336             {
337                 case R.id.btn_observe:
338                     tv_select_method_type.setText(OBSERVE);
339                     Log.i(TAG, "Method: " + OBSERVE);
340                     startObserve(curResource);
341                     btn_observe.setClickable(false);
342                     break;
343                 case R.id.btn_get:
344                     tv_select_method_type.setText(GET);
345                     Log.i(TAG, "Method: " + GET);
346                     startGet(curResource);
347                     btn_get.setClickable(false);
348                     break;
349                 case R.id.btn_put:
350                     tv_select_method_type.setText(PUT);
351                     Log.i(TAG, "Method: " + PUT);
352                     startPut(curResource);
353                     btn_put.setClickable(false);
354                     break;
355                 case R.id.btn_post:
356                     tv_select_method_type.setText(POST);
357                     Toast.makeText(this, "Not Supported Yet", Toast.LENGTH_SHORT)
358                     .show();
359                     Log.i(TAG, "Method: " + POST);
360                     break;
361                 case R.id.btn_delete:
362                     tv_select_method_type.setText(DELETE);
363                     Log.i(TAG, "Method: " + DELETE);
364                 try {
365                     startDelete(curResource);
366                 } catch (OcException e) {
367                     // TODO Auto-generated catch block
368                     e.printStackTrace();
369                 }
370                     btn_delete.setClickable(false);
371                     break;
372                 case R.id.btn_clean:
373                     cleanLogString();
374                     Log.i(TAG, "Log textbox is cleared");
375                     break;
376
377                 default:
378                     break;
379             }
380
381         }
382
383         protected static int observe_count()
384         {
385             return ++oc;
386         }
387
388         public void viewText()
389         {
390             SampleConsumer.this.runOnUiThread(new Runnable()
391             {
392                 public void run()
393                 {
394                     if (receive_result != null)
395                     {
396                         tv_receive_result.setText(receive_result);
397                         Log.i(TAG, "Received: " + receive_result);
398                     }
399                     if (found_uri != null)
400                     {
401                         tv_found_uri.setText(found_uri);
402                         Log.i(TAG, "URI: " + found_uri);
403                     }
404                     if (current_log_result != null)
405                     {
406                         tv_current_log_result.setText(current_log_result);
407                         Log.i(TAG, current_log_result);
408                     }
409
410                 }
411             });
412         }
413
414         @Override
415         public synchronized void onResourceFound(OcResource resource)
416         {
417             // synchronized (this) {
418             receive_result = "FoundResource";
419             String resourceURI;
420             String hostAddress;
421             if (SampleConsumer.curResource != null)
422             {
423                 current_log_result += "Found another resource, ignoring\n";
424             }
425             if (resource != null)
426             {
427                 if (resource.getUri().equals("/a/TempHumSensor"))
428                 {
429                     current_log_result += "==============================\n";
430                     current_log_result += "DISCOVERED Resource(Consumer):\n";
431                     resourceURI = resource.getUri();
432                     hostAddress = resource.getHost();
433                     current_log_result += "URI of the resource: " + resourceURI
434                                           + "\n";
435                     current_log_result += "Host address of the resource: "
436                                           + hostAddress + "\n";
437                     SampleConsumer.curResource = resource;
438                 }
439                 else
440                 {
441                     current_log_result += "Uri is not correct.";
442                 }
443             }
444             else
445             {
446                 current_log_result += "Resource is invalid\n";
447             }
448
449             viewText();
450         }
451
452         public String getCurrent_log_result()
453         {
454             return current_log_result;
455         }
456
457         public void setCurrent_log_result(String current_log_result)
458         {
459             this.current_log_result = current_log_result;
460         }
461
462         public String getFound_uri()
463         {
464             return found_uri;
465         }
466
467         public void setFound_uri(String found_uri)
468         {
469             this.found_uri = found_uri;
470         }
471
472         public String getSelect_method_type()
473         {
474             return select_method_type;
475         }
476
477         public void setSelect_method_type(String select_method_type)
478         {
479             this.select_method_type = select_method_type;
480         }
481
482         public String getReceive_result()
483         {
484             return receive_result;
485         }
486
487         public void setReceive_result(String recieve_result)
488         {
489             this.receive_result = recieve_result;
490         }
491         public class MessageReceiver extends BroadcastReceiver
492         {
493                 @Override
494                 public void onReceive(Context context, Intent intent)
495                 {
496                     final String message = intent
497                                            .getStringExtra(MESSAGE);
498                     tv_current_log_result.setText(message);
499                     Log.i(TAG, message);
500                     viewText();
501                 }
502         }
503
504         @Override
505         public void onGetCompleted(List<OcHeaderOption> options,
506                                    OcRepresentation rep)
507         {
508             setReceive_result("onGet");
509             setCurrent_log_result(getCurrent_log_result()
510                                   + "GET request was successful\n"
511                                   + "GET request was successful\n"
512                                   + "URI: " + rep.getUri() + "\n"
513                                   + "Temperature: " + rep.getValueInt("temperature") + "\n"
514                                   + "Humidity: " + rep.getValueInt("humidity") + "\n");
515             viewText();
516             btn_get.setClickable(true);
517         }
518
519         @Override
520         public void onPutCompleted(List<OcHeaderOption> options,
521                                    OcRepresentation rep)
522         {
523             setReceive_result("onPut");
524             current_log_result += "PUT request was successful\n";
525
526             int humidity;
527             int temperature;
528             humidity = rep.getValueInt("humidity");
529             temperature = rep.getValueInt("temperature");
530
531             setCurrent_log_result(getCurrent_log_result() + "temperature: "
532                                   + temperature + "\n");
533             setCurrent_log_result(getCurrent_log_result() + "humidity: " + humidity
534                                   + "\n");
535             viewText();
536             btn_put.setClickable(true);
537         }
538
539         @Override
540         public void onObserveCompleted(List<OcHeaderOption> options,
541                                        OcRepresentation rep, int seqNum)
542         {
543             setReceive_result("onObserve");
544             setCurrent_log_result(getCurrent_log_result() + "SequenceNumber : "
545                                   + seqNum + "\n");
546             setCurrent_log_result("========================================================\n"
547                                   + "Receive OBSERVE RESULT:\n"
548                                   + "URI: "
549                                   + rep.getUri()
550                                   + "\n"
551                                   + "SequenceNumber: "
552                                   + seqNum
553                                   + "\n"
554                                   + "Temperature: "
555                                   + rep.getValueInt("temperature")
556                                   + "\n"
557                                   + "Humidity: "
558                                   + rep.getValueInt("humidity") + "\n");
559             if (SampleConsumer.observe_count() > 30)
560             {
561                 setCurrent_log_result(getCurrent_log_result()
562                                       + "Cancelling Observe...\n");
563                 try
564                 {
565                     SampleConsumer.curResource.cancelObserve();
566                 }
567                 catch (OcException e)
568                 {
569                     // TODO Auto-generated catch block
570                     e.printStackTrace();
571                     setCurrent_log_result(getCurrent_log_result()
572                                           + "Cancel result :" + e.getMessage() + "\n");
573                 }
574             }
575             viewText();
576         }
577
578         @Override
579         public void onDeleteCompleted(List<OcHeaderOption> options)
580         {
581             setReceive_result("onDelete");
582             viewText();
583             btn_delete.setClickable(true);
584         }
585
586         @Override
587         public void onPutFailed(Throwable arg0) {
588             // TODO Auto-generated method stub
589
590         }
591
592         @Override
593         public void onObserveFailed(Throwable arg0) {
594             // TODO Auto-generated method stub
595
596         }
597
598         @Override
599         public void onDeleteFailed(Throwable arg0) {
600             // TODO Auto-generated method stub
601
602         }
603
604         @Override
605         public void onGetFailed(Throwable arg0) {
606             // TODO Auto-generated method stub
607
608         }
609 }