javascript - How to call Python function from Node.JS -


i'm working on making homebridge plugin project. homebridge node.js server have running on raspberry pi emulates apple homekit bridge.

using this link, able execute python code following node.js code:

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) { // function throwing 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 activate seton function first time (for example turn device on) works fine, when activate seton function second time (to turn device off) following errors , server exits:

events.js:141       throw er; // unhandled 'error' event       ^  error: write after end     @ writeafterend (_stream_writable.js:166:12)     @ socket.writable.write (_stream_writable.js:211:5)     @ socket.write (net.js:642:40)     @ rfbulbaccessory.seton (/usr/lib/node_modules/homebridge-rfbulb/index.js:47:12)     @ emitthree (events.js:97:13)     @ emit (events.js:175:7)     @ characteristic.setvalue (/usr/lib/node_modules/homebridge/node_modules/hap-nodejs/lib/characteristic.js:155:10)     @ bridge.<anonymous> (/usr/lib/node_modules/homebridge/node_modules/hap-nodejs/lib/accessory.js:710:22)     @ array.foreach (native)     @ bridge.accessory._handlesetcharacteristics (/usr/lib/node_modules/homebridge/node_modules/hap-nodejs/lib/accessory.js:655:8) 

what causing error? since function appears work fine single use.

you're getting error because you're closing input stream:

py.stdin.end(); 

after stream has been closed, can no longer write here:

py.stdin.write(json.stringify(data)); 

if python program you're running accepts multiple commands on stdin remove py.stdin.end() line.

however, it's python program runs once completes. if that's case, need respawn process every time want program run.

if (state === "on") {     py = spawn('python', ['/home/pi/desktop/rfbulb/nrf24l01plus.py']);     ... } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -