1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 package com.example.sample.consumer;
22 import java.util.HashMap;
23 import java.util.List;
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;
42 import com.sec.android.iot.sampleconsumer.R;
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;
55 public class SampleConsumer extends Activity implements View.OnClickListener,
56 OcPlatform.OnResourceFoundListener, OnGetListener, OnDeleteListener,
57 OnObserveListener, OnPutListener
59 private final String TAG = "NMConsumer : " + this.getClass().getSimpleName();
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 = "";
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;
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;
82 public String current_log_result = "";
83 public String found_uri = "";
84 public String select_method_type = "";
85 public String receive_result = "";
87 public static OcResource curResource;
88 public OcPlatform.OnResourceFoundListener OnResourceFoundListener;
90 public static int oc = 0;
92 * To initialize UI Function Setting
93 * To execute initOICStack for running find resource
96 public void onCreate(Bundle savedInstanceState)
98 super.onCreate(savedInstanceState);
100 setContentView(R.layout.sampleconsumer_layout);
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);
123 protected void onDestroy()
129 * To reset text String is null for cleaning log
133 protected void onStop()
138 tv_select_method_type = null;
139 tv_receive_result = null;
140 tv_current_log_result = null;
142 current_log_result = "";
144 select_method_type = "";
146 btn_observe.setClickable(true);
147 btn_get.setClickable(true);
148 btn_put.setClickable(true);
149 btn_delete.setClickable(true);
152 public void cleanLogString()
154 current_log_result = "";
156 select_method_type = "";
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);
165 public void initOICStack()
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();
177 public void findResourceCandidate()
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);
185 public void nmfindResource(String host, String resourceName)
189 OcPlatform.findResource(host, resourceName, OcConnectivityType.ALL,this);
192 catch (OcException e)
195 current_log_result += e.getMessage() + "\n";
196 tv_current_log_result.setText(current_log_result);
197 Log.i(TAG, current_log_result);
204 public void getRepresentation(OcResource resource)
206 if (resource != null)
208 current_log_result += "Getting Light Representation...\n";
209 tv_current_log_result.setText(current_log_result);
210 Log.i(TAG, current_log_result);
217 public void getLightRepresentation(OcResource resource)
219 if (resource != null)
221 current_log_result += "Getting Light Representation...\n";
222 tv_current_log_result.setText(current_log_result);
223 Log.i(TAG, current_log_result);
227 Map<String, String> queryParamsMap = new HashMap<String, String>();
228 resource.get(queryParamsMap, this);
230 catch (OcException e)
233 current_log_result += e.getMessage() + "\n";
234 tv_current_log_result.setText(current_log_result);
235 Log.i(TAG, current_log_result);
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";
250 public void startObserve(OcResource resource)
252 if (resource != null)
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);
261 resource.observe(ObserveType.OBSERVE, queryParamsMap, this);
263 catch (OcException e)
266 current_log_result += e.getMessage() + "\n";
267 tv_current_log_result.setText(current_log_result);
268 Log.i(TAG, current_log_result);
273 public void startGet(OcResource resource)
275 if (resource != null)
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);
284 resource.get(queryParamsMap, this);
286 catch (OcException e)
289 current_log_result += e.getMessage() + "\n";
290 tv_current_log_result.setText(current_log_result);
291 Log.i(TAG, current_log_result);
296 public void startPut(OcResource resource)
298 if (resource != null)
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);
311 resource.put(rep, queryParamsMap, this);
313 catch (OcException e)
316 current_log_result += e.getMessage() + "\n";
317 tv_current_log_result.setText(current_log_result);
318 Log.i(TAG, current_log_result);
323 public void startDelete(OcResource resource) throws OcException
325 curResource = resource;
326 if (resource != null)
328 resource.deleteResource(this);
333 public void onClick(View v)
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);
344 tv_select_method_type.setText(GET);
345 Log.i(TAG, "Method: " + GET);
346 startGet(curResource);
347 btn_get.setClickable(false);
350 tv_select_method_type.setText(PUT);
351 Log.i(TAG, "Method: " + PUT);
352 startPut(curResource);
353 btn_put.setClickable(false);
356 tv_select_method_type.setText(POST);
357 Toast.makeText(this, "Not Supported Yet", Toast.LENGTH_SHORT)
359 Log.i(TAG, "Method: " + POST);
361 case R.id.btn_delete:
362 tv_select_method_type.setText(DELETE);
363 Log.i(TAG, "Method: " + DELETE);
365 startDelete(curResource);
366 } catch (OcException e) {
367 // TODO Auto-generated catch block
370 btn_delete.setClickable(false);
374 Log.i(TAG, "Log textbox is cleared");
383 protected static int observe_count()
388 public void viewText()
390 SampleConsumer.this.runOnUiThread(new Runnable()
394 if (receive_result != null)
396 tv_receive_result.setText(receive_result);
397 Log.i(TAG, "Received: " + receive_result);
399 if (found_uri != null)
401 tv_found_uri.setText(found_uri);
402 Log.i(TAG, "URI: " + found_uri);
404 if (current_log_result != null)
406 tv_current_log_result.setText(current_log_result);
407 Log.i(TAG, current_log_result);
415 public synchronized void onResourceFound(OcResource resource)
417 // synchronized (this) {
418 receive_result = "FoundResource";
421 if (SampleConsumer.curResource != null)
423 current_log_result += "Found another resource, ignoring\n";
425 if (resource != null)
427 if (resource.getUri().equals("/a/TempHumSensor"))
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
435 current_log_result += "Host address of the resource: "
436 + hostAddress + "\n";
437 SampleConsumer.curResource = resource;
441 current_log_result += "Uri is not correct.";
446 current_log_result += "Resource is invalid\n";
452 public String getCurrent_log_result()
454 return current_log_result;
457 public void setCurrent_log_result(String current_log_result)
459 this.current_log_result = current_log_result;
462 public String getFound_uri()
467 public void setFound_uri(String found_uri)
469 this.found_uri = found_uri;
472 public String getSelect_method_type()
474 return select_method_type;
477 public void setSelect_method_type(String select_method_type)
479 this.select_method_type = select_method_type;
482 public String getReceive_result()
484 return receive_result;
487 public void setReceive_result(String recieve_result)
489 this.receive_result = recieve_result;
491 public class MessageReceiver extends BroadcastReceiver
494 public void onReceive(Context context, Intent intent)
496 final String message = intent
497 .getStringExtra(MESSAGE);
498 tv_current_log_result.setText(message);
505 public void onGetCompleted(List<OcHeaderOption> options,
506 OcRepresentation rep)
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");
516 btn_get.setClickable(true);
520 public void onPutCompleted(List<OcHeaderOption> options,
521 OcRepresentation rep)
523 setReceive_result("onPut");
524 current_log_result += "PUT request was successful\n";
528 humidity = rep.getValueInt("humidity");
529 temperature = rep.getValueInt("temperature");
531 setCurrent_log_result(getCurrent_log_result() + "temperature: "
532 + temperature + "\n");
533 setCurrent_log_result(getCurrent_log_result() + "humidity: " + humidity
536 btn_put.setClickable(true);
540 public void onObserveCompleted(List<OcHeaderOption> options,
541 OcRepresentation rep, int seqNum)
543 setReceive_result("onObserve");
544 setCurrent_log_result(getCurrent_log_result() + "SequenceNumber : "
546 setCurrent_log_result("========================================================\n"
547 + "Receive OBSERVE RESULT:\n"
555 + rep.getValueInt("temperature")
558 + rep.getValueInt("humidity") + "\n");
559 if (SampleConsumer.observe_count() > 30)
561 setCurrent_log_result(getCurrent_log_result()
562 + "Cancelling Observe...\n");
565 SampleConsumer.curResource.cancelObserve();
567 catch (OcException e)
569 // TODO Auto-generated catch block
571 setCurrent_log_result(getCurrent_log_result()
572 + "Cancel result :" + e.getMessage() + "\n");
579 public void onDeleteCompleted(List<OcHeaderOption> options)
581 setReceive_result("onDelete");
583 btn_delete.setClickable(true);
587 public void onPutFailed(Throwable arg0) {
588 // TODO Auto-generated method stub
593 public void onObserveFailed(Throwable arg0) {
594 // TODO Auto-generated method stub
599 public void onDeleteFailed(Throwable arg0) {
600 // TODO Auto-generated method stub
605 public void onGetFailed(Throwable arg0) {
606 // TODO Auto-generated method stub