Skip to content

Commit

Permalink
resolve signalTimeout on completion
Browse files Browse the repository at this point in the history
  • Loading branch information
splitice committed Sep 7, 2023
1 parent b8703ef commit 81df8cb
Showing 1 changed file with 59 additions and 55 deletions.
114 changes: 59 additions & 55 deletions lib/Af.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,73 +264,77 @@ class Af extends EventEmitter {

let shouldResend = false
let signalTimeout = Q.defer()
let areqCancelable = this.waitFor(afEventCnf, areqTimeout)
areqCancelable.catch(() => { })

const startAreq = async () => {
let cnf

do {
/* resend without src rtg */
cnf = await areqCancelable
cnf = cnf[0]
if (cnf.status === 0 || cnf.status === 'SUCCESS') { // success
this.emit('ind:dataConfirm', { dstEp, afParams });
return cnf
}
try {
let areqCancelable = this.waitFor(afEventCnf, areqTimeout)
areqCancelable.catch(() => { })

const startAreq = async () => {
let cnf

do {
/* resend without src rtg */
cnf = await areqCancelable
cnf = cnf[0]
if (cnf.status === 0 || cnf.status === 'SUCCESS') { // success
this.emit('ind:dataConfirm', { dstEp, afParams });
return cnf
}

if (shouldResend) {
shouldResend = false
areqCancelable = this.waitFor(afEventCnf, areqTimeout)
areqCancelable.catch(() => { })
await afSend()
}
} while (shouldResend)
if (shouldResend) {
shouldResend = false
areqCancelable = this.waitFor(afEventCnf, areqTimeout)
areqCancelable.catch(() => { })
await afSend()
}
} while (shouldResend)

lastError = new Error(format_af_error('AF:dataRequest fails, status code: ', cnf.status))
if (shouldIndirect) {
if (cnf.status == ZSC.cmdStatus.APS_NO_ACK || cnf.status == ZSC.cmdStatus.MAC_TRANSACTION_EXPIRED) { // || cnf.status == 205
signalTimeout.resolve("resend")
}
if (cnf.status != 205) {
lastError = new Error(format_af_error('AF:dataRequest fails, status code: ', cnf.status))
if (shouldIndirect) {
if (cnf.status == ZSC.cmdStatus.APS_NO_ACK || cnf.status == ZSC.cmdStatus.MAC_TRANSACTION_EXPIRED) { // || cnf.status == 205
signalTimeout.resolve("resend")
}
if (cnf.status != 205) {
throw lastError
}
} else {
throw lastError
}
} else {
throw lastError
}
}

let isResend = false
const afSend = async () => {
let rsp
if (!isBroadcast && dstEp.getSrcRtg && !isResend) {
const srcRtg = dstEp.getSrcRtg()
if (srcRtg) {
const newAfParams = Object.assign(Af.buildAfSrcRtg(srcRtg), afParams, { options: afParams.options & ~ZSC.AF.options.DISC_ROUTE })
rsp = await controller.request('AF', 'dataRequestSrcRtg', newAfParams)
shouldResend = true
let isResend = false
const afSend = async () => {
let rsp
if (!isBroadcast && dstEp.getSrcRtg && !isResend) {
const srcRtg = dstEp.getSrcRtg()
if (srcRtg) {
const newAfParams = Object.assign(Af.buildAfSrcRtg(srcRtg), afParams, { options: afParams.options & ~ZSC.AF.options.DISC_ROUTE })
rsp = await controller.request('AF', 'dataRequestSrcRtg', newAfParams)
shouldResend = true
}
}
if (!rsp) {
rsp = await controller.request('AF', 'dataRequest', afParams) // status code handled by catch
}
isResend = true
}
if (!rsp) {
rsp = await controller.request('AF', 'dataRequest', afParams) // status code handled by catch
}
isResend = true
}

const indirectSendFn = async () => {
try {
await afSend()
return await startAreq()
} catch (ex) {
areqCancelable.cancel()
throw ex
const indirectSendFn = async () => {
try {
await afSend()
return await startAreq()
} catch (ex) {
areqCancelable.cancel()
throw ex
}
}
}

if (!shouldIndirect) {
await indirectSendFn()
if (!shouldIndirect) {
await indirectSendFn()
}
return await controller.indirectSend(indirectSendFn, { signalTimeout, indirectTimeout: this.indirectTimeout, dstAddr, retries: opt.retries })
} finally {
signalTimeout.resolve(null)
}
return await controller.indirectSend(indirectSendFn, { signalTimeout, indirectTimeout: this.indirectTimeout, dstAddr, retries: opt.retries })
}

async _zclFoundation(srcEp, dstEp, cId, cmd, zclData, cfg) {
Expand Down

0 comments on commit 81df8cb

Please sign in to comment.