91ababd46fb790b68b1459d16786a6777cd20e1f
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SampleApp / linux / WeightSensorApp / src / WeightSensorApp.cpp
1
2 /******************************************************************
3  *
4  * Copyright 2015 Samsung Electronics All Rights Reserved.
5  *
6  *
7  *
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  ******************************************************************/
21
22 #include <WeightSensorApp.h>
23
24 int g_Observation = 0;
25
26 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request);
27
28 /*
29  * TempResourceFunctions
30  */
31
32 void WeightResource::registerResource()
33 {
34     uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
35
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);
39
40     if (OC_STACK_OK != result)
41     {
42         cout << "Resource creation was unsuccessful\n";
43     }
44 }
45
46 OCResourceHandle WeightResource::getHandle()
47 {
48     return m_resourceHandle;
49 }
50
51 void WeightResource::setResourceRepresentation(OCRepresentation &rep)
52 {
53     double tempWeight;
54
55
56
57     rep.getValue("2", tempWeight);
58
59
60     m_weight= tempWeight;
61
62     cout << "\t\t\t" << "Received representation: " << endl;
63
64     cout << "\t\t\t\t" << "Weight" << m_weight << endl;
65
66 }
67
68 OCRepresentation WeightResource::getResourceRepresentation()
69 {
70
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));
74
75     return m_resourceRep;
76 }
77
78 // Create the instance of the TemphumidResource class
79 WeightResource g_myResource;
80
81 void *TestSensorVal(void *param)
82 {
83
84     bool bFlag = true;
85     int nSleep_time=INTERVAL_FOR_CHECK;
86     double nWeight;
87
88     std::cout << "[WeightSensorAPP] ::" << __func__ << " is called."
89               << std::endl;
90
91 //      g_myResource.m_weight = 33;
92         nWeight=INIT_VAL;
93
94     // This function continuously monitors for the changes
95     while (1)
96     {
97
98
99         sleep(nSleep_time);
100
101
102         if (g_Observation)
103         {
104
105                         //g_myResource.m_weight+=0.01;
106
107             if(bFlag){
108
109                 nSleep_time =INTERVAL_FOR_MEASUREMENT;
110                 nWeight+=DIFF_VAL;
111                 g_myResource.m_weight=nWeight;
112             }
113             else{
114                 nSleep_time =INTERVAL_FOR_CHECK;
115                 g_myResource.m_weight=0;
116
117
118             }
119             bFlag=bFlag^true;
120
121                         cout << "\n weight updated to : " << g_myResource.m_weight << endl;
122             cout << "Notifying observers with resource handle: " << g_myResource.getHandle()
123                  << endl;
124
125             OCStackResult result = OCPlatform::notifyAllObservers(g_myResource.getHandle());
126
127             if (OC_STACK_NO_OBSERVERS == result)
128             {
129                 cout << "No More observers, stopping notifications" << endl;
130                 g_Observation = 0;
131             }
132         }
133         else{
134
135             nSleep_time=INTERVAL_FOR_CHECK ;
136         }
137
138     }
139     return NULL;
140 }
141
142 OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
143 {
144     cout << "\tIn Server CPP entity handler:\n";
145
146     auto response = std::make_shared<OC::OCResourceResponse>();
147
148     if (request)
149     {
150         // Get the request type and request flag
151         std::string requestType = request->getRequestType();
152         int requestFlag = request->getRequestHandlerFlag();
153
154         response->setRequestHandle(request->getRequestHandle());
155         response->setResourceHandle(request->getResourceHandle());
156
157         if (requestFlag & RequestHandlerFlag::RequestFlag)
158         {
159             cout << "\t\trequestFlag : Request\n";
160
161             // If the request type is GET
162             if (requestType == "GET")
163             {
164                 cout << "\t\t\trequestType : GET\n";
165
166                 // Check for query params (if any)
167                 // Process query params and do required operations ..
168
169                 // Get the representation of this resource at this point and send it as response
170                 OCRepresentation rep = g_myResource.getResourceRepresentation();
171
172                 if (response)
173                 {
174                     // TODO Error Code
175                     response->setErrorCode(200);
176                     response->setResourceRepresentation(rep, DEFAULT_INTERFACE);
177                 }
178             }
179             else if (requestType == "PUT")
180             {
181                 // PUT request operations
182             }
183             else if (requestType == "POST")
184             {
185                 // POST request operations
186             }
187             else if (requestType == "DELETE")
188             {
189                 // DELETE request operations
190             }
191         }
192
193         if (requestFlag & RequestHandlerFlag::ObserverFlag)
194         {
195             pthread_t threadId;
196
197             cout << "\t\trequestFlag : Observer\n";
198             g_Observation = 1;
199
200             static int startedThread = 0;
201
202             if (!startedThread)
203             {
204                 pthread_create(&threadId, NULL, TestSensorVal, (void *) NULL);
205                 startedThread = 1;
206             }
207         }
208     }
209     else
210     {
211         std::cout << "Request invalid" << std::endl;
212     }
213
214     return OCPlatform::sendResponse(response) == OC_STACK_OK ? OC_EH_OK : OC_EH_ERROR;
215 }
216
217 int main()
218 {
219     // Create PlatformConfig object
220     PlatformConfig cfg(COAP_SRVTYPE, COAP_MODE, COAP_IP, COAP_PORT, OC::QualityOfService::LowQos);
221
222     try
223     {
224         OC::OCPlatform::Configure(cfg);
225
226         OC::OCPlatform::startPresence(60);
227
228         g_myResource.registerResource();
229
230         int input = 0;
231         cout << "Type any key to terminate" << endl;
232         cin >> input;
233
234         OC::OCPlatform::stopPresence();
235     }
236     catch (std::exception e)
237     {
238         cout << e.what() << endl;
239     }
240 }
241