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 ******************************************************************/
24 #include "PrimitiveResource.h"
25 #include "RCSResourceObject.h"
31 using namespace OIC::Service;
33 RCSResourceObject::Ptr server;
34 static bool serverCallback = false;
36 # define checkServer NULL!=server?true:false
38 static Evas_Object *log_entry = NULL;
39 static Evas_Object *list = NULL;
40 static Evas_Object *naviframe = NULL;
42 typedef struct datetime_popup
46 } datetime_popup_fields;
48 // Function to update the log in UI
49 void *updateGroupLog(void *data)
51 string *log = (string *)data;
53 elm_entry_entry_append(log_entry, (*log).c_str());
54 elm_entry_cursor_end_set(log_entry);
58 void printAttribute(const RCSResourceAttributes &attrs)
60 string logMessage = "";
61 for (const auto & attr : attrs)
63 logMessage = logMessage + "KEY:" + attr.key().c_str() + "<br>";
64 logMessage = logMessage + "VALUE:" + attr.value().toString().c_str() + "<br>";
66 dlog_print(DLOG_INFO, LOG_TAG, " %s", logMessage.c_str());
67 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
71 static void onDestroy()
74 string logMessage = "SERVER DESTROYED";
76 if(isPresenceOn == PRESENCE_ON)
78 OCPlatform::stopPresence();
81 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
82 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
86 //hander for get request (if developer choose second option for resource Creation)
87 RCSGetResponse requestHandlerForGet(const RCSRequest &request,
88 RCSResourceAttributes &attrs)
90 string logMessage = "GET REQUEST RECEIVED<br>";
92 RCSResourceObject::LockGuard lock(*server);
93 RCSResourceAttributes attributes = server->getAttributes();
94 printAttribute(attributes);
95 logMessage += "RESPONSE SENT<br>";
97 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
98 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
100 return RCSGetResponse::defaultAction();
103 //hander for set request (if developer choose second option for resource Creation)
104 RCSSetResponse requestHandlerForSet(const RCSRequest &request,
105 RCSResourceAttributes &attrs)
107 string logMessage = "SET REQUEST RECEIVED<br>";
109 RCSResourceObject::LockGuard lock(*server);
110 for (const auto & attr : attrs)
112 logMessage = logMessage + "KEY:" + attr.key().c_str() + "<br>";
113 logMessage = logMessage + "VALUE:" + attr.value().toString().c_str() + "<br>";
114 server->setAttribute(attr.key(), attr.value());
117 printAttribute(attrs);
118 logMessage += "RESPONSE SENT<br>";
119 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
120 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
123 return RCSSetResponse::defaultAction();
126 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
128 Elm_Object_Item *it = (Elm_Object_Item *)event_info;
129 elm_list_item_selected_set(it, EINA_FALSE);
132 static void increaseTemp(void *data, Evas_Object *obj, void *event_info)
134 string logMessage = "";
138 RCSResourceObject::LockGuard lock(server);
139 server->getAttributes()[attributeKey] = server->getAttribute<int>(attributeKey) + 10;
140 string tempString = std::to_string(server->getAttribute<int>(attributeKey));
141 logMessage = "TEMPERATURE CHANGED : " + tempString + "<br>";
146 logMessage = "NO SERRVER <br>";
149 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
150 logMessage += "----------------------<br>";
151 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
155 static void decreaseTemp(void *data, Evas_Object *obj, void *event_info)
157 string logMessage = "";
161 RCSResourceObject::LockGuard lock(server);
162 server->getAttributes()[attributeKey] = server->getAttribute<int>(attributeKey) - 10;
163 string tempString = std::to_string(server->getAttribute<int>(attributeKey));
164 logMessage = "TEMPERATURE CHANGED : " + tempString + "<br>";
168 logMessage = "NO SERRVER <br>";
171 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
172 logMessage += "----------------------<br>";
173 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
177 static void initServer()
179 OCPlatform::startPresence(3);
183 server = RCSResourceObject::Builder(resourceUri, resourceType,
184 resourceInterface).setDiscoverable(true).setObservable(true).build();
186 catch (const RCSPlatformException &e)
188 dlog_print(DLOG_ERROR, LOG_TAG, "#### Create resource exception! (%s)", e.what());
191 server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
192 server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
193 server->setAttribute(attributeKey, DEFALUT_VALUE);
195 string logMessage = "SERVER CREATED<br>";
196 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
197 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
200 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showGroupAPIs, NULL);
204 popup_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
206 datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
207 evas_object_del(popup_fields->popup);
212 popup_set_clicked_cb(void *data, Evas_Object *obj, void *event_info)
214 datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
215 Evas_Object *entry = popup_fields->entry;
216 const char *temperatureString = elm_entry_entry_get(entry);
217 // Remove white spaces(if any) at the beginning
218 string logMessage = "";
220 while (temperatureString[beginning] == ' ')
225 int len = strlen(temperatureString);
226 if (NULL == temperatureString || 1 > len)
228 dlog_print(DLOG_INFO, LOG_TAG, "#### Read NULL Temperature Value");
229 logMessage = "Temperature Cannot be NULL<br>";
235 RCSResourceObject::LockGuard lock(server);
236 int temperate = atoi(temperatureString);
237 server->getAttributes()[attributeKey] = temperate;
238 logMessage = "TEMPERATURE CHANGED : " + to_string(server->getAttribute<int>
239 (attributeKey)) + "<br>";
243 logMessage += "NO SERVER<br>";
246 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
247 logMessage += "----------------------<br>";
248 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
251 evas_object_del(popup_fields->popup);
256 list_get_temperaure_cb(void *data, Evas_Object *obj, void *event_info)
258 Evas_Object *popup, *btn;
259 Evas_Object *nf = naviframe;
264 popup = elm_popup_add(nf);
265 elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
266 eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
267 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
268 elm_object_part_text_set(popup, "title,text", "Enter the temperature");
270 layout = elm_layout_add(popup);
271 elm_layout_file_set(layout, ELM_DEMO_EDJ, "popup_datetime_text");
272 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
273 elm_object_content_set(popup, layout);
275 entry = elm_entry_add(layout);
276 elm_entry_single_line_set(entry, EINA_TRUE);
277 elm_entry_scrollable_set(entry, EINA_TRUE);
278 evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
279 evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
280 eext_entry_selection_back_event_allow_set(entry, EINA_TRUE);
281 elm_object_part_text_set(entry, "elm.guide", "in degree celsius");
282 elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NUMBER);
283 elm_object_part_content_set(layout, "elm.swallow.content", entry);
285 datetime_popup_fields *popup_fields;
286 popup_fields = (datetime_popup_fields *)malloc(sizeof(datetime_popup_fields));
287 if (NULL == popup_fields)
289 dlog_print(DLOG_INFO, LOG_TAG, "#### Memory allocation failed");
293 popup_fields->popup = popup;
294 popup_fields->entry = entry;
298 btn = elm_button_add(popup);
299 elm_object_style_set(btn, "popup");
300 elm_object_text_set(btn, "Cancel");
301 elm_object_part_content_set(popup, "button1", btn);
302 evas_object_smart_callback_add(btn, "clicked", popup_cancel_clicked_cb, popup_fields);
305 btn = elm_button_add(popup);
306 elm_object_style_set(btn, "popup");
307 elm_object_text_set(btn, "Set");
308 elm_object_part_content_set(popup, "button2", btn);
309 evas_object_smart_callback_add(btn, "clicked", popup_set_clicked_cb, popup_fields);
311 evas_object_show(popup);
314 void *showGroupAPIs(void *data)
316 // Add items to the list only if the list is empty
317 const Eina_List *eina_list = elm_list_items_get(list);
318 int count = eina_list_count(eina_list);
321 elm_list_item_append(list, "1. Increase Temperature", NULL, NULL,
324 elm_list_item_append(list, "2. Decrease Temperature", NULL, NULL,
327 elm_list_item_append(list, "3. Set Temperature", NULL, NULL,
328 list_get_temperaure_cb, NULL);
336 naviframe_pop_cb(void *data, Elm_Object_Item *it)
340 if (NULL != log_entry)
342 evas_object_del(log_entry);
347 evas_object_del(list);
353 // Method to set up server screens
354 void serverCreateUI(void *data, Evas_Object *obj, void *event_info)
357 Evas_Object *scroller;
358 Evas_Object *nf = (Evas_Object *)data;
359 Evas_Object *button1;
360 Evas_Object *button2;
361 Elm_Object_Item *nf_it;
365 scroller = elm_scroller_add(nf);
366 elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
367 elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
370 layout = elm_layout_add(nf);
371 elm_layout_file_set(layout, ELM_DEMO_EDJ, "server_layout");
372 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
374 elm_object_content_set(scroller, layout);
377 button1 = elm_button_add(layout);
378 elm_object_part_content_set(layout, "button1", button1);
379 elm_object_text_set(button1, "Auto Control");
380 evas_object_smart_callback_add(button1, "clicked", start_server, NULL);
383 button2 = elm_button_add(layout);
384 elm_object_part_content_set(layout, "button2", button2);
385 elm_object_text_set(button2, "Dev Control");
386 evas_object_smart_callback_add(button2, "clicked", start_server_cb, NULL);
389 list = elm_list_add(layout);
390 elm_list_mode_set(list, ELM_LIST_COMPRESS);
391 evas_object_smart_callback_add(list, "selected", list_selected_cb, NULL);
392 elm_object_part_content_set(layout, "list", list);
395 // log_entry - text area for log
396 log_entry = elm_entry_add(layout);
397 elm_entry_scrollable_set(log_entry, EINA_TRUE);
398 elm_entry_editable_set(log_entry, EINA_FALSE);
399 elm_object_part_text_set(log_entry, "elm.guide", "Logs will be updated here!!!");
400 evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
401 evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
402 elm_object_part_content_set(layout, "log", log_entry);
404 nf_it = elm_naviframe_item_push(nf, "Resource Encapsulation", NULL, NULL, scroller, NULL);
405 elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, NULL);
408 void start_server(void *data, Evas_Object *obj, void *event_info)
411 string logMessage = "SERVER WITHOUT CALLBACK<br>";
413 serverCallback = false;
416 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
417 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
421 void start_server_cb(void *data, Evas_Object *obj, void *event_info)
424 string logMessage = "SERVER WITH CALLBACK<br>";
426 serverCallback = true;
431 server->setGetRequestHandler(requestHandlerForGet);
432 server->setSetRequestHandler(requestHandlerForSet);
433 logMessage = "HANDLERS SET<br>";
437 logMessage = "NO SERRVER FOUND<br>";
440 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
441 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,