ruudverheijden / node-p1-reader

Node.js package for reading and parsing data from the P1 port of a Dutch or Belgian Smart Meter.
MIT License
25 stars 25 forks source link

node app crashes on thrown error when meter is not found #6

Closed basvdijk closed 6 years ago

basvdijk commented 7 years ago

Sometimes (it doesn't happen all the time) the entire NodeJS app crashes on the exception below. Looks like the catch isn't catching this error. I am not sure if the problem is the node-p1-reader is throwing an Error. But I guess it shouldn't crash the entire app.

        try {
            let p1Reader = new P1Reader({ serialPort: pluginConfiguration.port });
            this.hardwareInstance = p1Reader;
            this.hardwareInstance.on('reading', this._updatePowerData.bind(this));
        } catch (error) {
            util.log('Initialisation of P1 plugin failed', error);
        }
        throw new Error('Could not find an attached Smart Meter');
        ^

Error: Could not find an attached Smart Meter
    at _tryNextSerialPort (project/node_modules/p1-reader/main.js:145:15)
    at SerialPort.<anonymous> (project/node_modules/p1-reader/main.js:103:25)
    at emitOne (events.js:115:13)
    at SerialPort.emit (events.js:210:7)
    at SerialPort.raw (project/node_modules/serialport/lib/parsers.js:7:13)
    at SerialPort._emitData (project/node_modules/serialport/lib/serialport.js:313:18)
    at SerialPort.<anonymous> (project/node_modules/serialport/lib/serialport.js:293:14)
    at SerialPort.<anonymous> (project/node_modules/serialport/lib/serialport.js:306:7)
timoline commented 6 years ago

I have exact the same problem......sometimes

ruudverheijden commented 6 years ago

Totally forgot about this issue... I experienced the same but I have no idea yet on what is causing the issue. Only thing I fixed for now is replacing the "throw new Error()" with a simple console.error() to at least prevent a crash. Will release a new version soon.

Please let me know if you have any more in-depth insights in why this issue occurs.