From ecbc1497295920c9571997754d6dd40507ec6f44 Mon Sep 17 00:00:00 2001 From: Lorenzo Maiorfi Date: Thu, 22 May 2014 11:27:28 +0200 Subject: [PATCH] JSON WebSocket Push Datasource Plugin --- datasources/plugin_json_ws.js | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 datasources/plugin_json_ws.js diff --git a/datasources/plugin_json_ws.js b/datasources/plugin_json_ws.js new file mode 100644 index 0000000..f14b17f --- /dev/null +++ b/datasources/plugin_json_ws.js @@ -0,0 +1,86 @@ +(function() +{ + var jsonWebSocketDatasource = function(settings, updateCallback) + { + var self = this; + var currentSettings = settings; + var ws; + + var onOpen=function() + { + console.info("WebSocket(%s) Opened",currentSettings.url); + } + + var onClose=function() + { + console.info("WebSocket Closed"); + } + + var onMessage=function(event) + { + var data=event.data; + + console.info("WebSocket received %s",data); + + var objdata=JSON.parse(data); + + if(typeof objdata == "object") + { + updateCallback(objdata); + } + else + { + updateCallback(data); + } + + } + + function createWebSocket() + { + if(ws) ws.close(); + + var url=currentSettings.url; + ws=new WebSocket(url); + + ws.onopen=onOpen; + ws.onclose=onClose; + ws.onmessage=onMessage; + } + + createWebSocket(); + + this.updateNow = function() + { + createWebSocket(); + } + + this.onDispose = function() + { + ws.close(); + } + + this.onSettingsChanged = function(newSettings) + { + currentSettings = newSettings; + + createWebSocket(); + } + }; + + freeboard.loadDatasourcePlugin({ + type_name : "JSON WebSocket", + display_name : "JSON WebSocket Push Datasource", + description : "A push datasource based on browser built-in WebSocket implementation, by Lorenzo Maiorfi - Innovactive Engineering s.r.l. You can download a sample dashboard using this datasource here", + settings : [ + { + name : "url", + display_name: "URL", + type : "text" + } + ], + newInstance: function(settings, newInstanceCallback, updateCallback) + { + newInstanceCallback( new jsonWebSocketDatasource(settings, updateCallback)); + } + }); +}()); \ No newline at end of file