Merge pull request 'Adding color control' (#8) from stephen/vxmppb:tuya into main

Reviewed-on: #8
This commit is contained in:
Stephen Vanderwarker 2022-01-30 14:01:27 -05:00
commit ce90a74e68
3 changed files with 39 additions and 2 deletions

View File

@ -29,6 +29,7 @@ mqtt_topic =
mqtt_user =
tuya_name =
tuya_bright_url =
tuya_color_url =
tuya_status_url =
tuya_toggle_url =
user_jid =

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

View File

@ -25,6 +25,7 @@
"mastodon": "1.2.2",
"mqtt": "4.2.6",
"request": "2.88.0",
"rgb-color": "2.1.2",
"simple-xmpp": "2.3.1"
},
"bugs": {