1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 package org.iotivity.service.sample.androidbundle.resources;
23 import android.app.Notification;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.util.Log;
28 import org.iotivity.service.resourcecontainer.BundleSoftSensorResource;
29 import org.iotivity.service.resourcecontainer.RcsResourceAttributes;
30 import org.iotivity.service.resourcecontainer.RcsValue;
32 import java.util.HashMap;
33 import java.util.Vector;
35 import org.iotivity.service.sample.androidbundle.DummyActivity;
36 import org.iotivity.service.sample.androidbundle.R;
38 public class DiscomfortIndexResource extends BundleSoftSensorResource {
39 private static final String LOG_TAG = DiscomfortIndexResource.class.getSimpleName();
41 public DiscomfortIndexResource(Context context){
43 this.setResourceType("oic.r.discomfortindex");
44 this.setName("discomfortIndex");
45 m_mapInputData = new HashMap<String, RcsValue>();
49 protected void initAttributes() {
50 this.m_attributes.put("discomfortIndex", 0);
51 this.m_attributes.put("humidity", 0);
52 this.m_attributes.put("temperature", 0);
56 protected void onUpdatedInputResource(String attributeName, Vector<RcsValue> values) {
57 m_mapInputData.put(attributeName, values.get(0));
62 protected void executeLogic() {
63 if(m_mapInputData.get("humidity") != null && m_mapInputData.get("temperature") != null){
65 Vector v = new Vector();
67 int t = m_mapInputData.get("temperature").asInt();
68 int h = m_mapInputData.get("humidity").asInt();
69 double F = (9.0 * (double) t) / 5.0 + 32.0;
71 // calculation of discomfortIndex
72 dDI = F - (F - 58.0) * (double)((100 - h) * 55) / 10000.0;
74 this.setAttribute("temperature", new RcsValue(t));
75 this.setAttribute("humidity", new RcsValue(h));
76 this.setAttribute("discomfortIndex", new RcsValue(dDI));
78 setAttribute("discomfortIndex",new RcsValue(dDI), true ); // notify observers
79 Log.i(LOG_TAG, "Discomfort Index" + dDI);
80 showNotification(" " + dDI, this.m_context);
83 Log.i(LOG_TAG, "Discomfort Index input data - humidity: " +
84 m_mapInputData.get("humidity") + " temp: " +
85 m_mapInputData.get("temperature") );
90 public void handleSetAttributesRequest(RcsResourceAttributes rcsResourceAttributes) {
91 this.setAttributes(rcsResourceAttributes);
96 public RcsResourceAttributes handleGetAttributesRequest() {
97 return this.getAttributes();
100 private void showNotification(String eventtext, Context ctx) {
101 // Set the icon, scrolling text and timestamp
102 Log.i(LOG_TAG, "*************************************** ");
103 Log.i(LOG_TAG, "*************************************** ");
104 Log.i(LOG_TAG, "*************************************** ");
105 Log.i(LOG_TAG, eventtext);
106 Log.i(LOG_TAG, "*************************************** ");
107 Log.i(LOG_TAG, "*************************************** ");
108 Log.i(LOG_TAG, "*************************************** ");