1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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 "PrimitiveResource.h"
22 #include "RCSResourceObject.h"
23 #include "OCPlatform.h"
28 using namespace OC::OCPlatform;
29 using namespace OIC::Service;
31 std::string resourceUri = "/a/TempSensor";
32 std::string resourceType = "core.TemperatureSensor";
33 std::string resourceInterface = "oic.if.";
34 std::string attributeKey = "Temperature";
35 RCSResourceObject::Ptr server;
37 //display the menu on the screen
40 std::cout << "=====================================================================\n\n";
41 std::cout << " 1 - Creation of Resource [Auto control for requests]" << std::endl;
42 std::cout << " 2 - Creation of Resource [Developer control for Get and Set requests]" <<std::endl;
43 std::cout << " 3 - Quit" << std::endl;
44 std::cout << "=====================================================================\n\n";
47 //hander for get request (if developer choose second option for resource Creation)
48 RCSGetResponse RequestHandlerForGet(const RCSRequest &request,
49 ResourceAttributes &attrs)
51 cout << "Recieved a Get request from Client" << std::endl;
52 RCSResourceObject::LockGuard lock(*server);
53 ResourceAttributes attr = server->getAttributes();
54 ResourceAttributes::const_iterator iter = attr.begin();
55 cout << "\nSending response to Client : " << std::endl;
56 for (unsigned int i = 0; i < attr.size(); ++i)
58 std::cout << "\tkey : " << iter->key() << "\n\tvalue : " << iter->value().toString() << std::endl;
61 return RCSGetResponse::create(attr);
64 //hander for set request (if developer choose second option for resource Creation)
65 RCSSetResponse RequestHandlerForSet(const RCSRequest &request,
66 ResourceAttributes &attrs)
68 cout << "Recieved a Set request from Client" << std::endl;
69 ResourceAttributes::const_iterator iter = attrs.begin();
70 for (unsigned int i = 0; i < attrs.size(); ++i)
72 std::cout << "\tkey : " << iter->key() << "\n\tvalue : " << iter->value().toString() << std::endl;
76 cout << "\n\nSending response to Client : " << std::endl;
77 for (unsigned int i = 0; i < attrs.size(); ++i)
79 std::cout << "\tkey : " << iter->key() << "\n\tvalue : " << iter->value().toString() << std::endl;
82 return RCSSetResponse::create(attrs);
89 int initialTemperature = 0;
96 //userInput for creation of Resource
97 std::cin >> userInput;
99 std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
109 //Creation of Resource & Auto control for all requests from Client
111 //creation of Resource
112 server = RCSResourceObject::Builder(resourceUri, resourceType,
113 resourceInterface).setDiscoverable(true).setObservable(false).build();
114 std::cout << "Resource created successfully " << std::endl;
116 //setting attribute for the Created Resource
117 server->setAttribute(attributeKey, initialTemperature);
123 //Creation of Resource & setting get and set handler for handling get and set request from client in application
125 server = RCSResourceObject::Builder(resourceUri, resourceType,
126 resourceInterface).setDiscoverable(true).setObservable(false).build();
127 std::cout << "Resource created successfully " << std::endl;
129 //setting attribute for the Created Resource
130 server->setAttribute(attributeKey, initialTemperature);
132 //setting handler for handling get request from the client
133 server->setGetRequestHandler(RequestHandlerForGet);
135 // setting handler for handling set request from the client
136 server->setSetRequestHandler(RequestHandlerForSet);
143 std::cout << "Invalid Input" << std::endl;
152 cout << "========================================================" << endl;
153 cout << "1. Increase Temperature by 10 degree" << endl;
154 cout << "2. Decrease Temperature by 10 degree" << endl;
155 cout << "3. Stop the Sensor" << endl;
156 cout << "========================================================" << endl;
158 //user Input for increasing/decreasing the temperature
159 cin >> temperatureInput;
163 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
164 std::cout << "Invalid input type, please try again" << std::endl;
168 switch (temperatureInput)
172 RCSResourceObject::LockGuard lock(*server);
173 ResourceAttributes attrs = server->getAttributes();
175 attrs[attributeKey] = (server->getAttribute<int>(attributeKey) + 10);
176 server->setAttribute(attributeKey, attrs[attributeKey]);
177 cout << "\nTemperature increased by 10 degree" << endl;
179 //getting the current attribute and priniting it on the console
180 attrs = server->getAttributes();
181 cout << "\nCurrent Temperature : ";
182 ResourceAttributes::const_iterator iter = attrs.begin();
183 for (unsigned int i = 0; i < attrs.size(); ++i)
185 std::cout << iter->value().toString() << std::endl;
192 RCSResourceObject::LockGuard lock(*server);
193 ResourceAttributes attrs = server->getAttributes();
194 attrs[attributeKey] = (server->getAttribute<int>(attributeKey) - 10);
195 server->setAttribute(attributeKey, attrs[attributeKey]);
196 cout << "\nTemperature decreased by 10 degree" << endl;
198 //getting the current attribute and priniting it on the console
199 attrs = server->getAttributes();
200 cout << "\nCurrent Temperature : ";
201 ResourceAttributes::const_iterator iter = attrs.begin();
202 for (unsigned int i = 0; i < attrs.size(); ++i)
204 std::cout << iter->value().toString() << std::endl;
211 cout << "Stopping the Resource" << endl;
217 cout << "Invalid input. Please try again." << endl;
229 cout << "main exception : " << e.what() << endl;