activate/deactivate toggle, optimizations
activate/deactivate toggle, optimizations

--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
     "name": "node-red-contrib-web-watch",
-    "version": "0.0.3",
+    "version": "0.0.4",
     "description": "A Node-RED node that watches for changes on webpages",
     "repository": {
         "type": "git",

--- a/web-watch/web-watch.html
+++ b/web-watch/web-watch.html
@@ -42,37 +42,95 @@
         defaults: {
             name: {value: ""},
             url: {value: "", required: true},
-            interval: {value: "5", required: true, validate: RED.validators.number()}
+            interval: {value: "60", required: true, validate: RED.validators.number()},
+            active: {value:true}
         },
         inputs: 0,
         outputs: 1,
         icon: "white-globe.png",
         label: function() {
-            return this.name || this.url || 'Web watch';
+            return this.name || this.url || this.type;
         },
         labelStyle: function() {
             return this.name?"node_label_italic":"";
         },
-        paletteLabel: "Web watch"
+        paletteLabel: "Web watch",
+        align: "right",
+        button: {
+            toggle: "active",
+            onclick: function() {
+                var label = this.name || this.url || this.type;
+                var node = this;
+                $.ajax({
+                    url: "web-watch/"+this.id+"/"+(this.active?"enable":"disable"),
+                    type: "POST",
+                    success: function(resp, textStatus, xhr) {
+                        if (xhr.status == 200) {
+                            RED.notify(label + " succesfully activated", "success");
+                        } else if (xhr.status == 201) {
+                            RED.notify(label + " succesfully deactivated", "success");
+                        }
+                    },
+                    error: function(jqXHR,textStatus,errorThrown) {
+                        if (jqXHR.status == 404) {
+                            RED.notify(node._("common.notification.error", {message: node._("common.notification.errors.not-deployed")}),"error");
+                        } else if (jqXHR.status == 0) {
+                            RED.notify(node._("common.notification.error", {message: node._("common.notification.errors.no-response")}),"error");
+                        } else {
+                            RED.notify(node._("common.notification.error",{message:node._("common.notification.errors.unexpected",{status:err.status,message:err.response})}),"error");
+                        }
+                    }
+                });
+            }
+        }
     });
