From 2f4fcb5c43c00cb80c2e32aedeffba92791939e3 Mon Sep 17 00:00:00 2001 From: Laziu Kim <laziu.cc@gmail.com> Date: Fri, 31 May 2019 16:44:27 +0900 Subject: [PATCH] add comment --- public/client.js | 10 ++++++++-- server.js | 12 ++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/public/client.js b/public/client.js index 8425b35..1f9d4d5 100644 --- a/public/client.js +++ b/public/client.js @@ -1,4 +1,6 @@ const faye = new Faye.Client('/faye', { timeout: 120 }); + +// get data from socket end set UI text faye.subscribe('/drone/navdata', data => { [ 'batteryPercentage', @@ -12,7 +14,6 @@ faye.subscribe('/drone/navdata', data => { ].forEach(type => $('#' + type).html(Math.round(data.demo[type], 4))); return showBatteryStatus(data.demo.batteryPercentage); }); - window.showBatteryStatus = batteryPercentage => { $('#batterybar').width(`${batteryPercentage}%`); if (batteryPercentage < 30) @@ -27,9 +28,12 @@ window.showBatteryStatus = batteryPercentage => { 'data-original-title': `Battery status: ${batteryPercentage}%` }); }; + +// get image stream from server and set UI faye.subscribe('/drone/image', src => $('#cam').attr({ src: src })); -keymap = { +// get keyinput +const keymap = { 87: { ev: 'move', action: 'front' }, // W 83: { ev: 'move', action: 'back' }, // S 65: { ev: 'move', action: 'left' }, // A @@ -64,6 +68,8 @@ $(document).keyup(ev => { speed = 0; faye.publish('/drone/drone', { action: 'stop' }); }); + +// get mouse event $('*[data-action]').on('mousedown', ev => { faye.publish('/drone/' + $(this).attr('data-action'), { action: $(this).attr('data-param'), diff --git a/server.js b/server.js index b877053..c548c0e 100644 --- a/server.js +++ b/server.js @@ -2,9 +2,11 @@ const express = require('express'); const faye = require('faye'); const path = require('path'); +// config drone const drone = require('ar-drone').createClient(); drone.config('general:navdata_demo', 'TRUE'); +// config express app const app = express(); app.configure(() => { app.set('port', process.env.PORT || 3001); @@ -13,11 +15,12 @@ app.configure(() => { app.use('/components', express.static(path.join(__dirname, 'components'))); }); +// create server and socket const server = require('http').createServer(app); - new faye.NodeAdapter({ mount: '/faye', timeout: 45 }).attach(server); const socket = new faye.Client(`http://localhost:${app.get('port')}/faye`); +// get request from socket -> set command to drone socket.subscribe('/drone/move', cmd => { console.log('move', cmd); if (drone[cmd.action]) drone[cmd.action](cmd.speed); @@ -30,13 +33,17 @@ socket.subscribe('/drone/drone', cmd => { console.log('drone command: ', cmd); if (drone[cmd.action]) drone[cmd.action](); }); + +// start server server.listen(app.get('port'), () => console.log('Express server listening on port ' + app.get('port')) ); -let currentImg = null; +// send navdata to socket drone.on('navdata', data => socket.publish('/drone/navdata', data)); +// save latest img from drone PngStream +let currentImg = null; let imageSendingPaused = false; drone.createPngStream().on('data', frame => { currentImg = frame; @@ -48,6 +55,7 @@ drone.createPngStream().on('data', frame => { }, 100); }); +// send latest img to server app.get('/image/:id', (req, res) => { res.writeHead(200, { 'Content-Type': 'image/png' }); res.end(currentImg, 'binary'); -- 2.22.0