b50efc9239d35a5e85cebe82ef8bcfc535aa4759
[platform/core/security/suspicious-activity-monitor.git] / tv-widget / ui / scripts / controllers / devices_controller.js
1 /**
2     @file devices_controller.js
3     @brief Script for IoT devices management.
4     @author Artem Motchanyi (a.motchanyi@samsung.com)
5     @date Created Aug 02, 2017
6     @par In Samsung Ukraine R&D Center (SRK) under a contract between
7     @par LLC "Samsung Electronics Ukraine Company" (Kyiv, Ukraine)
8     @par and "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
9     @par Copyright: (c) 2017 Samsung Electronics Co, Ltd. All rights reserved.
10 **/
11 function DevicesController() {
12     /**
13      * Key of the command to get owned devices list.
14      */
15     this.key_getOwnedDevices    = "devices.owned.get";
16     /**
17      * Key of the command to get unowned devices list.
18      */
19     this.key_getUnownedDevices  = "devices.unowned.get";
20     /**
21      * Key of the command to own device.
22      */
23     this.key_ownDevice          = "device.own";
24     /**
25      * Key of the command to unown device.
26      */
27     this.key_unownDevice        = "device.unown";
28     /**
29      * HTML builder for the devices controller.
30      */
31     var htmlBuilder = new HtmlBuilder();
32
33     /**
34      * Get owned devices list.
35      */
36     this.getOwnedDevices = function() {
37         if (connector) {
38             Loading.start();
39             htmlBuilder.removeOwnedDevicesList();
40             connector.sendCommand(this.key_getOwnedDevices, {});
41         }
42     };
43
44     /**
45      * Get owned devices callback.
46      * @param pkg - package with the owned devices list.
47      */
48     function getOwnedDevices_cb(pkg) {
49         Loading.stop();
50
51         if (!checkErrorCode(pkg.error_code)) {
52             alert(pkg.description);
53
54             return;
55         }
56
57         htmlBuilder.fillOwnedDevicesList(pkg.data.devices);
58     }
59
60     /**
61      * Get unowned devices list.
62      */
63     this.getUnownedDevices = function() {
64         if (connector) {
65             Loading.start();
66             htmlBuilder.removeUnownedDevicesList();
67             connector.sendCommand(this.key_getUnownedDevices, {});
68         }
69     };
70
71     /**
72      * Get unowned devices callback.
73      * @param pkg - package with the unowned devices list.
74      */
75     function getUnownedDevices_cb(pkg) {
76         Loading.stop();
77
78         if (!checkErrorCode(pkg.error_code)) {
79             alert(pkg.description);
80
81             return;
82         }
83
84         htmlBuilder.fillUnownedDevicesList(pkg.data.devices);
85     }
86
87     /**
88      * Pair (own) device by id.
89      * @param id - device id.
90      */
91     this.ownDevice = function(id) {
92         if (connector && confirm("Do you want to pair this device?")) {
93             connector.sendCommand(this.key_ownDevice, {"id": id});
94         }
95     };
96
97     /**
98      * Pairing device callback.
99      * @param pkg - package with the device pairing result.
100      */
101     function ownDevice_cb(pkg) {
102         if (!checkErrorCode(pkg.error_code)) {
103             alert(pkg.description);
104
105             return;
106         }
107
108         alert("Device paired");
109         htmlBuilder.removeUnownedDevice(pkg.data.id);
110     }
111
112     /**
113      * Unpair (unown) device by id.
114      * @param id - device id.
115      * @param hub - hub flag; true - device is a hub, false - isn't.
116      * @param status - device status.
117      */
118     this.unownDevice = function(id, hub, status) {
119         if (status === "0") {
120             alert("Disabled device cannot be unpaired");
121
122             return;
123         }
124
125         var confirmMsg = "Do you want to unpair this device?";
126
127         if (hub) {
128             confirmMsg = "Do you want to unpair HUB with all simple devices?";
129         }
130
131         if (connector && confirm(confirmMsg)) {
132             connector.sendCommand(this.key_unownDevice, {"id": id});
133         }
134     };
135
136     /**
137      * Unpairing device callback.
138      * @param pkg - package with the device unpairing result.
139      */
140     function unownDevice_cb(pkg) {
141         if (!checkErrorCode(pkg.error_code)) {
142             alert(pkg.description);
143
144             return;
145         }
146
147         alert("Device unpaired");
148         htmlBuilder.removeOwnedDevice(pkg.data.id);
149     }
150
151     /**
152      * Append device header HTML to the container.
153      * @param device - device header HTML.
154      * @param container - container selector.
155      */
156     this.appendDeviceHtml = function(device, container) {
157         htmlBuilder.appendDeviceHtml(device, container);
158     };
159
160     connector.registerCommandCallback(this.key_getOwnedDevices, getOwnedDevices_cb);
161     connector.registerCommandCallback(this.key_getUnownedDevices, getUnownedDevices_cb);
162     connector.registerCommandCallback(this.key_ownDevice, ownDevice_cb);
163     connector.registerCommandCallback(this.key_unownDevice, unownDevice_cb);
164
165     /**
166      * UI builder.
167      * Generates HTML containers and elements for this page's content.
168      */
169     function HtmlBuilder() {
170         var m_ownedDevicesList      = "#owned-devices";
171         var m_unownedDevicesList    = "#unowned-devices";
172
173         /**
174          * Append device header HTML to the container.
175          * @param device - device header HTML.
176          * @param container - container selector.
177          */
178         this.appendDeviceHtml = function(device, container) {
179             var deviceType = DevicesController.getDeviceType(device.type);
180             var deviceStatus = DevicesController.getDeviceStatus(device.status);
181             var deviceHTML = "<div class='device " + deviceType + "'>" +
182                                 "<div class='icon'></div>" +
183                                 "<div class='info'>" +
184                                     "<div class='name'>" + device.name + "</div>" +
185                                     "<div class='description'>" + device.description + "</div>" +
186                                 "</div>" +
187                                 "<div class='status " + deviceStatus + "'></div>" +
188                             "</div>";
189             $(container).append(deviceHTML);
190         };
191
192         /**
193          * Clear owned devices list on the home page.
194          */
195         this.removeOwnedDevicesList = function() {
196             $(m_ownedDevicesList).empty();
197         };
198
199         /**
200          * Fill owned devices list on the home page.
201          * @param devices - owned devices list.
202          */
203         this.fillOwnedDevicesList = function(devices) {
204             var firstCnt = 0;
205             var simpleDevicesList = [];
206
207             for (var int = 0; int < devices.length; int++) {
208                 var device = devices[int];
209                 var simpleDevice = device.parentUuid.length > 0;
210                 var simpleDeviceClass = simpleDevice ? "simple" : "";
211                 var deviceType = DevicesController.getDeviceType(device.type);
212                 var deviceStatus = DevicesController.getDeviceStatus(device.status);
213                 var deviceHtml = "<div class='device " + deviceType + " " + simpleDeviceClass + "' data-id='" + device.id + "' data-parentid='" + device.parentUuid + "' data-status='" + device.status + "'>" +
214                                     "<div class='tree'></div>" +
215                                     "<div class='icon'></div>" +
216                                     "<div class='info'>" +
217                                         "<div class='name'>" + device.name + "</div>" +
218                                         "<div class='description'>" + device.description + "</div>" +
219                                     "</div>" +
220                                     "<div class='status " + deviceStatus + "'></div>" +
221                                     "<div class='controls'>" +
222                                         "<button class='btn btn-info btn-lg' data-control='unown'>Unpair</button>" +
223                                     "</div>" +
224                                 "</div>";
225
226                 if (simpleDevice) {
227                     simpleDevicesList.push({"parentUuid": device.parentUuid, "html": deviceHtml});
228                 } else {
229                     $(m_ownedDevicesList).append(deviceHtml);
230                 }
231             }
232
233             //append primitive devices
234             for (var int = 0; int < simpleDevicesList.length; int++) {
235                 var device = simpleDevicesList[int];
236
237                 if ($(m_ownedDevicesList).find(".simple-list.simple-" + device.parentUuid).length === 0) {
238                     $(m_ownedDevicesList).find("div.device[data-id='" + device.parentUuid + "']").after("<div class='simple-list simple-" + device.parentUuid + "'></div>");
239                 }
240
241                 $(m_ownedDevicesList).find("div.device[data-id='" + device.parentUuid + "']").attr("data-hub", "hub");
242                 $(m_ownedDevicesList).find(".simple-list.simple-" + device.parentUuid).append(device.html);
243             }
244         };
245
246         /**
247          * Clear unowned devices list on the discovery page.
248          */
249         this.removeUnownedDevicesList = function() {
250             $(m_unownedDevicesList).empty();
251         };
252
253         /**
254          * Fill unowned devices list on the discovery page.
255          * @param devices - unowned devices list.
256          */
257         this.fillUnownedDevicesList = function(devices) {
258             for (var int = 0; int < devices.length; int++) {
259                 var device = devices[int];
260                 var deviceType = DevicesController.getDeviceType(device.type);
261                 var deviceStatus = DevicesController.getDeviceStatus(device.status);
262                 var deviceHtml = "<div class='device " + deviceType + "' data-id='" + device.id + "'>" +
263                                     "<div class='icon'></div>" +
264                                     "<div class='info'>" +
265                                         "<div class='name'>" + device.name + "</div>" +
266                                         "<div class='description'>" + device.description + "</div>" +
267                                     "</div>" +
268                                 "</div>";
269                 $(m_unownedDevicesList).append(deviceHtml);
270             }
271         };
272
273         /**
274          * Remove unowned device by id from the discovery page.
275          * @param id - device id.
276          */
277         this.removeUnownedDevice = function(id) {
278             $(m_unownedDevicesList).find(".device[data-id=" + id + "]").slideUp(500 ,
279                 function() {
280                     $(this).remove();
281                 }
282             );
283         };
284
285         /**
286          * Remove owned device by id from the home page.
287          * @param id - device id.
288          */
289         this.removeOwnedDevice = function(id) {
290             if ($(m_ownedDevicesList).find(".device[data-id=" + id + "]").data("hub") === "hub") {
291                 $(m_ownedDevicesList).find(".simple-list.simple-" + id).remove();
292             }
293
294             $(m_ownedDevicesList).find(".device[data-id=" + id + "]").slideUp(500 ,
295                 function() {
296                     $(this).remove();
297                 }
298             );
299         };
300     }
301 }
302
303 /**
304  * Get device id of the currently displayed dashboard.
305  * @returns device id.
306  */
307 DevicesController.getDeviceId = function() {
308     return $(Layout.Page.DEVICE).attr("data-id");
309 };
310
311 /**
312  * Get the device type CSS class by the device type.
313  * @param deviceType - device type.
314  *
315  * @returns device type CSS class.
316  */
317 DevicesController.getDeviceType = function(deviceType) {
318     switch(deviceType) {
319     case "tv":
320         return "tv";
321     case "phone":
322         return "smartphone";
323     case "light":
324         return "lamp";
325     case "robotcleaner":
326         return "robotcleaner";
327     case "airconditioner":
328         return "airconditioner";
329     case "refrigerator":
330         return "refrigerator";
331     case "smartplug":
332         return "smartplug";
333     case "washer":
334         return "washer";
335     default:
336         return "";
337     }
338 };
339
340 /**
341  * Get the device status CSS class by the device status.
342  * @param deviceStatus - device status.
343  *
344  * @returns device status CSS class.
345  */
346 DevicesController.getDeviceStatus = function(deviceStatus) {
347     switch(deviceStatus) {
348     case 0:
349         return "off";
350     case 1:
351         return "on";
352     default:
353         return "warning";
354     }
355 };