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.
11 function DevicesController() {
13 * Key of the command to get owned devices list.
15 this.key_getOwnedDevices = "devices.owned.get";
17 * Key of the command to get unowned devices list.
19 this.key_getUnownedDevices = "devices.unowned.get";
21 * Key of the command to own device.
23 this.key_ownDevice = "device.own";
25 * Key of the command to unown device.
27 this.key_unownDevice = "device.unown";
29 * HTML builder for the devices controller.
31 var htmlBuilder = new HtmlBuilder();
34 * Get owned devices list.
36 this.getOwnedDevices = function() {
39 htmlBuilder.removeOwnedDevicesList();
40 connector.sendCommand(this.key_getOwnedDevices, {});
45 * Get owned devices callback.
46 * @param pkg - package with the owned devices list.
48 function getOwnedDevices_cb(pkg) {
51 if (!checkErrorCode(pkg.error_code)) {
52 alert(pkg.description);
57 htmlBuilder.fillOwnedDevicesList(pkg.data.devices);
61 * Get unowned devices list.
63 this.getUnownedDevices = function() {
66 htmlBuilder.removeUnownedDevicesList();
67 connector.sendCommand(this.key_getUnownedDevices, {});
72 * Get unowned devices callback.
73 * @param pkg - package with the unowned devices list.
75 function getUnownedDevices_cb(pkg) {
78 if (!checkErrorCode(pkg.error_code)) {
79 alert(pkg.description);
84 htmlBuilder.fillUnownedDevicesList(pkg.data.devices);
88 * Pair (own) device by id.
89 * @param id - device id.
91 this.ownDevice = function(id) {
92 if (connector && confirm("Do you want to pair this device?")) {
93 connector.sendCommand(this.key_ownDevice, {"id": id});
98 * Pairing device callback.
99 * @param pkg - package with the device pairing result.
101 function ownDevice_cb(pkg) {
102 if (!checkErrorCode(pkg.error_code)) {
103 alert(pkg.description);
108 alert("Device paired");
109 htmlBuilder.removeUnownedDevice(pkg.data.id);
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.
118 this.unownDevice = function(id, hub, status) {
119 if (status === "0") {
120 alert("Disabled device cannot be unpaired");
125 var confirmMsg = "Do you want to unpair this device?";
128 confirmMsg = "Do you want to unpair HUB with all simple devices?";
131 if (connector && confirm(confirmMsg)) {
132 connector.sendCommand(this.key_unownDevice, {"id": id});
137 * Unpairing device callback.
138 * @param pkg - package with the device unpairing result.
140 function unownDevice_cb(pkg) {
141 if (!checkErrorCode(pkg.error_code)) {
142 alert(pkg.description);
147 alert("Device unpaired");
148 htmlBuilder.removeOwnedDevice(pkg.data.id);
152 * Append device header HTML to the container.
153 * @param device - device header HTML.
154 * @param container - container selector.
156 this.appendDeviceHtml = function(device, container) {
157 htmlBuilder.appendDeviceHtml(device, container);
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);
167 * Generates HTML containers and elements for this page's content.
169 function HtmlBuilder() {
170 var m_ownedDevicesList = "#owned-devices";
171 var m_unownedDevicesList = "#unowned-devices";
174 * Append device header HTML to the container.
175 * @param device - device header HTML.
176 * @param container - container selector.
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>" +
187 "<div class='status " + deviceStatus + "'></div>" +
189 $(container).append(deviceHTML);
193 * Clear owned devices list on the home page.
195 this.removeOwnedDevicesList = function() {
196 $(m_ownedDevicesList).empty();
200 * Fill owned devices list on the home page.
201 * @param devices - owned devices list.
203 this.fillOwnedDevicesList = function(devices) {
205 var simpleDevicesList = [];
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>" +
220 "<div class='status " + deviceStatus + "'></div>" +
221 "<div class='controls'>" +
222 "<button class='btn btn-info btn-lg' data-control='unown'>Unpair</button>" +
227 simpleDevicesList.push({"parentUuid": device.parentUuid, "html": deviceHtml});
229 $(m_ownedDevicesList).append(deviceHtml);
233 //append primitive devices
234 for (var int = 0; int < simpleDevicesList.length; int++) {
235 var device = simpleDevicesList[int];
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>");
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);
247 * Clear unowned devices list on the discovery page.
249 this.removeUnownedDevicesList = function() {
250 $(m_unownedDevicesList).empty();
254 * Fill unowned devices list on the discovery page.
255 * @param devices - unowned devices list.
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>" +
269 $(m_unownedDevicesList).append(deviceHtml);
274 * Remove unowned device by id from the discovery page.
275 * @param id - device id.
277 this.removeUnownedDevice = function(id) {
278 $(m_unownedDevicesList).find(".device[data-id=" + id + "]").slideUp(500 ,
286 * Remove owned device by id from the home page.
287 * @param id - device id.
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();
294 $(m_ownedDevicesList).find(".device[data-id=" + id + "]").slideUp(500 ,
304 * Get device id of the currently displayed dashboard.
305 * @returns device id.
307 DevicesController.getDeviceId = function() {
308 return $(Layout.Page.DEVICE).attr("data-id");
312 * Get the device type CSS class by the device type.
313 * @param deviceType - device type.
315 * @returns device type CSS class.
317 DevicesController.getDeviceType = function(deviceType) {
326 return "robotcleaner";
327 case "airconditioner":
328 return "airconditioner";
330 return "refrigerator";
341 * Get the device status CSS class by the device status.
342 * @param deviceStatus - device status.
344 * @returns device status CSS class.
346 DevicesController.getDeviceStatus = function(deviceStatus) {
347 switch(deviceStatus) {