iotivity 0.9.0
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / configuration / DiagnosticsCollection.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 "DiagnosticsCollection.h"
32
33 using namespace OC;
34
35 /// This function internally calls registerResource API.
36 void DiagnosticsCollection::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_diagnosticsHandle, m_diagnosticsUri,
48             m_diagnosticsTypes[0], m_diagnosticsInterfaces[0], callback,
49             OC_DISCOVERABLE | OC_OBSERVABLE);
50
51     if (OC_STACK_OK != result)
52     {
53         std::cout << "Resource creation (diagnostics) was unsuccessful\n";
54     }
55
56     result = bindInterfaceToResource(m_diagnosticsHandle, m_diagnosticsInterfaces[1]);
57     if (OC_STACK_OK != result)
58     {
59         std::cout << "Binding TypeName to Resource was unsuccessful\n";
60     }
61
62     result = bindInterfaceToResource(m_diagnosticsHandle, m_diagnosticsInterfaces[2]);
63     if (OC_STACK_OK != result)
64     {
65         std::cout << "Binding TypeName to Resource was unsuccessful\n";
66     }
67
68     result = registerResource(m_factoryResetHandle, m_factoryResetUri, m_factoryResetTypes[0],
69             m_factoryResetInterfaces[0], callback, OC_DISCOVERABLE | OC_OBSERVABLE);
70
71     if (OC_STACK_OK != result)
72     {
73         std::cout << "Resource creation (factoryReset) was unsuccessful\n";
74     }
75
76     result = registerResource(m_rebootHandle, m_rebootUri, m_rebootTypes[0], m_rebootInterfaces[0],
77             callback, OC_DISCOVERABLE | OC_OBSERVABLE);
78
79     if (OC_STACK_OK != result)
80     {
81         std::cout << "Resource creation (reboot) was unsuccessful\n";
82     }
83
84     result = registerResource(m_startCollectionHandle, m_startCollectionUri,
85             m_startCollectionTypes[0], m_startCollectionInterfaces[0], callback,
86             OC_DISCOVERABLE | OC_OBSERVABLE);
87
88     if (OC_STACK_OK != result)
89     {
90         std::cout << "Resource creation (startCollection) was unsuccessful\n";
91     }
92
93     result = bindResource(m_diagnosticsHandle, m_factoryResetHandle);
94     if (OC_STACK_OK != result)
95     {
96         std::cout << "Binding installedLocation resource to room was unsuccessful\n";
97     }
98
99     result = bindResource(m_diagnosticsHandle, m_rebootHandle);
100     if (OC_STACK_OK != result)
101     {
102         std::cout << "Binding time resource to room was unsuccessful\n";
103     }
104
105     result = bindResource(m_diagnosticsHandle, m_startCollectionHandle);
106     if (OC_STACK_OK != result)
107     {
108         std::cout << "Binding network resource to room was unsuccessful\n";
109     }
110
111     thread exec(
112             std::function< void(int second) >(
113                     std::bind(&DiagnosticsCollection::diagnosticsMonitor, this,
114                             std::placeholders::_1)), 10); // every 10 seconds
115     exec.detach();
116
117     std::cout << "Diagnostics Collection is Created!\n";
118 }
119
120 void DiagnosticsCollection::setDiagnosticsRepresentation(OCRepresentation& rep)
121 {
122     string value;
123
124     if (rep.getValue("value", value))
125     {
126         m_diagnosticsValue = value;
127
128         std::cout << "\t\t\t\t" << "m_diagnosticsValue: " << m_diagnosticsValue << std::endl;
129     }
130 }
131
132 void DiagnosticsCollection::setFactoryResetRepresentation(OCRepresentation& rep)
133 {
134     string value;
135
136     if (rep.getValue("value", value))
137     {
138         m_factoryResetValue = value;
139
140         std::cout << "\t\t\t\t" << "value: " << m_factoryResetValue << std::endl;
141     }
142 }
143
144 void DiagnosticsCollection::setRebootRepresentation(OCRepresentation& rep)
145 {
146     string value;
147
148     if (rep.getValue("value", value))
149     {
150         m_rebootValue = value;
151
152         std::cout << "\t\t\t\t" << "value: " << m_rebootValue << std::endl;
153     }
154 }
155
156 void DiagnosticsCollection::setStartCollectionRepresentation(OCRepresentation& rep)
157 {
158     string value;
159
160     if (rep.getValue("value", value))
161     {
162         m_startCollectionValue = value;
163
164         std::cout << "\t\t\t\t" << "value: " << m_startCollectionValue << std::endl;
165     }
166 }
167
168 OCRepresentation DiagnosticsCollection::getFactoryResetRepresentation()
169 {
170     m_factoryResetRep.setValue("value", m_factoryResetValue);
171
172     return m_factoryResetRep;
173 }
174
175 OCRepresentation DiagnosticsCollection::getRebootRepresentation()
176 {
177     m_rebootRep.setValue("value", m_rebootValue);
178
179     return m_rebootRep;
180 }
181
182 OCRepresentation DiagnosticsCollection::getStartCollectionRepresentation()
183 {
184     m_startCollectionRep.setValue("value", m_startCollectionValue);
185
186     return m_startCollectionRep;
187 }
188
189 OCRepresentation DiagnosticsCollection::getDiagnosticsRepresentation()
190 {
191     m_diagnosticsRep.clearChildren();
192
193     m_diagnosticsRep.addChild(getFactoryResetRepresentation());
194     m_diagnosticsRep.addChild(getRebootRepresentation());
195     m_diagnosticsRep.addChild(getStartCollectionRepresentation());
196
197     m_diagnosticsRep.setValue("value", m_diagnosticsValue);
198
199     return m_diagnosticsRep;
200 }
201
202 std::string DiagnosticsCollection::getDiagnosticsUri()
203 {
204     return m_diagnosticsUri;
205 }
206
207 std::string DiagnosticsCollection::getFactoryResetUri()
208 {
209     return m_factoryResetUri;
210 }
211
212 std::string DiagnosticsCollection::getRebootUri()
213 {
214     return m_rebootUri;
215 }
216
217 std::string DiagnosticsCollection::getStartCollectionUri()
218 {
219     return m_startCollectionUri;
220 }
221
222 void DiagnosticsCollection::diagnosticsMonitor(int second)
223 {
224     while (1)
225     {
226         sleep(second);
227
228         if (m_rebootValue == "true")
229         {
230             int res;
231             std::cout << "Reboot will be soon..." << std::endl;
232             m_rebootValue = defaultReboot;
233             res = system("sudo reboot"); // System reboot
234
235             std::cout << "return: " << res << std::endl;
236
237         }
238         else if (m_factoryResetValue == "true")
239         {
240             std::cout << "Factory Reset will be soon..." << std::endl;
241             m_factoryResetValue = defaultFactoryReset;
242             factoryReset();
243         }
244     }
245 }