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;
42 : m_messageFunc(), m_syncFunc(),
44 ~NSConsumerSimulator() = default;
46 NSConsumerSimulator(const NSConsumerSimulator &) = delete;
47 NSConsumerSimulator & operator = (const NSConsumerSimulator &) = delete;
49 NSConsumerSimulator(NSConsumerSimulator &&) = delete;
50 NSConsumerSimulator & operator = (NSConsumerSimulator &&) = delete;
54 OC::OCPlatform::findResource("", std::string("/oic/res?rt=oic.r.notification"),
55 OCConnectivityType::CT_DEFAULT,
56 std::bind(&NSConsumerSimulator::findResultCallback, this, std::placeholders::_1),
57 OC::QualityOfService::LowQos);
60 void syncToProvider(int & type, const int & id, const std::string & providerID)
62 if (m_syncResource == nullptr)
67 OC::OCRepresentation rep;
68 rep.setValue("PROVIDER_ID", providerID);
69 rep.setValue("MESSAGE_ID", id);
70 rep.setValue("STATE", type);
72 m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
77 if(!msgResourceCancelObserve(OC::QualityOfService::HighQos) &&
78 !syncResourceCancelObserve(OC::QualityOfService::HighQos))
83 void setCallback(std::function<void(const int&, const std::string&,
84 const std::string&, const std::string&)> messageFunc,
85 const std::function<void(const int&, const int&)> & syncFunc)
87 m_messageFunc = messageFunc;
88 m_syncFunc = syncFunc;
92 static void onPost(const OC::HeaderOptions &/*headerOption*/,
93 const OC::OCRepresentation & /*rep*/ , const int /*eCode*/)
96 void findResultCallback(std::shared_ptr<OC::OCResource> resource)
98 if(resource->uri() == "/notification")
100 resource->get(OC::QueryParamsMap(),
101 std::bind(&NSConsumerSimulator::onGet, this,
102 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
103 resource), OC::QualityOfService::LowQos);
106 void onGet(const OC::HeaderOptions &/*headerOption*/,
107 const OC::OCRepresentation & /*rep*/ , const int /*eCode*/,
108 std::shared_ptr<OC::OCResource> resource)
110 OC::QueryParamsMap map;
111 map.insert(std::pair<std::string,std::string>(std::string("consumerid"),
112 std::string("123456789012345678901234567890123456")));
116 std::vector<std::string> rts{"oic.r.notification"};
119 = OC::OCPlatform::constructResourceObject(
120 std::string(resource->host()), std::string(resource->uri() + "/message"),
121 OCConnectivityType(resource->connectivityType()), true, rts,
122 std::vector<std::string>(resource->getResourceInterfaces()));
124 m_msgResource->observe(OC::ObserveType::Observe, map,
125 std::bind(&NSConsumerSimulator::onObserve, this,
126 std::placeholders::_1, std::placeholders::_2,
127 std::placeholders::_3, std::placeholders::_4, resource),
128 OC::QualityOfService::LowQos);
130 catch(std::exception & e)
132 std::cout << "OC::ResoureInitException : " << e.what() << std::endl;
135 = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync",
136 resource->connectivityType(), true, resource->getResourceTypes(),
137 resource->getResourceInterfaces());
139 m_syncResource->observe(OC::ObserveType::Observe, map,
140 std::bind(&NSConsumerSimulator::onObserve, this,
141 std::placeholders::_1, std::placeholders::_2,
142 std::placeholders::_3, std::placeholders::_4, resource),
143 OC::QualityOfService::LowQos);
147 = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/topic",
148 resource->connectivityType(), true, resource->getResourceTypes(),
149 resource->getResourceInterfaces());
152 void onObserve(const OC::HeaderOptions &/*headerOption*/,
153 const OC::OCRepresentation &rep , const int & /*eCode*/, const int &,
154 std::shared_ptr<OC::OCResource> )
156 if (rep.getUri() == "/notification/message" && rep.hasAttribute("MESSAGE_ID")
157 && rep.getValue<int>("MESSAGE_ID") != 1)
159 m_messageFunc(int(rep.getValue<int>("MESSAGE_ID")),
160 std::string(rep.getValueToString("TITLE")),
161 std::string(rep.getValueToString("CONTENT")),
162 std::string(rep.getValueToString("SOURCE")));
164 if(rep.getValue<int>("MESSAGE_ID") == 3)
166 m_topicResource->get(OC::QueryParamsMap(),
167 std::bind(&NSConsumerSimulator::onTopicGet, this, std::placeholders::_1,
168 std::placeholders::_2, std::placeholders::_3, m_topicResource),
169 OC::QualityOfService::LowQos);
172 else if (rep.getUri() == "/notification/sync")
174 m_syncFunc(int(rep.getValue<int>("STATE")), int(rep.getValue<int>("ID")));
178 void onTopicGet(const OC::HeaderOptions &/*headerOption*/,
179 const OC::OCRepresentation & /*rep*/ , const int /*eCode*/,
180 std::shared_ptr<OC::OCResource> /*resource*/)
184 OCStackResult msgResourceCancelObserve(OC::QualityOfService qos)
186 return m_msgResource->cancelObserve(qos);
189 OCStackResult syncResourceCancelObserve(OC::QualityOfService qos)
191 return m_syncResource->cancelObserve(qos);
196 #endif //_NS_CONSUMER_SIMULATOR_H_