Imported Upstream version 1.1.1
[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;
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, timediffsecond;
85     double dWeight, dHeight;
86
87     int BMIResult;
88
89     if (!m_weight.empty() && !m_height.empty())
90     {
91         dWeight = std::stod(m_weight);
92         dHeight = std::stod(m_height);
93
94         timediffsecond = std::abs(difftime(m_timepstampW, m_timepstampH));
95
96         // check if time difference between weight data and height data is valid
97         if (timediffsecond > DIFFTIME)
98         {
99             BMIvalue = 0;
100             BMIResult = UNKOWNBMI;
101             std::cout << "[BMISensor] :   OUTOFDATEBMI: " << BMIResult << std::endl;
102         }
103         else if ((dWeight > 0) && (dHeight > 0))
104         {
105             // calculate BMI
106             BMIvalue = dWeight / (dHeight * dHeight);
107
108             std::cout << "[BMISensor] height : " << m_height << " weight : " << m_weight
109                       << " BMIvalue : " << BMIvalue  << " timediff : " << timediffsecond
110                       << std::endl;
111
112             if (BMIvalue >= OVERWEIGHT_VAL)
113             {
114                 BMIResult = (int)OBESE;
115                 std::cout << "[BMISensor] : BMIresult:" << BMIResult << " OBESE " << std::endl;
116             }
117             else if (BMIvalue >= NORMALRANGE_VAL )
118             {
119                 BMIResult = (int)OVERWEIGHT;
120                 std::cout << "[BMISensor] : BMIresult:" << BMIResult << " OVERWEIGHT " << std::endl;
121
122             }
123             else if (BMIvalue >= UNDERWEIGHT_VAL )
124             {
125                 BMIResult = (int)NORMALRANGE;
126                 std::cout << "[BMISensor] : BMIresult:" << BMIResult << " NORMALRANGE " << std::endl;
127
128             }
129             else
130             {
131                 BMIResult = (int)UNDERWEIGHT;
132                 std::cout << "[BMISensor] : BMIresult:" << BMIResult << " UNDERWEIGHT " << std::endl;
133             }
134         }
135         else
136         {
137             BMIvalue = -1;
138             BMIResult = UNKOWNBMI;
139             std::cout << "[BMISensor] :   UNKNOWNBMI: " << BMIResult << std::endl;
140         }
141
142         std::cout << std::endl;
143
144         m_BMIResult = std::to_string(BMIResult);
145
146         return SUCCESS;
147     }
148
149     return ERROR;
150 }