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 std::string&, const std::string&, const std::string&)> m_messageFunc;
33 std::function<void(int, const std::string&)> m_syncFunc;
35 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 std::string & id)
60 if (m_syncResource == nullptr)
62 std::cout << "m_syncResource is null" << std::endl;
66 OC::OCRepresentation rep;
67 rep.setValue("ID", id);
68 rep.setValue("STATE", type);
70 m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
73 void setCallback(const std::function<void(const std::string&, const std::string&, const std::string&)> & messageFunc,
74 const std::function<void(int, const std::string&)> & syncFunc)
76 m_messageFunc = messageFunc;
77 m_syncFunc = syncFunc;
81 static void onPost(const OC::HeaderOptions &/*headerOption*/,
82 const OC::OCRepresentation & /*rep*/ , const int eCode)
84 std::cout << __func__ << " result : " << eCode << std::endl;
86 void findResultCallback(std::shared_ptr<OC::OCResource> resource)
88 std::cout << __func__ << " " << resource->host() << std::endl;
89 resource->get(OC::QueryParamsMap(),
90 std::bind(&NSConsumerSimulator::onGet, this,
91 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, resource),
92 OC::QualityOfService::LowQos);
94 void onGet(const OC::HeaderOptions &/*headerOption*/,
95 const OC::OCRepresentation & rep , const int eCode,
96 std::shared_ptr<OC::OCResource> resource)
98 std::cout << __func__ << " " << rep.getHost() << " result : " << eCode << std::endl;
100 std::shared_ptr<OC::OCResource> msgResource
101 = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/message",
102 resource->connectivityType(), false, resource->getResourceTypes(),
103 resource->getResourceInterfaces());
105 = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync",
106 resource->connectivityType(), false, resource->getResourceTypes(),
107 resource->getResourceInterfaces());
109 msgResource->observe(OC::ObserveType::Observe, OC::QueryParamsMap(),
110 std::bind(&NSConsumerSimulator::onObserve, this,
111 std::placeholders::_1, std::placeholders::_2,
112 std::placeholders::_3, std::placeholders::_4, resource),
113 OC::QualityOfService::LowQos);
114 m_syncResource->observe(OC::ObserveType::Observe, OC::QueryParamsMap(),
115 std::bind(&NSConsumerSimulator::onObserve, this,
116 std::placeholders::_1, std::placeholders::_2,
117 std::placeholders::_3, std::placeholders::_4, resource),
118 OC::QualityOfService::LowQos);
121 void onObserve(const OC::HeaderOptions &/*headerOption*/,
122 const OC::OCRepresentation &rep , const int &eCode, const int &,
123 std::shared_ptr<OC::OCResource> )
125 std::cout << __func__ << " " << rep.getHost() << " result : " << eCode;
126 std::cout << " uri : " << rep.getUri() << std::endl;
128 if (rep.getUri() == "/notification/message" && rep.hasAttribute("ID")
129 && rep.getValueToString("ID") != "0000-0000-0000-0000")
131 std::cout << "ID : " << rep.getValueToString("ID") << std::endl;
132 std::cout << "TITLE : " << rep.getValueToString("TITLE") << std::endl;
133 std::cout << "CONTENT : " << rep.getValueToString("CONTENT") << std::endl;
134 m_messageFunc(std::string(rep.getValueToString("ID")),
135 std::string(rep.getValueToString("TITLE")),
136 std::string(rep.getValueToString("CONTENT")));
138 else if (rep.getUri() == "/notification/sync")
140 m_syncFunc(int(rep.getValue<int>("STATE")),
141 std::string(rep.getValueToString("ID")));
147 #endif //_NS_CONSUMER_SIMULATOR_H_