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

Reviewed-on: vxmppb/vxmppb#7
This commit is contained in:
Stephen Vanderwarker 2022-01-29 22:53:24 -05:00
commit fa38276c04
2 changed files with 32 additions and 2 deletions

View File

@ -28,6 +28,7 @@ mqtt_server =
mqtt_topic =
mqtt_user =
tuya_name =
tuya_bright_url =
tuya_status_url =
tuya_toggle_url =
user_jid =

View File

@ -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