From dccd3fd3d42abf6704dab0ab5d2178918fb5b630 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 15 Aug 2019 13:06:31 +0200 Subject: [PATCH] Don't open the device and leave it open on each measurement, or the backend will fail with 'Too many open files' at some point --- .../plugins/gpio/sensor/distance/vl53l1x.py | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/platypush/plugins/gpio/sensor/distance/vl53l1x.py b/platypush/plugins/gpio/sensor/distance/vl53l1x.py index b367b185..1e473a60 100644 --- a/platypush/plugins/gpio/sensor/distance/vl53l1x.py +++ b/platypush/plugins/gpio/sensor/distance/vl53l1x.py @@ -1,3 +1,5 @@ +import time + from platypush.plugins import action from platypush.plugins.gpio.sensor import GpioSensorPlugin @@ -32,6 +34,7 @@ class GpioSensorDistanceVl53L1XPlugin(GpioSensorPlugin): from VL53L1X import VL53L1X self._device = VL53L1X(i2c_bus=self.i2c_bus, i2c_address=self.i2c_address) + self._device.open() return self._device @action @@ -52,14 +55,24 @@ class GpioSensorDistanceVl53L1XPlugin(GpioSensorPlugin): """ device = self._get_device() - device.open() ret = {} - for i, r in enumerate(['short', 'medium', 'long']): - if eval(r): - device.start_ranging(i+1) - ret[r] = device.get_distance() - device.stop_ranging() + try: + for i, r in enumerate(['short', 'medium', 'long']): + if eval(r): + device.start_ranging(i+1) + ret[r] = device.get_distance() + device.stop_ranging() + except Exception as e: + self.logger.exception(e) + + try: + self._device.close() + except: + pass + + self._device = None + time.sleep(1) return ret