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 "SceneMemberResource.h"
24 #include "OCPlatform.h"
32 std::atomic_int g_numOfSceneMember(0);
35 SceneMemberResource::Ptr
36 SceneMemberResource::createSceneMemberResource(
37 RCSRemoteResourceObject::Ptr remoteObject)
39 SceneMemberResource::Ptr sceneMemberResource(new SceneMemberResource());
41 sceneMemberResource->m_uri = PREFIX_SCENE_MEMBER_URI + "/" +
42 std::to_string(g_numOfSceneMember++);
44 sceneMemberResource->m_remoteMemberObj = remoteObject;
46 sceneMemberResource->createResourceObject();
47 sceneMemberResource->initSetRequestHandler();
48 sceneMemberResource->setDefaultAttributes();
50 return sceneMemberResource;
53 SceneMemberResource::Ptr
54 SceneMemberResource::createSceneMemberResource(const RCSResourceAttributes & link)
56 return createSceneMemberResource(RCSResourceAttributes(link));
59 SceneMemberResource::Ptr
60 SceneMemberResource::createSceneMemberResource(RCSResourceAttributes && link)
62 auto href = link.at(SCENE_KEY_HREF).get<std::string>();
67 SceneUtils::getHostUriString(href, &address, &uri);
70 = OC::OCPlatform::constructResourceObject(
71 address, uri, OCConnectivityType::CT_ADAPTER_IP, false,
72 link.at(SCENE_KEY_RT).get<std::vector<std::string>>(),
73 link.at(SCENE_KEY_IF).get<std::vector<std::string>>());
75 return createSceneMemberResource(RCSRemoteResourceObject::fromOCResource(ocResourcePtr));
78 void SceneMemberResource::createResourceObject()
80 m_sceneMemberResourceObj
81 = RCSResourceObject::Builder(
82 m_uri, SCENE_MEMBER_RT, OC_RSRVD_INTERFACE_DEFAULT).
83 setDiscoverable(true).setObservable(false).build();
86 void SceneMemberResource::setDefaultAttributes()
88 m_sceneMemberResourceObj->setAttribute(SCENE_KEY_ID, SceneUtils::OICGenerateUUIDStr());
89 m_sceneMemberResourceObj->setAttribute(SCENE_KEY_NAME, std::string());
91 RCSResourceAttributes subAtt;
92 subAtt[SCENE_KEY_HREF]
93 = RCSResourceAttributes::Value(
94 m_remoteMemberObj->getAddress() + m_remoteMemberObj->getUri());
95 subAtt[SCENE_KEY_IF] = RCSResourceAttributes::Value(m_remoteMemberObj->getInterfaces());
96 subAtt[SCENE_KEY_RT] = RCSResourceAttributes::Value(m_remoteMemberObj->getTypes());
97 m_sceneMemberResourceObj->setAttribute(SCENE_KEY_PAYLOAD_LINK, subAtt);
99 m_sceneMemberResourceObj->setAttribute(
100 SCENE_KEY_SCENEMAPPINGS, std::vector<RCSResourceAttributes>());
101 m_sceneMemberResourceObj->setAttribute(SCENE_KEY_URI, m_uri);
104 void SceneMemberResource::initSetRequestHandler()
106 m_requestHandler.m_owner = std::weak_ptr<SceneMemberResource>(shared_from_this());
107 m_sceneMemberResourceObj->setSetRequestHandler(std::bind(
108 &SceneMemberResource::SceneMemberRequestHandler::onSetRequest,
109 m_requestHandler, std::placeholders::_1, std::placeholders::_2));
112 void SceneMemberResource::addMappingInfo(MappingInfo && mInfo)
114 RCSResourceAttributes newAtt;
116 RCSResourceObject::LockGuard guard(m_sceneMemberResourceObj);
117 newAtt = m_sceneMemberResourceObj->getAttributes();
120 auto mappingInfo = newAtt.at(SCENE_KEY_SCENEMAPPINGS).
121 get<std::vector<RCSResourceAttributes>>();
123 auto foundMInfo = std::find_if(mappingInfo.begin(), mappingInfo.end(),
124 [& mInfo](const RCSResourceAttributes & att) -> bool
126 return (att.at(SCENE_KEY_SCENE).get<std::string>() == mInfo.sceneName) &&
127 (att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>() == mInfo.key);
130 if (foundMInfo != mappingInfo.end())
132 mappingInfo.erase(foundMInfo);
134 RCSResourceAttributes newMapInfo;
135 newMapInfo[SCENE_KEY_SCENE] = RCSResourceAttributes::Value(std::move(mInfo.sceneName));
136 newMapInfo[SCENE_KEY_MEMBERPROPERTY] = RCSResourceAttributes::Value(std::move(mInfo.key));
137 newMapInfo[SCENE_KEY_MEMBERVALUE] = std::move(mInfo.value);
138 mappingInfo.push_back(newMapInfo);
140 m_sceneMemberResourceObj->setAttribute(SCENE_KEY_SCENEMAPPINGS, mappingInfo);
143 void SceneMemberResource::addMappingInfo(const MappingInfo & mInfo)
145 addMappingInfo(MappingInfo(mInfo));
148 std::vector<SceneMemberResource::MappingInfo> SceneMemberResource::getMappingInfos() const
150 std::vector<MappingInfo> retMInfo;
152 auto mInfo = m_sceneMemberResourceObj->getAttributeValue(SCENE_KEY_SCENEMAPPINGS).
153 get<std::vector<RCSResourceAttributes>>();
154 std::for_each(mInfo.begin(), mInfo.end(),
155 [& retMInfo](const RCSResourceAttributes & att)
157 retMInfo.push_back(MappingInfo::create(att));
163 std::string SceneMemberResource::getId() const
165 return m_sceneMemberResourceObj->getAttributeValue(SCENE_KEY_ID).get<std::string>();
168 std::string SceneMemberResource::getFullUri() const
170 return std::string(COAP_TAG + SceneUtils::getNetAddress() + m_uri);
173 std::string SceneMemberResource::getTargetUri() const
175 return std::string(m_remoteMemberObj->getAddress() + m_remoteMemberObj->getUri());
178 RCSRemoteResourceObject::Ptr SceneMemberResource::getRemoteResourceObject() const
180 return m_remoteMemberObj;
183 RCSResourceObject::Ptr SceneMemberResource::getRCSResourceObject() const
185 return m_sceneMemberResourceObj;
188 void SceneMemberResource::execute(std::string && sceneName)
190 execute(std::move(sceneName), nullptr);
193 void SceneMemberResource::execute(const std::string & sceneName)
195 execute(std::string(sceneName));
198 void SceneMemberResource::execute(std::string && sceneName, MemberexecuteCallback executeCB)
200 RCSResourceAttributes setAtt;
202 auto mInfo = getMappingInfos();
203 std::for_each(mInfo.begin(), mInfo.end(),
204 [& setAtt, & sceneName](const MappingInfo & info)
206 if(info.sceneName == sceneName)
208 setAtt[info.key] = info.value;
212 if (setAtt.empty() && executeCB != nullptr)
214 executeCB(RCSResourceAttributes(), SCENE_RESPONSE_SUCCESS);
217 m_remoteMemberObj->setRemoteAttributes(setAtt, executeCB);
220 void SceneMemberResource::execute(
221 const std::string & sceneName, MemberexecuteCallback executeCB)
223 execute(std::string(sceneName), std::move(executeCB));
226 void SceneMemberResource::setName(const std::string & name)
228 setName(std::string(name));
231 void SceneMemberResource::setName(std::string && name)
233 m_sceneMemberResourceObj->setAttribute(SCENE_KEY_NAME, std::move(name));
236 std::string SceneMemberResource::getName() const
238 return m_sceneMemberResourceObj->getAttributeValue(SCENE_KEY_NAME).toString();
241 std::vector<SceneMemberResource::MappingInfo> SceneMemberResource::findMappingInfos(
242 const std::string & sceneValue) const
244 auto mInfo = getMappingInfos();
245 std::vector<MappingInfo> retMInfo;
247 std::for_each(mInfo.begin(), mInfo.end(),
248 [& retMInfo, & sceneValue](const MappingInfo & info)
250 if(info.sceneName == sceneValue)
252 retMInfo.push_back(MappingInfo(info));
258 bool SceneMemberResource::hasSceneValue(const std::string & sceneValue) const
260 auto mInfo = getMappingInfos();
261 if (std::find_if(mInfo.begin(), mInfo.end(),
262 [& sceneValue](const MappingInfo & info) -> bool
264 return info.sceneName == sceneValue;
272 SceneMemberResource::MappingInfo
273 SceneMemberResource::MappingInfo::create(const RCSResourceAttributes & att)
275 return MappingInfo(att.at(SCENE_KEY_SCENE).get<std::string>(),
276 att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
277 att.at(SCENE_KEY_MEMBERVALUE));
280 RCSSetResponse SceneMemberResource::SceneMemberRequestHandler::
281 onSetRequest(const RCSRequest & request, RCSResourceAttributes & attributes)
283 if (attributes.contains(SCENE_KEY_SCENEMAPPINGS))
285 addMappingInfos(request, attributes);
288 if (attributes.contains(SCENE_KEY_NAME))
290 setSceneMemberName(request, attributes);
293 return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
294 setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
298 SceneMemberResource::SceneMemberRequestHandler::addMappingInfos(
299 const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
301 int eCode = SCENE_RESPONSE_SUCCESS;
302 auto ptr = m_owner.lock();
305 eCode = SCENE_CLIENT_BADREQUEST;
309 auto mInfo = attributes.at(SCENE_KEY_SCENEMAPPINGS).
310 get<std::vector<RCSResourceAttributes>>();
311 std::for_each(mInfo.begin(), mInfo.end(),
312 [& ptr](const RCSResourceAttributes & att)
314 ptr->addMappingInfo(SceneMemberResource::MappingInfo::create(att));
318 return RCSSetResponse::create(attributes, eCode).
319 setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
323 SceneMemberResource::SceneMemberRequestHandler::setSceneMemberName(
324 const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
326 int eCode = SCENE_RESPONSE_SUCCESS;
327 auto ptr = m_owner.lock();
330 eCode = SCENE_CLIENT_BADREQUEST;
334 ptr->setName(attributes.at(SCENE_KEY_NAME).get<std::string>());
337 return RCSSetResponse::create(attributes, eCode).
338 setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);