1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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 ******************************************************************/
22 * This file contains the exported symbol.
30 #include "DiscomfortIndexSensor.h"
34 #include "OCAndroid.h"
37 using namespace DiscomfortIndexSensorName;
39 #define SENSOR_NAME "DiscomfortIndexSensor"
42 { (char *)"temperature", (char *)"humidity" };
44 physicalInput DiscomfortIndexSensor::s_PHYSICAL_SOFTSENSORs[PHYSICAL_EA] =
46 { (char *)"Thing_TempHumSensor", 2, (void *) &inputName },
47 { (char *)"Thing_TempHumSensor1", 2, (void *) &inputName }
50 ICtxDelegate *g_pDelegate;
52 void InitializeContext(ICtxDelegate *pDelegate)
54 std::vector < ContextData > contextData;
56 DiscomfortIndexSensor *eventCls = new DiscomfortIndexSensor();
57 pDelegate->registerCallback(eventCls);
58 g_pDelegate = pDelegate;
60 std::cout << "DiscomfortIndexSensor loaded" << std::endl;
65 DiscomfortIndexSensor::DiscomfortIndexSensor()
67 m_result.m_timestamp = "";
68 m_result.m_humidity = "";
69 m_result.m_temperature = "";
70 m_result.m_discomfortIndex = "";
73 void DiscomfortIndexSensor::onCtxEvent(enum CTX_EVENT_TYPE eventType,
74 std::vector< ContextData > contextDataList)
79 runLogic(contextDataList);
82 std::cout << "Not support onCtxEvent" << std::endl;
87 int DiscomfortIndexSensor::runLogic(std::vector< ContextData > &contextDataList)
89 std::cout << "[DiscomfortIndexSensor] DiscomfortIndexSensor::" << __func__ << " is called."
94 if (getInput(contextDataList, m_DI) == SUCCESS)
96 if ((result = makeDiscomfortIndex(m_DI)) != SUCCESS)
98 std::cout << "Error : makeDiscomfortIndex() result = " << result << std::endl;
102 std::vector < ContextData > outList;
103 ContextData out = setOutput(4, m_DI);
105 outList.push_back(out);
106 g_pDelegate->addOutput(outList);
115 * Get Input data (temperature, humidity) using resource Client of Iotivity base.
117 DIResult DiscomfortIndexSensor::getInput(std::vector< ContextData > &contextDataList, InValue *data)
122 if ((contextSize = contextDataList.size()) == 0)
124 std::cout << "Physical Context data is not exist." << std::endl;
128 for (int i = 0; i < contextSize; i++)
130 for (int k = 0; k < PHYSICAL_EA; k++)
132 if (contextDataList[i].rootName == s_PHYSICAL_SOFTSENSORs[k].m_thingName)
134 std::vector < std::map< std::string, std::string > > lVector =
135 contextDataList[i].outputProperty;
136 int requiredInputNum = s_PHYSICAL_SOFTSENSORs[k].m_inputNum;
137 char **pchar = (char **) (s_PHYSICAL_SOFTSENSORs[k].m_pInputStruct);
138 if (requiredInputNum == 0)
140 std::cout << "No input List." << std::endl;
144 for (unsigned int j = 0; j < lVector.size(); j++)
146 std::string name = lVector[j]["name"];
148 if (name.compare(*pchar) == 0)
150 data->m_temperature = lVector[j]["value"];
153 else if (name.compare(*(++pchar)) == 0)
155 data->m_humidity = lVector[j]["value"];
160 if (requiredInputNum == 0)
170 if (result_flag == PHYSICAL_EA)
172 std::cout << "Success : getInput()" << std::endl;
180 * Calculation of DiscomfortIndex with TEMP&HUMI of InValue.
182 DIResult DiscomfortIndexSensor::makeDiscomfortIndex(InValue *data)
184 int discomfortIndex = (int) ERROR;
187 m_result.m_temperature = "";
188 m_result.m_humidity = "";
190 for (int i = 0; i < PHYSICAL_EA; i++)
194 m_result.m_temperature += ", ";
195 m_result.m_humidity += ", ";
199 int t = std::stoi((data + i)->m_temperature);
200 int h = std::stoi((data + i)->m_humidity);
201 double F = (9.0 * (double) t) / 5.0 + 32.0;
203 std::cout << "Device Number : " << i << std::endl;
205 dI = F - (F - 58.0) * (double) ((100 - h) * 55) / 10000.0;
207 std::cout << "Discomfort level : " << dI << ", Temperature :" << t << ", Humidity :" << h
210 (data + i)->m_discomfortIndex = std::to_string(0);
211 m_result.m_temperature += std::to_string(t) + ", ";
212 m_result.m_humidity += std::to_string(h) + ", ";
216 sumDI = sumDI / PHYSICAL_EA;
217 std::cout << "[result] Avg. DI level : " << sumDI << std::endl;
220 discomfortIndex = (int) ALL_DISCOMPORT;
221 std::cout << "DI : " << discomfortIndex << " : All person discomfort. : " << sumDI
224 else if (sumDI >= 75.0)
226 discomfortIndex = (int) HALF_DISCOMPORT;
227 std::cout << "DI : " << discomfortIndex << " : Half of person discomfort. : " << sumDI
230 else if (sumDI >= 68.0)
232 discomfortIndex = (int) LITTLE_DISCOMPORT;
233 std::cout << "DI : " << discomfortIndex << " : A little person discomfort. : " << sumDI
238 discomfortIndex = (int) ALL_COMPORT;
239 std::cout << "DI : " << discomfortIndex << " : All person comfort. : " << sumDI
243 m_result.m_discomfortIndex = std::to_string(discomfortIndex);
244 std::cout << "[result] Discomfort Index : " << m_result.m_discomfortIndex << std::endl;
249 ContextData DiscomfortIndexSensor::setOutput(int property_count, InValue *data)
253 std::map < std::string, std::string > output_property;
255 out.rootName = SENSOR_NAME;
256 out.outputPropertyCount = property_count;
258 output_property.insert(std::make_pair("name", "timestamp"));
259 output_property.insert(std::make_pair("type", "string"));
260 output_property.insert(std::make_pair("value", m_result.m_timestamp));
262 out.outputProperty.push_back(output_property);
264 output_property.clear();
265 output_property.insert(std::make_pair("name", "temperature"));
266 output_property.insert(std::make_pair("type", "string"));
267 output_property.insert(std::make_pair("value", m_result.m_temperature));
269 out.outputProperty.push_back(output_property);
271 output_property.clear();
272 output_property.insert(std::make_pair("name", "humidity"));
273 output_property.insert(std::make_pair("type", "string"));
274 output_property.insert(std::make_pair("value", m_result.m_humidity));
276 out.outputProperty.push_back(output_property);
278 output_property.clear();
279 output_property.insert(std::make_pair("name", "discomfortIndex"));
280 output_property.insert(std::make_pair("type", "int"));
281 output_property.insert(std::make_pair("value", m_result.m_discomfortIndex));
283 out.outputProperty.push_back(output_property);