change location of resource container directory
[platform/upstream/iotivity.git] / service / resource-container / examples / BMISensorBundle / src / BMISensor.cpp
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 /**
22  * This file contains the exported symbol.
23  */
24 #include "BMISensor.h"
25
26 #include <iostream>
27 #include <stdlib.h>
28 #include "SysTimer.h"
29
30 #ifdef __ANDROID__
31 #include "OCAndroid.h"
32 #endif
33
34 using namespace BMISensorName;
35
36
37 BMISensor::BMISensor()
38 {
39     m_weight = "";
40     m_height = "";
41     m_BMIResult = "";
42     time(&m_timepstampW);
43     time(&m_timepstampH);
44 }
45
46 BMISensor::~BMISensor()
47 {
48
49 }
50
51 int BMISensor::executeBMISensorLogic(std::map<std::string, std::string> *pInputData,
52                                      std::string *pOutput)
53 {
54     BMIResult result;
55
56     if (pInputData->find("weight") != pInputData->end())
57     {
58         m_weight = pInputData->at("weight");
59         time(&m_timepstampW);
60     }
61
62     if (pInputData->find("height") != pInputData->end())
63     {
64         m_height = pInputData->at("height");
65         time(&m_timepstampH);
66     }
67
68     if ((result = makeBMI()) != SUCCESS)
69     {
70         return -1;
71     }
72
73     (*pOutput) = m_BMIResult;
74
75     return 0;
76 }
77
78 /**
79  * Calculation of BMI with Weight&Height
80  */
81 BMIResult BMISensor::makeBMI(void)
82 {
83     double BMIvalue, timediffsecond;
84     double dWeight, dHeight;
85
86     int BMIResult;
87
88     if (!m_weight.empty() && !m_height.empty())
89     {
90         dWeight = std::stod(m_weight);
91         dHeight = std::stod(m_height);
92
93         timediffsecond = abs(difftime(m_timepstampW, m_timepstampH));
94
95         // check if time difference between weight data and height data is valid
96         if (timediffsecond > DIFFTIME)
97         {
98             BMIvalue = 0;
99             BMIResult = UNKOWNBMI;
100             std::cout << "[BMISensor] :   OUTOFDATEBMI: " << BMIResult << std::endl;
101         }
102         else if ((dWeight > 0) && (dHeight > 0))
103         {
104             // calculate BMI
105             BMIvalue = dWeight / (dHeight * dHeight);
106
107             std::cout << "[BMISensor] height : " << m_height << " weight : " << m_weight
108                       << " BMIvalue : " << BMIvalue  << " timediff : " << timediffsecond
109                       << std::endl;
110
111             if (BMIvalue >= OVERWEIGHT_VAL)
112             {
113                 BMIResult = (int)OBESE;
114                 std::cout << "[BMISensor] : BMIresult:" << BMIResult << " OBESE " << std::endl;
115             }
116             else if (BMIvalue >= NORMALRANGE_VAL )
117             {
118                 BMIResult = (int)OVERWEIGHT;
119                 std::cout << "[BMISensor] : BMIresult:" << BMIResult << " OVERWEIGHT " << std::endl;
120
121             }
122             else if (BMIvalue >= UNDERWEIGHT_VAL )
123             {
124                 BMIResult = (int)NORMALRANGE;
125                 std::cout << "[BMISensor] : BMIresult:" << BMIResult << " NORMALRANGE " << std::endl;
126
127             }
128             else
129             {
130                 BMIResult = (int)UNDERWEIGHT;
131                 std::cout << "[BMISensor] : BMIresult:" << BMIResult << " UNDERWEIGHT " << std::endl;
132             }
133         }
134         else
135         {
136             BMIvalue = -1;
137             BMIResult = UNKOWNBMI;
138             std::cout << "[BMISensor] :   UNKNOWNBMI: " << BMIResult << std::endl;
139         }
140
141         std::cout << std::endl;
142
143         m_BMIResult = std::to_string(BMIResult);
144
145         return SUCCESS;
146     }
147
148     return ERROR;
149 }