Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / tizen / ConServerApp / src / factorysetresource.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 "factorysetresource.h"
22
23 #include <functional>
24 #include <dlog.h>
25
26 #include "OCPlatform.h"
27 #include "OCApi.h"
28 #include "ThingsManager.h"
29
30 using namespace OC;
31
32 FactorySetResource::FactorySetResource()
33 {
34     m_configurationUri = "/factorySet"; // URI of the resource
35     m_configurationTypes.clear();
36     m_configurationTypes.push_back("factorySet"); // resource type name.
37     m_configurationRep.setUri(m_configurationUri);
38     m_configurationRep.setResourceTypes(m_configurationTypes);
39 }
40
41 FactorySetResource::~FactorySetResource() {}
42
43 // This function internally calls registerResource API.
44 void FactorySetResource::createResource(ResourceEntityHandler callback)
45 {
46     using namespace OC::OCPlatform;
47
48     if (NULL == callback)
49     {
50         dlog_print(DLOG_INFO, "FactorySetResource", "#### Callback should be binded");
51         return;
52     }
53
54     // This will internally create and register the resource
55     OCStackResult result = registerResource(m_configurationHandle, m_configurationUri,
56                                             m_configurationTypes[0], m_configurationInterfaces[0],
57                                             callback, OC_DISCOVERABLE | OC_OBSERVABLE);
58
59     if (OC_STACK_OK != result)
60     {
61         dlog_print(DLOG_INFO, "FactorySetResource", "#### Resource creation"
62                    "(configuration) was unsuccessful");
63         return;
64     }
65
66     dlog_print(DLOG_INFO, "FactorySetResource", "#### Configuration Resource is Created");
67 }
68
69 void FactorySetResource::setFactorySetRepresentation(OCRepresentation &rep)
70 {
71     string value;
72
73     if (rep.getValue("loc", value))
74     {
75         m_location = value;
76         dlog_print(DLOG_INFO, "FactorySetResource", "#### m_location: %s",
77                    m_location.c_str());
78     }
79
80     if (rep.getValue("st", value))
81     {
82         dlog_print(DLOG_INFO, "FactorySetResource", "#### SystemTime is not"
83                    "allowed to be written");
84     }
85
86     if (rep.getValue("c", value))
87     {
88         m_currency = value;
89         dlog_print(DLOG_INFO, "FactorySetResource", "#### m_currency: %s",
90                    m_currency.c_str());
91     }
92
93     if (rep.getValue("r", value))
94     {
95         m_region = value;
96         dlog_print(DLOG_INFO, "FactorySetResource", "#### m_region: %s",
97                    m_region.c_str());
98     }
99 }
100
101 OCRepresentation FactorySetResource::getFactorySetRepresentation()
102 {
103     m_configurationRep.setValue("loc", m_location);
104     m_configurationRep.setValue("st", m_systemTime);
105     m_configurationRep.setValue("c", m_currency);
106     m_configurationRep.setValue("r", m_region);
107
108     return m_configurationRep;
109 }
110
111 std::string FactorySetResource::getUri()
112 {
113     return m_configurationUri;
114 }
115
116 // Deletes the factoryset resource which has been created using createResource()
117 void FactorySetResource::deleteResource()
118 {
119     // Unregister the Configuration resource
120     if (NULL != m_configurationHandle)
121     {
122         OCPlatform::unregisterResource(m_configurationHandle);
123     }
124 }