2 *******************************************************************
4 * Copyright 2015 Intel Corporation.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 package org.iotivity.base.examples;
25 import android.app.Activity;
26 import android.os.Bundle;
27 import android.os.Message;
28 import android.text.method.ScrollingMovementMethod;
29 import android.util.Log;
30 import android.view.View;
31 import android.widget.Button;
32 import android.widget.TextView;
34 import org.iotivity.base.ModeType;
35 import org.iotivity.base.OcConnectivityType;
36 import org.iotivity.base.OcException;
37 import org.iotivity.base.OcHeaderOption;
38 import org.iotivity.base.OcPlatform;
39 import org.iotivity.base.OcRepresentation;
40 import org.iotivity.base.OcResource;
41 import org.iotivity.base.PlatformConfig;
42 import org.iotivity.base.QualityOfService;
43 import org.iotivity.base.ServiceType;
45 import java.util.EnumSet;
46 import java.util.HashMap;
47 import java.util.LinkedList;
48 import java.util.List;
53 * FridgeClient is a sample client app which should be started after the fridgeServer is started.
54 * It creates DeviceResource, DoorResources, LightResource and performs a GET operation on them.
56 public class FridgeClient extends Activity implements
57 OcPlatform.OnResourceFoundListener,
58 OcResource.OnGetListener {
59 public static final String DEVICE_URI = "/device";
60 public static final String LIGHT = "/light";
61 public static final String LEFT_DOOR = "/door/left";
62 public static final String RIGHT_DOOR = "/door/right";
63 public static final String RANDOM_DOOR = "/door/random";
64 public static final String API_VERSION = "v.1.0";
65 public static final String CLIENT_TOKEN = "21ae43gf";
66 public static final int API_VERSION_KEY = 2048;
67 public static final int CLIENT_TOKEN_KEY = 3000;
69 private final List<OcResource> mResourceList = new LinkedList<OcResource>();
70 private OcResource mFridgeResource;
73 * configure OIC platform and call findResource
75 private void startFridgeClient() {
76 PlatformConfig cfg = new PlatformConfig(
80 "0.0.0.0", // bind to all available interfaces
82 QualityOfService.LOW);
84 logMessage("Configuring platform");
85 OcPlatform.Configure(cfg);
86 logMessage("Initiating fridge discovery");
88 OcPlatform.findResource("",
89 OcPlatform.WELL_KNOWN_QUERY + "?rt=" + "intel.fridge",
90 EnumSet.of(OcConnectivityType.CT_DEFAULT),
92 } catch (OcException e) {
93 logMessage(" Failed to discover resource");
94 Log.e(TAG, e.getMessage());
96 logMessage("-----------------------------------------------------");
100 * An event handler to be executed whenever a "findResource" request completes successfully
102 * @param ocResource found resource
105 public synchronized void onResourceFound(OcResource ocResource) {
106 if (null != mFridgeResource || !ocResource.getUri().equals(DEVICE_URI)) {
107 logMessage("Didn't find the correct fridge resource. Exiting");
110 mFridgeResource = ocResource;
111 logMessage("Discovered a fridge with \nHost: " + mFridgeResource.getHost());
113 List<String> lightTypes = new LinkedList<>();
114 lightTypes.add("intel.fridge.light");
115 List<String> doorTypes = new LinkedList<>();
116 doorTypes.add("intel.fridge.door");
117 List<String> resourceInterfaces = new LinkedList<>();
118 resourceInterfaces.add(OcPlatform.DEFAULT_INTERFACE);
119 logMessage("Creating child resource proxies for the previously known fridge components");
120 OcResource light = null;
121 OcResource leftDoor = null;
122 OcResource rightDoor = null;
123 OcResource randomDoor = null;
125 light = OcPlatform.constructResourceObject(mFridgeResource.getHost(),
127 mFridgeResource.getConnectivityTypeSet(),
128 false, //isObservable
131 mResourceList.add(light);
133 leftDoor = OcPlatform.constructResourceObject(mFridgeResource.getHost(),
135 mFridgeResource.getConnectivityTypeSet(),
136 false, //isObservable
139 mResourceList.add(leftDoor);
141 rightDoor = OcPlatform.constructResourceObject(mFridgeResource.getHost(),
143 mFridgeResource.getConnectivityTypeSet(),
144 false, //isObservable
147 mResourceList.add(rightDoor);
149 randomDoor = OcPlatform.constructResourceObject(mFridgeResource.getHost(),
151 mFridgeResource.getConnectivityTypeSet(),
152 false, //isObservable
155 mResourceList.add(randomDoor);
156 } catch (OcException e) {
157 logMessage("Error in constructResourceObject");
158 Log.e(TAG, e.getMessage());
161 List<OcHeaderOption> headerOptions = new LinkedList<>();
162 OcHeaderOption apiVersion = new OcHeaderOption(API_VERSION_KEY, API_VERSION);
163 OcHeaderOption clientToken = new OcHeaderOption(CLIENT_TOKEN_KEY, CLIENT_TOKEN);
164 headerOptions.add(apiVersion);
165 headerOptions.add(clientToken);
166 mFridgeResource.setHeaderOptions(headerOptions);
168 logMessage("Calling GET api on mFridgeResource and other component resources");
170 mFridgeResource.get(new HashMap<String, String>(), this);
171 if (null != light) light.get(new HashMap<String, String>(), this);
172 if (null != leftDoor) leftDoor.get(new HashMap<String, String>(), this);
173 if (null != rightDoor) rightDoor.get(new HashMap<String, String>(), this);
174 if (null != randomDoor) randomDoor.get(new HashMap<String, String>(), this);
175 } catch (OcException e) {
176 logMessage("Error in GET calls");
177 Log.e(TAG, e.getMessage());
182 * An event handler to be executed whenever a "get" request completes successfully
184 * @param headerOptionList list of the header options
185 * @param ocRepresentation representation of a resource
188 public synchronized void onGetCompleted(List<OcHeaderOption> headerOptionList,
189 OcRepresentation ocRepresentation) {
190 logMessage("Got a response from " + ocRepresentation.getUri());
194 * An event handler to be executed whenever a "get" request fails
196 * @param throwable exception
199 public synchronized void onGetFailed(Throwable throwable) {
200 logMessage("GET request has failed");
201 Log.e(TAG, throwable.toString());
204 //******************************************************************************
205 // End of the OIC specific code
206 //******************************************************************************
208 protected void onCreate(Bundle savedInstanceState) {
209 super.onCreate(savedInstanceState);
210 setContentView(R.layout.activity_fridge_client);
212 mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
213 mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
214 final Button button = (Button) findViewById(R.id.button);
216 if (null == savedInstanceState) {
217 button.setOnClickListener(new View.OnClickListener() {
219 public void onClick(View v) {
220 button.setEnabled(false);
221 new Thread(new Runnable() {
229 String consoleOutput = savedInstanceState.getString("consoleOutputString");
230 mConsoleTextView.setText(consoleOutput);
234 private void logMessage(final String text) {
235 runOnUiThread(new Runnable() {
237 final Message msg = new Message();
239 mConsoleTextView.append("\n");
240 mConsoleTextView.append(text);
246 private static String TAG = "FridgeClient: ";
247 private TextView mConsoleTextView;