removed interval, runs only on input from now on
removed interval, runs only on input from now on

--- a/onvif/snapshot.html
+++ b/onvif/snapshot.html
@@ -15,10 +15,6 @@
         <label for="node-input-password"><i class="fa fa-key"></i> Password</label>
         <input type="text" id="node-input-password" placeholder="Password">
     </div>
-    <div class="form-row">
-        <label for="node-input-interval"><i class="fa fa-clock-o"></i> Interval (S)</label>
-        <input type="text" id="node-input-interval" placeholder="Interval">
-    </div>
 </script>
 
 <script type="text/x-red" data-help-name="ONVIF Snapshot">
@@ -32,10 +28,9 @@
         defaults: {
             name: {value: ""},
             url: {value: "", required: true},
-            interval: {value: "60", required: true, validate: RED.validators.number()},
             username: {value: ""},
             password: {value: ""},
-            active: {value: true}
+            active: {value: true, validate: RED.validators.boolean()}
         },
         inputs: 1,
         outputs: 1,
@@ -44,51 +39,9 @@
             return this.name || this.url || this.type;
         },
         labelStyle: function() {
-            return this.name?"node_label_italic":"";
+            return this.name ? "node_label_italic" : "";
         },
         paletteLabel: "Snapshot",
-        align: "left",
-        button: {
-            toggle: "active",
-            onclick: function() {
-                var label = this.name || this.url || this.type;
-                var node = this;
-                $.ajax({
-                    url: "onvif-snapshot/"+this.id+"/"+(this.active?"enable":"disable"),
-                    type: "POST",
-                    success: function(resp, textStatus, xhr) {
-                        var historyEvent = {
-                            t:'edit',
-                            node:node,
-                            changes:{
-                                active: !node.active
-                            },
-                            dirty:node.dirty,
-                            changed:node.changed
-                        };
-                        node.changed = true;
-                        node.dirty = true;
-                        RED.nodes.dirty(true);
-                        RED.history.push(historyEvent);
-                        RED.view.redraw();
-                        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(label + " not deployed", "error");
-                        } else if (jqXHR.status == 0) {
-                            RED.notify(label + " no response", "error");
-                        } else {
-                            RED.notify(label + " unexpected error", "error");
-                        }
-                    }
-                });
-            }
-        },
         oneditprepare: function() {
         },
         oneditsave: function() {

--- a/onvif/snapshot.js
+++ b/onvif/snapshot.js
@@ -24,9 +24,6 @@
                     if(_msg.hasOwnProperty("password")) {
                         msg.password = _msg.password;
                     }
-                    if(_msg.hasOwnProperty("interval")) {
-                        msg.interval = _msg.interval;
-                    }
                     if(_msg.hasOwnProperty("active")) {
                         msg.active = _msg.active;
                     }
@@ -37,7 +34,6 @@
             config.url = msg.url || config.url;
             config.username = msg.username || config.username;
             config.password = msg.password || config.password;
-            config.interval = msg.interval || config.interval;
             node.active = config.active = msg.active || config.active;
 
             if(msg.hasOwnProperty("payload")) {
@@ -45,78 +41,39 @@
             }
             msg.node = this.type;
 
-            runInterval(msg, node, config);
+            run(msg, node, config);
         });
 
         if (!config.url) {
             node.warn("No URL is specified. Please specify in node configuration.");
             return;
         }
-
-        config.interval = parseInt(config.interval);
-        node.intervalId = null;
-
-        runInterval(msg, node, config);
-
-        node.on("close", () => {
-            if (this.intervalId != null) {
-                clearInterval(this.intervalId);
-            }
-        });
     }
     RED.nodes.registerType("ONVIF Snapshot", snapshot);
 
-    RED.httpAdmin.post("/onvif-snapshot/:id/:state", RED.auth.needsPermission("onvif-snapshot.write"), (req, res) => {
-        let node = RED.nodes.getNode(req.params.id);
-        let 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 run(msg, node, config) {
+        if (node.active == false) return;
 
-    function runInterval(msg, node, config) {
-        if (node.intervalId != null) {
-            clearInterval(node.intervalId);
-        }
-        if (node.active == false) return;
-        node.log("URL (" + config.interval + " seconds): " + config.url);
+        let onvifInstance = new onvif.OnvifDevice({
+            xaddr: config.url,
+            user : config.username,
+            pass : config.password
+        });
 
-        let fetch = function() {
-            let onvifInstance = new onvif.OnvifDevice({
-                xaddr: config.url,
-                user : config.username,
-                pass : config.password
-            });
-
-            onvifInstance.init().then((info) => {
-                node.log('Fetching snapshot from ' + config.url);
-                return onvifInstance.fetchSnapshot();
-            }).then((res) => {
-                let prefix = 'data:' + res.headers['content-type'] + ';base64,';
-                let base64Image = Buffer.from(res.body, 'binary').toString('base64');
-                msg.payload = prefix + base64Image;
-                msg.binaryImage = res.body;
-                node.send(msg);
-            }).catch((error) => {
-                msg.payload = null;
-                msg.error = error;
-                node.send(msg);
-            });
-        }
-        fetch();
-        node.intervalId = setInterval(() => {
-            fetch();
-        }, config.interval * 1000);
+        onvifInstance.init().then((info) => {
+            node.log('Fetching snapshot from ' + config.url);
+            return onvifInstance.fetchSnapshot();
+        }).then((res) => {
+            let prefix = 'data:' + res.headers['content-type'] + ';base64,';
+            let base64Image = Buffer.from(res.body, 'binary').toString('base64');
+            msg.payload = prefix + base64Image;
+            msg.binaryImage = res.body;
+            node.send(msg);
+        }).catch((error) => {
+            msg.payload = null;
+            msg.error = error;
+            node.send(msg);
+        });
     }
 }
 

comments