[WEB App] Policy management was implemented
authorArtem Motchanyi <a.motchanyi@samsung.com>
Wed, 31 May 2017 08:21:58 +0000 (11:21 +0300)
committerArtem Motchanyi <a.motchanyi@samsung.com>
Wed, 31 May 2017 10:35:11 +0000 (13:35 +0300)
control_app/iot-manager-dashboard/js/controllers/dashboard_controller.js
control_app/iot-manager-dashboard/js/controllers/policies_controller.js
control_app/iot-manager-dashboard/js/controls.js
control_app/iot-manager-dashboard/js/main.js
control_app/iot-manager-dashboard/js/tools/carousel.js
control_app/iot-manager-dashboard/js/tools/loading.js
control_app/iot-manager-dashboard/views/index.html
control_app/iot-manager-dashboard/views/policies.html
control_app/iot-manager-dashboard/views/popup.html
control_app/iot-manager-service/src/network/network_manager.cpp

index cd31a5e..73b44bc 100644 (file)
@@ -13,7 +13,6 @@ function DashboardController() {
     this.key_getDeviceDashboard = "dashboard.get";
     var htmlBuilder = new HtmlBuilder();
     var m_agents = [];
-    var m_device = {};
 
     //return device id of the current dashboard
     this.getDeviceId = function() {
@@ -40,7 +39,7 @@ function DashboardController() {
         }
 
         m_agents = content.data.dashboard.agents;
-        m_device = content.data.dashboard.device;
+        DashboardController.Context.device = content.data.dashboard.device;
         htmlBuilder.generateDashboard(content.data.dashboard);
     }
 
@@ -48,7 +47,7 @@ function DashboardController() {
     this.generateAgentPage = function(agentId) {
         m_agents.forEach(function(agent) {
             if (agent.id == agentId) {
-                htmlBuilder.generateAgentPage(m_device, agent);
+                htmlBuilder.generateAgentPage(DashboardController.Context.device, agent);
 
                 return;
             }
@@ -59,13 +58,12 @@ function DashboardController() {
 
     /*UI builder*/
     function HtmlBuilder() {
-        var m_dashboardPage     = "#dashboard";
-        var m_dashboard         = "#dashboard-content";
-        var m_reportsPage       = "#reports";
-        var m_reports           = "#reports-content";
-        var m_reportsList       = "#reports-list";
-        var m_agentPolicies     = "#agent-policies-content";
-        var m_agentPoliciesList = "#agent-policies-content .policies-list";
+        var m_dashboardPage         = "#dashboard";
+        var m_dashboard             = "#dashboard-content";
+        var m_reports               = "#reports-content";
+        var m_reportsList           = "#reports-list";
+        var m_agentPolicies         = "#agent-policies-content";
+        var m_agentPoliciesList     = "#agent-policies-content .policies-list";
 
         this.getDeviceId = function() {
             return $(m_dashboardPage).attr("data-id");
@@ -118,6 +116,23 @@ function DashboardController() {
             generateReportPage(data.device, data.reports);
         }
 
+        this.generateAgentPage = function(device, agent) {
+            $(m_agentPolicies).empty();
+            devicesController.appendDeviceHtml(device, m_agentPolicies);
+            $(m_agentPolicies).append(
+                    "<div class='agent'>" +
+                    "<div class='name'>" + agent.name + "</div>" +
+                    "</div>"
+            );
+            $(m_agentPolicies).append("<div class='policies-list'></div>");
+
+            agent.policies.forEach(function(policyGroup) {
+                $(m_agentPoliciesList).append(PoliciesController.getPolicyGroupHtml(policyGroup.group, policyGroup.policies, true));
+            });
+
+            $(m_agentPoliciesList).trigger("create");
+        }
+
         function generateReportPage(device, reports) {
             $(m_reports).empty();
             devicesController.appendDeviceHtml(device, m_reports);
@@ -125,7 +140,7 @@ function DashboardController() {
 
             reports.forEach(function(report){
                 $(m_reportsList).append(
-                    "<div class='item " + report.name + "' data-item='" + report.name + "'>" +
+                    "<div class='item' data-item='" + report.name + "'>" +
                         "<div class='name-lable'>Name:</div><div class='name'>" + report.name + "</div>" +
                         "<div class='date-lable'>Date:</div><div class='date'>" + report.date + "</div>" +
                         "<div class='body'><textarea readonly='readonly' data-role='none'>" + JSON.stringify(report.data, null, 4) + "</textarea></div>" +
@@ -137,22 +152,10 @@ function DashboardController() {
             var reportCarousel = new Carousel();
             reportCarousel.init(m_reportsList);
         }
-
-        this.generateAgentPage = function(device, agent) {
-            $(m_agentPolicies).empty();
-            devicesController.appendDeviceHtml(device, m_agentPolicies);
-            $(m_agentPolicies).append(
-                "<div class='agent'>" +
-                    "<div class='name'>" + agent.name + "</div>" +
-                "</div>"
-            );
-            $(m_agentPolicies).append("<div class='policies-list'></div>");
-
-            agent.policies.forEach(function(policyGroup) {
-                $(m_agentPoliciesList).append(PoliciesController.getPolicyGroupHtml(policyGroup.group, policyGroup.policies, true));
-            });
-
-            $(m_agentPoliciesList).trigger("create");
-        }
     }
+}
+
+//context of the current dashboard
+DashboardController.Context = {
+    device: {}
 }
\ No newline at end of file
index 5447f7f..6423136 100644 (file)
 **/
 
 function PoliciesController() {
+    this.key_getDevicePolicies = "device.policies.get";
     this.key_setDevicePolicies = "device.policies.set";
     var htmlBuilder = new HtmlBuilder();
 
+    //toggle togglable policies
+    this.togglePolicy = function(policyContainer) {
+        var enabled = $(policyContainer).find(".state").hasClass("on");
+
+        if (enabled) {
+            //switch off
+            $(policyContainer).attr("data-state", 0).find(".state").removeClass("on").addClass("off");
+        } else {
+            //switch on
+            $(policyContainer).attr("data-state", 1).find(".state").removeClass("off").addClass("on");
+        }
+    }
+
+    //get current dashboard's device policies
+    this.getCurrentDevicePolicies = function() {
+        this.getDevicePolicies(DashboardController.Context.device.id);
+    }
+
+    //get device policies
+    this.getDevicePolicies = function(id) {
+        if (connector) {
+            Loading.start();
+            htmlBuilder.clearPolicies();
+            connector.sendMessage(connector.getDefaultRemotePort(), this.key_getDevicePolicies, {"id": id});
+        }
+    }
+
+    //get device policies callback
+    function getDevicePolicies_cb(sender, content) {
+        Loading.stop();
+
+        if (!checkErrorCode(content.error_code)) {
+            Popup.show(content.description, Popup.Icon.ERROR);
+
+            return;
+        }
+
+        htmlBuilder.generatePolicyPage(content.data.device, content.data.policies);
+    }
+
+    //set device policies from the policy page form
+    this.applyDevicePolicies = function() {
+        var policies = [];
+        var container = htmlBuilder.getDevicePolicyListContainer();
+
+        $(container).find(".policy-group").each(function() {
+            var groupName = $(this).attr("data-group");
+            var groupInstance = {
+                group: groupName,
+                policies: []
+            };
+
+            $(this).find(".policy").each(function() {
+                var policyName = $(this).attr("data-policy");
+                var policyInstance = {
+                    name: policyName,
+                    state: 0,
+                    items: []
+                };
+
+                if ($(this).hasClass("toggle")) {
+                    policyInstance.state = $(this).attr("data-state");
+                } else {
+                    switch(policyName) {
+                    case "iptables":
+                        policyInstance.items = $(this).find("textarea").val().split('\n');
+                        break;
+                    }
+                }
+
+                groupInstance.policies.push(policyInstance);
+            });
+
+            policies.push(groupInstance);
+        });
+
+        this.setDevicePolicies(policies);
+    }
+
     //set device policies
-    this.setDevicePolicies = function() {
-        //TODO: implement
+    this.setDevicePolicies = function(policiesList) {
+        if (connector) {
+            Loading.start();
+            connector.sendMessage(connector.getDefaultRemotePort(), this.key_setDevicePolicies, {"policies": policiesList});
+        }
     }
 
     //set device policies callback
     function setDevicePolicies_cb(sender, content) {
-        //TODO: implement
+        Loading.stop();
+
+        if (!checkErrorCode(content.error_code)) {
+            Popup.show(content.description, Popup.Icon.ERROR);
+
+            return;
+        }
+
+        Popup.show("Successfully applied", Popup.Icon.OK);
     }
 
+    listeners[this.key_getDevicePolicies] = getDevicePolicies_cb;
     listeners[this.key_setDevicePolicies] = setDevicePolicies_cb;
 
     /*UI builder*/
     function HtmlBuilder() {
+        var m_devicePolicies        = "#policies-content";
+        var m_devicePoliciesList    = "#policies-content .policies-list";
+
+        this.getDevicePolicyListContainer = function() {
+            return m_devicePoliciesList;
+        }
+
+        this.clearPolicies = function() {
+            $(m_devicePolicies).empty();
+        }
+
+        this.generatePolicyPage = function(device, policiesList) {
+            devicesController.appendDeviceHtml(device, m_devicePolicies);
+            $(m_devicePolicies).append("<div class='policies-list'></div>");
+
+            policiesList.forEach(function(policyGroup) {
+                $(m_devicePoliciesList).append(PoliciesController.getPolicyGroupHtml(policyGroup.group, policyGroup.policies, false));
+            });
+
+            $(m_devicePoliciesList).trigger("create");
+        }
     }
 }
 
@@ -43,7 +156,7 @@ PoliciesController.getPolicyGroupHtml = function(groupName, policyList, readonly
     }
 
     var groupHtml =
-        "<div class='nomargin' data-role='collapsible' data-inset='false' data-group='" + groupName + "'>" +
+        "<div class='nomargin policy-group' data-role='collapsible' data-inset='false' data-group='" + groupName + "'>" +
             "<h3>" + formatedGroupName + "</h3>" +
             "<ul data-role='listview'>";
 
@@ -61,9 +174,13 @@ PoliciesController.getPolicyGroupHtml = function(groupName, policyList, readonly
 PoliciesController.getPolicyHtml = function(policy, readonly) {
     var formatedPolicy = {};
     var disabled = "";
+    var readonlyTextarea = "";
+    var toggleClass = "toggle";
 
     if (readonly) {
         disabled = "disabled";
+        readonlyTextarea = "readonly='readonly'";
+        toggleClass = "";
     }
 
     switch(policy.name) {
@@ -72,31 +189,38 @@ PoliciesController.getPolicyHtml = function(policy, readonly) {
         formatedPolicy.name = "USB";
         formatedPolicy.description = "Policy lets disable or enable USB";
         formatedPolicy.isToggle = true;
-        formatedPolicy.state = ((policy.state == 1) ? "on" : "off");
-        formatedPolicy.policyBody = "<div class='state " + formatedPolicy.state + " " + disabled + "'></div>";
+        formatedPolicy.state = policy.state;
+        formatedPolicy.stateClass = ((policy.state == 1) ? "on" : "off");
+        formatedPolicy.policyBody = "<div class='state " + formatedPolicy.stateClass + " " + disabled + "'></div>";
         break;
     case "screen-capture":
         formatedPolicy.uname = policy.name;
         formatedPolicy.name = "Screen capture";
         formatedPolicy.description = "Policy lets disable or enable screen capture";
         formatedPolicy.isToggle = true;
-        formatedPolicy.state = ((policy.state == 1) ? "on" : "off");
-        formatedPolicy.policyBody = "<div class='state " + formatedPolicy.state + " " + disabled + "'></div>";
+        formatedPolicy.state = policy.state;
+        formatedPolicy.stateClass = ((policy.state == 1) ? "on" : "off");
+        formatedPolicy.policyBody = "<div class='state " + formatedPolicy.stateClass + " " + disabled + "'></div>";
         break;
     case "bluetooth":
         formatedPolicy.uname = policy.name;
         formatedPolicy.name = "Bluetooth";
         formatedPolicy.description = "Policy lets disable or enable bluetooth";
         formatedPolicy.isToggle = true;
-        formatedPolicy.state = ((policy.state == 1) ? "on" : "off");
-        formatedPolicy.policyBody = "<div class='state " + formatedPolicy.state + " " + disabled + "'></div>";
+        formatedPolicy.state = policy.state;
+        formatedPolicy.stateClass = ((policy.state == 1) ? "on" : "off");
+        formatedPolicy.policyBody = "<div class='state " + formatedPolicy.stateClass + " " + disabled + "'></div>";
         break;
     case "iptables":
         formatedPolicy.uname = policy.name;
         formatedPolicy.name = "IP tables";
         formatedPolicy.description = "Policy lets add IP restrictions";
         formatedPolicy.isToggle = false;
-        formatedPolicy.policyBody = "<div class='body'><textarea readonly='readonly'>" + JSON.stringify(policy.items, null, 4) + "</textarea></div>";
+        var policies = "";
+        policy.items.forEach(function(item) {
+            policies += item + "\n";
+        });
+        formatedPolicy.policyBody = "<div class='body'><textarea " + readonlyTextarea + ">" + policies + "</textarea></div>";
         break;
     default:
         console.log("Error, add case for policy: " + policy.name);
@@ -104,7 +228,7 @@ PoliciesController.getPolicyHtml = function(policy, readonly) {
     }
 
     var policyHtml =
-        "<div class='policy' data-policy='" + formatedPolicy.uname + "'>" +
+        "<div class='policy " + ((formatedPolicy.isToggle) ? toggleClass : "") + "' data-policy='" + formatedPolicy.uname + "' data-state='" + ((formatedPolicy.isToggle) ? formatedPolicy.state : "") + "'>" +
             "<div class='info'>" +
                 "<div class='name'>" + formatedPolicy.name + "</div>" +
                 "<div class='description'>" + formatedPolicy.description + "</div>" +
index da96318..c3aa454 100644 (file)
@@ -19,6 +19,7 @@ $(document).ready(function() {
     bindEvent("click", "#dashboard .controls .btn-reports", deviceReportsControllHandler);
     bindEvent("click", "#dashboard .controls .btn-policies", devicePoliciesControllHandler);
     bindEvent("click", "#dashboard .agent-list .agent", agentPoliciesControllHandler);
+    bindEvent("click", "#policies-content .policies-list li .policy.toggle", policyToggleControllHandler);
 });
 
 function leftHeaderControllHandler() {
@@ -47,6 +48,9 @@ function rightHeaderControllHandler() {
     case "my-devices":
         devicesController.getOwnedDevices();
         break;
+    case "set-policies":
+        policiesController.applyDevicePolicies();
+        break;
     }
 }
 
@@ -81,4 +85,8 @@ function deviceReportsControllHandler() {
 function agentPoliciesControllHandler() {
     var id = $(this).data("id");
     go(Page.AGENT_POLICIES, id);
+}
+
+function policyToggleControllHandler() {
+    policiesController.togglePolicy($(this));
 }
\ No newline at end of file
index ec53ec8..3abd83a 100644 (file)
@@ -9,7 +9,7 @@
     @par Copyright: (c) Samsung Electronics Co, Ltd 2015. All rights reserved.
 **/
 
-var hardKeyEventListener = null;
+var inited = false;
 var depth = 0;
 var pageHistory = [];
 
@@ -37,17 +37,9 @@ var connector;      // native service connector
 var listeners = {}; // map of listeners where key is a <entity-who-send-request>.command and key is listener
 
 var unregister = function() {
-    if (hardKeyEventListener !== null) {
-        document.removeEventListener("tizenhwkey", hardKeyEventListener);
-        hardKeyEventListener = null;
-        window.tizen.application.getCurrentApplication().exit();
-    }
-}
-
-//page data
-function PageData(command, data) {
-    this.command = command;
-    this.data = data || "";
+    unregisterHardKeys();
+    inited = false;
+    window.tizen.application.getCurrentApplication().exit();
 }
 
 function go(page, args) {
@@ -74,9 +66,13 @@ function go(page, args) {
         break;
     case Page.DISCOVERY:
         devicesController.getUnownedDevices();
+        break;
     case Page.DASHBOARD:
         dashboardController.getDeviceDashboard(args);
         break;
+    case Page.POLICIES:
+        policiesController.getCurrentDevicePolicies();
+        break;
     case Page.AGENT_POLICIES:
         dashboardController.generateAgentPage(args);
         break;
@@ -99,13 +95,15 @@ function back() {
 
     try {
         if (depth > 0) {
-            console.log(pageHistory.pop());
+            var prevPage = pageHistory.pop();
             currentPage = pageHistory[pageHistory.length - 1];
             depth--;
 
             switch (currentPage) {
             case Page.HOME:
-                devicesController.getOwnedDevices();
+                if (prevPage != Page.DASHBOARD) {
+                    devicesController.getOwnedDevices();
+                }
                 break;
             default:
                 break;
@@ -126,6 +124,33 @@ function back() {
     }
 }
 
+function hardKeyEvent(e) {
+    if (e.keyName == "back") {
+        back();
+    }
+    else if (e.keyName == "menu") {
+        if (currentPage == Page.LOGIN) {
+            return;
+        }
+
+        if (Menu.isMenuActive()) {
+            Menu.hideMenu();
+        }
+        else {
+            Menu.showMenu();
+        }
+    }
+}
+
+function registerHardKeys() {
+    unregisterHardKeys();
+    document.addEventListener("tizenhwkey", hardKeyEvent);
+}
+
+function unregisterHardKeys() {
+    document.removeEventListener("tizenhwkey", hardKeyEvent);
+}
+
 function checkErrorCode(error_code) {
     if (error_code != 0) {
         console.log("Error in the received package!");
@@ -168,11 +193,11 @@ function initAllPages() {
 
 // Initialize function
 var init = function() {
-    // register once
-    if (hardKeyEventListener !== null) {
+    if (inited) {
         return;
     }
 
+    inited = true;
     //Do your initialization job
     console.log("init() called");
     $.mobile.defaultPageTransition = "none";
@@ -196,27 +221,8 @@ var init = function() {
 
     authorizationController.checkSession();
 
-    var hardKeyEvent = function(e) {
-        if (e.keyName == "back") {
-            back();
-        }
-        else if (e.keyName == "menu") {
-            if (currentPage == Page.LOGIN) {
-                return;
-            }
-
-            if (Menu.isMenuActive()) {
-                Menu.hideMenu();
-            }
-            else {
-                Menu.showMenu();
-            }
-        }
-    }
-
     // add eventListener for tizenhwkey (Back Button & Menu Button)
-    document.addEventListener("tizenhwkey", hardKeyEvent);
-    hardKeyEventListener = hardKeyEvent;
+    registerHardKeys();
 };
 
 function unbindEvent(event, selector, eventHandler) {
index 01b3402..b538ee1 100644 (file)
@@ -34,25 +34,25 @@ function Carousel() {
         active($(items).find("." + pinsClass), $(items).find("." + pinsClass + " ." + circleClass + ":first"));
     }
 
-    function back(event) {
+    function back() {
         if (ptr == 0) {
             ptr = end;
         } else {
             ptr--;
         }
 
-        active($(items).find(".item"), $(items).find("." + itemsArr[ptr]));
+        active($(items).find(".item"), $(items).find(".item[data-item='" + itemsArr[ptr] + "']"));
         active($(items).find("." + pinsClass + " ." + circleClass), $(items).find("." + pinsClass + " ." + circleClass + ":nth-child(" + (1 + ptr) + ")"));
     }
 
-    function forward(event) {
+    function forward() {
         if (ptr == end) {
             ptr = 0;
         } else {
             ptr++;
         }
 
-        active($(items).find(".item"), $(items).find("." + itemsArr[ptr]));
+        active($(items).find(".item"), $(items).find(".item[data-item='" + itemsArr[ptr] + "']"));
         active($(items).find("." + pinsClass + " ." + circleClass), $(items).find("." + pinsClass + " ." + circleClass + ":nth-child(" + (1 + ptr) + ")"));
     }
 
index f6a7fee..45a6dcb 100644 (file)
@@ -15,8 +15,10 @@ Loading.element = "#gif-loading";
 
 Loading.start = function() {
     $(Loading.element).addClass("active");
+    unregisterHardKeys();
 }
 
 Loading.stop = function() {
     $(Loading.element).removeClass("active");
+    registerHardKeys();
 }
\ No newline at end of file
index 513ee6f..ec48752 100644 (file)
@@ -8,7 +8,6 @@
     <link rel="stylesheet" href="../css/styles.css"/>
     <link rel="stylesheet" href="../css/tools/loading.css"/>
     <link rel="stylesheet" href="../css/tools/carousel.css"/>
-    <!-- <script type="text/javascript" src="../resources/jquery-mobile/js/jquery-1.11.3.js"></script> -->
     <script type="text/javascript" src="../resources/jquery-mobile/js/jquery-1.12.4.js"></script>
     <script type="text/javascript" src="../resources/jquery-mobile/js/jquery.mobile-1.4.5.js"></script>
     <script type="text/javascript" src="../js/service_connector.js"></script>
index 30aae4b..feb4d60 100644 (file)
@@ -2,10 +2,7 @@
     <div data-role="header" data-position="fixed" data-tap-toggle="false" class="header">
         <div class="left-header back" data-case="back"></div>
         <div class="mid-header"><span>Device policies</span></div>
+        <div class="right-header" data-case="set-policies"><span>Apply</span></div>
     </div>
-    <div data-role="content" class="nopadding">
-        <div class="content">
-            TBD
-        </div>
-    </div>
+    <div id="policies-content" data-role="content" class="nopadding"></div>
 </div>
\ No newline at end of file
index f224050..62de360 100644 (file)
@@ -3,7 +3,7 @@ function Popup() {}
 
 Popup.show = function(text, icon, duration) {
     if (!duration) {
-        duration = 400;
+        duration = 1000;
     }
 
     if (!icon) {
index f273994..431c09d 100644 (file)
@@ -18,7 +18,7 @@
 #include <dlfcn.h>
 
 #define NMLIB_MODULE_NAME "/usr/lib/libnmlib.so"
-#define HOSTNAME "coap+tcp://106.125.46.44:5683"
+#define HOSTNAME "coap+tcp://52.71.167.178:5683"
 
 #define NM_INIT_FUNC_NAME                   "NM_init"
 #define NM_CLEANUP_FUNC_NAME                "NM_cleanup"