diff --git a/lib/brain.js b/lib/brain.js index 10aa012770..8f0d6be317 100644 --- a/lib/brain.js +++ b/lib/brain.js @@ -41,6 +41,7 @@ const facephoto = require('./compliance/flows/facephoto') const sanctions = require('./compliance/flows/sanctions') const usSsn = require('./compliance/flows/US-SSN') const customTier = require('./compliance/flows/custom-info-request') +const updater = require('./update/new-update-handler') const { getLowestAmountPerRequirement, getAmountToHardLimit, getTriggered } = require('./compliance/triggers/triggers') const { ORDERED_REQUIREMENTS, REQUIREMENTS } = require('./compliance/triggers/consts') @@ -142,6 +143,7 @@ const Brain = function (config) { this.manualTriggersDataProvided = _.zipObject(AUTOMATABLE_REQUIREMENTS, Array(AUTOMATABLE_REQUIREMENTS.length).fill(false)) this.txBlockedByManualTrigger = false this.termsAcceptButtonPressed = false + this.updater = updater this.numberOfCassettes = _.isFinite(parseInt(deviceConfig.billDispenser.cassettes)) ? deviceConfig.billDispenser.cassettes @@ -499,6 +501,7 @@ Brain.prototype._initTraderEvents = function _initTraderEvents () { this.trader.on('error', function (err) { console.log(err.stack) }) this.trader.on('unpair', function () { self._unpair() }) this.trader.on('reboot', function () { self._reboot() }) + this.trader.on('update', function () { self._update() }) this.trader.on('shutdown', function () { self._shutdown() }) this.trader.on('restartServices', function () { self._restartServices('Remote restart services', true) }) } @@ -3567,6 +3570,15 @@ Brain.prototype._shutdown = function _shutdown () { }) } +Brain.prototype._update = function _update () { + // can only run if machine is no in the middle of a transaction + if (!this.isStaticState()) { + console.log('In the middle of a transaction. Will update when it is done...') + return + } + this.updater.handleUpdateRequest(this.trader) +} + Brain.prototype._abortTransaction = function _abortTransaction () { this._idle() } diff --git a/lib/trader.js b/lib/trader.js index adcc68f9da..ba5b1a1276 100644 --- a/lib/trader.js +++ b/lib/trader.js @@ -622,6 +622,7 @@ Trader.prototype.postPollEvents = function postPollEvents (res) { } if (res.reboot) this.emit('reboot') + if (res.update) this.emit('update') if (res.shutdown) this.emit('shutdown') if (res.restartServices) this.emit('restartServices') this.emit('pollUpdate', isNewState(this)) @@ -889,7 +890,7 @@ Trader.prototype.networkHeartbeat = function networkHeartbeat (obj) { body: obj, method: 'POST' }) - .catch(err => console.error('Failed to send network heartbeat', err)) + .catch(err => console.error('Failed to send network heartbeat', err)) } Trader.prototype.networkPerformance = function networkPerformance (obj) { @@ -898,7 +899,17 @@ Trader.prototype.networkPerformance = function networkPerformance (obj) { body: obj, method: 'POST' }) - .catch(err => console.error('Failed to send network performance', err)) + .catch(err => console.error('Failed to send network performance', err)) +} + +Trader.prototype.machineUpdate = function machineUpdate (obj, downloadHandler) { + this.request({ + path: '/update', + body: obj, + method: 'GET' + }) + .then(res => downloadHandler(res)) + .catch(() => console.error('Failed to download package!')) } let oldState = {} diff --git a/lib/update/new-update-handler.js b/lib/update/new-update-handler.js new file mode 100644 index 0000000000..e106434f6d --- /dev/null +++ b/lib/update/new-update-handler.js @@ -0,0 +1,64 @@ +const fs = require('fs') +const path = require('path') +const _ = require('lodash/fp') + +const SOFTWARE_CONFIG_PATH = path.resolve(__dirname, '../../software_config.json') +const DEVICE_CONFIG_PATH = path.resolve(__dirname, '../../device_config.json') +const softwareConfig = JSON.parse(fs.readFileSync(SOFTWARE_CONFIG_PATH)) +const deviceConfig = JSON.parse(fs.readFileSync(DEVICE_CONFIG_PATH)) +let config = _.merge(softwareConfig, deviceConfig) +config.updater.dataPath = config.brain.dataPath + +const downloadDir = config.downloadDir +const packagePath = config.downloadDir + '/update.tar' + +var NewUpdater = function (config) { + this.config = config + // this.extractor = require('./extractor').factory(this.config.extractor) + this.downloading = false + this.finished = null +} + +NewUpdater.prototype._init = function _init () { + if (fs.existsSync(downloadDir)) { + if (fs.existsSync(packagePath)) fs.unlinkSync(packagePath) + } else { + fs.mkdirSync(downloadDir) + } +} + +const downloadHandler = res => { + try { + var packagePath = this.config.downloadDir + '/update.tar' + var fileOut = fs.createWriteStream(packagePath) + res.pipe(fileOut) + res.on('end', function () { + if (res.complete) { + console.log('IS COMPLETE!') + } else { + console.log('[WARN]: The update file we\'ve received is incomplete. ' + + 'The connection was terminated before the whole content ' + + 'was transmitted. The update will NOT continue.') + } + }) + res.on('error', () => console.log('ERROR')) + var file = fs.createWriteStream('/tmp/node/test.gz') + res.on('data', function (chunk) { + file.write(chunk) + }).on('end', function () { + file.end() + }) + } catch (err) { + console.log(err) + } +} + +NewUpdater.prototype.handleUpdateRequest = function handleUpdateRequest (trader) { + trader.machineUpdate({ updateAcceptance: true }, downloadHandler) + this.downloading = true +} + +NewUpdater.factory = config => new NewUpdater(config.updater) +const updater = NewUpdater.factory(config) + +module.exports = updater