From de35cedb1f5b7cb8dd5b74261697bcdd10ba02e7 Mon Sep 17 00:00:00 2001 From: stephen Date: Sun, 30 Jan 2022 13:55:13 -0500 Subject: [PATCH] Adding Tuya color control --- index.js | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 162f8a2..5dc6d0c 100755 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const ini = require('ini'); const mastodon = require('mastodon'); const mqtt = require('mqtt'); const request = require('request'); +const rgbcolor = require('rgb-color'); const xmpp = require('simple-xmpp'); try { @@ -42,6 +43,7 @@ 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_color_url = config.tuya_color_url; const tuya_status_url = config.tuya_status_url; const tuya_toggle_url = config.tuya_toggle_url; const user_jid = config.user_jid; @@ -73,7 +75,6 @@ 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'); @@ -237,7 +238,41 @@ console.log(message) console.log('lights turned to ' + per + "%"); } - // Ping a user to let them know you would like to communitcate + else if (message.includes(tuya_name + " color")) { + request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => {}) + var color=message.substring(message.indexOf("color") + 6); + var colorRgb=rgbcolor(color); + console.log(color) + if (colorRgb.isValid()) { + var obj = colorRgb.channels(); + console.log(obj.r + ', ' + obj.g + ', ' + obj.b); + } + request(tuya_color_url + "?r=" + colorRgb.r + "&g=" + colorRgb.g + "&b=" + colorRgb.b + "&devname=" + tuya_name,(error, res, body) => { + if (error) { + return console.log(error) + }; + + if (!error && res.statusCode == 200) { + 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'); + } + }); + console.log(tuya_name + "set to " + color); + } + + // Ping a user to let them know you would like to communicate else if (message === "ping" || message === 'Ping') { xmpp.send(from, "Pinging " + user_name + "! Please stand by."); xmpp.send(user_jid, from + " has pinged you!");