iotivity 0.9.0
[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 /// This function internally calls registerResource API.
36 void FactorySetCollection::createResources(ResourceEntityHandler callback)
37 {
38     using namespace OC::OCPlatform;
39
40     if (callback == NULL)
41     {
42         std::cout << "callback should be binded\t";
43         return;
44     }
45
46     // This will internally create and register the resource.
47     OCStackResult result = registerResource(m_factorySetHandle, m_factorySetUri,
48             m_factorySetTypes[0], m_factorySetInterfaces[0], callback,
49             OC_DISCOVERABLE | OC_OBSERVABLE);
50
51     if (OC_STACK_OK != result)
52     {
53         std::cout << "Resource creation (configuration) was unsuccessful\n";
54     }
55
56     result = bindInterfaceToResource(m_factorySetHandle, m_factorySetInterfaces[1]);
57     if (OC_STACK_OK != result)
58     {
59         std::cout << "Binding TypeName to Resource was unsuccessful\n";
60     }
61
62     result = bindInterfaceToResource(m_factorySetHandle, m_factorySetInterfaces[2]);
63     if (OC_STACK_OK != result)
64     {
65         std::cout << "Binding TypeName to Resource was unsuccessful\n";
66     }
67
68     result = registerResource(m_configurationCollectionHandle, m_configurationCollectionUri,
69             m_configurationCollectionTypes[0], m_configurationCollectionInterfaces[0], callback,
70             OC_DISCOVERABLE | OC_OBSERVABLE);
71
72     if (OC_STACK_OK != result)
73     {
74         std::cout << "Resource creation (installedLocation) was unsuccessful\n";
75     }
76
77     result = bindResource(m_factorySetHandle, m_configurationCollectionHandle);
78     if (OC_STACK_OK != result)
79     {
80         std::cout << "Binding installedLocation resource to room was unsuccessful\n";
81     }
82
83     defaultConfigurationCollection = new ConfigurationCollection(defaultConfigurationURIPrefix,
84             defaultConfigurationResourceTypePrefix);
85     //defaultConfigurationCollection->bindEntityHander(callback);
86     defaultConfigurationCollection->createResources(callback);
87
88     std::cout << "FactorySet Collection is Created!\n";
89 }
90
91 void FactorySetCollection::setFactorySetRepresentation(OCRepresentation& rep)
92 {
93     string value;
94
95     if (rep.getValue("value", value))
96     {
97         m_factorySetValue = value;
98
99         std::cout << "\t\t\t\t" << "m_factorySetValue: " << m_factorySetValue << std::endl;
100     }
101 }
102
103 void FactorySetCollection::setConfigurationCollectionRepresentation(OCRepresentation& rep)
104 {
105     string value;
106
107     if (rep.getValue("link", value))
108     {
109         // NOT ALLOWED
110
111         std::cout << "\t\t\t\t" << "link: " << m_configurationCollectionLink << std::endl;
112     }
113 }
114
115 OCRepresentation FactorySetCollection::getConfigurationCollectionRepresentation()
116 {
117     m_configurationCollectionRep.setValue("link", m_configurationCollectionLink);
118
119     return m_configurationCollectionRep;
120 }
121
122 OCRepresentation FactorySetCollection::getFactorySetRepresentation()
123 {
124     m_factorySetRep.clearChildren();
125
126     m_factorySetRep.addChild(getConfigurationCollectionRepresentation());
127
128     m_factorySetRep.setValue("value", m_factorySetValue);
129
130     return m_factorySetRep;
131 }
132
133 std::string FactorySetCollection::getFactorySetUri()
134 {
135     return m_factorySetUri;
136 }
137
138 std::string FactorySetCollection::getConfigurationCollectionUri()
139 {
140     return m_configurationCollectionUri;
141 }