From ec445bc0f461693c03e7da2756e7708d190d70e7 Mon Sep 17 00:00:00 2001 From: David Maher Date: Wed, 24 Jun 2020 22:26:03 -0400 Subject: [PATCH] Autodetect libpython --- README.md | 7 ------- config.schema.json | 6 ------ index.js | 17 +++++++++++++---- package.json | 2 +- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4d927f1..ed2792e 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,6 @@ Edit your `config.json` accordingly. Configuration sample: | name | For logging purposes. | No | | gpio | The BCM number of the pin your RF transmitter is connected to. (Default: 17) | No | | repeat | RF code repeat cycles. (Default: 10) | No | -| libpython | **See note below.** | No | | devices | Array of switch config (multiple switches supported). | Yes | | \|- name | Name of your device. | Yes | | \|- on_code | RF code to turn on your device. | Yes | @@ -43,11 +42,5 @@ Edit your `config.json` accordingly. Configuration sample: | \|- protocol | RF code protocol. (Default: 1) | No | | \|- codelength | RF code length. (Default: 24) | No | -### libpython Setting -If you are running a version of Python other than 3.7, you may need to update this value. You probably won't want to touch this unless you encounter problems. Here is how to find that value. -1. Run `python3-config --libs`. -2. You'll see something like `-lpython3.7m -lcrypt -lpthread -ldl -lutil -lm`. -3. You want the first item listed, excluding the -l. In this example, it would be `python3.7m`. - ### Note on Getting RF Codes I've had the best luck with `RFSniffer` from the [433Utils](https://github.com/ninjablocks/433Utils) project. Your mileage may vary. diff --git a/config.schema.json b/config.schema.json index 9bee2e7..047df5f 100644 --- a/config.schema.json +++ b/config.schema.json @@ -26,12 +26,6 @@ "placeholder": 10, "description": "RF code repeat cycles." }, - "libpython": { - "title": "libpython Version", - "type": "string", - "placeholder": "python3.7m", - "description": "See readme for more information." - }, "devices": { "title": "Devices", "type": "array", diff --git a/index.js b/index.js index d541cb7..edb968b 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const python = require("node-calls-python").interpreter; +const process = require("child_process"); var Accessory, Service, Characteristic, UUIDGen; module.exports = function(homebridge) { @@ -16,15 +17,23 @@ function rfSwitchPlatform(log, config, api) { this.gpio = config.gpio || 17; this.repeat = config.repeat || 10; - this.libpython = config.libpython || 'python3.7m'; - this.accessories = []; + this.libpython = config.libpython; - this.commandQueue = []; - this.transmitting = false; + if (this.libpython == null) { + var pyconfig = process.execSync('python3-config --libs').toString(); + var index = pyconfig.indexOf('-lpython'); + pyconfig = pyconfig.substr(index + 2); + index = pyconfig.indexOf(' '); + this.libpython = pyconfig.substr(0, index); + } python.fixlink('lib' + this.libpython + '.so'); + this.accessories = []; + this.commandQueue = []; + this.transmitting = false; + var rpiRf = python.importSync('rpi_rf'); this.rfDevice = python.createSync(rpiRf, 'RFDevice', this.gpio, 1, null, this.repeat, 24); python.callSync(this.rfDevice, 'enable_tx'); diff --git a/package.json b/package.json index 5bc0db9..115725a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "displayName": "Homebridge rpi-rf Switch", "name": "homebridge-rpi-rf-switch", - "version": "2.1.2", + "version": "2.1.3", "description": "rpi-rf plugin for Homebridge", "repository": { "type": "git",