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 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
77 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
81 //hander for get request (if developer choose second option for resource Creation)
82 RCSGetResponse requestHandlerForGet(const RCSRequest &request,
83 RCSResourceAttributes &attrs)
85 string logMessage = "GET REQUEST RECEIVED<br>";
87 RCSResourceObject::LockGuard lock(*server);
88 RCSResourceAttributes attributes = server->getAttributes();
89 printAttribute(attributes);
90 logMessage += "RESPONSE SENT<br>";
92 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
93 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
95 return RCSGetResponse::defaultAction();
98 //hander for set request (if developer choose second option for resource Creation)
99 RCSSetResponse requestHandlerForSet(const RCSRequest &request,
100 RCSResourceAttributes &attrs)
102 string logMessage = "SET REQUEST RECEIVED<br>";
104 RCSResourceObject::LockGuard lock(*server);
105 for (const auto & attr : attrs)
107 logMessage = logMessage + "KEY:" + attr.key().c_str() + "<br>";
108 logMessage = logMessage + "VALUE:" + attr.value().toString().c_str() + "<br>";
109 server->setAttribute(attr.key(), attr.value());
112 printAttribute(attrs);
113 logMessage += "RESPONSE SENT<br>";
114 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
115 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
118 return RCSSetResponse::defaultAction();
121 static void list_selected_cb(void *data, Evas_Object *obj, void *event_info)
123 Elm_Object_Item *it = (Elm_Object_Item *)event_info;
124 elm_list_item_selected_set(it, EINA_FALSE);
127 static void increaseTemp(void *data, Evas_Object *obj, void *event_info)
129 string logMessage = "";
133 RCSResourceObject::LockGuard lock(server);
134 server->getAttributes()[attributeKey] = server->getAttribute<int>(attributeKey) + 10;
135 string tempString = std::to_string(server->getAttribute<int>(attributeKey));
136 logMessage = "TEMPERATURE CHANGED : " + tempString + "<br>";
141 logMessage = "NO SERRVER <br>";
144 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
145 logMessage += "----------------------<br>";
146 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
150 static void decreaseTemp(void *data, Evas_Object *obj, void *event_info)
152 string logMessage = "";
156 RCSResourceObject::LockGuard lock(server);
157 server->getAttributes()[attributeKey] = server->getAttribute<int>(attributeKey) - 10;
158 string tempString = std::to_string(server->getAttribute<int>(attributeKey));
159 logMessage = "TEMPERATURE CHANGED : " + tempString + "<br>";
163 logMessage = "NO SERRVER <br>";
166 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
167 logMessage += "----------------------<br>";
168 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
172 static void initServer()
174 OCPlatform::startPresence(3);
178 server = RCSResourceObject::Builder(resourceUri, resourceType,
179 resourceInterface).setDiscoverable(true).setObservable(true).build();
181 catch (const RCSPlatformException &e)
183 dlog_print(DLOG_ERROR, LOG_TAG, "#### Create resource exception! (%s)", e.what());
186 server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::UPDATED);
187 server->setSetRequestHandlerPolicy(RCSResourceObject::SetRequestHandlerPolicy::NEVER);
188 server->setAttribute(attributeKey, DEFALUT_VALUE);
190 string logMessage = "SERVER CREATED<br>";
191 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
192 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
195 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))showGroupAPIs, NULL);
199 popup_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
201 datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
202 evas_object_del(popup_fields->popup);
207 popup_set_clicked_cb(void *data, Evas_Object *obj, void *event_info)
209 datetime_popup_fields *popup_fields = (datetime_popup_fields *)data;
210 Evas_Object *entry = popup_fields->entry;
211 const char *temperatureString = elm_entry_entry_get(entry);
212 // Remove white spaces(if any) at the beginning
213 string logMessage = "";
215 while (temperatureString[beginning] == ' ')
220 int len = strlen(temperatureString);
221 if (NULL == temperatureString || 1 > len)
223 dlog_print(DLOG_INFO, LOG_TAG, "#### Read NULL Temperature Value");
224 logMessage = "Temperature Cannot be NULL<br>";
230 RCSResourceObject::LockGuard lock(server);
231 int temperate = atoi(temperatureString);
232 server->getAttributes()[attributeKey] = temperate;
233 logMessage = "TEMPERATURE CHANGED : " + to_string(server->getAttribute<int>
234 (attributeKey)) + "<br>";
238 logMessage += "NO SERVER<br>";
241 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
242 logMessage += "----------------------<br>";
243 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
246 evas_object_del(popup_fields->popup);
251 list_get_temperaure_cb(void *data, Evas_Object *obj, void *event_info)
253 Evas_Object *popup, *btn;
254 Evas_Object *nf = naviframe;
259 popup = elm_popup_add(nf);
260 elm_popup_align_set(popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
261 eext_object_event_callback_add(popup, EEXT_CALLBACK_BACK, eext_popup_back_cb, NULL);
262 evas_object_size_hint_weight_set(popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
263 elm_object_part_text_set(popup, "title,text", "Enter the temperature");
265 layout = elm_layout_add(popup);
266 elm_layout_file_set(layout, ELM_DEMO_EDJ, "popup_datetime_text");
267 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
268 elm_object_content_set(popup, layout);
270 entry = elm_entry_add(layout);
271 elm_entry_single_line_set(entry, EINA_TRUE);
272 elm_entry_scrollable_set(entry, EINA_TRUE);
273 evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
274 evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
275 eext_entry_selection_back_event_allow_set(entry, EINA_TRUE);
276 elm_object_part_text_set(entry, "elm.guide", "in degree celsius");
277 elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NUMBER);
278 elm_object_part_content_set(layout, "elm.swallow.content", entry);
280 datetime_popup_fields *popup_fields;
281 popup_fields = (datetime_popup_fields *)malloc(sizeof(datetime_popup_fields));
282 if (NULL == popup_fields)
284 dlog_print(DLOG_INFO, LOG_TAG, "#### Memory allocation failed");
288 popup_fields->popup = popup;
289 popup_fields->entry = entry;
293 btn = elm_button_add(popup);
294 elm_object_style_set(btn, "popup");
295 elm_object_text_set(btn, "Cancel");
296 elm_object_part_content_set(popup, "button1", btn);
297 evas_object_smart_callback_add(btn, "clicked", popup_cancel_clicked_cb, popup_fields);
300 btn = elm_button_add(popup);
301 elm_object_style_set(btn, "popup");
302 elm_object_text_set(btn, "Set");
303 elm_object_part_content_set(popup, "button2", btn);
304 evas_object_smart_callback_add(btn, "clicked", popup_set_clicked_cb, popup_fields);
306 evas_object_show(popup);
309 void *showGroupAPIs(void *data)
311 // Add items to the list only if the list is empty
312 const Eina_List *eina_list = elm_list_items_get(list);
313 int count = eina_list_count(eina_list);
316 elm_list_item_append(list, "1. Increase Temperature", NULL, NULL,
319 elm_list_item_append(list, "2. Decrease Temperature", NULL, NULL,
322 elm_list_item_append(list, "3. Set Temperature", NULL, NULL,
323 list_get_temperaure_cb, NULL);
331 naviframe_pop_cb(void *data, Elm_Object_Item *it)
335 if (NULL != log_entry)
337 evas_object_del(log_entry);
342 evas_object_del(list);
348 // Method to set up server screens
349 void serverCreateUI(void *data, Evas_Object *obj, void *event_info)
352 Evas_Object *scroller;
353 Evas_Object *nf = (Evas_Object *)data;
354 Evas_Object *button1;
355 Evas_Object *button2;
356 Elm_Object_Item *nf_it;
360 scroller = elm_scroller_add(nf);
361 elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
362 elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
365 layout = elm_layout_add(nf);
366 elm_layout_file_set(layout, ELM_DEMO_EDJ, "server_layout");
367 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
369 elm_object_content_set(scroller, layout);
372 button1 = elm_button_add(layout);
373 elm_object_part_content_set(layout, "button1", button1);
374 elm_object_text_set(button1, "Auto Control");
375 evas_object_smart_callback_add(button1, "clicked", start_server, NULL);
378 button2 = elm_button_add(layout);
379 elm_object_part_content_set(layout, "button2", button2);
380 elm_object_text_set(button2, "Dev Control");
381 evas_object_smart_callback_add(button2, "clicked", start_server_cb, NULL);
384 list = elm_list_add(layout);
385 elm_list_mode_set(list, ELM_LIST_COMPRESS);
386 evas_object_smart_callback_add(list, "selected", list_selected_cb, NULL);
387 elm_object_part_content_set(layout, "list", list);
390 // log_entry - text area for log
391 log_entry = elm_entry_add(layout);
392 elm_entry_scrollable_set(log_entry, EINA_TRUE);
393 elm_entry_editable_set(log_entry, EINA_FALSE);
394 elm_object_part_text_set(log_entry, "elm.guide", "Logs will be updated here!!!");
395 evas_object_size_hint_weight_set(log_entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
396 evas_object_size_hint_align_set(log_entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
397 elm_object_part_content_set(layout, "log", log_entry);
399 nf_it = elm_naviframe_item_push(nf, "Resource Encapsulation", NULL, NULL, scroller, NULL);
400 elm_naviframe_item_pop_cb_set(nf_it, naviframe_pop_cb, NULL);
403 void start_server(void *data, Evas_Object *obj, void *event_info)
406 string logMessage = "SERVER WITHOUT CALLBACK<br>";
408 serverCallback = false;
411 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
412 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,
416 void start_server_cb(void *data, Evas_Object *obj, void *event_info)
419 string logMessage = "SERVER WITH CALLBACK<br>";
421 serverCallback = true;
426 server->setGetRequestHandler(requestHandlerForGet);
427 server->setSetRequestHandler(requestHandlerForSet);
428 logMessage = "HANDLERS SET<br>";
432 logMessage = "NO SERRVER FOUND<br>";
435 dlog_print(DLOG_INFO, LOG_TAG, "#### %s", logMessage.c_str());
436 ecore_main_loop_thread_safe_call_sync((void * ( *)(void *))updateGroupLog,