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 <WeightSensorApp.h>
24 int g_Observation = 0;
26 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
29 * TempResourceFunctions
32 void WeightResource::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 WeightResource::getHandle()
48 return m_resourceHandle;
51 void WeightResource::setResourceRepresentation(OCRepresentation &rep)
57 rep.getValue("2", tempWeight);
62 cout << "\t\t\t" << "Received representation: " << endl;
64 cout << "\t\t\t\t" << "Weight" << m_weight << endl;
68 OCRepresentation WeightResource::getResourceRepresentation()
71 m_resourceRep.setValue("0", std::string("weight"));
72 m_resourceRep.setValue("1", std::string("double"));
73 m_resourceRep.setValue("2", std::to_string(m_weight));
78 // Create the instance of the TemphumidResource class
79 WeightResource g_myResource;
81 void *TestSensorVal(void *param)
85 int nSleep_time=INTERVAL_FOR_CHECK;
88 std::cout << "[WeightSensorAPP] ::" << __func__ << " is called."
91 // g_myResource.m_weight = 33;
94 // This function continuously monitors for the changes
105 //g_myResource.m_weight+=0.01;
109 nSleep_time =INTERVAL_FOR_MEASUREMENT;
111 g_myResource.m_weight=nWeight;
114 nSleep_time =INTERVAL_FOR_CHECK;
115 g_myResource.m_weight=0;
121 cout << "\n weight updated to : " << g_myResource.m_weight << endl;
122 cout << "Notifying observers with resource handle: " << g_myResource.getHandle()
125 OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle());
127 if (OC_STACK_NO_OBSERVERS == result)
129 cout << "No More observers, stopping notifications" << endl;
135 nSleep_time=INTERVAL_FOR_CHECK ;
142 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
144 cout << "\tIn Server CPP entity handler:\n";
146 auto response = std::make_shared<OC::OCResourceResponse>();
150 // Get the request type and request flag
151 std::string requestType = request->getRequestType();
152 int requestFlag = request->getRequestHandlerFlag();
154 response->setRequestHandle(request->getRequestHandle());
155 response->setResourceHandle(request->getResourceHandle());
157 if (requestFlag & RequestHandlerFlag::RequestFlag)
159 cout << "\t\trequestFlag : Request\n";
161 // If the request type is GET
162 if (requestType == "GET")
164 cout << "\t\t\trequestType : GET\n";
166 // Check for query params (if any)
167 // Process query params and do required operations ..
169 // Get the representation of this resource at this point and send it as response
170 OCRepresentation rep = g_myResource.getResourceRepresentation();
175 response->setErrorCode(200);
176 response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
179 else if (requestType == "PUT")
181 // PUT request operations
183 else if (requestType == "POST")
185 // POST request operations
187 else if (requestType == "DELETE")
189 // DELETE request operations
193 if (requestFlag & RequestHandlerFlag::ObserverFlag)
197 cout << "\t\trequestFlag : Observer\n";
200 static int startedThread = 0;
204 pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL);
211 std::cout << "Request invalid" << std::endl;
214 return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;
219 // Create PlatformConfig object
220 PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos);
224 OC::OCPlatform::Configure(cfg);
226 OC::OCPlatform::startPresence(60);
228 g_myResource.registerResource();
231 cout << "Type any key to terminate" << endl;
234 OC::OCPlatform::stopPresence();
236 catch (std::exception e)
238 cout << e.what() << endl;