Skip to content

Commit

Permalink
add exec_shellCmd func + improve doc
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierjs committed Aug 2, 2018
1 parent d9d90de commit 87a325f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
```



Expand All @@ -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)
7 changes: 7 additions & 0 deletions client/JetsonJSClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
15 changes: 11 additions & 4 deletions server/services/AppWS.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -84,15 +91,15 @@ 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
}

// the *entire* stdout and stderr (buffered)
console.log('INFO in AppWS - exec_cmd() : ', shellCmd, 'results:')
console.log(`stdout: ${stdout}`)
console.log(`stderr: ${stderr}`)
callback(true)
callback(true, stdout, stderr)
})
}

Expand Down

0 comments on commit 87a325f

Please sign in to comment.