klein0r / ioBroker.luftdaten

ioBroker adapter to get the sensor values of your local or any remote luftdaten.info sensor into ioBroker
https://haus-automatisierung.com/
MIT License
22 stars 8 forks source link

Error when JSON is empty #3

Closed Stabilostick closed 6 years ago

Stabilostick commented 6 years ago

If station xxxx disappears from luftdaten.info, JSON data from api.luftdaten.info/v1/sensor/xxxx/ could be empty leading to an error in the adapter:

2018-06-26 21:45:00.613 - info: luftdaten.0 sensor type: remote
2018-06-26 21:45:00.613 - info: luftdaten.0 sensor identifier: 2628
2018-06-26 21:45:00.613 - info: luftdaten.0 remote request
2018-06-26 21:45:03.183 - error: luftdaten.0 uncaught exception: Cannot read property 'sensordatavalues' of undefined
2018-06-26 21:45:03.183 - error: luftdaten.0 TypeError: Cannot read property 'sensordatavalues' of undefined
at Request._callback (C:\dev\iobroker\node_modules\iobroker.luftdaten\main.js:69:48)
at Request.self.callback (C:\dev\iobroker\node_modules\iobroker.luftdaten\node_modules\request\request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request. (C:\dev\iobroker\node_modules\iobroker.luftdaten\node_modules\request\request.js:1157:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage. (C:\dev\iobroker\node_modules\iobroker.luftdaten\node_modules\request\request.js:1079:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
2018-06-26 21:45:03.693 - info: luftdaten.0 terminating

To change this behaviour, I suggest the following change in main.js, line 56ff:

} else if (sensorType == "remote") {
        adapter.log.info('remote request');

        var url = "http://api.luftdaten.info/v1/sensor/" + sensorIdentifier + "/";

        request(
            {
                url: url,
                json: true
            },
            function (error, response, content) {
                adapter.log.debug('Request done');

                if (!error && response.statusCode == 200) {
                    if (content.length > 0) {
                        for (var key in content[0].sensordatavalues) {
       [...]
                        }
                    }
                   else {
                        adapter.log.warn("No JSON-data in response from '" + url + "'. Please check remote sensor identifier.");
                    }
                } else {
                    adapter.log.error(error);
                }
            }
        );
    }

The same could be checked for the local query.

Pix--- commented 6 years ago

Could you write a pull request?

Stabilostick commented 6 years ago

Thank you.