Update on android sample resource bundle
[platform/upstream/iotivity.git] / service / resource-container / examples / android / AndroidBundle / app / src / main / java / org / iotivity / service / sample / androidbundle / resources / HumidityResource.java
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics 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
22 package org.iotivity.service.sample.androidbundle.resources;
23
24 import android.content.Context;
25 import android.hardware.Sensor;
26 import android.hardware.SensorEvent;
27 import android.hardware.SensorEventListener;
28 import android.hardware.SensorManager;
29 import android.util.Log;
30
31 import org.iotivity.service.resourcecontainer.AndroidBundleResource;
32 import org.iotivity.service.resourcecontainer.RcsResourceAttributes;
33 import org.iotivity.service.resourcecontainer.RcsValue;
34
35 public class HumidityResource extends AndroidBundleResource  implements SensorEventListener {
36     private static final String LOG_TAG = HumidityResource.class.getSimpleName();
37     private final SensorManager mSensorManager;
38     private final Sensor humiditySensor;
39
40     public HumidityResource(Context context){
41         super(context);
42         this.setResourceType("oic.r.humidity");
43         this.setName("humiditySensor");
44         mSensorManager = (SensorManager) context.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
45         humiditySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_RELATIVE_HUMIDITY);
46         mSensorManager.registerListener(this, humiditySensor, SensorManager.SENSOR_DELAY_NORMAL);
47     }
48
49     @Override
50     protected void initAttributes() {
51         this.m_attributes.put("humidity", 0d);
52     }
53
54     @Override
55     public void handleSetAttributesRequest(RcsResourceAttributes attrs) {
56         Log.i(LOG_TAG, "Set Attributes called with ");
57         for(String key: attrs.keySet()){
58             Log.i(LOG_TAG, " " + key + ": " + attrs.get(key));
59         }
60     }
61
62     @Override
63     public RcsResourceAttributes handleGetAttributesRequest() {
64         Log.i(LOG_TAG, "Get Attributes called");
65         Log.i(LOG_TAG, "Returning: ");
66         for(String key: m_attributes.keySet()){
67             Log.i(LOG_TAG, " " + key + ": " + m_attributes.get(key));
68         }
69         return this.m_attributes;
70     }
71
72     @Override
73     public void onSensorChanged(SensorEvent sensorEvent) {
74         Log.i(LOG_TAG, "Sensor event " + sensorEvent.values[0]);
75
76         setAttribute("humidity", new RcsValue( (int) (sensorEvent.values[0]) ) , true);
77     }
78
79     @Override
80     public void onAccuracyChanged(Sensor sensor, int i) {
81
82     }
83 }