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 #ifndef _NS_CONSUMER_SIMULATOR_H_
22 #define _NS_CONSUMER_SIMULATOR_H_
26 #include "OCPlatform.h"
29 class NSConsumerSimulator
32 std::function<void(const int&, const std::string&, const std::string&,
33 const std::string&)> m_messageFunc;
34 std::function<void(const int&, const int&)> m_syncFunc;
36 std::shared_ptr<OC::OCResource> m_syncResource;
37 std::shared_ptr<OC::OCResource> m_msgResource;
38 std::shared_ptr<OC::OCResource> m_topicResource;
44 : m_messageFunc(), m_syncFunc(),
45 m_syncResource(), isTopicPost(false) { };
46 ~NSConsumerSimulator() = default;
48 NSConsumerSimulator(const NSConsumerSimulator &) = delete;
49 NSConsumerSimulator & operator = (const NSConsumerSimulator &) = delete;
51 NSConsumerSimulator(NSConsumerSimulator &&) = delete;
52 NSConsumerSimulator & operator = (NSConsumerSimulator &&) = delete;
56 OC::OCPlatform::findResource("", std::string("/oic/res?rt=oic.wk.notification"),
57 OCConnectivityType::CT_DEFAULT,
58 std::bind(&NSConsumerSimulator::findResultCallback, this, std::placeholders::_1),
59 OC::QualityOfService::LowQos);
62 void syncToProvider(int & type, const int & id, const std::string & providerID)
64 if (m_syncResource == nullptr)
69 OC::OCRepresentation rep;
70 rep.setValue("providerid", providerID);
71 rep.setValue("messageid", id);
72 rep.setValue("state", type);
74 m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
79 if(!msgResourceCancelObserve(OC::QualityOfService::HighQos) &&
80 !syncResourceCancelObserve(OC::QualityOfService::HighQos))
85 void setCallback(std::function<void(const int&, const std::string&,
86 const std::string&, const std::string&)> messageFunc,
87 const std::function<void(const int&, const int&)> & syncFunc)
89 m_messageFunc = messageFunc;
90 m_syncFunc = syncFunc;
94 static void onPost(const OC::HeaderOptions &/*headerOption*/,
95 const OC::OCRepresentation & /*rep*/ , const int /*eCode*/)
98 void findResultCallback(std::shared_ptr<OC::OCResource> resource)
100 if(resource->uri() == "/notification")
102 resource->get(std::string("oic.wk.notification"), std::string("oic.if.baseline"),
103 OC::QueryParamsMap(), std::bind(&NSConsumerSimulator::onGet, this,
104 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
105 resource), OC::QualityOfService::LowQos);
108 void onGet(const OC::HeaderOptions &/*headerOption*/,
109 const OC::OCRepresentation & /*rep*/ , const int /*eCode*/,
110 std::shared_ptr<OC::OCResource> resource)
112 OC::QueryParamsMap map;
113 map.insert(std::pair<std::string,std::string>(std::string("consumerid"),
114 std::string("123456789012345678901234567890123456")));
118 std::vector<std::string> rts{"oic.wk.notification"};
121 = OC::OCPlatform::constructResourceObject(
122 std::string(resource->host()), std::string(resource->uri() + "/message"),
123 OCConnectivityType(resource->connectivityType()), true, rts,
124 std::vector<std::string>(resource->getResourceInterfaces()));
126 m_msgResource->observe(OC::ObserveType::Observe, map,
127 std::bind(&NSConsumerSimulator::onObserve, this,
128 std::placeholders::_1, std::placeholders::_2,
129 std::placeholders::_3, std::placeholders::_4, resource),
130 OC::QualityOfService::LowQos);
132 catch(std::exception & e)
134 std::cout << "OC::ResoureInitException : " << e.what() << std::endl;
137 = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync",
138 resource->connectivityType(), true, resource->getResourceTypes(),
139 resource->getResourceInterfaces());
141 m_syncResource->observe(OC::ObserveType::Observe, map,
142 std::bind(&NSConsumerSimulator::onObserve, this,
143 std::placeholders::_1, std::placeholders::_2,
144 std::placeholders::_3, std::placeholders::_4, resource),
145 OC::QualityOfService::LowQos);
149 = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/topic",
150 resource->connectivityType(), true, resource->getResourceTypes(),
151 resource->getResourceInterfaces());
154 void onObserve(const OC::HeaderOptions &/*headerOption*/,
155 const OC::OCRepresentation &rep , const int & /*eCode*/, const int &,
156 std::shared_ptr<OC::OCResource> )
158 if (rep.getUri() == "/notification/message" && rep.hasAttribute("messageid")
159 && rep.getValue<int>("messageid") != 1)
161 m_messageFunc(int(rep.getValue<int>("messageid")),
162 std::string(rep.getValueToString("title")),
163 std::string(rep.getValueToString("contenttext")),
164 std::string(rep.getValueToString("source")));
166 if(rep.getValue<int>("messageid") == 3)
168 m_topicResource->get(std::string("oic.wk.notification"),
169 std::string("oic.if.baseline"), OC::QueryParamsMap(),
170 std::bind(&NSConsumerSimulator::onTopicGet, this, std::placeholders::_1,
171 std::placeholders::_2, std::placeholders::_3, m_topicResource),
172 OC::QualityOfService::LowQos);
175 else if (rep.getUri() == "/notification/sync")
177 m_syncFunc(int(rep.getValue<int>("state")), int(rep.getValue<int>("messageid")));
181 void onTopicGet(const OC::HeaderOptions &/*headerOption*/,
182 const OC::OCRepresentation & rep , const int /*eCode*/,
183 std::shared_ptr<OC::OCResource> /*resource*/)
189 OC::OCRepresentation postRep;
191 std::vector<OC::OCRepresentation> topicArr =
192 rep.getValue<std::vector<OC::OCRepresentation>>("topiclist");
194 std::vector<OC::OCRepresentation> postTopicArr;
196 for(std::vector<OC::OCRepresentation>::iterator it = topicArr.begin();
197 it != topicArr.end(); ++it)
199 /* std::cout << *it; ... */
200 OC::OCRepresentation topic = *it;
201 OC::OCRepresentation postTopic;
203 postTopic.setValue("topicname", topic.getValueToString("topicname"));
204 postTopic.setValue("topicstate", (int) topic.getValue<int>("topicstate"));
206 postTopicArr.push_back(topic);
208 // std::cout << "tName : " << tName << std::endl;
209 // std::cout << "tState : " << tState << std::endl;
212 postRep.setValue<std::vector<OC::OCRepresentation>>
213 ("topiclist", postTopicArr);
215 OC::QueryParamsMap map;
216 map.insert(std::pair<std::string,std::string>(std::string("consumerid"),
217 std::string("123456789012345678901234567890123456")));
218 m_topicResource->post(postRep, map, &onPost, OC::QualityOfService::LowQos);
222 OCStackResult msgResourceCancelObserve(OC::QualityOfService qos)
224 return m_msgResource->cancelObserve(qos);
227 OCStackResult syncResourceCancelObserve(OC::QualityOfService qos)
229 return m_syncResource->cancelObserve(qos);
234 #endif //_NS_CONSUMER_SIMULATOR_H_