From 2aafdd6c42c088001387ad357991e81d23848ac2 Mon Sep 17 00:00:00 2001 From: stephen <1-stephen@users.noreply.gitlab.vanderwarker.dev> Date: Mon, 25 Apr 2022 20:36:36 -0400 Subject: [PATCH] Adding tuya reauth, will become crontab in documentation. Adding function for request error responses (see #24) --- config.ini.dist | 1 + index.js | 112 ++++++++++++++++-------------------------------- 2 files changed, 39 insertions(+), 74 deletions(-) diff --git a/config.ini.dist b/config.ini.dist index d71d41e..c24bad5 100644 --- a/config.ini.dist +++ b/config.ini.dist @@ -42,6 +42,7 @@ bright_url = color_url = status_url = toggle_url = +reauth_url [unlocks] api_url = diff --git a/index.js b/index.js index 6e2669b..5fd9b3b 100755 --- a/index.js +++ b/index.js @@ -59,6 +59,7 @@ try { const tuya_color_url = config.tuya.color_url; const tuya_status_url = config.tuya.status_url; const tuya_toggle_url = config.tuya.toggle_url; + const tuya_reauth_url = config.tuya.reauth_url; // Unlocks API config const unlocks_api_url = config.unlocks.api_url; @@ -134,7 +135,21 @@ try { console.log(from); console.log('-------------'); }; - } + } + function resError(error,res){ + if (!error && res.statusCode == 401) { + console.log('Invalid credentials'); + xmpp.send(from, 'Invalid credentials'); + } + if (!error && res.statusCode == 404) { + console.log('Record not found'); + xmpp.send(from, 'Record not found'); + } + if (!error && res.statusCode == 500) { + console.log('Server error.'); + xmpp.send(from, 'Server error. Please ping admin'); + } + }; // setup gotify url let gotifyURL = `${gotify_api_url}/message?token=${gotify_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`; let gotifyEUrl = encodeURI(gotifyURL); @@ -170,20 +185,8 @@ try { var unlockBodyMatch = body.match(unlockBody); const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n; xmpp.send(from, unlockFinalMessage); - debug(); - } - if (!error && res.statusCode == 401) { - console.log('Invalid credentials'); - xmpp.send(from, 'Invalid credentials'); - } - if (!error && res.statusCode == 404) { - console.log('Record not found'); - xmpp.send(from, 'Record not found'); - } - if (!error && res.statusCode == 500) { - console.log('Server error.'); - xmpp.send(from, 'Server error. Please ping admin'); } + resError(error,res); }); debug(); } @@ -260,18 +263,24 @@ try { console.log("Tuya API: Unknown reply!"); } } - if (!error && res.statusCode == 401) { - console.log('Invalid credentials'); - xmpp.send(from, 'Invalid credentials'); - } - if (!error && res.statusCode == 404) { - console.log('Record not found'); - xmpp.send(from, 'Record not found'); - } - if (!error && res.statusCode == 500) { - console.log('Server error.'); - xmpp.send(from, 'Server error. Please !ping'); + resError(error,res); + }); + debug(); + } + } else if (message === '!lightsauth') { + if (from != user_jid) { + xmpp.send(from, pc); + } else { + // refresh tokens for Tuya API + request(tuya_reauth_url, (error, res, body) => { + if (error) { + return console.log(error) + }; + + if (!error && res.statusCode == 200) { + xmpp.send(from, 'Refreshing Tuya API auth keys'); } + resError(error,res); }); debug(); } @@ -290,18 +299,7 @@ try { xmpp.send(from, stat); console.log(stat); } - if (!error && res.statusCode == 401) { - console.log('Invalid credentials'); - xmpp.send(from, 'Invalid credentials'); - } - if (!error && res.statusCode == 404) { - console.log('Record not found'); - xmpp.send(from, 'Record not found'); - } - if (!error && res.statusCode == 500) { - console.log('Server error.'); - xmpp.send(from, 'Server error. Please ping admin'); - } + resError(error,res); }); debug(); } @@ -314,24 +312,12 @@ try { if (error) { return console.log(error) }; - if (!error && res.statusCode == 200) { const stat = tuya_name + " has been turned off"; xmpp.send(from, stat); console.log(stat); } - if (!error && res.statusCode == 401) { - console.log('Invalid credentials'); - xmpp.send(from, 'Invalid credentials'); - } - if (!error && res.statusCode == 404) { - console.log('Record not found'); - xmpp.send(from, 'Record not found'); - } - if (!error && res.statusCode == 500) { - console.log('Server error.'); - xmpp.send(from, 'Server error. Please ping admin'); - } + resError(error,res); }); debug(); } @@ -351,18 +337,7 @@ try { xmpp.send(from, tuya_name + "set to " + per + "%"); console.log(tuya_name + "set to " + per + "%"); } - if (!error && res.statusCode == 401) { - console.log('Invalid credentials'); - xmpp.send(from, 'Invalid credentials'); - } - if (!error && res.statusCode == 404) { - console.log('Record not found'); - xmpp.send(from, 'Record not found'); - } - if (!error && res.statusCode == 500) { - console.log('Server error.'); - xmpp.send(from, 'Server error. Please ping admin'); - } + resError(error,res); }); if (config.debug === '1') { console.log('lights turned to ' + per + "%"); @@ -390,18 +365,7 @@ try { xmpp.send(from, tuya_name + "set to " + color); console.log(tuya_name + "set to " + color); } - if (!error && res.statusCode == 401) { - console.log('Invalid credentials'); - xmpp.send(from, 'Invalid credentials'); - } - if (!error && res.statusCode == 404) { - console.log('Record not found'); - xmpp.send(from, 'Record not found'); - } - if (!error && res.statusCode == 500) { - console.log('Server error.'); - xmpp.send(from, 'Server error. Please ping admin'); - } + resError(error,res); }); if (config.debug === '1') { console.log(tuya_name + "set to " + color);