1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 /// This sample shows how one could create a resource (collection) with children.
28 #include "OCPlatform.h"
30 #include "ThingsManager.h"
31 #include "DiagnosticsCollection.h"
35 /// This function internally calls registerResource API.
36 void DiagnosticsCollection::createResources(ResourceEntityHandler callback)
38 using namespace OC::OCPlatform;
42 std::cout << "callback should be binded\t";
46 // This will internally create and register the resource.
47 OCStackResult result = registerResource(m_diagnosticsHandle, m_diagnosticsUri,
48 m_diagnosticsTypes[0], m_diagnosticsInterfaces[0], callback,
49 OC_DISCOVERABLE | OC_OBSERVABLE);
51 if (OC_STACK_OK != result)
53 std::cout << "Resource creation (diagnostics) was unsuccessful\n";
56 result = bindInterfaceToResource(m_diagnosticsHandle, m_diagnosticsInterfaces[1]);
57 if (OC_STACK_OK != result)
59 std::cout << "Binding TypeName to Resource was unsuccessful\n";
62 result = bindInterfaceToResource(m_diagnosticsHandle, m_diagnosticsInterfaces[2]);
63 if (OC_STACK_OK != result)
65 std::cout << "Binding TypeName to Resource was unsuccessful\n";
68 result = registerResource(m_factoryResetHandle, m_factoryResetUri, m_factoryResetTypes[0],
69 m_factoryResetInterfaces[0], callback, OC_DISCOVERABLE | OC_OBSERVABLE);
71 if (OC_STACK_OK != result)
73 std::cout << "Resource creation (factoryReset) was unsuccessful\n";
76 result = registerResource(m_rebootHandle, m_rebootUri, m_rebootTypes[0], m_rebootInterfaces[0],
77 callback, OC_DISCOVERABLE | OC_OBSERVABLE);
79 if (OC_STACK_OK != result)
81 std::cout << "Resource creation (reboot) was unsuccessful\n";
84 result = registerResource(m_startCollectionHandle, m_startCollectionUri,
85 m_startCollectionTypes[0], m_startCollectionInterfaces[0], callback,
86 OC_DISCOVERABLE | OC_OBSERVABLE);
88 if (OC_STACK_OK != result)
90 std::cout << "Resource creation (startCollection) was unsuccessful\n";
93 result = bindResource(m_diagnosticsHandle, m_factoryResetHandle);
94 if (OC_STACK_OK != result)
96 std::cout << "Binding installedLocation resource to room was unsuccessful\n";
99 result = bindResource(m_diagnosticsHandle, m_rebootHandle);
100 if (OC_STACK_OK != result)
102 std::cout << "Binding time resource to room was unsuccessful\n";
105 result = bindResource(m_diagnosticsHandle, m_startCollectionHandle);
106 if (OC_STACK_OK != result)
108 std::cout << "Binding network resource to room was unsuccessful\n";
112 std::function< void(int second) >(
113 std::bind(&DiagnosticsCollection::diagnosticsMonitor, this,
114 std::placeholders::_1)), 10); // every 10 seconds
117 std::cout << "Diagnostics Collection is Created!\n";
120 void DiagnosticsCollection::setDiagnosticsRepresentation(OCRepresentation& rep)
124 if (rep.getValue("value", value))
126 m_diagnosticsValue = value;
128 std::cout << "\t\t\t\t" << "m_diagnosticsValue: " << m_diagnosticsValue << std::endl;
132 void DiagnosticsCollection::setFactoryResetRepresentation(OCRepresentation& rep)
136 if (rep.getValue("value", value))
138 m_factoryResetValue = value;
140 std::cout << "\t\t\t\t" << "value: " << m_factoryResetValue << std::endl;
144 void DiagnosticsCollection::setRebootRepresentation(OCRepresentation& rep)
148 if (rep.getValue("value", value))
150 m_rebootValue = value;
152 std::cout << "\t\t\t\t" << "value: " << m_rebootValue << std::endl;
156 void DiagnosticsCollection::setStartCollectionRepresentation(OCRepresentation& rep)
160 if (rep.getValue("value", value))
162 m_startCollectionValue = value;
164 std::cout << "\t\t\t\t" << "value: " << m_startCollectionValue << std::endl;
168 OCRepresentation DiagnosticsCollection::getFactoryResetRepresentation()
170 m_factoryResetRep.setValue("value", m_factoryResetValue);
172 return m_factoryResetRep;
175 OCRepresentation DiagnosticsCollection::getRebootRepresentation()
177 m_rebootRep.setValue("value", m_rebootValue);
182 OCRepresentation DiagnosticsCollection::getStartCollectionRepresentation()
184 m_startCollectionRep.setValue("value", m_startCollectionValue);
186 return m_startCollectionRep;
189 OCRepresentation DiagnosticsCollection::getDiagnosticsRepresentation()
191 m_diagnosticsRep.clearChildren();
193 m_diagnosticsRep.addChild(getFactoryResetRepresentation());
194 m_diagnosticsRep.addChild(getRebootRepresentation());
195 m_diagnosticsRep.addChild(getStartCollectionRepresentation());
197 m_diagnosticsRep.setValue("value", m_diagnosticsValue);
199 return m_diagnosticsRep;
202 std::string DiagnosticsCollection::getDiagnosticsUri()
204 return m_diagnosticsUri;
207 std::string DiagnosticsCollection::getFactoryResetUri()
209 return m_factoryResetUri;
212 std::string DiagnosticsCollection::getRebootUri()
217 std::string DiagnosticsCollection::getStartCollectionUri()
219 return m_startCollectionUri;
222 void DiagnosticsCollection::diagnosticsMonitor(int second)
228 if (m_rebootValue == "true")
231 std::cout << "Reboot will be soon..." << std::endl;
232 m_rebootValue = defaultReboot;
233 res = system("sudo reboot"); // System reboot
235 std::cout << "return: " << res << std::endl;
238 else if (m_factoryResetValue == "true")
240 std::cout << "Factory Reset will be soon..." << std::endl;
241 m_factoryResetValue = defaultFactoryReset;