Merge pull request #786 from lasanthaDLPDS/master

Fixing wso2/product-iots/issues#1242
revert-dabc3590
Madhawa Perera 7 years ago committed by GitHub
commit 8ac016f30f

@ -20,29 +20,43 @@ function onRequest(context) {
var log = new Log("stats.js"); var log = new Log("stats.js");
var carbonServer = require("carbon").server; var carbonServer = require("carbon").server;
var device = context.unit.params.device; var device = context.unit.params.device;
// graph configuration // graph configuration
var graphData = { var graphData = {
Sensors:["accelerometer","magnetic", "gravity", "pressure", "proximity", "gyroscope"], Sensors:["accelerometer","magnetic", "gravity", "pressure", "proximity", "gyroscope"],
Realtime_Data:['battery','light','rotation'] Realtime_Data:['battery','light','rotation']
}; };
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"]; var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
var constants = require("/app/modules/constants.js"); var constants = require("/app/modules/constants.js");
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
var websocketEndpoint = devicemgtProps["wssURL"].replace("https", "wss"); var websocketEndpoint = devicemgtProps["wssURL"].replace("https", "wss");
var jwtService = carbonServer.osgiService( var jwtService = carbonServer.osgiService(
'org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService'); 'org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService');
var jwtClient = jwtService.getJWTClient(); var jwtClient = jwtService.getJWTClient();
var encodedClientKeys = session.get(constants["ENCODED_TENANT_BASED_WEB_SOCKET_CLIENT_CREDENTIALS"]); var encodedClientKeys = session.get(constants["ENCODED_TENANT_BASED_WEB_SOCKET_CLIENT_CREDENTIALS"]);
var token = ""; var token = "";
var tokenPair = null;
var user = userModule.getCarbonUser();
var tenantDomain = user.domain;
if (encodedClientKeys) { if (encodedClientKeys) {
var tokenUtil = require("/app/modules/oauth/token-handler-utils.js")["utils"]; var tokenUtil = require("/app/modules/oauth/token-handler-utils.js")["utils"];
var resp = tokenUtil.decode(encodedClientKeys).split(":"); var resp = tokenUtil.decode(encodedClientKeys).split(":");
var tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username,"default", {}); if (tenantDomain == "carbon.super") {
tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username,"default", {});
if (tokenPair) { if (tokenPair) {
token = tokenPair.accessToken; token = tokenPair.accessToken;
} }
websocketEndpoint = websocketEndpoint + "/secured-websocket/org.wso2.iot.android.sense/1.0.0?" + websocketEndpoint = websocketEndpoint + "/secured-websocket/org.wso2.iot.android.sense/1.0.0?" +
"deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token; "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token;
} else {
tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username + "@" + tenantDomain,"default", {});
if (tokenPair) {
token = tokenPair.accessToken;
}
websocketEndpoint = websocketEndpoint + "/secured-websocket/t/"+tenantDomain+"/org.wso2.iot.android.sense/1.0.0?" +
"deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token;
}
} }
return {"device": device, "websocketEndpoint": websocketEndpoint, "graphData":graphData}; return {"device": device, "websocketEndpoint": websocketEndpoint, "graphData":graphData};
} }

