From 86530bc374921cec48cb3fb32679538af98a6250 Mon Sep 17 00:00:00 2001 From: GPrathap Date: Sun, 24 Jan 2016 23:26:51 +0530 Subject: [PATCH] changed the way server address is taken --- .../public/js/config/config.js | 8 +-- .../public/js/mainHandler | 55 +++++++++++-------- .../public/js/modules/controller.js | 2 +- .../statistics.hbs | 8 +-- .../statistics.js | 29 ++-------- 5 files changed, 45 insertions(+), 57 deletions(-) diff --git a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/config/config.js b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/config/config.js index b4e93125ca..028695ee6d 100644 --- a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/config/config.js +++ b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/config/config.js @@ -15,21 +15,19 @@ * specific language governing permissions and limitations * under the License. */ -//TODO this needs to a private resource + var config_api = function () { var config_api = this; - var domain = "localhost"; - var port = "9793"; var context_controller = "/drone_analyzer/controller/send_command"; config_api.config_3dobject_holder = "#virtualDrone"; config_api.realtime_plotting_update_interval = 30; config_api.realtime_plotting_totalPoints = 30; config_api.realtime_plotting_data_window = {}; config_api.effectController = {uy: 70.0, uz: 15.0, ux: 10.0, fx: 2.0, fz: 15.0, Tmax: 1}; - config_api.drone_control = "http://" + domain + ":" + port + "/" + context_controller; + config_api.drone_control = context_controller; config_api.drone_controlType = "POST"; config_api.drone_controlDataType = "json"; - config_api.web_socket_endpoint = "wss://localhost:9443/drone_analyzer/datastream/drone_status"; + config_api.web_socket_endpoint = "/drone_analyzer/datastream/drone_status"; config_api.modules_status = { "realtimePlotting": false, "sensorReadings": false, diff --git a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/mainHandler b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/mainHandler index e96eb3427e..28199109a3 100644 --- a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/mainHandler +++ b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/mainHandler @@ -58,7 +58,6 @@ $('.btn-minimize').click(function (e) { if ($target.is(':visible')) { if ($(this).parent().attr('id') === "RealtimePlotting") { plotting.forceToRedraw(function (status) { - d3.select("#realtimechart").select("svg").remove(); plotting.realtime_plotting("#realtimechart", "#range_min", "#range_max", "#window_update_value", 600, $("#realtimechart").height(), "#window_size_current_value", @@ -71,33 +70,40 @@ $('.btn-minimize').click(function (e) { $('#connectionOpen').on('click', function () { $('#connectionOpen').toggleClass('active'); }); - - $("#xmppConnectionOpen").on('click', function () { $('#xmppConnectionOpen').toggleClass('active'); if ($('#xmppConnectionOpen').html() === "Start") { - sendMessage(); - $('#xmppConnectionOpen').html("Stop"); + sendMessage("Start the process", function(state){ + console.log("sending message to server..."+ state); + if(state<2){ + $('#xmppConnectionOpen').html("Stop"); + }else{ + $('#xmppConnectionOpen').html("Start"); + } + }); + } else if ($('#xmppConnectionOpen').html() === "Stop") { - closeSocket(); - $('#xmppConnectionOpen').html("Start"); - $("#connectionOpen").html("Connect to server").removeClass("btn btn-info").addClass("btn btn-primary"); + closeSocket(function(state){ + console.log("closing WebSocket..."+ state); + if(state<2){ + $('#xmppConnectionOpen').html("Stop"); + }else{ + $('#xmppConnectionOpen').html("Start"); + } + }); + $("#connectionOpen").html("Connect to XMPP Server").removeClass("btn btn-info").addClass("btn btn-primary"); } }); $('.btn-minimize').parent().parent().next('.box-content').hide(); - - var webSocket; config_api.realtime_plotting_data_window["attitude"] = new Queue(); - var current_status = {}; - -function openSocket() { - if (webSocket !== undefined && webSocket.readyState !== WebSocket.CLOSED) { +function openSocket(wssAddress) { + if (webSocket !== undefined && webSocket.readyState == 1) { writeResponse("WebSocket is already opened."); } else { - webSocket = new WebSocket(config_api.web_socket_endpoint); + webSocket = new WebSocket(wssAddress+config_api.web_socket_endpoint); } webSocket.onopen = function (event) { if (event === undefined) { @@ -121,15 +127,20 @@ function openSocket() { writeResponse("Message has been corrupted."); } }; - } -function sendMessage(message) { - webSocket.send(message); +function sendMessage(message, callback) { + if(webSocket.readyState<2){ + webSocket.send(message); + } + callback(webSocket.readyState); } -function closeSocket() { - webSocket.close(); +function closeSocket(callback) { + if(webSocket.readyState<2){ + webSocket.close(); + } + callback(webSocket.readyState); } function writeResponse(text) { @@ -137,8 +148,6 @@ function writeResponse(text) { } window.onbeforeunload = function () { - webSocket.onclose = function () { - }; - webSocket.close() + webSocket.close(); }; diff --git a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/modules/controller.js b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/modules/controller.js index a45b65b73b..f9d7f00864 100644 --- a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/modules/controller.js +++ b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/public/js/modules/controller.js @@ -17,7 +17,7 @@ */ $("#module_control button").click(function (index) { console.log("Asking Server to send the " + $(this).attr('id') + " command to Ar Drone"); - var url = config_api.drone_control + "?action=" + $(this).attr('id') + "&speed=6&duration=7"; + var url = config_api.drone_control; ajax_handler.ajaxRequest(url, config_api.drone_controlType, {action: $(this).attr('id'), speed: 7, duration: 7}, config_api.drone_controlDataType, function (data, status) { console.log(JSON.stringify(data)); diff --git a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/statistics.hbs b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/statistics.hbs index a4404ee87a..2f004fd746 100644 --- a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/statistics.hbs +++ b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/statistics.hbs @@ -6,7 +6,7 @@
+ onclick="openSocket('{{wssAddress}}');">Connect to XMPP Server
@@ -25,7 +25,7 @@
-
+
@@ -230,8 +230,6 @@
- - {{#zone "bottomJs"}} {{js "/js/d3.min.js" }} {{js "/js/3dobject_controller/three.min.js" }} diff --git a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/statistics.js b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/statistics.js index dc92c0e900..0740b424bd 100644 --- a/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/statistics.js +++ b/features/device-mgt-iot-droneanalyzer-feature/org.wso2.carbon.device.mgt.iot.droneanalyzer.feature/src/main/resources/jaggeryapps/devicemgt/app/units/iot.unit.device.droneanalyzer.statistics/statistics.js @@ -16,27 +16,10 @@ * under the License. */ function onRequest (context) { - /*var log = new Log("detail.js"); - var deviceType = request.getParameter("type"); - var deviceId = request.getParameter("id"); - - if (deviceType != null && deviceType != undefined && deviceId != null && deviceId != undefined) { - var deviceModule = require("/modules/device.js").deviceModule; - var device = deviceModule.viewDevice(deviceType, deviceId); - - if (device) { - var viewModel = {}; - var deviceInfo = device.properties.DEVICE_INFO; - if (deviceInfo != undefined && String(deviceInfo.toString()).length > 0) { - deviceInfo = parse(stringify(deviceInfo)); - viewModel.system = device.properties.IMEI; - viewModel.machine = "Virtual Firealarm"; - viewModel.vendor = device.properties.VENDOR; - } - device.viewModel = viewModel; - } - context.device = device; - - return context; - }*/ + var log = new Log("statistics.js"); + var serverAddress = require("/app/modules/serverAddress.js").serverAddress; + var wssAddress = serverAddress.getWSSAddress(); + var httpsAddress = serverAddress.getHPPSTSAddress(); + var device = context.unit.params.device; + return { "device": device, "wssAddress": wssAddress, "httpsAddress": httpsAddress}; } \ No newline at end of file