Adding Tuya color control

This commit is contained in:
Stephen Vanderwarker 2022-01-30 13:55:13 -05:00
parent 54912d6376
commit de35cedb1f
Signed by: stephen
GPG Key ID: EF429EF847868C14

View File

@ -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!");