1 //******************************************************************
3 // Copyright 2016 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "RCSRequest.h"
23 #include "SceneListResource.h"
29 SceneListResource::SceneListResource()
30 : m_SceneListName(), m_SceneListObj(), m_RequestHandler()
32 m_SceneListObj = RCSResourceObject::Builder(
33 SCENE_LIST_URI, SCENE_LIST_RT, OC_RSRVD_INTERFACE_DEFAULT).
34 addInterface(OC::BATCH_INTERFACE).
35 setDiscoverable(true).setObservable(false).build();
38 RCSResourceObject::LockGuard guard(m_SceneListObj);
39 m_SceneListObj->setAttribute(SCENE_KEY_NAME, SCENE_LIST_DEFAULT_NAME);
40 m_SceneListObj->setAttribute(SCENE_KEY_RTS, SCENE_LIST_RT);
43 m_SceneListObj->setSetRequestHandler(&SceneListRequestHandler::onSetRequest);
46 SceneListResource * SceneListResource::getInstance()
48 static SceneListResource instance;
52 void SceneListResource::addSceneCollectionResource(
53 SceneCollectionResource::Ptr newObject)
55 std::unique_lock<std::mutex> collectionlock(m_SceneCollectionLock);
56 m_SceneCollections.push_back(newObject);
57 m_SceneListObj->bindResource(newObject->getRCSResourceObject());
60 std::string SceneListResource::getName() const
62 return m_SceneListName;
65 void SceneListResource::setName(std::string && newName)
67 m_SceneListName = newName;
69 RCSResourceObject::LockGuard guard(m_SceneListObj);
70 m_SceneListObj->setAttribute(SCENE_KEY_NAME, m_SceneListName);
73 void SceneListResource::setName(const std::string & newName)
75 setName(std::string(newName));
78 const std::vector<SceneCollectionResource::Ptr>
79 SceneListResource::getSceneCollections()
81 std::unique_lock<std::mutex> collectionlock(m_SceneCollectionLock);
82 std::vector<SceneCollectionResource::Ptr> retCollections(m_SceneCollections);
83 return retCollections;
87 SceneListResource::SceneListRequestHandler::onSetRequest(
88 const RCSRequest & request, RCSResourceAttributes & attributes)
90 if (request.getInterface() != OC::BATCH_INTERFACE)
92 return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
93 setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
97 = SceneCollectionResource::createSceneCollectionObject();
99 if (attributes.contains(SCENE_KEY_NAME))
101 newObject->setName(attributes.at(SCENE_KEY_NAME).get<std::string>());
104 SceneListResource::getInstance()->addSceneCollectionResource(newObject);
106 auto responseAtt = attributes;
107 responseAtt[SCENE_KEY_NAME] = RCSResourceAttributes::Value(newObject->getName());
108 responseAtt[SCENE_KEY_ID] = RCSResourceAttributes::Value(newObject->getId());
110 auto uri = COAP_TAG + newObject->getAddress() + newObject->getUri();
111 responseAtt[SCENE_KEY_PAYLOAD_LINK]
112 = RCSResourceAttributes::Value(uri);
114 return RCSSetResponse::create(responseAtt, SCENE_RESPONSE_SUCCESS).
115 setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);