Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / service / resource-container / examples / HueSampleBundle / src / HueLight.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 "HueLight.h"
22
23 #include <iostream>
24
25 using namespace std;
26 using namespace OIC::Service;
27
28 HueLight::HueLight()
29 {
30     m_connector = nullptr;
31 }
32
33 HueLight::HueLight(HueConnector *connector, std::string address)
34 {
35     m_address = address;
36     m_connector = connector;
37     initAttributes();
38 }
39
40 HueLight::~HueLight()
41 {
42     m_connector = nullptr;
43 }
44
45 void HueLight::initAttributes()
46 {
47     BundleResource::setAttribute("on-off", false, false);
48     BundleResource::setAttribute("dim", 0, false);
49     BundleResource::setAttribute("color", 0, false);
50 }
51
52 RCSResourceAttributes HueLight::handleGetAttributesRequest(
53     const std::map< std::string, std::string > &/*queryParams*/)
54 {
55     cout << "HueLight::handleGetAttributesRequest" << endl;
56     // TODO read from HueLight and update attribute data
57     return BundleResource::getAttributes();
58 }
59
60 void HueLight::handleSetAttributesRequest(const RCSResourceAttributes &value,
61                                           const std::map< std::string, std::string > &/*queryParams*/)
62 {
63     cout << "HueLight::handleSetAttributesRequest" << std::endl;
64
65     // TODO construct single write
66
67     for (RCSResourceAttributes::const_iterator it = value.begin(); it != value.end(); it++)
68     {
69         std::string attributeName = it->key();
70         RCSResourceAttributes::Value attrValue = it->value();
71
72         if (attributeName == "on-off")
73         {
74             //m_connector->transmit(this->m_address + "/state", "{\"on\":" + attrValue.toString() + "}");
75         }
76
77         if (attributeName == "dim")
78         {
79             // needs conversion * 2.5
80             //m_connector->transmit(this->m_address + "/state", "{\"bri\":" + attrValue.toString() + "}");
81         }
82
83         if (attributeName == "color")
84         {
85             // needs conversion *650
86             //m_connector->transmit(this->m_address + "/state", "{\"hue\":" + attrValue.toString() + "}");
87         }
88     }
89
90     BundleResource::setAttributes(value);
91 }