From 2531e35682c44cb92d4eb743fe753cb5b1e91308 Mon Sep 17 00:00:00 2001 From: stephen Date: Sat, 29 Jan 2022 22:40:21 -0500 Subject: [PATCH] Adding brightness control See #5 --- config.ini.dist | 1 + index.js | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/config.ini.dist b/config.ini.dist index 734c1b5..a34f17c 100644 --- a/config.ini.dist +++ b/config.ini.dist @@ -28,6 +28,7 @@ mqtt_server = mqtt_topic = mqtt_user = tuya_name = +tuya_bright_url = tuya_status_url = tuya_toggle_url = user_jid = diff --git a/index.js b/index.js index 52d1079..162f8a2 100755 --- a/index.js +++ b/index.js @@ -41,6 +41,7 @@ const mqtt_server = config.mqtt_server; const mqtt_topic = config.mqtt_topic; const mqtt_user = config.mqtt_user; const tuya_name = config.tuya_name; +const tuya_bright_url = config.tuya_bright_url; const tuya_status_url = config.tuya_status_url; const tuya_toggle_url = config.tuya_toggle_url; const user_jid = config.user_jid; @@ -72,7 +73,7 @@ xmpp.on('online', function(data, to) { }); xmpp.on('chat', function(from, message) { - +console.log(message) // HELP if (message === 'help' || message === 'Help') { xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:commands'); @@ -205,7 +206,35 @@ xmpp.on('chat', function(from, message) { xmpp.send(from, 'Server error. Please ping admin'); } }); - console.log('lights on'); + console.log('lights off'); + } + + else if (message.includes(tuya_name + " brightness")) { + var per=message.substring(message.indexOf("brightness") + 10); + var perc=(per*10) + request(tuya_bright_url + "?brite=" + perc + "&devname=" + tuya_name,(error, res, body) => { + if (error) { + return console.log(error) + }; + + if (!error && res.statusCode == 200) { + 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'); + } + }); + console.log('lights turned to ' + per + "%"); } // Ping a user to let them know you would like to communitcate