<commit_before>/* jshint node: true */
'use strict';

const EventEmitter = require('eventemitter2'),
      CCZnp = require('cc-znp'),
      ZSC = CCZnp.constants,
      zdoHelper = require('./zdo_helper'),
      ZigbeeError = require('../errors/zigbee_error');

const requestFn = {}

class Zdo extends EventEmitter {
    constructor(controller){
        super()
        this.setMaxListeners(40)
        this._controller = controller;
    }

    /*************************************************************************************************/
    /*** Public APIs                                                                               ***/
    /*************************************************************************************************/
    async request (apiName, valObj) {
        var requestType = zdoHelper.getRequestType(apiName);
    
        var handler = requestFn[requestType]
        if(!handler) {
            throw new Error('Unknown request type.')
        }
        return await handler(this, apiName, valObj)
    }

    
    /*************************************************************************************************/
    /*** Protected Methods                                                                         ***/
    /*************************************************************************************************/
    async _sendZdoRequestViaZnp (apiName, valObj) {
        const rsp = await this._controller._znp.zdoRequest(apiName, valObj)
        if(!rsp){
            throw new Error(`Unable to execute ${apiName}`)
        }
        if (apiName !== 'startupFromApp' && rsp.status)
            throw new ZigbeeError('request unsuccess: ' + rsp.status, rsp.status)
        return rsp
    }
}

requestFn.rspless = function (self, apiName, valObj) {
    return self._sendZdoRequestViaZnp(apiName, valObj);
}

requestFn.generic = async function (self, apiName, valObj) {
    var areq,
        areqEvtKey = zdoHelper.generateEventOfRequest(apiName, valObj),
        abortKey,
        abortPromiseCancelable,
        abortPromise

    if (areqEvtKey) {
        abortKey = zdoHelper.generateDstAddrOfRequest(valObj)
        areq = self._controller.waitFor(areqEvtKey)
        if(abortKey) {
            abortPromiseCancelable = self.waitFor(abortKey)
            abortPromise = abortPromiseCancelable.then(()=>{throw new Error("__abort__")}, ()=>{})
        }

    }

    const promises = []
    if(abortPromise){
        promises.push(abortPromise)
    }
    try {
        promises.push(self._sendZdoRequestViaZnp(apiName, valObj))
        await Promise.race(promises)
    } catch(err){
        if(areq) areq.cancel()
        if(abortPromiseCancelable) abortPromiseCancelable.cancel()
        throw err<commit_msg>zdo areq needs to timeout<commit_after>/* jshint node: true */
'use strict';

const EventEmitter = require('eventemitter2'),
      CCZnp = require('cc-znp'),
      ZSC = CCZnp.constants,
      zdoHelper = require('./zdo_helper'),
      ZigbeeError = require('../errors/zigbee_error');

const requestFn = {}

const ZdoTimeout = 60000

class Zdo extends EventEmitter {
    constructor(controller){
        super()
        this.setMaxListeners(40)
        this._controller = controller;
    }

    /*************************************************************************************************/
    /*** Public APIs                                                                               ***/
    /*************************************************************************************************/
    async request (apiName, valObj) {
        var requestType = zdoHelper.getRequestType(apiName);
    
        var handler = requestFn[requestType]
        if(!handler) {
            throw new Error('Unknown request type.')
        }
        return await handler(this, apiName, valObj)
    }

    
    /*************************************************************************************************/
    /*** Protected Methods                                                                         ***/
    /*************************************************************************************************/
    async _sendZdoRequestViaZnp (apiName, valObj) {
        const rsp = await this._controller._znp.zdoRequest(apiName, valObj)
        if(!rsp){
            throw new Error(`Unable to execute ${apiName}`)
        }
        if (apiName !== 'startupFromApp' && rsp.status)
            throw new ZigbeeError('request unsuccess: ' + rsp.status, rsp.status)
        return rsp
    }
}

requestFn.rspless = function (self, apiName, valObj) {
    return self._sendZdoRequestViaZnp(apiName, valObj);
}

requestFn.generic = async function (self, apiName, valObj) {
    var areq,
        areqEvtKey = zdoHelper.generateEventOfRequest(apiName, valObj),
        abortKey,
        abortPromiseCancelable,
        abortPromise

    if (areqEvtKey) {
        abortKey = zdoHelper.generateDstAddrOfRequest(valObj)
        areq = self._controller.waitFor(areqEvtKey, ZdoTimeout)
        if(abortKey) {
            abortPromiseCancelable = self.waitFor(abortKey)
            abortPromise = abortPromiseCancelable.then(()=>{throw new Error("__abort__")}, ()=>{})
        }

    }

    const promises = []
    if(abortPromise){
        promises.push(abortPromise)
    }
    try {
        promises.push(self._sendZdoRequestViaZnp(apiName, valObj))
        await Promise.race(promises)
    } catch(err){
        if(areq) areq.cancel()
        if(abortPromiseCancelable) abortPromiseCancelable.cancel()
        throw err
{'fraction_non_alphanumeric': 0.23358449946178686, 'fraction_numerical': 0.001973448152134912, 'mean_word_length': 2.52179406190777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3060424', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1433, 'n_words': 271}