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