iotivity 0.9.0
[platform/upstream/iotivity.git] / android / SimpleClient / app / src / main / java / org / iotivity / simpleclient / OnPost2.java
1 //******************************************************************
2 //
3 // Copyright 2014 MediaTek 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
21 package org.iotivity.simpleclient;
22
23 import android.util.Log;
24
25 import org.iotivity.base.AbstractPostCallback;
26 import org.iotivity.base.OCHeaderOption;
27 import org.iotivity.base.OCRepresentation;
28 import org.iotivity.base.OCStackResult;
29 import org.iotivity.base.ObserveType;
30
31 public class OnPost2 extends AbstractPostCallback {
32
33     final private static String TAG = "OnPost2";
34
35     public void Callback(OCHeaderOption[] options, OCRepresentation rep, int eCode) {
36 //FIXME : Need to check why ocstack is returning error code 28 to us after 12/6/14 rebase
37 //        if(eCode == OCStackResult.OC_STACK_OK || eCode == OCStackResult.OC_STACK_RESOURCE_CREATED) {
38         if(true) {
39
40             Log.i(TAG, "POST2 request was successful");
41
42             if(rep.hasAttribute("createduri")) {
43                 String createdUri =  rep.getValueString("createduri");
44                 Log.i(TAG, "Uri of the created resource : " + createdUri); // FIXME
45                 new SimpleToast().execute(
46                         "Uri of the created resource : " + createdUri
47                 );
48             }
49             else {
50                 SimpleClient.mylight.m_state = rep.getValueBool("state");
51                 Log.i(TAG, "state : " + SimpleClient.mylight.m_state);
52                 SimpleClient.mylight.m_power = rep.getValueInt("power");
53                 Log.i(TAG, "power : " + SimpleClient.mylight.m_power);
54                 SimpleClient.mylight.m_name = rep.getValueString("name");
55                 Log.i(TAG, "name  : " + SimpleClient.mylight.m_name);
56                 new SimpleToast().execute(
57                         "state : " + SimpleClient.mylight.m_state +
58                                 "\npower : " + SimpleClient.mylight.m_power +
59                                 "\nname  : " + SimpleClient.mylight.m_name
60                 );
61             }
62
63             if(SimpleClient.OBSERVE_TYPE_TO_USE == ObserveType.Observe)
64                 Log.i(TAG, "Observe is used");
65             else if(SimpleClient.OBSERVE_TYPE_TO_USE == ObserveType.ObserveAll)
66                 Log.i(TAG, "ObserveAll is used");
67
68             OnObserve onObserve = new OnObserve();
69             SimpleClient.curResource.observe(SimpleClient.OBSERVE_TYPE_TO_USE, onObserve);
70         }
71         else {
72             Log.e(TAG, "onPost Response error : " + eCode);
73         }
74     }
75 }