From 87a325ffcc8cfda67eac3b8c83d2c58ba87da22e Mon Sep 17 00:00:00 2001 From: Xavier Bourry Date: Thu, 2 Aug 2018 22:55:53 +0200 Subject: [PATCH] add exec_shellCmd func + improve doc --- README.md | 32 +++++++++++++++++++++++++++++--- client/JetsonJSClient.js | 7 +++++++ server/services/AppWS.js | 15 +++++++++++---- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6386148..d3dc248 100644 --- a/README.md +++ b/README.md @@ -72,11 +72,37 @@ JETSONJSCLIENT.send_value({ }) ``` -`JETSONJSCLIENT.shutdown()` : shutdown the Jetson. It is an hardware shutdown (equivalent to the Unix command `shutdown -h now`) - +`JETSONJSCLIENT.exec_shellCmd()` : execute a shell command. It can be useful to switch ON/OFF the Jetson GPIOs +`JETSONJSCLIENT.shutdown()` : shutdown the Jetson. It is an hardware shutdown (equivalent to the Unix command `shutdown -h now`) +### Final webapp +The final web application (not running in the Jetson but on the user's browser) connects to the Jetson through websockets and get the values. +There is no buffering : if a value is sent from the Jetson when the user is not connected, this value will be lost. We would rather drop some values than introducing a latency. The [test application](/test/index.html), served statically by the NodeJS server on port 3000 show how to connect externally to the Jetson websocket server and read the values : +```javascript +var socket=new WebSocket('ws://'+jetsonIP+':8888') //port hould be server.serviceExtWSPort in settings.js + +// Connection opened +socket.addEventListener('open', function (event) { + console.log('Connected!') +}); + +// Listen for messages +var domLogs=document.getElementById('logs'); +domLogs.value=''; +socket.addEventListener('message', function (event) { + var dataParsed=JSON.parse(event.data); + var typeLabel=dataParsed.t; + var data=dataParsed.m; + + switch(typeLabel){ + case 'VAL': + domLogs.value+=JSON.stringify(data)+'\n' + break; + } +}); +``` @@ -96,4 +122,4 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI ## References * [Jeeliz official website](https://jeeliz.com) * [Nvidia Jetson Download center](https://developer.nvidia.com/embedded/downloads) - +* Nvidia Jetson GPIOS (eLinux.org): [hardware](https://elinux.org/Jetson/GPIO), [software](https://elinux.org/Jetson/Tutorials/GPIO) diff --git a/client/JetsonJSClient.js b/client/JetsonJSClient.js index fb5f83a..2d72465 100644 --- a/client/JetsonJSClient.js +++ b/client/JetsonJSClient.js @@ -274,6 +274,13 @@ const JETSONJSCLIENT=(function(){ } }, + 'exec_shellCmd': function(cmd){ + if (_state!==_states.idle){ + return false + } + return send('SHELLCMD', cmd) + }, + 'shutdown': function(){ console.log('INFO in JetsonJSClient.js : shutdown() launched. THE DEVICE WILL SHUTDOWN BRO!!!') send('CMD', 'SHUTDOWN') diff --git a/server/services/AppWS.js b/server/services/AppWS.js index 74f6ece..e5a2377 100644 --- a/server/services/AppWS.js +++ b/server/services/AppWS.js @@ -67,13 +67,20 @@ const onMessage=(typeLabel, dataDict)=>{ console.log('INFO in AppWS : CMD received - CMD =', dataDict) switch(dataDict){ case 'SHUTDOWN': - exec('shutdown -h now', (isSuccess)=>{ - + exec('shutdown -h now', (isSuccess, stdOut, stdErr)=>{ + }) break } break; + case 'SHELLCMD': + console.log('INFO in AppWS : SHELLCMD received - SHELLCMD =', dataDict) + exec(dataDict, (isSuccess, stdOut, stdErr)=>{ + + }) + break + default: console.log('WARNING in AppWS - onMessage : unknow message type ', typeLabel) break @@ -84,7 +91,7 @@ const exec_cmd=(shellCmd, callback)=>{ exec(shellCmd, (err, stdout, stderr) => { if (err) { console.log('WARNING in AppWS - exec_cmd() : cannot execute the command ', shellCmd, 'err =', err) - callback(false) + callback(false, '', '') return } @@ -92,7 +99,7 @@ const exec_cmd=(shellCmd, callback)=>{ console.log('INFO in AppWS - exec_cmd() : ', shellCmd, 'results:') console.log(`stdout: ${stdout}`) console.log(`stderr: ${stderr}`) - callback(true) + callback(true, stdout, stderr) }) }