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 numOfSceneMember(0);
35 SceneMemberResource::Ptr
36 SceneMemberResource::createSceneMemberResource(
37 RCSRemoteResourceObject::Ptr remoteObject)
39 SceneMemberResource::Ptr newSceneMemberObject(new SceneMemberResource());
41 newSceneMemberObject->m_uri = PREFIX_SCENE_MEMBER_URI + "/" +
42 std::to_string(numOfSceneMember++);
44 newSceneMemberObject->m_remoteMemberObj = remoteObject;
46 newSceneMemberObject->createResourceObject();
47 newSceneMemberObject->initSetRequestHandler();
48 newSceneMemberObject->setDefaultAttributes();
50 return newSceneMemberObject;
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(mInfo.sceneName);
136 newMapInfo[SCENE_KEY_MEMBERPROPERTY] = RCSResourceAttributes::Value(mInfo.key);
137 newMapInfo[SCENE_KEY_MEMBERVALUE] = 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::getMappingInfo()
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 MappingInfo info(att.at(SCENE_KEY_SCENE).get<std::string>(),
158 att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
159 att.at(SCENE_KEY_MEMBERVALUE));
160 retMInfo.push_back(info);
166 std::string SceneMemberResource::getId() const
168 return m_sceneMemberResourceObj->getAttributeValue(SCENE_KEY_ID).get<std::string>();
171 std::string SceneMemberResource::getFullUri() const
173 return std::string(COAP_TAG + SceneUtils::getNetAddress() + m_uri);
176 RCSRemoteResourceObject::Ptr SceneMemberResource::getRemoteResourceObject() const
178 return m_remoteMemberObj;
181 RCSResourceObject::Ptr SceneMemberResource::getRCSResourceObject() const
183 return m_sceneMemberResourceObj;
186 void SceneMemberResource::execute(std::string && sceneName)
188 execute(std::move(sceneName), nullptr);
191 void SceneMemberResource::execute(const std::string & sceneName)
193 execute(std::string(sceneName));
196 void SceneMemberResource::execute(std::string && sceneName, MemberexecuteCallback executeCB)
198 RCSResourceAttributes setAtt;
200 auto mInfo = getMappingInfo();
201 std::for_each (mInfo.begin(), mInfo.end(),
202 [& setAtt, & sceneName](const MappingInfo & info)
204 if(info.sceneName == sceneName)
206 setAtt[info.key] = info.value;
210 if (setAtt.empty() && executeCB != nullptr)
212 executeCB(RCSResourceAttributes(), SCENE_RESPONSE_SUCCESS);
215 m_remoteMemberObj->setRemoteAttributes(setAtt, executeCB);
218 void SceneMemberResource::execute(
219 const std::string & sceneName, MemberexecuteCallback executeCB)
221 execute(std::string(sceneName), std::move(executeCB));
224 void SceneMemberResource::setName(const std::string & name)
226 setName(std::string(name));
229 void SceneMemberResource::setName(std::string && name)
231 m_sceneMemberResourceObj->setAttribute(SCENE_KEY_NAME, std::move(name));
234 std::string SceneMemberResource::getName() const
236 return m_sceneMemberResourceObj->getAttributeValue(SCENE_KEY_NAME).toString();
239 RCSSetResponse SceneMemberResource::SceneMemberRequestHandler::
240 onSetRequest(const RCSRequest & request, RCSResourceAttributes & attributes)
242 if (attributes.contains(SCENE_KEY_SCENEMAPPINGS))
244 addMappingInfos(request, attributes);
247 if (attributes.contains(SCENE_KEY_NAME))
249 setSceneMemberName(request, attributes);
252 return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
253 setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
257 SceneMemberResource::SceneMemberRequestHandler::addMappingInfos(
258 const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
260 auto ptr = m_owner.lock();
263 return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
264 setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
267 auto mInfo = attributes.at(SCENE_KEY_SCENEMAPPINGS).
268 get<std::vector<RCSResourceAttributes>>();
269 std::for_each (mInfo.begin(), mInfo.end(),
270 [& ptr](const RCSResourceAttributes & att)
272 ptr->addMappingInfo(SceneMemberResource::MappingInfo(
273 att.at(SCENE_KEY_SCENE).get<std::string>(),
274 att.at(SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
275 att.at(SCENE_KEY_MEMBERVALUE)));
278 return RCSSetResponse::create(attributes).
279 setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
283 SceneMemberResource::SceneMemberRequestHandler::setSceneMemberName(
284 const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
286 auto ptr = m_owner.lock();
289 return RCSSetResponse::create(attributes, SCENE_CLIENT_BADREQUEST).
290 setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
293 ptr->setName(attributes.at(SCENE_KEY_NAME).get<std::string>());
295 return RCSSetResponse::create(attributes).
296 setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);