-    RED.nodes.registerType('Web watch in', {
+    RED.nodes.registerType("Web watch in", {
         category: "advanced-input",
         color: "#3c8dbc",
         defaults: {
             name: {value: ""},
             url: {value: "", required: true},
-            interval: {value: "5", required: true, validate: RED.validators.number()}
+            interval: {value: "60", required: true, validate: RED.validators.number()},
+            active: {value:true}
         },
         inputs: 1,
         outputs: 1,
         icon: "white-globe.png",
         label: function() {
-            return this.name || this.url || 'Web watch in';
+            return this.name || this.url || this.type;
         },
         labelStyle: function() {
             return this.name?"node_label_italic":"";
         },
-        paletteLabel: "Web watch in"
+        paletteLabel: "Web watch in",
+        align: "right",
+        button: {
+            toggle: "active",
+            onclick: function() {
+                var label = this.name || this.url || this.type;
+                var node = this;
+                $.ajax({
+                    url: "web-watch/"+this.id+"/"+(this.active?"enable":"disable"),
+                    type: "POST",
+                    success: function(resp, textStatus, xhr) {
+                        if (xhr.status == 200) {
+                            RED.notify(label + " succesfully activated", "success");
+                        } else if (xhr.status == 201) {
+                            RED.notify(label + " succesfully deactivated", "success");
+                        }
+                    },
+                    error: function(jqXHR,textStatus,errorThrown) {
+                        if (jqXHR.status == 404) {
+                            RED.notify(node._("common.notification.error", {message: node._("common.notification.errors.not-deployed")}),"error");
+                        } else if (jqXHR.status == 0) {
+                            RED.notify(node._("common.notification.error", {message: node._("common.notification.errors.no-response")}),"error");
+                        } else {
+                            RED.notify(node._("common.notification.error",{message:node._("common.notification.errors.unexpected",{status:err.status,message:err.response})}),"error");
+                        }
+                    }
+                });
+            }
+        }
     });
 </script>
 

--- a/web-watch/web-watch.js
+++ b/web-watch/web-watch.js
@@ -1,47 +1,30 @@
 module.exports = function(RED) {
     "use strict";
-    var request = require('request');
+    var request = require("request");
 
     function WebpageWatch(config) {
         RED.nodes.createNode(this, config);
+        this.active = config.active;
         var node = this;
 
         if (!config.url) {
-            node.warn('Web watch URL: No URL is specified. Please specify in node configuration.');
+            node.warn("No URL is specified. Please specify in node configuration.");
             return;
         }
 
         config.interval = parseInt(config.interval);
-        var intervalId = null;
-        var cacheHtml = null;
+        node.intervalId = null;
+        node.cacheHtml = null;
         var msg = {
-            payload : {
-                headers: null,
-                url: config.url,
-                body: null
-            }
+            url: config.url,
+            node: node.type
         };
+        runInterval(msg, node, config);
+        node.log("URL (" + config.interval + " seconds): " + config.url);
 
-        node.log("Web watch URL (" + config.interval + " seconds): " + config.url);
-
-        intervalId = setInterval(function() {
-            request(config.url, function (error, response, body) {
-                if (!error && response.statusCode == 200) {
-                    if (cacheHtml == undefined) {
-                        cacheHtml = body;
-                    } else if (cacheHtml != "" && cacheHtml != body) {
-                        cacheHtml = body;
-                        msg.payload.headers = response.headers;
-                        msg.payload.body = body;
-                        node.send(msg);
-                    }
-                }
-            });
-        }, config.interval * 1000);
-
-        node.on('close', function() {
-            if (intervalId != null) {
-                clearInterval(intervalId);
+        node.on("close", function() {
+            if (this.intervalId != null) {
+                clearInterval(this.intervalId);
             }
         });
     }
@@ -49,52 +32,31 @@
 
     function WebpageWatchIn(config) {
         RED.nodes.createNode(this, config);
+        this.active = config.active;
         var node = this;
 
         config.interval = parseInt(config.interval);
-        var intervalId = null;
-        var cacheHtml = null;
+        node.intervalId = null;
+        node.cacheHtml = null;
         var msg = {
-            headers: null,
             url: config.url,
-            payload: null
+            node: node.type
         };
 
-        function runInterval(msg) {
-            if (intervalId != null) {
-                clearInterval(intervalId);
-            }
-
-            intervalId = setInterval(function() {
-                request(config.url, function (error, response, body) {
-                    if (!error && response.statusCode == 200) {
-                        if (cacheHtml == undefined) {
-                            cacheHtml = body;
-                        } else if (cacheHtml != "" && cacheHtml != body) {
-                            cacheHtml = body;
-                            msg.headers = response.headers;
-                            msg.payload = body;
-                            node.send(msg);
-                        }
-                    }
-                });
-            }, config.interval * 1000);
-        }
-
         if (config.url) {
-            node.log("Web watch in URL (" + config.interval + " seconds): " + config.url);
-            runInterval(msg);
+            node.log("URL (" + config.interval + " seconds): " + config.url);
+            runInterval(msg, node, config);
         }
 
         node.on("input", function (msg) {
             // try json parse on msg.payload first
             try {
                 var _msg = JSON.parse(msg.payload);
-                if (typeof _msg === 'object') {
-                    if(_msg.hasOwnProperty('url')) {
+                if (typeof _msg === "object") {
+                    if(_msg.hasOwnProperty("url")) {
                         msg.url = _msg.url;
                     }
-                    if(_msg.hasOwnProperty('interval')) {
+                    if(_msg.hasOwnProperty("interval")) {
                         msg.interval = _msg.interval;
                     }
                 }
@@ -104,31 +66,71 @@
             }
 
             if (!msg.url) {
-                node.warn('Web watch in: No URL is specified. Either specify in node configuration or by passing in msg.url');
+                this.warn("No URL is specified. Either specify in node configuration or by passing in msg.url");
                 return;
             }
 
             config.url = msg.url;
             config.interval = msg.interval || config.interval;
 
-            node.log("Web watch in URL (" + config.interval + " seconds): " + config.url);
+            this.log("URL (" + config.interval + " seconds): " + config.url);
 
-            if(msg.hasOwnProperty('payload')) {
+            if(msg.hasOwnProperty("payload")) {
                 msg._payload = msg.payload;
             }
-            if(msg.hasOwnProperty('topic')) {
+            if(msg.hasOwnProperty("topic")) {
                 msg._topic = msg.topic;
             }
-
-            runInterval(msg);
+            msg.node = this.type;
+            runInterval(msg, this, config);
         });
 
-        node.on('close', function() {
-            if (intervalId != null) {
-                clearInterval(intervalId);
+        node.on("close", function() {
+            if (this.intervalId != null) {
+                clearInterval(this.intervalId);
             }
         });
     }
     RED.nodes.registerType("Web watch in", WebpageWatchIn);
+
+    RED.httpAdmin.post("/web-watch/:id/:state", RED.auth.needsPermission("web-watch.write"), function(req,res) {
+        var node = RED.nodes.getNode(req.params.id);
+        var state = req.params.state;
+        if (node !== null && typeof node !== "undefined" ) {
+            if (state === "enable") {
+                node.active = true;
+                res.sendStatus(200);
+            } else if (state === "disable") {
+                node.active = false;
+                res.sendStatus(201);
+            } else {
+                res.sendStatus(404);
+            }
+        } else {
+            res.sendStatus(404);
+        }
+    });
+
+    function runInterval(msg, node, config) {
+        if (node.intervalId != null) {
+            clearInterval(node.intervalId);
+        }
+
+        node.intervalId = setInterval(function() {
+            if (node.active == false) return;
+            request(config.url, function (error, response, body) {
+                if (!error && response.statusCode == 200) {
+                    if (node.cacheHtml == null) {
+                        node.cacheHtml = body;
+                    } else if (node.cacheHtml != "" && node.cacheHtml != body) {
+                        node.cacheHtml = body;
+                        msg.headers = response.headers;
+                        msg.payload = body;
+                        node.send(msg);
+                    }
+                }
+            });
+        }, config.interval * 1000);
+    }
 }
 

comments