Clean up some SonarQube warnings (trailing whitespace, etc).
[platform/upstream/iotivity.git] / android / examples / simpleserver / src / main / java / org / iotivity / base / examples / simpleserver / LightRepThread.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2015 Intel Corporation.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
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
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
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.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 /*
24  * // Starts a new thread for OBSERVE. It increments the value of mPower by 10 every 3 seconds
25  */
26 package org.iotivity.base.examples.simpleserver;
27
28 import android.util.Log;
29
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;
35
36 import java.util.List;
37
38 /**
39  * LightRepThread
40  *
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()
43  */
44 public class LightRepThread extends Thread {
45     private static String TAG = "LightRepThread: ";
46
47     private LightResource mLightResource;
48     private List<Byte> mObservationList;
49     private static int count = 0;
50
51     LightRepThread(Object lr, List<Byte> observationList) {
52         mLightResource = (LightResource)lr;
53         mObservationList = observationList;
54     }
55
56     public void run() {
57         while(count < 20) {
58             try {
59                 sleep(3000);
60             } catch (InterruptedException e) {
61                 Log.e(TAG, e.getMessage());
62             }
63
64             // increment current power value by 10 every 3 seconds
65             mLightResource.setPower(mLightResource.getPower() + 10);
66             try {
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);
76                 } else {
77                     // notify all observers if mObservationList is empty
78                     OcPlatform.notifyAllObservers(mLightResource.getHandle());
79                 }
80             } catch (OcException e) {
81                 Log.e(TAG, e.getMessage());
82             }
83             ++count;
84         }
85     }
86 }