7413ca2a10d6ca088a26f491c66c6499cb43f9c1
[contrib/iotivity.git] / android / SimpleClient / app / src / main / java / org / iotivity / simpleclient / SimpleClient.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.app.Activity;
24 import android.os.Bundle;
25 import android.util.Log;
26 import android.view.Menu;
27 import android.view.MenuItem;
28
29 import org.iotivity.base.OCPlatform;
30 import org.iotivity.base.OCRepresentation;
31 import org.iotivity.base.OCResource;
32 import org.iotivity.base.ObserveType;
33 import org.iotivity.base.PlatformConfig;
34
35
36 public class SimpleClient extends Activity {
37     final private static String TAG = "SimpleClient";
38
39     class Light {
40         public boolean m_state;
41         public int m_power;
42         public String m_name;
43
44         public Light() {
45             m_state = false;
46             m_power = 0;
47             m_name = "vincent";
48         }
49     }
50
51     static int OBSERVE_TYPE_TO_USE = ObserveType.Observe;
52
53     public static Light mylight;
54     public static OCResource curResource;
55     static int oc = 0;
56     static Activity mActivity;
57
58     @Override
59     protected void onCreate(Bundle savedInstanceState) {
60         super.onCreate(savedInstanceState);
61         mActivity = this;
62
63         mylight = new Light();
64
65                 PlatformConfig cfg = new PlatformConfig(PlatformConfig.ServiceType.INPROC,
66                                                                 PlatformConfig.ModeType.CLIENT,
67                                                                 "0.0.0.0",
68                                                                 0,
69                                                                 PlatformConfig.QualityOfService.LO_QOS);
70
71         OCPlatform.configure(cfg);
72         FoundResource foundResource = new FoundResource();
73         OCPlatform.findResource("", "coap://224.0.1.187/oc/core?rt=core.light", foundResource);
74
75         setContentView(R.layout.activity_simple_client);
76     }
77
78
79     @Override
80     public boolean onCreateOptionsMenu(Menu menu) {
81         // Inflate the menu; this adds items to the action bar if it is present.
82         getMenuInflater().inflate(R.menu.simple_client, menu);
83         return true;
84     }
85
86     @Override
87     public boolean onOptionsItemSelected(MenuItem item) {
88         // Handle action bar item clicks here. The action bar will
89         // automatically handle clicks on the Home/Up button, so long
90         // as you specify a parent activity in AndroidManifest.xml.
91         int id = item.getItemId();
92         if (id == R.id.action_settings) {
93             return true;
94         }
95         return super.onOptionsItemSelected(item);
96     }
97
98     protected static void getLightRepresentation(OCResource resource) {
99         if(resource != null) {
100             Log.e(TAG, "Getting Light Representation ...");
101
102             OnGet onGet = new OnGet();
103             resource.get(onGet);
104         }
105     }
106
107     protected static void putLightRepresentation(OCResource resource) {
108         if(resource != null) {
109
110             OCRepresentation rep = new OCRepresentation();
111
112             Log.e(TAG, "Putting Light Representation ...");
113
114             mylight.m_state = true;
115             mylight.m_power = 15;
116
117             Log.i(TAG, "setValueBool : " + SimpleClient.mylight.m_state);
118             Log.i(TAG, "setValueInt  : " + SimpleClient.mylight.m_power);
119             rep.setValueBool("state", mylight.m_state);
120             rep.setValueInt("power", mylight.m_power);
121
122             OnPut onPut = new OnPut();
123             resource.put(rep, onPut);
124         }
125     }
126
127     protected static void postLightRepresentation(OCResource resource) {
128         if(resource != null) {
129
130             OCRepresentation rep = new OCRepresentation();
131
132             Log.e(TAG, "Posting Light Representation ...");
133
134             mylight.m_state = false;
135             mylight.m_power = 105;
136
137             Log.i(TAG, "setValueBool : " + SimpleClient.mylight.m_state);
138             Log.i(TAG, "setValueInt  : " + SimpleClient.mylight.m_power);
139             rep.setValueBool("state", mylight.m_state);
140             rep.setValueInt("power", mylight.m_power);
141
142             OnPost onPost = new OnPost();
143             resource.post(rep, onPost);
144         }
145     }
146
147     protected static int observe_count() {
148         return ++oc;
149     }
150
151 }