[IOT-1095] Add request parameters to attribute handlers
[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     BundleResource::setAttributes(value);
42 }
43
44 RCSResourceAttributes DiscomfortIndexSensorResource::handleGetAttributesRequest(
45     const std::map< std::string, std::string > &queryParams)
46 {
47     return BundleResource::getAttributes();
48 }
49
50 void DiscomfortIndexSensorResource::executeLogic()
51 {
52     std::string strDiscomfortIndex;
53
54     m_pDiscomfortIndexSensor->executeDISensorLogic(&m_mapInputData, &strDiscomfortIndex);
55
56     setAttribute("discomfortIndex", RCSResourceAttributes::Value(strDiscomfortIndex.c_str()), true);
57
58     for (auto it : m_mapInputData)
59     {
60         setAttribute(it.first, RCSResourceAttributes::Value(it.second.c_str()));
61     }
62 }
63
64 void DiscomfortIndexSensorResource::onUpdatedInputResource(const std::string attributeName,
65         std::vector<RCSResourceAttributes::Value> values)
66 {
67     double sum = 0;
68     double dConvert;
69     int inputCount = 0;
70     std::string itString;
71
72     for (auto it : values)
73     {
74         itString = it.toString();
75         std::stringstream ss(itString); //turn the string into a stream
76         ss >> dConvert; //convert
77         sum += dConvert;
78         ++inputCount;
79     }
80
81     double result = sum / inputCount;
82     std::string indexCount;//string which will contain the indexCount
83     std::stringstream convert; // stringstream used for the conversion
84     convert << result;//add the value of Number to the characters in the stream
85     indexCount = convert.str();//set indexCount to the content of the stream
86
87     m_mapInputData[attributeName] = indexCount;
88
89     // execute logic only if all the input data are ready
90     if (m_mapInputData.find("temperature") != m_mapInputData.end()
91         && m_mapInputData.find("humidity") != m_mapInputData.end())
92     {
93         executeLogic();
94     }
95 }