1 //******************************************************************
3 // Copyright 2015 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 #include <WeightSensorApp.h>
27 int g_Observation = 0;
29 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
32 * TempResourceFunctions
35 void WeightResource::registerResource()
37 uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
39 // This will internally create and register the resource.
40 OCStackResult result = OC::OCPlatform::registerResource(m_resourceHandle, m_resourceUri,
41 m_resourceTypes[0], m_resourceInterfaces[0], &entityHandler, resourceProperty);
43 if (OC_STACK_OK != result)
45 cout << "Resource creation was unsuccessful\n";
49 OCResourceHandle WeightResource::getHandle()
51 return m_resourceHandle;
54 void WeightResource::setResourceRepresentation(OCRepresentation &rep)
56 double tempWeight = 0.0;
58 rep.getValue("weight", tempWeight);
60 m_weight = tempWeight;
62 cout << "\t\t\t" << "Received representation: " << endl;
63 cout << "\t\t\t\t" << "Weight" << m_weight << endl;
67 OCRepresentation WeightResource::getResourceRepresentation()
69 m_resourceRep.setValue("weight", std::to_string(m_weight));
74 // Create the instance of the TemphumidResource class
75 WeightResource g_myResource;
77 void *TestSensorVal(void *param)
82 int nSleep_time = INTERVAL_FOR_CHECK;
85 std::cout << "[WeightSensorAPP] ::" << __func__ << " is called."
90 // This function continuously monitors for the changes
99 nSleep_time = INTERVAL_FOR_MEASUREMENT;
101 g_myResource.m_weight = nWeight;
105 nSleep_time = INTERVAL_FOR_CHECK;
108 bFlag = bFlag ^ true;
110 cout << "\n weight updated to : " << g_myResource.m_weight << endl;
111 cout << "Notifying observers with resource handle: " << g_myResource.getHandle()
114 OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle());
116 if (OC_STACK_NO_OBSERVERS == result)
118 cout << "No More observers, stopping notifications" << endl;
125 nSleep_time = INTERVAL_FOR_CHECK ;
132 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
134 cout << "\tIn Server CPP entity handler:\n";
136 auto response = std::make_shared<OC::OCResourceResponse>();
140 // Get the request type and request flag
141 std::string requestType = request->getRequestType();
142 int requestFlag = request->getRequestHandlerFlag();
144 response->setRequestHandle(request->getRequestHandle());
145 response->setResourceHandle(request->getResourceHandle());
147 if (requestFlag & RequestHandlerFlag::RequestFlag)
149 cout << "\t\trequestFlag : Request\n";
151 // If the request type is GET
152 if (requestType == "GET")
154 cout << "\t\t\trequestType : GET\n";
156 // Check for query params (if any)
157 // Process query params and do required operations ..
159 // Get the representation of this resource at this point and send it as response
160 OCRepresentation rep = g_myResource.getResourceRepresentation();
165 response->setErrorCode(200);
166 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
169 else if (requestType == "PUT")
171 // TODO: PUT request operations
173 else if (requestType == "POST")
175 // TODO: POST request operations
177 else if (requestType == "DELETE")
179 // TODO: DELETE request operations
183 if (requestFlag & RequestHandlerFlag::ObserverFlag)
185 pthread_t threadId = {};
187 cout << "\t\trequestFlag : Observer\n";
190 static int startedThread = 0;
194 pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL);
201 std::cout << "Request invalid" << std::endl;
204 return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;
209 // Create PlatformConfig object
210 PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos);
214 OC::OCPlatform::Configure(cfg);
216 OC::OCPlatform::startPresence(60);
218 g_myResource.registerResource();
221 cout << "Type any key to terminate" << endl;
224 OC::OCPlatform::stopPresence();
226 catch (std::exception e)
228 cout << e.what() << endl;