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;
40 : m_messageFunc(), m_syncFunc(),
42 ~NSConsumerSimulator() = default;
44 NSConsumerSimulator(const NSConsumerSimulator &) = delete;
45 NSConsumerSimulator & operator = (const NSConsumerSimulator &) = delete;
47 NSConsumerSimulator(NSConsumerSimulator &&) = delete;
48 NSConsumerSimulator & operator = (NSConsumerSimulator &&) = delete;
52 OC::OCPlatform::findResource("", std::string("/oic/res?rt=oic.r.notification"),
53 OCConnectivityType::CT_DEFAULT,
54 std::bind(&NSConsumerSimulator::findResultCallback, this, std::placeholders::_1),
55 OC::QualityOfService::LowQos);
58 void syncToProvider(int & type, const int & id, const std::string & providerID)
60 if (m_syncResource == nullptr)
62 std::cout << "m_syncResource is null" << std::endl;
66 OC::OCRepresentation rep;
67 rep.setValue("PROVIDER_ID", providerID);
68 rep.setValue("MESSAGE_ID", id);
69 rep.setValue("STATE", type);
71 m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
74 void setCallback(std::function<void(const int&, const std::string&,
75 const std::string&, const std::string&)> messageFunc,
76 const std::function<void(const int&, const int&)> & syncFunc)
78 m_messageFunc = messageFunc;
79 m_syncFunc = syncFunc;
83 static void onPost(const OC::HeaderOptions &/*headerOption*/,
84 const OC::OCRepresentation & /*rep*/ , const int eCode)
86 std::cout << __func__ << " result : " << eCode << std::endl;
88 void findResultCallback(std::shared_ptr<OC::OCResource> resource)
91 std::cout << __func__ << " " << resource->host() << std::endl;
93 if(resource->uri() == "/notification")
95 resource->get(OC::QueryParamsMap(),
96 std::bind(&NSConsumerSimulator::onGet, this,
97 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, resource),
98 OC::QualityOfService::LowQos);
101 void onGet(const OC::HeaderOptions &/*headerOption*/,
102 const OC::OCRepresentation & rep , const int eCode,
103 std::shared_ptr<OC::OCResource> resource)
105 std::cout << __func__ << " " << rep.getHost() << " result : " << eCode << std::endl;
107 OC::QueryParamsMap map;
108 map.insert(std::pair<std::string,std::string>(std::string("consumerid"), std::string("123456789012345678901234567890123456")));
112 std::cout << "resourc : host " << resource->host() << std::endl;
113 std::cout << "resourc : uri " << resource->uri() << std::endl;
114 std::cout << " resource->connectivityType() " << resource->connectivityType() << std::endl;
115 std::cout << "resourc : getResourceInterfaces " << resource->getResourceInterfaces()[0] << std::endl;
116 std::cout << "resourc : getResourceTypes " << resource->getResourceTypes()[0] << std::endl;
119 std::vector<std::string> rts{"oic.r.notification"};
121 std::shared_ptr<OC::OCResource> msgResource
122 = OC::OCPlatform::constructResourceObject(
123 std::string(resource->host()), std::string(resource->uri() + "/message"),
124 OCConnectivityType(resource->connectivityType()), true, rts,
125 std::vector<std::string>(resource->getResourceInterfaces()));
127 msgResource->observe(OC::ObserveType::Observe, map,
128 std::bind(&NSConsumerSimulator::onObserve, this,
129 std::placeholders::_1, std::placeholders::_2,
130 std::placeholders::_3, std::placeholders::_4, resource),
131 OC::QualityOfService::LowQos);
133 catch(std::exception & e)
135 std::cout << "OC::ResoureInitException : " << e.what() << std::endl;
138 = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync",
139 resource->connectivityType(), true, resource->getResourceTypes(),
140 resource->getResourceInterfaces());
142 m_syncResource->observe(OC::ObserveType::Observe, map,
143 std::bind(&NSConsumerSimulator::onObserve, this,
144 std::placeholders::_1, std::placeholders::_2,
145 std::placeholders::_3, std::placeholders::_4, resource),
146 OC::QualityOfService::LowQos);
149 void onObserve(const OC::HeaderOptions &/*headerOption*/,
150 const OC::OCRepresentation &rep , const int &eCode, const int &,
151 std::shared_ptr<OC::OCResource> )
153 std::cout << __func__ << " " << rep.getHost() << " result : " << eCode;
154 std::cout << " uri : " << rep.getUri() << std::endl;
156 if (rep.getUri() == "/notification/message" && rep.hasAttribute("MESSAGE_ID")
157 && rep.getValue<int>("MESSAGE_ID") != 1)
159 std::cout << "ID : " << rep.getValue<int>("ID") << std::endl;
160 std::cout << "TITLE : " << rep.getValueToString("TITLE") << std::endl;
161 std::cout << "CONTENT : " << rep.getValueToString("CONTENT") << std::endl;
162 m_messageFunc(int(rep.getValue<int>("MESSAGE_ID")),
163 std::string(rep.getValueToString("TITLE")),
164 std::string(rep.getValueToString("CONTENT")),
165 std::string(rep.getValueToString("SOURCE")));
167 else if (rep.getUri() == "/notification/sync")
169 std::cout << "else if (rep.getUri() == sync) " << std::endl;
170 m_syncFunc(int(rep.getValue<int>("STATE")), int(rep.getValue<int>("ID")));
176 #endif //_NS_CONSUMER_SIMULATOR_H_