replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-container / examples / DiscomfortIndexSensorBundle / src / DiscomfortIndexSensorResource.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 #include "DiscomfortIndexSensorResource.h"
22
23 #include <string>
24 #include <sstream>
25
26
27 DiscomfortIndexSensorResource::DiscomfortIndexSensorResource()
28 {
29     m_pDiscomfortIndexSensor = new DiscomfortIndexSensor();
30 }
31
32 DiscomfortIndexSensorResource::~DiscomfortIndexSensorResource()
33 {
34     delete m_pDiscomfortIndexSensor;
35 }
36
37 void DiscomfortIndexSensorResource::handleSetAttributesRequest(
38     const RCSResourceAttributes &value,
39     const std::map< std::string, std::string > &queryParams)
40 {
41     (void)queryParams;
42     BundleResource::setAttributes(value);
43 }
44
45 RCSResourceAttributes DiscomfortIndexSensorResource::handleGetAttributesRequest(
46     const std::map< std::string, std::string > &queryParams)
47 {
48     (void)queryParams;
49     return BundleResource::getAttributes();
50 }
51
52 void DiscomfortIndexSensorResource::executeLogic()
53 {
54     std::string strDiscomfortIndex;
55
56     m_pDiscomfortIndexSensor->executeDISensorLogic(&m_mapInputData, &strDiscomfortIndex);
57
58     setAttribute("discomfortIndex", RCSResourceAttributes::Value(strDiscomfortIndex.c_str()), true);
59
60     for (auto it : m_mapInputData)
61     {
62         setAttribute(it.first, RCSResourceAttributes::Value(it.second.c_str()));
63     }
64 }
65
66 void DiscomfortIndexSensorResource::onUpdatedInputResource(const std::string attributeName,
67         std::vector<RCSResourceAttributes::Value> values)
68 {
69     double sum = 0.0;
70     double dConvert = 0.0;
71     int inputCount = 0;
72     std::string itString;
73
74     for (auto it : values)
75     {
76         itString = it.toString();
77         std::stringstream ss(itString); //turn the string into a stream
78         ss >> dConvert; //convert
79         sum += dConvert;
80         ++inputCount;
81     }
82
83     double result = 0.0;
84     if (inputCount)
85     {
86         result = sum / inputCount;
87     }
88     std::string indexCount;//string which will contain the indexCount
89     std::stringstream convert; // stringstream used for the conversion
90     convert << result;//add the value of Number to the characters in the stream
91     indexCount = convert.str();//set indexCount to the content of the stream
92
93     m_mapInputData[attributeName] = indexCount;
94
95     // execute logic only if all the input data are ready
96     if (m_mapInputData.find("temperature") != m_mapInputData.end()
97         && m_mapInputData.find("humidity") != m_mapInputData.end())
98     {
99         executeLogic();
100     }
101 }