2 /******************************************************************
4 * Copyright 2015 Samsung Electronics All Rights Reserved.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 ******************************************************************/
22 #include <HeightSensorApp.h>
24 int g_Observation = 0;
26 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
29 * TempResourceFunctions
32 void HeightResource::registerResource()
34 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
36 // This will internally create and register the resource.
37 OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle, m_resourceUri,
38 m_resourceTypes[0], m_resourceInterfaces[0], &entityHandler, resourceProperty);
40 if (OC_STACK_OK != result)
42 cout << "Resource creation was unsuccessful\n";
46 OCResourceHandle HeightResource::getHandle()
48 return m_resourceHandle;
51 void HeightResource::setResourceRepresentation(OCRepresentation &rep)
57 rep.getValue("2", tempHeight);
62 cout << "\t\t\t" << "Received representation: " << endl;
64 cout << "\t\t\t\t" << "height" << m_height << endl;
68 OCRepresentation HeightResource::getResourceRepresentation()
71 m_resourceRep.setValue("0", std::string("height"));
72 m_resourceRep.setValue("1", std::string("double"));
73 m_resourceRep.setValue("2", std::to_string(m_height));
78 // Create the instance of the TemphumidResource class
79 HeightResource g_myResource;
81 void *TestSensorVal(void *param)
85 int nSleep_time=INTERVAL_FOR_CHECK;
88 std::cout << "[HeightSensorAPP] ::" << __func__ << " is called."
91 // g_myResource.m_height = 1;
95 // This function continuously monitors for the changes
108 nSleep_time =INTERVAL_FOR_MEASUREMENT;
110 g_myResource.m_height=nHeight;
113 nSleep_time =INTERVAL_FOR_CHECK;
114 g_myResource.m_height=0;
121 cout << "\n height updated to : " << g_myResource.m_height << endl;
122 cout << "Notifying observers with resource handle: " << g_myResource.getHandle()
125 OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle());
128 if (OC_STACK_NO_OBSERVERS == result)
130 cout << "No More observers, stopping notifications" << endl;
137 nSleep_time=INTERVAL_FOR_CHECK ;
144 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
146 cout << "\tIn Server CPP entity handler:\n";
148 auto response = std::make_shared<OC::OCResourceResponse>();
152 // Get the request type and request flag
153 std::string requestType = request->getRequestType();
154 int requestFlag = request->getRequestHandlerFlag();
156 response->setRequestHandle(request->getRequestHandle());
157 response->setResourceHandle(request->getResourceHandle());
159 if (requestFlag & RequestHandlerFlag::RequestFlag)
161 cout << "\t\trequestFlag : Request\n";
163 // If the request type is GET
164 if (requestType == "GET")
166 cout << "\t\t\trequestType : GET\n";
168 // Check for query params (if any)
169 // Process query params and do required operations ..
171 // Get the representation of this resource at this point and send it as response
172 OCRepresentation rep = g_myResource.getResourceRepresentation();
177 response->setErrorCode(200);
178 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
181 else if (requestType == "PUT")
183 // PUT request operations
185 else if (requestType == "POST")
187 // POST request operations
189 else if (requestType == "DELETE")
191 // DELETE request operations
195 if (requestFlag & RequestHandlerFlag::ObserverFlag)
199 cout << "\t\trequestFlag : Observer\n";
202 static int startedThread = 0;
206 pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL);
213 std::cout << "Request invalid" << std::endl;
216 return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;
221 // Create PlatformConfig object
222 PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos);
226 OC::OCPlatform::Configure(cfg);
228 OC::OCPlatform::startPresence(60);
230 g_myResource.registerResource();
233 cout << "Type any key to terminate" << endl;
236 OC::OCPlatform::stopPresence();
238 catch (std::exception e)
240 cout << e.what() << endl;