Imported Upstream version 0.9.1
[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 DiagnosticsResource::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     thread exec(
57             std::function< void(int second) >(
58                     std::bind(&DiagnosticsResource::diagnosticsMonitor, this,
59                             std::placeholders::_1)), 10); // every 10 seconds
60     exec.detach();
61
62     std::cout << "Diagnostics Resource is Created!\n";
63 }
64
65 void DiagnosticsResource::setDiagnosticsRepresentation(OCRepresentation& rep)
66 {
67     string value;
68
69     if (rep.getValue("fr", value))
70     {
71         m_factoryReset = value;
72         std::cout << "\t\t\t\t" << "m_factoryReset: " << m_factoryReset << std::endl;
73     }
74
75     if (rep.getValue("rb", value))
76     {
77         m_reboot = value;
78         std::cout << "\t\t\t\t" << "m_reboot: " << m_reboot << std::endl;
79     }
80
81     if (rep.getValue("ssc", value))
82     {
83         m_startStatCollection = value;
84         std::cout << "\t\t\t\t" << "m_startStatCollection: " << m_startStatCollection << std::endl;
85     }
86 }
87
88 OCRepresentation DiagnosticsResource::getDiagnosticsRepresentation()
89 {
90     m_diagnosticsRep.setValue("fr", m_factoryReset);
91     m_diagnosticsRep.setValue("rb", m_reboot);
92     m_diagnosticsRep.setValue("ssc", m_startStatCollection);
93
94     return m_diagnosticsRep;
95 }
96
97 std::string DiagnosticsResource::getUri()
98 {
99     return m_diagnosticsUri;
100 }
101
102 void DiagnosticsResource::diagnosticsMonitor(int second)
103 {
104     while (1)
105     {
106         sleep(second);
107
108         if (m_reboot == "true")
109         {
110             int res;
111             std::cout << "Reboot will be soon..." << std::endl;
112             m_reboot = defaultReboot;
113             res = system("sudo reboot"); // System reboot
114
115             std::cout << "return: " << res << std::endl;
116
117         }
118         else if (m_factoryReset == "true")
119         {
120             std::cout << "Factory Reset will be soon..." << std::endl;
121             m_factoryReset = defaultFactoryReset;
122             factoryReset();
123         }
124     }
125 }