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