Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / configuration / FactorySetCollection.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 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 sample shows how one could create a resource (collection) with children.
23 ///
24
25 #include <functional>
26 #include <thread>
27
28 #include "OCPlatform.h"
29 #include "OCApi.h"
30 #include "ThingsManager.h"
31 #include "FactorySetCollection.h"
32
33 using namespace OC;
34
35 FactorySetResource::FactorySetResource()
36 {
37     m_configurationUri = "/factorySet"; // URI of the resource
38     m_configurationTypes.clear();
39     m_configurationTypes.push_back("factorySet"); // resource type name.
40     m_configurationRep.setUri(m_configurationUri);
41     m_configurationRep.setResourceTypes(m_configurationTypes);
42 }
43
44 FactorySetResource::~FactorySetResource(){}
45
46 /// This function internally calls registerResource API.
47 void FactorySetResource::createResources(ResourceEntityHandler callback)
48 {
49     using namespace OC::OCPlatform;
50
51     if (callback == NULL)
52     {
53         std::cout << "callback should be binded\t";
54         return;
55     }
56
57     // This will internally create and register the resource.
58     OCStackResult result = registerResource(m_configurationHandle, m_configurationUri,
59             m_configurationTypes[0], m_configurationInterfaces[0], callback,
60             OC_DISCOVERABLE | OC_OBSERVABLE);
61
62     if (OC_STACK_OK != result)
63     {
64         std::cout << "Resource creation (configuration) was unsuccessful\n";
65     }
66
67     std::cout << "FactorySet Resource is Created!\n";
68 }
69
70 void FactorySetResource::setFactorySetRepresentation(OCRepresentation& rep)
71 {
72     string value;
73
74     if (rep.getValue("loc", value))
75     {
76         m_location = value;
77         std::cout << "\t\t\t\t" << "m_location: " << m_location << std::endl;
78     }
79
80     if (rep.getValue("st", value))
81     {
82         std::cout << "\t\t\t\t" << "SystemTime is not allowed to be written." << std::endl;
83     }
84
85     if (rep.getValue("c", value))
86     {
87         m_currency = value;
88         std::cout << "\t\t\t\t" << "m_currency: " << m_currency << std::endl;
89     }
90
91     if (rep.getValue("r", value))
92     {
93         m_region = value;
94         std::cout << "\t\t\t\t" << "m_region: " << m_region << std::endl;
95     }
96 }
97
98 OCRepresentation FactorySetResource::getFactorySetRepresentation()
99 {
100     m_configurationRep.setValue("loc", m_location);
101     m_configurationRep.setValue("st", m_systemTime);
102     m_configurationRep.setValue("c", m_currency);
103     m_configurationRep.setValue("r", m_region);
104
105     return m_configurationRep;
106 }
107
108 std::string FactorySetResource::getUri()
109 {
110     return m_configurationUri;
111 }
112