[英]How do I call a Python function from Node.js?
I'm working on making a Homebridge plugin for a project. 我正在为一个项目制作Homebridge插件。 Homebridge
is a Node.js server which I have running on a Raspberry Pi which emulates an Apple HomeKit Bridge. Homebridge
是一台Node.js服务器,我已经在模仿Apple HomeKit Bridge的Raspberry Pi上运行。
Using this link, I was able to execute Python code from the following Node.js code: 使用此链接,我能够从以下Node.js代码执行Python代码:
var Service, Characteristic;
var spawn = require('child_process').spawn;
var py = spawn('python', ['/home/pi/Desktop/RFbulb/nRF24L01PLUS.py']);
var data = [10,10,10];
var dataString = '';
var RFstatus = true;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-RFbulb", "RFbulb", RFbulbAccessory);
}
function RFbulbAccessory(log, config) {
this.log = log;
this.config = config;
this.name = config["name"];
this.address = config["address"];
this.service = new Service.Lightbulb(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
}
RFbulbAccessory.prototype.setOn = function(on, callback) { // This is the function throwing the error
var state = on ? "on": "off";
if (state == "on") {
data = [1,parseInt(this.address, 10),100];
dataString = '';
py.stdout.on('data', function(data) {
dataString += data.toString();
});
py.stdout.on('end', function() {
console.log(dataString);
});
py.stdin.write(JSON.stringify(data));
py.stdin.end();
RFstatus = true;
}
callback(null);
}
RFbulbAccessory.prototype.getServices = function() {
return [this.service];
}
Interestingly enough, when I activate the setOn
function the first time (for example, to turn the device on) it works fine, but when I activate the setOn
function a second time (to turn the device off) I get the following errors and the server exits: 有趣的是,当我第一次激活setOn
函数(例如,打开设备)时,它可以正常工作,但是当我setOn
激活setOn
函数(以关闭设备)时,出现以下错误,并且服务器出口:
events.js:141
throw er; // Unhandled 'error' event
^
Error: write after end
at writeAfterEnd (_stream_writable.js:166:12)
at Socket.Writable.write (_stream_writable.js:211:5)
at Socket.write (net.js:642:40)
at RFbulbAccessory.setOn (/usr/lib/node_modules/homebridge-RFbulb/index.js:47:12)
at emitThree (events.js:97:13)
at emit (events.js:175:7)
at Characteristic.setValue (/usr/lib/node_modules/homebridge/node_modules/hap-nodejs/lib/Characteristic.js:155:10)
at Bridge.<anonymous> (/usr/lib/node_modules/homebridge/node_modules/hap-nodejs/lib/Accessory.js:710:22)
at Array.forEach (native)
at Bridge.Accessory._handleSetCharacteristics (/usr/lib/node_modules/homebridge/node_modules/hap-nodejs/lib/Accessory.js:655:8)
What could be causing this error? 是什么导致此错误? Especially since the function appears to work fine for a single use. 特别是因为该功能一次使用似乎可以正常工作。
You're getting that error because you're closing the input stream: 由于关闭输入流而收到该错误:
py.stdin.end();
After a stream has been closed, you can no longer write to it like you are here: 关闭流后,您将无法像在这里一样对其进行写操作:
py.stdin.write(JSON.stringify(data));
If the Python program you're running accepts multiple commands over STDIN then simply remove the py.stdin.end()
line. 如果您正在运行的Python程序通过STDIN接受多个命令,则只需删除py.stdin.end()
行。
However, it's likely that your Python program runs once then completes. 但是,您的Python程序很可能会运行一次然后完成。 If that's the case, you will need to respawn the process every time you want the program to run. 如果是这样,您每次需要运行程序时都需要重新生成该进程。
if (state === "on") {
py = spawn('python', ['/home/pi/Desktop/RFbulb/nRF24L01PLUS.py']);
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.