Saturday, 8 April 2017

how to run a python script from node js

i have written a server in nodejs on raaspberry pi. iwant to run a python script from it but it does not execute the line . on previous instances i have run files but comming this instance of socket.io i cant run the file!!though the console shows accurate output passed to it from the client .but it fails to read the file. the socket responsible for movement . `/*jshint esversion: 6 */

var express = require('express');
// library for executing system calls
const spawn = require('child_process').spawn;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
app = express();
server = require('http').createServer(app);
io = require('socket.io').listen(server);
var colour = require('colors');
var ip = require('ip');

var PortNumber = 3000;

var xPos = 0, yPos = 0;

server.listen(PortNumber);
app.use(express.static('public'));


io.sockets.on('connection', function (socket) {

    socket.emit('ip', {value: 'http://' + ip.address() + ':8081'});

    // otp generation socket
    socket.on('userEmail', function (data) {
        var userEmail = data.value;

        // system call to generate OTP
        const OTPgeneration = spawn('python', [__dirname + '/python/OTPgeneration.py', userEmail]);
        OTPgeneration.stdout.on('data', (data) => {
            console.log(`${data}`);
        });
    });

    // otp submit button socket
    socket.on('userOtp', function (data) {
        var userOtp = data.value;
        console.log(userOtp);

        // system call to generate OTP
        const otpValidate = spawn('python', [__dirname + '/python/OTPvalidation.py', userOtp]);
        otpValidate.stdout.on('data', (data) => {
            console.log(`${data}`);

            // Get last output from python file
            var lastChar = `${data}`.slice(-2);

            if (lastChar === '0\n') {
                console.log('Wrong OTP');
            }
            else io.sockets.emit('otpConformation', {value: 'confirm'}); //sends the confirmation to all connected clients
        });
    });

    // x y values socket
    socket.on('servoPosition', function (data) {
        servoPosition = data.value;
        // servoPosition[0] is x and servoPosition[1] is y
        console.log('Reveived X: ' + servoPosition[0] + '  Y: ' + servoPosition[1]);
        // system call for i2c comminication
        const i2cData = spawn('python', [__dirname + '/python/sendI2C.py', servoPosition[0], servoPosition[1]]);
        i2cData.stdout.on('data', (data) => {
            console.log(`${data}`);
        });
    });

    // movement socket
    socket.on('movement', function (data) {
        var m = data.value;
            console.log("Movement :" + m);
  const submitMove = spawn('python', [__dirname + '/python/move.py', m]);
        submitMove.stdout.on('data', (data) => {
            console.log(`${data}`);
        });

    });
});

console.log('Server Running @'.green + ip.address().green + ":3000".green);

function readTextFile(file) {
    var rawFile = new XMLHttpRequest();
    //var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function () {
        if(rawFile.readyState === 4) {
            if(rawFile.status === 200 || rawFile.status === 0) {
                allText = rawFile.responseText;
                //alert(allText);
            }
        }
    };
    rawFile.send(null);
}
`



via pradumna

No comments:

Post a Comment