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 "ResourceObject.h"
23 #include "OCPlatform.h"
28 using namespace OC::OCPlatform;
29 using namespace OIC::Service;
31 std::string resourceUri = "/a/TempHumSensor";
32 std::string resourceType = "core.TemperatureSensor";
33 std::string resourceInterface = "oic.if.";
34 std::string attributeKey = "Temperature";
35 ResourceObject::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 PrimitiveGetResponse RequestHandlerForGet(const PrimitiveRequest &request,
49 ResourceAttributes &attrs)
51 cout << "\nRecieved a Get request from Client" << std::endl;
52 ResourceObject::LockGuard lock(*server);
53 ResourceAttributes attr = server->getAttributes();
54 ResourceAttributes::const_iterator iter = attr.begin();
55 for (unsigned int i = 0; i < attr.size(); ++i)
57 std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
60 return PrimitiveGetResponse::create(attr);
63 //hander for set request (if developer choose second option for resource Creation)
64 PrimitiveSetResponse RequestHandlerForSet(const PrimitiveRequest &request,
65 ResourceAttributes &attrs)
67 cout << "\nRecieved a Set request from Client" << std::endl;
68 ResourceAttributes::const_iterator iter = attrs.begin();
69 for (unsigned int i = 0; i < attrs.size(); ++i)
71 std::cout << "\nkey : " << iter->key() << "\nvalue : " << iter->value().toString() << std::endl;
74 return PrimitiveSetResponse::create(attrs);
81 int initialTemperature = 0;
88 //userInput for creation of Resource
89 std::cin >> userInput;
91 std::cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
101 //Creation of Resource & Auto control for all requests from Client
103 //creation of Resource
104 server = ResourceObject::Builder(resourceUri, resourceType,
105 resourceInterface).setDiscoverable(true).setObservable(false).build();
106 std::cout << "Resource created successfully " << std::endl;
108 //setting attribute for the Created Resource
109 server->setAttribute(attributeKey, initialTemperature);
115 //Creation of Resource & setting get and set handler for handling get and set request from client in application
117 server = ResourceObject::Builder(resourceUri, resourceType,
118 resourceInterface).setDiscoverable(true).setObservable(false).build();
119 std::cout << "Resource created successfully " << std::endl;
121 //setting attribute for the Created Resource
122 server->setAttribute(attributeKey, initialTemperature);
124 //setting handler for handling get request from the client
125 server->setGetRequestHandler(RequestHandlerForGet);
127 // setting handler for handling set request from the client
128 server->setSetRequestHandler(RequestHandlerForSet);
135 std::cout << "Invalid Input" << std::endl;
144 cout << "========================================================" << endl;
145 cout << "1. Increase Temperature by 10 degree" << endl;
146 cout << "2. Decrease Temperature by 10 degree" << endl;
147 cout << "3. Stop the Sensor" << endl;
148 cout << "========================================================" << endl;
150 //user Input for increasing/decreasing the temperature
151 cin >> temperatureInput;
155 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
156 std::cout << "Invalid input type, please try again" << std::endl;
160 switch (temperatureInput)
164 ResourceObject::LockGuard lock(*server);
165 ResourceAttributes attrs = server->getAttributes();
167 attrs[attributeKey] = (server->getAttribute<int>(attributeKey) + 10);
168 server->setAttribute(attributeKey, attrs[attributeKey]);
169 cout << "\nTemperature increased by 10 degree" << endl;
171 //getting the current attribute and priniting it on the console
172 attrs = server->getAttributes();
173 cout << "\nCurrent Temperature : ";
174 ResourceAttributes::const_iterator iter = attrs.begin();
175 for (unsigned int i = 0; i < attrs.size(); ++i)
177 std::cout << iter->value().toString() << std::endl;
184 ResourceObject::LockGuard lock(*server);
185 ResourceAttributes attrs = server->getAttributes();
186 attrs[attributeKey] = (server->getAttribute<int>(attributeKey) - 10);
187 server->setAttribute(attributeKey, attrs[attributeKey]);
188 cout << "\nTemperature decreased by 10 degree" << endl;
190 //getting the current attribute and priniting it on the console
191 attrs = server->getAttributes();
192 cout << "\nCurrent Temperature : ";
193 ResourceAttributes::const_iterator iter = attrs.begin();
194 for (unsigned int i = 0; i < attrs.size(); ++i)
196 std::cout << iter->value().toString() << std::endl;
203 cout << "Stopping the Resource" << endl;
209 cout << "Invalid input. Please try again." << endl;
221 cout << "main exception : " << e.what() << endl;