@ -158,6 +158,15 @@ function threeDlineGraph(type, xChartData, yChartData, zChartData) {
graph: graph graph: graph
}); });
new Rickshaw.Graph.HoverDetail({
graph: graph,
formatter: function (series, x, y) {
var date = '<span class="date">' + moment(x * 1000).format('Do MMM YYYY h:mm:ss a') + '</span>';
var swatch = '<span class="detail_swatch" style="background-color: ' + series.color + '"></span>';
return swatch + series.name + ": " + parseInt(y) + '<br>' + date;
}
});
graph.render(); graph.render();
return graph; return graph;
@ -234,58 +243,61 @@ function connect(target) {
if (ws) { if (ws) {
ws.onmessage = function (event) { ws.onmessage = function (event) {
var dataPoint = JSON.parse(event.data); var dataPoint = JSON.parse(event.data);
if (dataPoint) { if (dataPoint) {
var time = parseInt(dataPoint[4]) / 1000; var time = parseInt(dataPoint[4]) / 1000;
switch (dataPoint[typeId]) { switch (dataPoint[typeId]) {
case "battery": case "battery":
graphUpdate(batteryData, time, dataPoint[batteryId], graphMap["battery"]); graphUpdate(batteryData, time, dataPoint[batteryId]);
graphMap["battery"].update();
break; break;
case "light": case "light":
graphUpdate(lightData, time, dataPoint[lightId], graphMap["light"]); graphUpdate(lightData, time, dataPoint[lightId]);
graphMap["light"].update();
break; break;
case "pressure": case "pressure":
graphUpdate(pressureData, time, dataPoint[pressureId], graphMap["pressure"]); graphUpdate(pressureData, time, dataPoint[pressureId]);
graphMap["pressure"].update();
break; break;
case "proximity": case "proximity":
graphUpdate(proximityData, time, dataPoint[proximityId], graphMap["proximity"]); graphUpdate(proximityData, time, dataPoint[proximityId]);
graphMap["proximity"].update();
break; break;
case "accelerometer": case "accelerometer":
dataUpdate(accelerometer_xData, time, dataPoint[accelerometer_xId]); graphUpdate(accelerometer_xData, time, dataPoint[accelerometer_xId]);
dataUpdate(accelerometer_yData, time, dataPoint[accelerometer_yId]); graphUpdate(accelerometer_yData, time, dataPoint[accelerometer_yId]);
dataUpdate(accelerometer_zData, time, dataPoint[accelerometer_zId]); graphUpdate(accelerometer_zData, time, dataPoint[accelerometer_zId]);
graphMap["accelerometer"].update(); graphMap["accelerometer"].update();
break; break;
case "magnetic": case "magnetic":
dataUpdate(magnetic_xData, time, dataPoint[magnetic_xId]); graphUpdate(magnetic_xData, time, dataPoint[magnetic_xId]);
dataUpdate(magnetic_yData, time, dataPoint[magnetic_yId]); graphUpdate(magnetic_yData, time, dataPoint[magnetic_yId]);
dataUpdate(magnetic_zData, time, dataPoint[magnetic_zId]); graphUpdate(magnetic_zData, time, dataPoint[magnetic_zId]);
graphMap["magnetic"].update(); graphMap["magnetic"].update();
break; break;
case "gyroscope": case "gyroscope":
dataUpdate(gyroscope_xData, time, dataPoint[gyroscope_xId]); graphUpdate(gyroscope_xData, time, dataPoint[gyroscope_xId]);
dataUpdate(gyroscope_yData, time, dataPoint[gyroscope_yId]); graphUpdate(gyroscope_yData, time, dataPoint[gyroscope_yId]);
dataUpdate(gyroscope_zData, time, dataPoint[gyroscope_zId]); graphUpdate(gyroscope_zData, time, dataPoint[gyroscope_zId]);
graphMap["gyroscope"].update(); graphMap["gyroscope"].update();
break; break;
case "rotation": case "rotation":
dataUpdate(magnetic_xData, time, dataPoint[rotation_xId]); graphUpdate(magnetic_xData, time, dataPoint[rotation_xId]);
dataUpdate(magnetic_yData, time, dataPoint[rotation_yId]); graphUpdate(magnetic_yData, time, dataPoint[rotation_yId]);
dataUpdate(magnetic_zData, time, dataPoint[rotation_zId]); graphUpdate(magnetic_zData, time, dataPoint[rotation_zId]);
graphMap["rotation"].update(); graphMap["rotation"].update();
break; break;
case "gravity": case "gravity":
dataUpdate(gravity_xData, time, dataPoint[gravity_xId]); graphUpdate(gravity_xData, time, dataPoint[gravity_xId]);
dataUpdate(gravity_yData, time, dataPoint[gravity_yId]); graphUpdate(gravity_yData, time, dataPoint[gravity_yId]);
dataUpdate(gravity_zData, time, dataPoint[gravity_zId]); graphUpdate(gravity_zData, time, dataPoint[gravity_zId]);
graphMap["gravity"].update(); graphMap["gravity"].update();
break; break;
} }
@ -294,16 +306,7 @@ function connect(target) {
} }
} }
function graphUpdate(chartData, xValue, yValue, graph) { function graphUpdate(chartData, xValue, yValue) {
chartData.push({
x: parseInt(xValue),
y: parseFloat(yValue)
});
chartData.shift();
graph.update();
}
function dataUpdate(chartData, xValue, yValue) {
chartData.push({ chartData.push({
x: parseInt(xValue), x: parseInt(xValue),
y: parseFloat(yValue) y: parseFloat(yValue)

Loading…
Cancel
Save