f6849407b714a94af4d1bfc57fa5c9946b28c855
[platform/upstream/iotivity.git] / service / resource-container / examples / android / AndroidBundle / app / src / main / java / org / iotivity / service / sample / androidbundle / LightSensor.java
1 package org.iotivity.service.sample.androidbundle;\r
2 \r
3 import android.content.Context;\r
4 import android.hardware.Sensor;\r
5 import android.hardware.SensorEvent;\r
6 import android.hardware.SensorEventListener;\r
7 import android.hardware.SensorManager;\r
8 import android.util.Log;\r
9 \r
10 import org.iotivity.service.resourcecontainer.AndroidBundleResource;\r
11 import org.iotivity.service.resourcecontainer.RcsResourceAttributes;\r
12 import org.iotivity.service.resourcecontainer.RcsValue;\r
13 \r
14 /**\r
15  * Created by markus.jung on 11/26/2015.\r
16  */\r
17 public class LightSensor extends AndroidBundleResource implements SensorEventListener {\r
18     private static final String LOG_TAG = AndroidLightResource.class.getSimpleName();\r
19     private final SensorManager mSensorManager;\r
20     private final Sensor lightSensor;\r
21 \r
22     public LightSensor(Context context){\r
23         super(context);\r
24         this.setResourceType("oic.r.lightsensor");\r
25         this.setName("lightSensor");\r
26         mSensorManager = (SensorManager) context.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);\r
27         lightSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);\r
28         mSensorManager.registerListener(this, lightSensor, SensorManager.SENSOR_DELAY_NORMAL);\r
29     }\r
30 \r
31     @Override\r
32     protected void initAttributes() {\r
33         this.m_attributes.put("intensity", 0);\r
34     }\r
35 \r
36     @Override\r
37     public void handleSetAttributesRequest(RcsResourceAttributes attrs) {\r
38         Log.i(LOG_TAG, "Set Attributes called with ");\r
39         for(String key: attrs.keySet()){\r
40             Log.i(LOG_TAG, " " + key + ": " + attrs.get(key));\r
41         }\r
42     }\r
43 \r
44     @Override\r
45     public RcsResourceAttributes handleGetAttributesRequest() {\r
46         Log.i(LOG_TAG, "Get Attributes called");\r
47         Log.i(LOG_TAG, "Returning: ");\r
48         for(String key: m_attributes.keySet()){\r
49             Log.i(LOG_TAG, " " + key + ": " + m_attributes.get(key));\r
50         }\r
51         return this.m_attributes;\r
52     }\r
53 \r
54     @Override\r
55     public void onSensorChanged(SensorEvent sensorEvent) {\r
56         Log.i(LOG_TAG, "Sensor event " + sensorEvent.values[0]);\r
57 \r
58         setAttribute("intensity", new RcsValue( (int) (sensorEvent.values[0]) ) , true);\r
59     }\r
60 \r
61     @Override\r
62     public void onAccuracyChanged(Sensor sensor, int i) {\r
63 \r
64     }\r
65 }\r