From 204e146fdb3057ebed0e02e3620c3654863003c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Suat=20=C3=96zg=C3=BCr?= Date: Sun, 27 Sep 2020 19:52:30 +0200 Subject: [PATCH] wip --- public_html/css/style.css | 9 ++++-- public_html/index.html | 27 +++++++++++++---- public_html/js/scann3r.js | 62 ++++++++++++++++++++++++++------------- src/index.js | 2 +- src/lib/Project.js | 1 + src/lib/Scan.js | 25 +++++++++++----- src/modules/Camera.js | 9 ++---- src/modules/Proxy.js | 31 +++++++++++++++----- src/modules/Stepper.js | 26 +++++----------- src/modules/WebServer.js | 5 ++-- src/modules/WebSocket.js | 29 +++++++++++------- 11 files changed, 144 insertions(+), 82 deletions(-) diff --git a/public_html/css/style.css b/public_html/css/style.css index 2cca052..fb134d7 100644 --- a/public_html/css/style.css +++ b/public_html/css/style.css @@ -100,10 +100,9 @@ h2 { height: 180px } -.thumbnail-box .thumbnail-text { +/*.thumbnail-box .thumbnail-text { width: 88px; - float: left; -} +}*/ .thumbnail-outer { margin: 10px; @@ -147,4 +146,8 @@ i { .js-abort { display:none; +} + +button.xsmall { + font-size:12px; height:25px; padding:3px; } \ No newline at end of file diff --git a/public_html/index.html b/public_html/index.html index 00c9274..c42be45 100644 --- a/public_html/index.html +++ b/public_html/index.html @@ -14,7 +14,7 @@ - + @@ -49,17 +49,34 @@

About

-
-
-
- +
+
+
+ + +
+ + +
diff --git a/public_html/js/scann3r.js b/public_html/js/scann3r.js index 078c229..0a1682c 100644 --- a/public_html/js/scann3r.js +++ b/public_html/js/scann3r.js @@ -124,8 +124,8 @@ var Scann3r = { this.gallery.prepend(t1); $('#dialog-finished').html(t2) .dialog({ - width: 250, - height: 330 + width: 350, + height: 550 }); }); @@ -305,8 +305,7 @@ var Scann3r = { } } } - } - , + }, gallery: { init: (sio) => { @@ -314,25 +313,34 @@ var Scann3r = { this.template = $('#thumb-template').clone(); $('#thumb-template').remove(); }, - loadPage: + loadPage: function (page) { + let x = Scann3r.sio.emit('getProjects', 0, 100, (r) => { + for (let n in r) { + this.append(this.createThumb(r[n])); + } + }); + }, - function (page) { - let x = Scann3r.sio.emit('getProjects', 0, 100, (r) => { - for (let n in r) { - this.append(this.createThumb(r[n])); - } - }); - } - , createThumb: (data) => { + let humanReadableFilesize = (bytes) => { + if (bytes < 1024) { + return bytes + ' bytes'; + } else if (bytes < 1024 * 1024) { + return (bytes / 1024).toFixed(1) + ' KB'; + } else if (bytes < 1024 * 1024 * 1024) { + return (bytes / 1024 / 1024).toFixed(1) + ' MB'; + } else { + return (bytes / 1024 / 1024 / 1024).toFixed(1) + ' GB'; + } + }; + + let t = this.template.clone(); t.attr('id', 'foo'); t.addClass('thumb-' + data.id); - t.find('.thumbnail-image').attr('src', data.thumb); - t.find('.thumbnail-text').text('#' + data.id); - t.find('.zip').attr('href', '/' + data.id + '/images-' + data.id + '.zip'); - t.find('.trash').click(function () { + + t.find('.t-delete').click(() => { if (confirm('Are you sure?')) { Scann3r.sio.emit('delete', data.id, (err, r) => { if (err) { @@ -344,7 +352,14 @@ var Scann3r = { } }); - t.find('.cloud').click(function () { + console.log(data); + + t.find('.infolist').append(`
  • ${data.rotorCount}x${data.turntableCount} Images
  • `); + t.find('.infolist').append(`
  • ${humanReadableFilesize(data.zipSize)}
  • `); + t.find('.infolist').append(`
  • ${data.range}
  • `); + + + t.find('.t-cloud-up').click(() => { Scann3r.sio.emit('proxy', data.id, (err, r) => { if (err) { @@ -352,10 +367,17 @@ var Scann3r = { } }); }); + + + t.find('.thumbnail-image').attr('src', data.thumb); + t.find('.thumbnail-text').text('#' + data.id); + t.find('.t-download').attr('href', '/' + data.id + '/images-' + data.id + '.zip'); + + if (typeof data.resultZip != 'undefined') { - t.find('.resultZip').attr('href', '/' + data.id + '/' + data.resultZip); + t.find('.t-cloud-down').attr('href', '/' + data.id + '/' + data.resultZip); } else { - t.find('.resultZip').hide(); + t.find('.t-cloud-down').hide(); } return t; }, diff --git a/src/index.js b/src/index.js index 7433fd0..8664d55 100644 --- a/src/index.js +++ b/src/index.js @@ -46,10 +46,10 @@ if (!fs.existsSync(configfile)) { registry.set('turntable', new Stepper(config.data.turntable, gpio.turntableStep, gpio.turntableDir, gpio.turntableEnable)); registry.set('rotor', new Stepper(config.data.rotor, gpio.rotorStep, gpio.rotorDir, gpio.rotorEnable)); registry.set('camera', new Camera(registry)); + registry.set('proxy', new Proxy(registry)); registry.set('webServer', new WebServer(registry)); registry.set('webSocket', new WebSocket(registry)); registry.set('notification', new Notification(registry)); - registry.set('proxy', new Proxy(registry)); log.info('Ready!'); } catch(e) { diff --git a/src/lib/Project.js b/src/lib/Project.js index 45f13c5..210d27f 100644 --- a/src/lib/Project.js +++ b/src/lib/Project.js @@ -48,6 +48,7 @@ class Project { let id = await r.incrby('projectId', 1); await r.lpush('projects', id); await r.hset(`project:${id}`, 'id', id); + await r.hset(`project:${id}`, 'date', new Date().toISOString() ); let p = new Project(id, registry); p.createFolder(); return p; diff --git a/src/lib/Scan.js b/src/lib/Scan.js index 6187c96..403a77f 100644 --- a/src/lib/Scan.js +++ b/src/lib/Scan.js @@ -1,5 +1,6 @@ const {exec} = require('child_process'); const Project = require('./Project.js'); +const fs = require('fs').promises; class Scan { @@ -19,20 +20,20 @@ class Scan { let config = this.registry.get('config'); this.config = config; this.currentProgress = { - photo: '-', + photo: '', text: '', percent: 0 } - let rotorAngleFrom = config.get('rotorAngleRangeToScan.values')[0]; - let rotorAngleTo = config.get('rotorAngleRangeToScan.values')[1]; + this.rotorAngleFrom = config.get('rotorAngleRangeToScan.values')[0]; + this.rotorAngleTo = config.get('rotorAngleRangeToScan.values')[1]; let rotorAnglesPerScan = config.get('rotorAnglesPerScan.value'); let imagesPerRevision = config.get('imagesPerRevision.value'); this.turntableRangeMax = config.get('turntable.range.max'); - this.rotorStart = rotorAngleFrom; - this.rotorSteps = Math.floor((rotorAngleTo - rotorAngleFrom) / rotorAnglesPerScan); + this.rotorStart = this.rotorAngleFrom; + this.rotorSteps = Math.floor((this.rotorAngleTo - this.rotorAngleFrom) / rotorAnglesPerScan); this.turntableSteps = Math.floor(this.turntableRangeMax / imagesPerRevision); this.rotorCurrent = 0; @@ -46,6 +47,12 @@ class Scan { async start() { this.project = await Project.create(this.registry); + this.project.set('rotorCount', this.rotorCount+1); + this.project.set('turntableCount', this.turntableCount+1); + this.project.set('crop', JSON.stringify(this.cropValues)); + this.project.set('range', this.rotorAngleFrom + "° .. " + this.rotorAngleTo + "°"); + + this.progress({text: `Starting project #${this.project.id}.`}); this.progress({text: `Moving rotor and turntable to start position.`}); @@ -117,9 +124,13 @@ class Scan { async zip() { this.progress({text: `Creating ZIP file.`}); let filesToZip = this.project.getPath('cropped') + '*.jpg'; - let zipFile = this.project.getPath() + 'images-' + this.project.id + '.zip'; + let zipFile = this.project.getZipFileLocation(); let cmd = `/usr/bin/zip -j ${zipFile} ${filesToZip}`; - return this._exec(cmd); + await this._exec(cmd); + console.log(zipFile); + let stats = await fs.stat( zipFile); + let size = stats.size; + this.project.set('zipSize', size); } async crop(filename) { diff --git a/src/modules/Camera.js b/src/modules/Camera.js index 0f5415b..96c49f4 100644 --- a/src/modules/Camera.js +++ b/src/modules/Camera.js @@ -1,14 +1,10 @@ const PiCamera = require('pi-camera'); -const fs = require('fs'); -const {exec} = require("child_process"); const log = require('../lib/Log.js').createLogger({name: 'Camera'}); function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } -var snapCount = 0; - class Camera { onPreviewDone(img) { @@ -72,7 +68,7 @@ class Camera { let image = await this.camPreview.snapDataUrl(); this.onPreviewDone(image); } catch (e) { - console.log('Could not snap preview', e); + log.error('Could not take preview picture ' + e.message); } this.snapping = false; if (this.refresh) { @@ -91,7 +87,7 @@ class Camera { this.initCam(); await this.camProd.snap(); } catch (e) { - console.log('Could not snap image', e); + log.error('Could not take picture ' + e.message); } this.snapping = false; } @@ -107,7 +103,6 @@ class Camera { this.snapPreview(); } } - } async stopPreview() { diff --git a/src/modules/Proxy.js b/src/modules/Proxy.js index bf17d9f..607d839 100644 --- a/src/modules/Proxy.js +++ b/src/modules/Proxy.js @@ -17,17 +17,18 @@ function sleep(ms) { class Proxy { onInfo(msg) { - // @todo abhängigkeiten nochmal irgendwie sauberer lösen - this.registry.get('webSocket').toast(msg); } + onRegistered(msg) { + } + + info(msg) { this.onInfo(msg); } constructor(registry) { log.info("Instanciating Proxy " + registry.get('config').get('version')); - this.io = registry.get('webSocket').io; this.registry = registry; this.files = {}; this.socket = null; @@ -35,7 +36,19 @@ class Proxy { } maintenance() { - console.log(Object.keys(this.files).length); + if ( this.socket != null && Object.keys(this.files).length == 0) { + this.socket.close(); + this.socket = null; + this.files = {}; + } + } + + removeAllFiles() { + + } + + removeFile(uid) { + } async addFile(project) { @@ -62,7 +75,7 @@ class Proxy { await this.connectToProxy(); this.socket.emit('register', uuid, size, (err, r) => { if (!err) { - this.io.emit('proxy', r); + this.onRegistered(r); this.files[uuid].status = STATUS_REGISTERED; } else { this.registry.get('notification').notify('error-' + uuid, 'Error', 'Proxy error: ' + err, 0, false); @@ -83,7 +96,7 @@ class Proxy { return; } - this.socket = client.connect("ws://mc.sui.li:808", { + this.socket = client.connect(this.registry.get('config').get('proxy.url'), { path: '/ws', reconnection: false, timeout: 2000, @@ -100,9 +113,14 @@ class Proxy { this.socket.on('disconnect', () => { console.log('proxy disconnected'); + this.files = {}; this.socket = null; }); + this.socket.on('expired',(uid) => { + delete this.files[uid]; + }); + this.socket.on('getData', async (uid, cb) => { let me = this.files[uid]; let buffer = Buffer.alloc(BUFFER_SIZE); @@ -154,7 +172,6 @@ class Proxy { } } - throw new Error('noooo'); } } diff --git a/src/modules/Stepper.js b/src/modules/Stepper.js index 57ee839..0bb4c38 100644 --- a/src/modules/Stepper.js +++ b/src/modules/Stepper.js @@ -5,14 +5,12 @@ let timeoutInterval = null; class Stepper { + onTurn(step) { + } constructor(config, step, dir, enable) { - - - this.onTurn = function(step) {}; - this._config = config; this._abort = false; this._steps = config.value; @@ -26,9 +24,6 @@ class Stepper { this._step.write(0); this._dir.write(0); - - - if (enable !== null) { let pin = enable._gpio; if (typeof enableTimeouts['e' + this._enablePin] == 'undefined') { @@ -43,12 +38,6 @@ class Stepper { setInterval(this.autoToggleDisable, 1000); } } - - } - - - get turning() { - return this._turning; } autoToggleDisable() { @@ -83,10 +72,10 @@ class Stepper { if (this._steps > this._targetSteps) { this._steps--; - this._dir.write( this._config.invert?0:1 ); + this._dir.write(this._config.invert ? 0 : 1); } else if (this._steps < this._targetSteps) { this._steps++; - this._dir.write( this._config.invert?1:0 ); + this._dir.write(this._config.invert ? 1 : 0); } else { this._turning = false; this.onTurn(this._steps); @@ -94,17 +83,16 @@ class Stepper { return; } - if (this._steps % 250== 0) { + if (this._steps % 250 == 0) { this.onTurn(this._steps); } this._step.write(1); - var timer = new NanoTimer(); + var timer = new NanoTimer(); timer.setTimeout(() => { this._step.write(0); - timer.setTimeout(() => this._turn( res), [timer], '500u'); + timer.setTimeout(() => this._turn(res), [timer], '500u'); }, [timer], '500u'); - } setHome() { diff --git a/src/modules/WebServer.js b/src/modules/WebServer.js index c581f50..999d257 100644 --- a/src/modules/WebServer.js +++ b/src/modules/WebServer.js @@ -23,7 +23,7 @@ class WebServer { //log.info(req.method + ' ' + req.url); let file = '' + req.url.replace(/\?.*$/, ''); - if (file == '' || file.match(/\/$/)) { + if (file === '' || file.match(/\/$/)) { file += 'index.html'; } @@ -34,7 +34,6 @@ class WebServer { res.end(); } else if (fs.existsSync('./public_html' + file)) { let ext = file.split('.').pop(); - res.statusCode = 200; const page = fs.readFileSync('./public_html' + file); res.setHeader('Content-Type', _.get(mimeTypes, ext, 'application/octetstream')); @@ -42,7 +41,6 @@ class WebServer { res.end(); } else if (fs.existsSync(registry.get('config').get('misc.projectsFolder') + file)) { let ext = file.split('.').pop(); - res.statusCode = 200; const page = fs.readFileSync(registry.get('config').get('misc.projectsFolder') + file); res.setHeader('Content-Type', _.get(mimeTypes, ext, 'application/octetstream')); @@ -54,6 +52,7 @@ class WebServer { res.write("404 not found\n"); res.end(); } + log.info(`${req.method} ${req.url} - ${req.connection.remoteAddress} - ${res.statusCode}`); } ).listen(port); diff --git a/src/modules/WebSocket.js b/src/modules/WebSocket.js index e547571..4107d1a 100644 --- a/src/modules/WebSocket.js +++ b/src/modules/WebSocket.js @@ -1,6 +1,5 @@ const socketIo = require('socket.io'); const Scan = require('../lib/Scan.js'); -const fs = require('fs'); const log = require('../lib/Log.js').createLogger({name: 'WebSocket'}); const rimraf = require('rimraf'); const Project = require('../lib/Project'); @@ -18,13 +17,13 @@ class WebSocket { this.sliderAction = { rotor: (type, value) => { - if (type == 'slide') { + if (type === 'slide') { return; } this.registry.get('rotor').turnTo(value); }, turntable: (type, value) => { - if (type == 'slide') { + if (type === 'slide') { return; } this.registry.get('turntable').turnTo(value); @@ -45,23 +44,24 @@ class WebSocket { light: (type, value) => { this.registry.get('config').set('light.value', value); - this.registry.get('gpio').light1.write(((value == 1 || value == 2) * 1)); - this.registry.get('gpio').light2.write((value == 2) * 1); + value = value * 1; + this.registry.get('gpio').light1.write(((value === 1 || value === 2) * 1)); + this.registry.get('gpio').light2.write((value === 2) * 1); }, imagesPerRevision: (type, value) => { - if (type == 'slide') { + if (type === 'slide') { return; } this.registry.get('config').set('imagesPerRevision.value', value); }, rotorAnglesPerScan: (type, value) => { - if (type == 'slide') { + if (type === 'slide') { return; } this.registry.get('config').set('rotorAnglesPerScan.value', value); }, rotorAngleRangeToScan: (type, value) => { - if (type == 'slide') { + if (type === 'slide') { return; } this.registry.get('config').set('rotorAngleRangeToScan.values', value); @@ -85,6 +85,14 @@ class WebSocket { this.registry.get('camera').onPreviewDone = (file) => { this.io.emit('updateCameraPreview', file); } + + this.registry.get('proxy').onInfo = (msg) => { + this.io.emit('toast', {id: id, heading: heading, text: text, percent: percent}); + } + + this.registry.get('proxy').onRegistered = (data) => { + this.io.emit('proxy', data); + } } @@ -103,7 +111,7 @@ class WebSocket { socket.emit('initSlider', slider, options); } - console.log( this.config.get('version') ); + console.log(this.config.get('version')); socket.emit('info', 'info-version', this.config.get('version')); socket.emit('imgArea', this.config.get('crop.values')); socket.emit('invert', this.config.get('rotor.invert')); @@ -216,8 +224,9 @@ class WebSocket { } toast(id, heading, text, percent) { - this.io.emit('toast', {id: id, heading: heading, text: text, percent:percent} ); + this.io.emit('toast', {id: id, heading: heading, text: text, percent: percent}); } + toastClose(id) { this.io.emit('toastClose', id); }