Skip to content

Commit

Permalink
feat: use l-s update feature
Browse files Browse the repository at this point in the history
  • Loading branch information
josepfo committed Jul 21, 2022
1 parent 34c49f9 commit 151909d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) })
}
Expand Down Expand Up @@ -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()
}
Expand Down
15 changes: 13 additions & 2 deletions lib/trader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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) {
Expand All @@ -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 = {}
Expand Down
64 changes: 64 additions & 0 deletions lib/update/new-update-handler.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 151909d

Please sign in to comment.