I can't stop NR (I can only kill process)

Hi, yes I could advice myself what he wrote, anyway the module works, it just hang NR from quit. It's highly probable that a little correction will fix the problem, I'm asking here if someone kindly can advice whrere the problem is. Unfortunately there aren't other nodes that can read `cat /dev/input/event0
I post the code here:
JSON:

{
  "name": "node-red-contrib-dev-input",
  "version": "0.0.1",
  "description": "Basic Node-Red node for use in /dev/input/event reading.",
  "main": "dev-input.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/MattGyver/node-red-contrib-dev-input.git"
  },
  "keywords": [
    "node-red",
    "input",
    "event",
    "hid"
  ],
  "node-red": {
    "nodes": {
      "devinput": "dev-input.js"
    }
  },
  "author": "Mattias Nord",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/MattGyver/node-red-contrib-dev-input/issues"
  },
  "homepage": "https://github.com/MattGyver/node-red-contrib-dev-input#readme"
}

HTML:

<script type="text/javascript">
    RED.nodes.registerType('dev-input',{
        category: 'input',
        color: '#a6bbcf',
        defaults: {
            name: {value:""},
            device: {valie:""}
        },
        inputs:0,
        outputs:1,
        icon: "file.png",
        label: function() {
            return this.name||"dev-input";
        }
    });
</script>

<script type="text/x-red" data-template-name="dev-input">
    <div class="form-row">
        <label for="node-input-name"><i class="icon-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-row">
        <label for="node-input-device"><i class="icon-tag"></i> Device</label>
        <input type="text" id="node-input-device" placeholder="/dev/input/event0">
    </div>
</script>

<script type="text/x-red" data-help-name="dev-input">
    <p>A simple node that reads specified /dev/input event and forward the event data in msg.payload.</p>
</script>

JS:

module.exports = function(RED) {

  var msg = { topic:"dev-input" }

  function parseBuffer( buffer ) {
    var eventObject = {};

    eventObject.code   = buffer[12];

    return eventObject;
  }

  function DevInput(config) {
    RED.nodes.createNode(this,config);
    var node = this;

    if (config.device != undefined) {
      var FS = require('fs');

      if (!FS.existsSync(''+config.device)) {
        throw "Can't find device.";
      }

      FS.createReadStream(''+config.device).on('data', function( buffer ) {
        msg.payload = parseBuffer(buffer)
        node.send(msg);
      });
    }

  }
  RED.nodes.registerType("dev-input",DevInput);
}

Thank you a lot

is the code

      FS.createReadStream(''+config.device).on('data', function( buffer ) {
        msg.payload = parseBuffer(buffer)
        node.send(msg);

openng the file but there is nothing that closes it when NR is switching off? Can it be that the reason it hangs?
Thank you

Certainly the on close function should release any resources acquired.
https://nodered.org/docs/creating-nodes/node-js#closing-the-node

1 Like

Mate, thanks for keep me helping on this post,
I've understand it but I have no clue how to.. my javascript level is low and I?m able onoy to write functions with checks and for cycles (( can you kindly help me to insert this code on quit event?
May I eventaully open another topic asking specific help to users how to do this? Thank you!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.