09bd408ea6b8f18600cbf4b743b94204f709de58
[platform/upstream/iotivity.git] / service / resource-container / examples / android / AndroidBundle / app / src / main / java / org / iotivity / service / sample / androidbundle / resources / DiscomfortIndexResource.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 package org.iotivity.service.sample.androidbundle.resources;
22
23 import android.content.Context;
24 import android.util.Log;
25
26 import org.iotivity.service.resourcecontainer.AndroidBundleSoftSensorResource;
27 import org.iotivity.service.resourcecontainer.RcsResourceAttributes;
28 import org.iotivity.service.resourcecontainer.RcsValue;
29
30 import java.util.Vector;
31
32 public class DiscomfortIndexResource extends AndroidBundleSoftSensorResource {
33     private static final String LOG_TAG = DiscomfortIndexResource.class.getSimpleName();
34
35     public DiscomfortIndexResource(Context context){
36         super(context);
37         this.setResourceType("oic.r.discomfortindex");
38         this.setName("discomfortIndex");
39     }
40
41     @Override
42     protected void initAttributes() {
43         this.m_attributes.put("discomfortIndex", 0);
44         this.m_attributes.put("humidity", 0);
45         this.m_attributes.put("temperature",0);
46     }
47
48     @Override
49     protected void onUpdatedInputResource(String attributeName, Vector<RcsValue> values) {
50         m_mapInputData.put(attributeName, values.get(0));
51     }
52
53     @Override
54     protected void executeLogic() {
55         if(m_mapInputData.get("humidity") != null && m_mapInputData.get("temperature") != null){
56             double dDI = 0.0;
57             Vector v = new Vector();
58
59
60             int t = m_mapInputData.get("temperature").asInt();
61             int h = m_mapInputData.get("humidity").asInt();
62             double F = (9.0 * (double) t) / 5.0 + 32.0;
63
64             // calculation of discomfortIndex
65             dDI = F - (F - 58.0) * (double)((100 - h) * 55) / 10000.0;
66
67             this.setAttribute("temperature", new RcsValue(t));
68             this.setAttribute("humidity", new RcsValue(h));
69             this.setAttribute("discomfortIndex", new RcsValue(dDI));
70
71             Log.i(LOG_TAG, "Discomfort Index" + dDI);
72         }
73     }
74
75     @Override
76     public void handleSetAttributesRequest(RcsResourceAttributes rcsResourceAttributes) {
77         this.setAttributes(rcsResourceAttributes);
78         executeLogic();
79     }
80
81     @Override
82     public RcsResourceAttributes handleGetAttributesRequest() {
83         return this.getAttributes();
84     }
85 }