Skip to content

Commit

Permalink
Merge pull request #1166 from siiky/refactor/lam-1133/cleanup
Browse files Browse the repository at this point in the history
LAM-1133 review GM SDK usage
  • Loading branch information
RafaelTaranto authored Aug 22, 2024
2 parents 9e8e8b7 + 9836535 commit e7876ac
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 929 deletions.
7 changes: 3 additions & 4 deletions lib/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ Brain.prototype._initBillValidatorEvents = function _initBillValidatorEvents ()
billValidator.on('stackerOpen', function () { self._stackerOpen() })
billValidator.on('stackerClosed', function () { self._idle() })
billValidator.on('actionRequiredMaintenance', function () { self.actionRequiredMaintenance() })
billValidator.on('enabled', function (data) { self._billsEnabled(data) })
billValidator.on('enabled', function () { self._billsEnabled() })
billValidator.on('cashSlotRemoveBills', () => this._cashSlotRemoveBills())
billValidator.on('leftoverBillsInCashSlot', () => this._leftoverBillsInCashSlot())
}
Expand Down Expand Up @@ -2753,6 +2753,7 @@ Brain.prototype._startAddressScan = function _startAddressScan () {
clearTimeout(this.screenTimeout)
this.startDisabled = false

if (err && err.message === 'Timeout') return this._idle()
if (err && err.message === 'Invalid address') return this._invalidAddress()
if (err && err.message === 'Non-zero amount invoice supplied.') return this._invalidAddress(true)
if (err) this.emit('error', err)
Expand Down Expand Up @@ -3404,10 +3405,8 @@ Brain.prototype._billJam = function _billJam () {
this.browser().send({ action: 'networkDown' })
}

Brain.prototype._billsEnabled = function _billsEnabled (data) {
Brain.prototype._billsEnabled = function _billsEnabled () {
this.billValidatorErrorFlag = false // If enabled, previous errors are no longer an issue
console.log('Bills enabled codes: 0x%s, 0x%s', data.data1.toString(16),
data.data2.toString(16))
}

Brain.prototype._stackerOpen = function _stackerOpen () {
Expand Down
183 changes: 102 additions & 81 deletions lib/genmega/genmega-dispenser/genmega-dispenser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var BillDispenser = function (config) {
this.initializing = false
this.device = config.device
this.license = config.license
this.type = 'genmega'
this.deviceType = null
this.numberOfCassettes = null
}
Expand All @@ -23,8 +22,18 @@ BillDispenser.factory = function factory (config) {

module.exports = BillDispenser

BillDispenser.prototype._handleError = function _handleError (iRet) {
throw new Error(returnValuesTable[iRet.toString()])
BillDispenser.prototype._makeError = function _makeError (iRet) {
return iRet < 0 ?
new Error(returnValuesTable[iRet]) :
null
}

BillDispenser.prototype._throwError = function _throwError (error) {
if (error) throw error
}

BillDispenser.prototype._throwMakeError = function _throwMakeError (iRet) {
this._throwError(this._makeError(iRet))
}

BillDispenser.prototype._setup = function _setup (data, statuses) {
Expand All @@ -41,6 +50,14 @@ BillDispenser.prototype._isRecycler = function _isRecycler () {
return this.deviceType === '1'
}

BillDispenser.prototype._status = function _status () {
const { iRet, result: statuses } = genmega.CDUStatus()
return {
error: this._makeError(iRet),
statuses,
}
}

BillDispenser.prototype.init = function init (data) {
return new Promise((resolve) => {
if (this.initializing || this.initialized) return resolve()
Expand All @@ -50,11 +67,8 @@ BillDispenser.prototype.init = function init (data) {
this._verifyLicenseKey(this.license)
this._openSerialPort(this.device)

const { iRet, result: statuses } = genmega.CDUStatus()

if (iRet < 0) {
throw new Error(returnValuesTable[iRet.toString()])
}
const { error, statuses } = this._status()
this._throwError(error)

this._setup(data, statuses)

Expand All @@ -66,53 +80,73 @@ BillDispenser.prototype.init = function init (data) {
})
}

BillDispenser.prototype.dispense = function dispense (notes) {
return new Promise((resolve) => {
let error = null
if (this._isDispenser()) {
const { iRet: iRetDispense, result } = genmega.CDUDispense(notes, this.numberOfCassettes)
if (iRetDispense < 0) {
this.close()
error = new Error(returnValuesTable[iRetDispense.toString()])
// If the operation failed all the bills were rejected
return resolve({ value: this._processDispenseResult(result, notes), error })
}
return resolve({ value: this._processDispenseResult(result, notes) })
BillDispenser.prototype._dispense = function (notes) {
const { iRet, result } = genmega.CDUDispense(notes, this.numberOfCassettes)
return {
value: this._processDispenseResult(result, notes),
error: this._makeError(iRet),
}
}

BillDispenser.prototype._present = function () {
return this._makeError(genmega.CDUPresent().iRet)
}

BillDispenser.prototype._retract = function () {
return this._makeError(genmega.CDURetract().iRet)
}

BillDispenser.prototype.dispense = function dispense (notes, _currentBatch, _batchAmount) {
const asDispenser = resolve => {
const ret = this._dispense(notes)
if (ret.error) {
this.close()
return resolve(ret)
}
if (this._isRecycler()) {
const { iRet: iRetDispense, result } = genmega.CDUDispense(notes, this.numberOfCassettes)
if (iRetDispense < 0) {
this.close()
error = new Error(returnValuesTable[iRetDispense.toString()])
}
const dispenseResult = this._processDispenseResult(result, notes)
const { iRet: iRetPreset } = genmega.CDUPresent()
if (iRetPreset < 0) {
error = new Error(returnValuesTable[iRetPreset.toString()])
}

const asRecycler = resolve => {
const ret = this._dispense(notes)
if (ret.error) {
this.close()
return resolve(ret)
}

ret.error = this._present()
if (ret.error) return resolve(ret)

const timeout = setTimeout(() => {
const { error: statusError, statuses } = this._status()
if (ret.error = statusError) {
clearInterval(timeout)
return resolve(ret)
}
let interval = setTimeout(() => {
const { iRet: iRetStatus, data: statuses } = genmega.CDUStatus()
if (iRetStatus < 0) {
error = new Error(returnValuesTable[iRetStatus.toString()])
}
// bills were not retrieved
if (statuses.iShutterRemain) {
const { iRet: iRetRetract } = genmega.CDURetract()
if (iRetRetract < 0) {
error = new Error(returnValuesTable[iRetRetract.toString()])
}
}
// close shutter
const { iRetShutter } = genmega.CDUShutterAction(SHUTTER_ACTION.CLOSE)
if (iRetShutter < 0) {
error = new Error(returnValuesTable[iRetShutter.toString()])

// bills were not retrieved
if (statuses.iShutterRemain === '1') {
ret.error = this._retract()
if (ret.error) {
clearInterval(timeout)
return resolve(ret)
}
clearInterval(interval)
return resolve({ value: dispenseResult, error })
}, 30000)
}
throw new Error('Unknown device type!')
})
}

const { iRet: iRetShutter } = genmega.CDUShutterAction(SHUTTER_ACTION.CLOSE)
ret.error = this._makeError(iRetShutter)

clearInterval(timeout)
return resolve(ret)
}, 30000)
}

const asUnknown = (_resolve, reject) =>
reject(new Error('Unknown device type!'))

return new Promise(
this._isDispenser() ? asDispenser :
this._isRecycler() ? asRecycler :
asUnknown
)
}

BillDispenser.prototype._processDispenseResult = function _processDispenseResult (data, notes) {
Expand All @@ -123,58 +157,45 @@ BillDispenser.prototype._processDispenseResult = function _processDispenseResult
}

BillDispenser.prototype._verifyLicenseKey = function _verifyLicenseKey (license) {
const { iRet } = genmega.CDUVerifyLicenseKey(license)
if (iRet < 0) {
throw new Error(returnValuesTable[iRet.toString()])
}
this._throwMakeError(genmega.CDUVerifyLicenseKey(license).iRet)
}

BillDispenser.prototype._openSerialPort = function _openSerialPort (serialPortName) {
if (!serialPortName) throw new Error('No serial port name provided!')
const { iRet } = genmega.CDUOpen(serialPortName)
if (iRet < 0) {
this._handleError(iRet)
}
this._throwMakeError(genmega.CDUOpen(serialPortName).iRet)
}

BillDispenser.prototype._resetDevice = function _resetDevice (statuses) {
if (this._isDispenser()) {
const { iRet } = genmega.CDUReset(INIT_MODE.NORMAL)
if (iRet < 0) this._handleError(iRet)
return
}
if (this._isDispenser())
return this._throwMakeError(genmega.CDUReset(INIT_MODE.NORMAL).iRet)

if (this._isRecycler()) {
// has cash in shutter
if (statuses.iShutterRemain === '1') {
const { iRet: iRetRetract } = genmega.CDURetract() // retract a cash to reject bin with closing the shutter
const { iRet: iRetReset } = genmega.CDUReset(INIT_MODE.NORMAL)
if (iRetRetract < 0) this._handleError(iRetRetract)
if (iRetReset < 0) this._handleError(iRetReset)
return
this._throwError(this._retract()) // retract a cash to reject bin with closing the shutter
return this._throwMakeError(genmega.CDUReset(INIT_MODE.NORMAL).iRet)
}

// has cash in stacker or transporter
// TODO: if possible test if transporter should be handled this way
if (statuses.iStackerRemain === '1' || statuses.iTransporterRemain === '1') {
const { iRet: iRetForceEject } = genmega.CDUForceEject() // move the detected notes in stacker into eject-ready position
const { iRet: iRetReset } = genmega.CDUReset(INIT_MODE.FORCED) // reject the notes to reject Bin if the notes are on the feeding path
if (iRetForceEject < 0) this._handleError(iRetForceEject)
if (iRetReset < 0) this._handleError(iRetReset)
return
this._throwMakeError(genmega.CDUForceEject().iRet) // move the detected notes in stacker into eject-ready position
return this._throwMakeError(genmega.CDUReset(INIT_MODE.FORCED).iRet) // reject the notes to reject Bin if the notes are on the feeding path
}

if (statuses.iShutterStatus === '1') {
const { iRet } = genmega.CDUShutterAction(SHUTTER_ACTION.CLOSE)
if (iRet < 0) this._handleError(iRet)
this._throwMakeError(genmega.CDUShutterAction(SHUTTER_ACTION.CLOSE).iRet)
}

return
}

throw new Error('Unknown device type!')
}

BillDispenser.prototype._setCassetteNumber = function _setCassetteNumber () {
if (this.numberOfCassettes > MAX_SUPPORTED_CASSETTES) throw new Error('Number of cassettes not supported!')
const { iRet } = genmega.CDUSetCassetteNumber(this.numberOfCassettes)
if (iRet < 0) {
this._handleError(iRet)
}
this._throwMakeError(genmega.CDUSetCassetteNumber(this.numberOfCassettes).iRet)
}

BillDispenser.prototype.close = function close () {
Expand Down
Loading

0 comments on commit e7876ac

Please sign in to comment.