Resource container update
[platform/upstream/iotivity.git] / service / basis / resourceContainer / examples / SampleBundle / src / DiscomfortIndexSensor.cpp
1 /******************************************************************
2  *
3  * Copyright 2014 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  * This file contains the exported symbol.
23  */
24 #include <stdlib.h>
25 #include <map>
26 #include <string>
27 #include <sstream>
28 #include <iostream>
29
30 #include "DiscomfortIndexSensor.h"
31 #include "SysTimer.h"
32
33 #ifdef __ANDROID__
34 #include "OCAndroid.h"
35 #endif
36
37 using namespace DiscomfortIndexSensorName;
38
39 #define SENSOR_NAME "DiscomfortIndexSensor"
40
41 char *inputName[2] =
42 { (char *)"temperature", (char *)"humidity" };
43
44 physicalInput DiscomfortIndexSensor::s_PHYSICAL_SOFTSENSORs[PHYSICAL_EA] =
45 {
46     { (char *)"Thing_TempHumSensor", 2, (void *) &inputName },
47     { (char *)"Thing_TempHumSensor1", 2, (void *) &inputName }
48 };
49
50 DiscomfortIndexSensor::DiscomfortIndexSensor()
51 {
52     m_result.m_timestamp = "";
53     m_result.m_humidity = "";
54     m_result.m_temperature = "";
55     m_result.m_discomfortIndex = "";
56 }
57
58 int DiscomfortIndexSensor::runLogic(std::vector< SensorData > &sensorData)
59 {
60     std::cout << "[DiscomfortIndexSensor] DiscomfortIndexSensor::" << __func__ << " is called."
61               << std::endl;
62
63     DIResult result;
64
65     if (getInput(sensorData, m_DI) == SUCCESS)
66     {
67         if ((result = makeDiscomfortIndex(m_DI)) != SUCCESS)
68         {
69             std::cout << "Error : makeDiscomfortIndex() result = " << result << std::endl;
70             return -1;
71         }
72
73         //g_pSoftSensorCore->pushResults(setOutput(4, m_DI));
74
75         return 0;
76     }
77
78     return -1;
79 }
80
81 /**
82  * Get Input data (temperature, humidity) using resource Client of Iotivity base.
83  */
84 DIResult DiscomfortIndexSensor::getInput(std::vector< SensorData > &sensorData, InValue *data)
85 {
86     //int result_flag = 0;
87     //int contextSize = 0;
88
89     //if ((contextSize = sensorData.size()) == 0)
90     //{
91     //    std::cout << "Physical Context data is not exist." << std::endl;
92     //    return ERROR;
93     //}
94
95     //for (int i = 0; i < contextSize; i++)
96     //{
97     //    for (int k = 0; k < PHYSICAL_EA; k++)
98     //    {
99     //        if (sensorData[i].sensorName == s_PHYSICAL_SOFTSENSORs[k].m_thingName)
100     //        {
101     //            std::vector < std::map< std::string, std::string > > lVector =
102     //                sensorData[i].data;
103     //            int requiredInputNum = s_PHYSICAL_SOFTSENSORs[k].m_inputNum;
104     //            char **pchar = (char **) (s_PHYSICAL_SOFTSENSORs[k].m_pInputStruct);
105     //            if (requiredInputNum == 0)
106     //            {
107     //                std::cout << "No input List." << std::endl;
108     //                return ERROR;
109     //            }
110
111     //            for (unsigned int j = 0; j < lVector.size(); j++)
112     //            {
113     //                std::string name = lVector[j]["name"];
114
115     //                if (name.compare(*pchar) == 0)
116     //                {
117     //                    data->m_temperature = lVector[j]["value"];
118     //                    requiredInputNum--;
119     //                }
120     //                else if (name.compare(*(++pchar)) == 0)
121     //                {
122     //                    data->m_humidity = lVector[j]["value"];
123     //                    requiredInputNum--;
124     //                }
125     //            }
126
127     //            if (requiredInputNum == 0)
128     //            {
129     //                data++;
130     //                result_flag++;
131     //            }
132     //            break;
133     //        } // if
134     //    } // for
135     //}
136
137     //if (result_flag == PHYSICAL_EA)
138     //{
139     //    std::cout << "Success : getInput()" << std::endl;
140     //    return SUCCESS;
141     //}
142
143     //return ERROR;
144 }
145
146 /**
147  * Calculation of DiscomfortIndex with TEMP&HUMI of InValue.
148  */
149 DIResult DiscomfortIndexSensor::makeDiscomfortIndex(InValue *data)
150 {
151     int discomfortIndex = (int) ERROR;
152     double sumDI = 0.0;
153
154     m_result.m_temperature = "";
155     m_result.m_humidity = "";
156
157     for (int i = 0; i < PHYSICAL_EA; i++)
158     {
159         if (i != 0)
160         {
161             m_result.m_temperature += ", ";
162             m_result.m_humidity += ", ";
163         }
164
165         double dI = 0.0;
166         int t = std::stoi((data + i)->m_temperature);
167         int h = std::stoi((data + i)->m_humidity);
168         double F = (9.0 * (double) t) / 5.0 + 32.0;
169
170         std::cout << "Device Number : " << i << std::endl;
171
172         dI = F - (F - 58.0) * (double) ((100 - h) * 55) / 10000.0;
173
174         std::cout << "Discomfort level : " << dI << ", Temperature :" << t << ", Humidity :" << h
175                   << std::endl;
176
177         (data + i)->m_discomfortIndex = std::to_string(0);
178         m_result.m_temperature += std::to_string(t) + ", ";
179         m_result.m_humidity += std::to_string(h) + ", ";
180         sumDI += dI;
181     }
182
183     sumDI = sumDI / PHYSICAL_EA;
184     std::cout << "[result] Avg. DI level : " << sumDI << std::endl;
185     if (sumDI >= 80.0)
186     {
187         discomfortIndex = (int) ALL_DISCOMPORT;
188         std::cout << "DI : " << discomfortIndex << " : All person discomfort. : " << sumDI
189                   << std::endl;
190     }
191     else if (sumDI >= 75.0)
192     {
193         discomfortIndex = (int) HALF_DISCOMPORT;
194         std::cout << "DI : " << discomfortIndex << " : Half of person discomfort. : " << sumDI
195                   << std::endl;
196     }
197     else if (sumDI >= 68.0)
198     {
199         discomfortIndex = (int) LITTLE_DISCOMPORT;
200         std::cout << "DI : " << discomfortIndex << " : A little person discomfort. : " << sumDI
201                   << std::endl;
202     }
203     else
204     {
205         discomfortIndex = (int) ALL_COMPORT;
206         std::cout << "DI : " << discomfortIndex << " : All person comfort. : " << sumDI
207                   << std::endl;
208     }
209
210     m_result.m_discomfortIndex = std::to_string(discomfortIndex);
211     std::cout << "[result] Discomfort Index : " << m_result.m_discomfortIndex << std::endl;
212
213     return SUCCESS;
214 }
215
216 SensorData DiscomfortIndexSensor::setOutput(int property_count, InValue *data)
217 {
218     //SensorData out;
219
220     //std::map < std::string, std::string > output_property;
221
222     //out.sensorName = SENSOR_NAME;
223
224     //output_property.insert(std::make_pair("name", "timestamp"));
225     //output_property.insert(std::make_pair("type", "string"));
226     //output_property.insert(std::make_pair("value", m_result.m_timestamp));
227
228     //out.data.push_back(output_property);
229
230     //output_property.clear();
231     //output_property.insert(std::make_pair("name", "temperature"));
232     //output_property.insert(std::make_pair("type", "string"));
233     //output_property.insert(std::make_pair("value", m_result.m_temperature));
234
235     //out.data.push_back(output_property);
236
237     //output_property.clear();
238     //output_property.insert(std::make_pair("name", "humidity"));
239     //output_property.insert(std::make_pair("type", "string"));
240     //output_property.insert(std::make_pair("value", m_result.m_humidity));
241
242     //out.data.push_back(output_property);
243
244     //output_property.clear();
245     //output_property.insert(std::make_pair("name", "discomfortIndex"));
246     //output_property.insert(std::make_pair("type", "int"));
247     //output_property.insert(std::make_pair("value", m_result.m_discomfortIndex));
248
249     //out.data.push_back(output_property);
250
251     //return out;
252 }
253