Skip to content

Commit

Permalink
feat: enable replication count selector, error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
antonfisher committed Nov 26, 2018
1 parent ebec1f6 commit bb467fd
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 26 deletions.
20 changes: 14 additions & 6 deletions src/application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class Application extends EventEmitter {
this.run();
}

setReplicationCount(value) {
this.replicationCount = value;
if (this.replicator) {
this.replicator.setReplicationCount(this.replicationCount);
}
}

run() {
this.devicesManager = new DevicesManager({devicesPath: this.devicesPath})
.on(DevicesManager.EVENTS.ERROR, (message) => this.logger.error(`[DevicesManager] ${message}`))
Expand Down Expand Up @@ -69,10 +76,11 @@ class Application extends EventEmitter {
this.replicator.onFileUpdate(dev, relativePath)
);

this.logger.info('application is ready');
this.emit(Application.EVENTS.READY);

this.startFtpServer();
setTimeout(() => {
this.logger.info('application is ready');
this.emit(Application.EVENTS.READY);
//this.startFtpServer();
}, 100);
}

startFtpServer() {
Expand All @@ -91,8 +99,8 @@ class Application extends EventEmitter {
getRoot: () => '/'
});

this.ftpServer.on('error', (error) => {
this.logger.error('FTP Server error:', error);
this.ftpServer.on('error', (error, a, b) => {
this.logger.error('FTP Server error:', error, a, b);
});

this.ftpServer.on('client:connected', (connection) => {
Expand Down
10 changes: 9 additions & 1 deletion src/hardware/Display.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Display extends PhatDisplayWrapper {
_startUpdater() {
this._stopUpdater();
this._updaterInterval = setInterval(() => {
if (this.buffer[this.selected]) {
if (typeof this.buffer[this.selected] !== 'undefined') {
this.writeString(this.buffer[this.selected]);
} else {
this.writeString(`?${this.selected}`);
Expand Down Expand Up @@ -92,6 +92,14 @@ class Display extends PhatDisplayWrapper {
clearInterval(this._clockInterval);
this._clockInterval = null;
}

destroy() {
this._stopClock();
this._stopScrollIp();
this._stopUpdater();

super.destroy();
}
}

module.exports = Display;
5 changes: 0 additions & 5 deletions src/hardware/SelectorReplications.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ class SelectorReplications extends Selector {
constructor() {
super(PIN_MAP);
}

getValue() {
const pin = super.getSelected();
return OPTIONS[pin];
}
}

SelectorReplications.OPTIONS = OPTIONS;
Expand Down
7 changes: 4 additions & 3 deletions src/hardware/drivers/Led.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const rpio = require('rpio');

const BLINK_ONCE_DURATION = 500;
const BLINK_DURATION = 500;
const BLINK_ONCE_DURATION = 333;

class Led {
constructor(pin) {
Expand Down Expand Up @@ -41,8 +42,8 @@ class Led {
clearTimeout(this._blinkTimeout);
this._blinkTimeout = setTimeout(() => {
rpio.write(this.pin, rpio.LOW);
}, 0);
}, 10);
}, BLINK_DURATION);
}, BLINK_DURATION * 2);
} else {
rpio.write(this.pin, rpio.LOW);
}
Expand Down
2 changes: 1 addition & 1 deletion src/hardware/drivers/PhatDisplayWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PhatDisplayWrapper {

destroy() {
this.clear();
this._pythonDriverProcess.kill();
setTimeout(() => this._pythonDriverProcess.kill(), 100);
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/hardware/drivers/Selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class Selector extends EventEmitter {
rpio.open(pin, rpio.INPUT, rpio.PULL_DOWN);
rpio.poll(pin, () => this._onSelect(pin), rpio.POLL_HIGH);
if (rpio.read(pin)) {
setTimeout(() => this._onSelect(pin), 10);
this.selectedPin = pin;
//setTimeout(() => this._onSelect(pin), 10);
}
});
}
Expand All @@ -38,6 +39,10 @@ class Selector extends EventEmitter {
return this.selectedPin;
}

getValue() {
return this.pinsMap[this.selectedPin];
}

_onSelect(pin) {
if (this.selectedPin !== pin) {
this.selectedPin = pin;
Expand Down
4 changes: 2 additions & 2 deletions src/hardware/drivers/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class Switch extends EventEmitter {
}
rpio.open(this.pin, rpio.INPUT, rpio.PULL_DOWN);
rpio.poll(this.pin, () => this._onSwitch(), rpio.POLL_BOTH);
//setTimeout(() => this._onSwitch(), 10);
this.isOn = rpio.read(this.pin);
}

getState() {
getValue() {
return this.isOn;
}

Expand Down
6 changes: 3 additions & 3 deletions src/hardware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class Hardware extends EventEmitter {
});

this.selectorReplications = new SelectorReplications();
this.selectorReplications.on('select', (operation) => {
this.display.writeString(`RS:${operation}`); // debug
});
// this.selectorReplications.on('select', (operation) => {
// this.display.writeString(`RS:${operation}`); // debug
// });

this.switchOnOff = new SwitchOnOff();
this.switchOnOff.on('switch', (value) => {
Expand Down
38 changes: 34 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,43 @@ const {version, description, homepage} = require('../package.json');
const logger = require('./logger');
const Hardware = require('./hardware');
const Application = require('./application');
const Replicator = require('./application/replicator');
const parseCliArgs = require('./parseCliArgs');

let hardware;
let application;

function onUnhandledError(err) {
try {
hardware.display.writeString('panic');
} catch (e) {
const message = 'WARN: cannot use display:';
try {
logger.warn(message, e);
} catch (loggerE) {
console.log(message, e);
}
}
try {
cleanUp();
logger.error(err);
} catch (e) {
console.log('LOGGER ERROR', e); //eslint-disable-line no-console
console.log('ERROR', err); //eslint-disable-line no-console
}
process.exit(1);
setTimeout(() => {
process.exit(1);
}, 1000);
}

process.on('unhandledRejection', onUnhandledError);
process.on('uncaughtException', onUnhandledError);
process.on('SIGINT', function() {
logger.info('Exit...');
cleanUp();
process.exit();
setTimeout(() => {
process.exit();
}, 1000);
});

function cleanUp() {
Expand Down Expand Up @@ -66,7 +81,8 @@ parseCliArgs(
let replicationCount;
if (rpi) {
hardware = new Hardware();
hardware.ledIO.setValue(true);
hardware.ledIO.setBlink(true);
hardware.display.setMode(hardware.selectorDisplay.getValue());
replicationCount = hardware.selectorReplications.getValue();
}

Expand All @@ -77,7 +93,12 @@ parseCliArgs(
});
application.on(Application.EVENTS.READY, () => {
if (rpi) {
hardware.ledIO.setValue(false);
if (hardware.switchOnOff.getValue()) {
application.startFtpServer();
}
hardware.ledIO.setBlink(false);
} else {
application.startFtpServer();
}
});

Expand All @@ -96,6 +117,15 @@ parseCliArgs(
}, 1000);
}
});

application.replicator
.on(Replicator.EVENTS.REPLICATION_STARTED, () => hardware.ledIO.setBlink(true))
.on(Replicator.EVENTS.REPLICATION_STARTED, () => hardware.ledIO.setBlink(false));

hardware.selectorReplications.on('select', (value) => {
logger.info(`[USER] changed replication count: ${value}`);
application.setReplicationCount(value);
});
}
}
);

0 comments on commit bb467fd

Please sign in to comment.