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 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * // Starts a new thread for OBSERVE. It increments the value of mPower by 10 every 3 seconds
26 package org.iotivity.base.examples.simpleserver;
28 import android.util.Log;
30 import org.iotivity.base.EntityHandlerResult;
31 import org.iotivity.base.OcException;
32 import org.iotivity.base.OcPlatform;
33 import org.iotivity.base.OcRepresentation;
34 import org.iotivity.base.OcResourceResponse;
36 import java.util.List;
41 * Spawn a thread for OBSERVE. This increments the resource's power value by 10 every 3 seconds.
42 * If there is an observationList, call notifyListOfObservers(). Otherwise, call notifyAllObservers()
44 public class LightRepThread extends Thread {
45 private static String TAG = "LightRepThread: ";
47 private LightResource mLightResource;
48 private List<Byte> mObservationList;
49 private static int count = 0;
51 LightRepThread(Object lr, List<Byte> observationList) {
52 mLightResource = (LightResource)lr;
53 mObservationList = observationList;
60 } catch (InterruptedException e) {
61 Log.e(TAG, e.getMessage());
64 // increment current power value by 10 every 3 seconds
65 mLightResource.setPower(mLightResource.getPower() + 10);
67 // if observationList is not empty, call notifyListOfObservers
68 if (mObservationList.size() > 0) {
69 OcResourceResponse ocResourceResponse = new OcResourceResponse();
70 ocResourceResponse.setErrorCode(StringConstants.ERROR_CODE);
71 ocResourceResponse.setResponseResult(EntityHandlerResult.OK);
72 OcRepresentation r = mLightResource.get();
73 ocResourceResponse.setResourceRepresentation
74 (mLightResource.get(), OcPlatform.DEFAULT_INTERFACE);
75 OcPlatform.notifyListOfObservers(mLightResource.getHandle(), mObservationList, ocResourceResponse);
77 // notify all observers if mObservationList is empty
78 OcPlatform.notifyAllObservers(mLightResource.getHandle());
80 } catch (OcException e) {
81 Log.e(TAG, e.getMessage());