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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
27 #include <OCPlatform.h>
30 #include "ThingsMaintenance.h"
36 std::map< std::string, MaintenanceRequestEntry > maintenanceRequestTable;
37 ThingsMaintenance* ThingsMaintenance::thingsMaintenanceInstance = NULL;
39 MaintenanceRequestEntry::MaintenanceRequestEntry(std::string ID, MaintenanceCallback callback,
40 std::shared_ptr< OCResource > resource, std::string updateVal)
43 m_callback = callback;
44 m_resource = resource;
45 m_updateVal = updateVal;
48 MaintenanceUnitInfo::MaintenanceUnitInfo(std::string name,
49 std::string attribute,
53 m_attribute = attribute;
57 std::string MaintenanceUnitInfo::getJSON()
61 res = "{\"name\":\"" + m_name + "\",\"attribute\":\"" + m_attribute + "\"}";
66 ThingsMaintenance::ThingsMaintenance()
68 MaintenanceUnitInfo unit[] =
70 { "rb", "Reboot", "/oic/mnt"},
71 { "ssc", "StartStatCollection", "/oic/mnt"},
72 { "fr", "Factory Reset", "/oic/mnt" } };
74 for (int i = 0; i < NUMDIAGUNIT; i++)
75 MaintenanceUnitTable.push_back(unit[i]);
78 ThingsMaintenance::~ThingsMaintenance()
82 void ThingsMaintenance::setGroupManager(GroupManager *groupmanager)
84 g_groupmanager = groupmanager;
87 ThingsMaintenance* ThingsMaintenance::getInstance()
89 if (thingsMaintenanceInstance == NULL)
91 thingsMaintenanceInstance = new ThingsMaintenance();
93 return thingsMaintenanceInstance;
96 void ThingsMaintenance::deleteInstance()
98 if (thingsMaintenanceInstance)
100 delete thingsMaintenanceInstance;
101 thingsMaintenanceInstance = NULL;
105 std::string ThingsMaintenance::getAttributeByMaintenanceName(MaintenanceName name)
107 for (auto it = MaintenanceUnitTable.begin(); MaintenanceUnitTable.end() != it; it++)
109 if ((*it).m_name == name)
110 return (*it).m_attribute;
116 std::string ThingsMaintenance::getUriByMaintenanceName(MaintenanceName name)
118 for (auto it = MaintenanceUnitTable.begin(); MaintenanceUnitTable.end() != it; it++)
120 if ((*it).m_name == name)
127 std::string ThingsMaintenance::getUpdateVal(std::string mnt)
129 std::map< std::string, MaintenanceRequestEntry >::iterator it =
130 maintenanceRequestTable.find(mnt);
132 if (it == maintenanceRequestTable.end())
135 return it->second.m_updateVal;
138 std::shared_ptr< OCResource > ThingsMaintenance::getResource(std::string mnt)
140 std::map< std::string, MaintenanceRequestEntry >::iterator it =
141 maintenanceRequestTable.find(mnt);
143 if (it == maintenanceRequestTable.end())
146 return it->second.m_resource;
149 MaintenanceCallback ThingsMaintenance::getCallback(std::string mnt)
151 std::map< std::string, MaintenanceRequestEntry >::iterator it =
152 maintenanceRequestTable.find(mnt);
154 if (it == maintenanceRequestTable.end())
157 return it->second.m_callback;
160 std::string ThingsMaintenance::getHostFromURI(std::string oldUri)
165 if ((f = oldUri.find("/factoryset/oic/")) != string::npos)
166 newUri = oldUri.replace(f, oldUri.size(), "");
167 else if ((f = oldUri.find("/oic/")) != string::npos)
168 newUri = oldUri.replace(f, oldUri.size(), "");
173 std::string ThingsMaintenance::getListOfSupportedMaintenanceUnits()
177 res = "{\"Maintenance Units\":[";
179 auto it = MaintenanceUnitTable.begin();
182 res = res + (*it).getJSON();
185 if (it == MaintenanceUnitTable.end())
196 void ThingsMaintenance::onGetChildInfoForUpdate(const HeaderOptions& headerOptions,
197 const OCRepresentation& rep, const int eCode, std::string mnt)
199 if (eCode != OC_STACK_OK)
201 std::cout << "onGet Response error: " << eCode << std::endl;
202 getCallback(mnt)(headerOptions, rep, eCode);
206 std::cout << "GET request was successful" << std::endl;
208 std::cout << "\tResource URI: " << rep.getUri() << std::endl;
210 std::vector < OCRepresentation > children = rep.getChildren();
211 for (auto oit = children.begin(); oit != children.end(); ++oit)
213 std::cout << "\t\tChild Resource URI: " << oit->getUri() << std::endl;
216 // Get information by using maintenance name(mnt)
217 std::shared_ptr < OCResource > resource = getResource(mnt);
218 std::string actionstring = mnt;
219 std::string uri = getUriByMaintenanceName(mnt);
220 std::string attrKey = mnt;
227 // In this nest, we create a new action set of which name is the dignostics name.
228 // Required information consists of a host address, URI, attribute key, and
230 ActionSet *newActionSet = new ActionSet();
231 newActionSet->actionsetName = mnt;
233 for (auto oit = children.begin(); oit != children.end(); ++oit)
235 Action *newAction = new Action();
237 // oit->getUri() includes a host address as well as URI.
238 // We should split these to each other and only use the host address to create
239 // a child resource's URI. Note that the collection resource and its child
240 // resource are located in same host.
241 newAction->target = getHostFromURI(oit->getUri()) + uri;
243 Capability *newCapability = new Capability();
244 newCapability->capability = attrKey;
245 newCapability->status = getUpdateVal(mnt);
247 newAction->listOfCapability.push_back(newCapability);
248 newActionSet->listOfAction.push_back(newAction);
251 // Request to create a new action set by using the above actionSet
252 g_groupmanager->addActionSet(resource, newActionSet,
254 void(const HeaderOptions& headerOptions,
255 const OCRepresentation& rep, const int eCode) >(
256 std::bind(&ThingsMaintenance::onCreateActionSet, this,
257 std::placeholders::_1, std::placeholders::_2,
258 std::placeholders::_3, mnt)));
260 delete(newActionSet);
265 void ThingsMaintenance::onCreateActionSet(const HeaderOptions& headerOptions,
266 const OCRepresentation& rep, const int eCode, std::string mnt)
268 if (eCode != OC_STACK_OK)
270 std::cout << "onPut Response error: " << eCode << std::endl;
271 getCallback(mnt)(headerOptions, rep, eCode);
275 std::cout << "PUT request was successful" << std::endl;
277 std::shared_ptr < OCResource > resource = getResource(mnt);
280 // Now, it is time to execute the action set.
281 g_groupmanager->executeActionSet(resource, mnt,
283 void(const HeaderOptions& headerOptions,
284 const OCRepresentation& rep, const int eCode) >(
285 std::bind(&ThingsMaintenance::onExecuteForGroupAction, this,
286 std::placeholders::_1, std::placeholders::_2,
287 std::placeholders::_3, mnt)));
291 void ThingsMaintenance::onExecuteForGroupAction(const HeaderOptions& headerOptions,
292 const OCRepresentation& rep, const int eCode, std::string mnt)
294 if (eCode != OC_STACK_OK)
296 std::cout << "onPut Response error: " << eCode << std::endl;
297 getCallback(mnt)(headerOptions, rep, eCode);
301 std::cout << "PUT request was successful" << std::endl;
302 getCallback(mnt)(headerOptions, rep, eCode);
304 // Delete the created actionset
305 std::shared_ptr < OCResource > resource = getResource(mnt);
308 g_groupmanager->deleteActionSet(resource, mnt,
310 void(const HeaderOptions& headerOptions,
311 const OCRepresentation& rep, const int eCode) >(
312 std::bind(&ThingsMaintenance::onDeleteGroupAction, this,
313 std::placeholders::_1, std::placeholders::_2,
314 std::placeholders::_3, mnt)));
318 void ThingsMaintenance::onDeleteGroupAction(const HeaderOptions& headerOptions,
319 const OCRepresentation& rep, const int eCode, std::string mnt)
321 if (eCode != OC_STACK_OK)
323 std::cout << "Delete actionset returned with error: " << eCode << mnt << std::endl;
327 std::cout << "Deleted the actionset created!" << mnt<< std::endl;
330 void ThingsMaintenance::onPut(const HeaderOptions& headerOptions, const OCRepresentation& rep,
331 const int eCode, std::string mnt)
333 if (eCode != OC_STACK_OK)
335 std::cout << "onPut Response error: " << eCode << std::endl;
336 getCallback(mnt)(headerOptions, rep, eCode);
340 std::cout << "PUT request was successful" << std::endl;
342 getCallback(mnt)(headerOptions, rep, eCode);
346 bool ThingsMaintenance::isSimpleResource(std::shared_ptr< OCResource > resource)
348 for (unsigned int i = 0; i < resource->getResourceTypes().size(); ++i)
350 if (resource->getResourceTypes().at(0).find(".resourceset", 0) != std::string::npos)
357 OCStackResult ThingsMaintenance::reboot(std::shared_ptr< OCResource > resource,
358 MaintenanceCallback callback)
362 std::cout << "resource is NULL\n";
363 return OC_STACK_ERROR;
366 std::string mnt = "rb";
368 // Check the request queue if a previous request is still left. If so, remove it.
369 std::map< std::string, MaintenanceRequestEntry >::iterator iter =
370 maintenanceRequestTable.find(mnt);
371 if (iter != maintenanceRequestTable.end())
372 maintenanceRequestTable.erase(iter);
374 // Create new request entry stored in the queue
375 MaintenanceRequestEntry newCallback(mnt, callback, resource, "true");
376 maintenanceRequestTable.insert(std::make_pair(mnt, newCallback));
378 QueryParamsMap query;
379 OCRepresentation rep;
381 if (isSimpleResource(resource))
383 // This resource is a simple resource. Just send a PUT message
384 OCRepresentation rep;
385 rep.setValue < std::string > (mnt, "true");
387 return resource->put(resource->getResourceTypes().at(0), DEFAULT_INTERFACE, rep, query,
389 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
391 std::bind(&ThingsMaintenance::onPut, this, std::placeholders::_1,
392 std::placeholders::_2, std::placeholders::_3, mnt)));
396 // This resource is a collection resource. It just acquires child resource's URI and
397 // send GET massages to the child resources in turn.
398 // First, request the child resources's URI.
399 // TODO: Add a deletion of actionset
400 return resource->get(resource->getResourceTypes().at(0), DEFAULT_INTERFACE, query,
402 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
404 std::bind(&ThingsMaintenance::onGetChildInfoForUpdate, this,
405 std::placeholders::_1, std::placeholders::_2,
406 std::placeholders::_3, mnt)));
410 OCStackResult ThingsMaintenance::factoryReset(std::shared_ptr< OCResource > resource,
411 MaintenanceCallback callback)
415 std::cout << "resource is NULL\n";
416 return OC_STACK_ERROR;
419 std::string mnt = "fr";
421 // Check the request queue if a previous request is still left. If so, remove it.
422 std::map< std::string, MaintenanceRequestEntry >::iterator iter =
423 maintenanceRequestTable.find(mnt);
424 if (iter != maintenanceRequestTable.end())
425 maintenanceRequestTable.erase(iter);
427 // Create new request entry stored in the queue
428 MaintenanceRequestEntry newCallback(mnt, callback, resource, "true");
429 maintenanceRequestTable.insert(std::make_pair(mnt, newCallback));
431 QueryParamsMap query;
432 OCRepresentation rep;
434 if (isSimpleResource(resource))
436 // This resource is a simple resource. Just send a PUT message
437 OCRepresentation rep;
438 rep.setValue < std::string > ("value", "true");
440 return resource->put(resource->getResourceTypes().at(0), DEFAULT_INTERFACE, rep, query,
442 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
444 std::bind(&ThingsMaintenance::onPut, this, std::placeholders::_1,
445 std::placeholders::_2, std::placeholders::_3, mnt)));
449 // This resource is a collection resource. It just acquires child resource's URI and
450 // send GET massages to the child resources in turn.
451 // First, request the child resources's URI.
452 // TODO: Add a deletion of actionset
453 return resource->get(resource->getResourceTypes().at(0), DEFAULT_INTERFACE, query,
455 void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
457 std::bind(&ThingsMaintenance::onGetChildInfoForUpdate, this,
458 std::placeholders::_1, std::placeholders::_2,
459 std::placeholders::_3, mnt)));