Remove a Things Manager class and expose its component classes for SDK
[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 #include <string.h>
28
29 #include "OCPlatform.h"
30 #include "OCApi.h"
31 #include "DiagnosticsCollection.h"
32
33 using namespace OC;
34 using namespace std;
35
36 /// This function internally calls registerResource API.
37 void MaintenanceResource::createResources(ResourceEntityHandler callback)
38 {
39     using namespace OC::OCPlatform;
40
41     if (callback == NULL)
42     {
43         std::cout << "callback should be binded\t";
44         return;
45     }
46
47     // This will internally create and register the resource.
48     OCStackResult result = registerResource(m_maintenanceHandle, m_maintenanceUri,
49             m_maintenanceTypes[0], m_maintenanceInterfaces[0], callback,
50             OC_DISCOVERABLE | OC_OBSERVABLE);
51
52     if (OC_STACK_OK != result)
53     {
54         std::cout << "Resource creation (maintenance) was unsuccessful\n";
55     }
56
57     thread exec(
58             std::function< void(int second) >(
59                     std::bind(&MaintenanceResource::maintenanceMonitor, this,
60                             std::placeholders::_1)), 10); // every 10 seconds
61     exec.detach();
62
63     std::cout << "maintenance Resource is Created!\n";
64 }
65
66 void MaintenanceResource::setMaintenanceRepresentation(OCRepresentation& rep)
67 {
68     string value;
69
70     if (rep.getValue("fr", value))
71     {
72         m_factoryReset = value;
73         std::cout << "\t\t\t\t" << "m_factoryReset: " << m_factoryReset << std::endl;
74     }
75
76     if (rep.getValue("rb", value))
77     {
78         m_reboot = value;
79         std::cout << "\t\t\t\t" << "m_reboot: " << m_reboot << std::endl;
80     }
81
82     if (rep.getValue("ssc", value))
83     {
84         m_startStatCollection = value;
85         std::cout << "\t\t\t\t" << "m_startStatCollection: " << m_startStatCollection << std::endl;
86     }
87 }
88
89 OCRepresentation MaintenanceResource::getMaintenanceRepresentation()
90 {
91     m_maintenanceRep.setValue("fr", m_factoryReset);
92     m_maintenanceRep.setValue("rb", m_reboot);
93     m_maintenanceRep.setValue("ssc", m_startStatCollection);
94
95     return m_maintenanceRep;
96 }
97
98 std::string MaintenanceResource::getUri()
99 {
100     return m_maintenanceUri;
101 }
102
103 void MaintenanceResource::maintenanceMonitor(int second)
104 {
105     while (1)
106     {
107         sleep(second);
108
109         if (m_reboot == "true")
110         {
111             int res;
112             std::cout << "Reboot will be soon..." << std::endl;
113             m_reboot = defaultReboot;
114             res = system("sudo reboot"); // System reboot
115
116             std::cout << "return: " << res << std::endl;
117
118         }
119         else if (m_factoryReset == "true")
120         {
121             std::cout << "Factory Reset will be soon..." << std::endl;
122             m_factoryReset = defaultFactoryReset;
123             factoryReset();
124         }
125     }
126 }