TuyaAPI / cli

🔧 A CLI for Tuya devices
MIT License
266 stars 43 forks source link

Remove dnode dependency #4

Closed Apollon77 closed 5 years ago

Apollon77 commented 5 years ago

... you can use it like:

Rule:

module.exports = function(callback) {
    return {
        summary: 'Catch Tuya Data from Response',
        *beforeSendResponse(requestDetail, responseDetail) {
            const body = responseDetail.response.body.toString('utf8');
            if (body.includes('tuya.m.my.group.device.list')) {
                try {
                    const response = JSON.parse(body);
                }
                catch (err) {
                    return null;
                }
                callback(response);
                return null;
            }
        },

        *beforeDealHttpsRequest(requestDetail) {
            return requestDetail.host.includes('tuya');
        }
    };
};

And in main file:

rule: require('./lib/anyproxy-rule.js')(resonse => { ... }),
codetheweb commented 5 years ago

Wow, that's a lot smarter than what I was doing. Thanks.

I'll change this later today.

Apollon77 commented 5 years ago

Yes, found that way after experimenting a bit while implementing in my ioBroker adapter directly.

In fact i defined a function and then have the function name as parameter instead of the function definition because it feeled weired in this "object definition context" :-)

codetheweb commented 5 years ago

Fixed in v1.4.0.