forked from vxmppb/vxmppb
Adding tuya reauth, will become crontab in documentation.
Adding function for request error responses (see #24)
This commit is contained in:
parent
eb4d9d6b7c
commit
2aafdd6c42
|
@ -42,6 +42,7 @@ bright_url =
|
||||||
color_url =
|
color_url =
|
||||||
status_url =
|
status_url =
|
||||||
toggle_url =
|
toggle_url =
|
||||||
|
reauth_url
|
||||||
|
|
||||||
[unlocks]
|
[unlocks]
|
||||||
api_url =
|
api_url =
|
||||||
|
|
108
index.js
108
index.js
|
@ -59,6 +59,7 @@ try {
|
||||||
const tuya_color_url = config.tuya.color_url;
|
const tuya_color_url = config.tuya.color_url;
|
||||||
const tuya_status_url = config.tuya.status_url;
|
const tuya_status_url = config.tuya.status_url;
|
||||||
const tuya_toggle_url = config.tuya.toggle_url;
|
const tuya_toggle_url = config.tuya.toggle_url;
|
||||||
|
const tuya_reauth_url = config.tuya.reauth_url;
|
||||||
|
|
||||||
// Unlocks API config
|
// Unlocks API config
|
||||||
const unlocks_api_url = config.unlocks.api_url;
|
const unlocks_api_url = config.unlocks.api_url;
|
||||||
|
@ -135,6 +136,20 @@ try {
|
||||||
console.log('-------------');
|
console.log('-------------');
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
function resError(error,res){
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
};
|
||||||
// setup gotify url
|
// setup gotify url
|
||||||
let gotifyURL = `${gotify_api_url}/message?token=${gotify_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`;
|
let gotifyURL = `${gotify_api_url}/message?token=${gotify_key}&message=${from} ${gotify_message}&title=${gotify_title}&priority=${gotify_priority}`;
|
||||||
let gotifyEUrl = encodeURI(gotifyURL);
|
let gotifyEUrl = encodeURI(gotifyURL);
|
||||||
|
@ -170,20 +185,8 @@ try {
|
||||||
var unlockBodyMatch = body.match(unlockBody);
|
var unlockBodyMatch = body.match(unlockBody);
|
||||||
const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n;
|
const unlockFinalMessage = user_name + " has " + unlockBodyMatch[1] + " unlocks for " + n;
|
||||||
xmpp.send(from, unlockFinalMessage);
|
xmpp.send(from, unlockFinalMessage);
|
||||||
debug();
|
|
||||||
}
|
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
|
resError(error,res);
|
||||||
});
|
});
|
||||||
debug();
|
debug();
|
||||||
}
|
}
|
||||||
|
@ -260,18 +263,24 @@ try {
|
||||||
console.log("Tuya API: Unknown reply!");
|
console.log("Tuya API: Unknown reply!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!error && res.statusCode == 401) {
|
resError(error,res);
|
||||||
console.log('Invalid credentials');
|
});
|
||||||
xmpp.send(from, 'Invalid credentials');
|
debug();
|
||||||
}
|
}
|
||||||
if (!error && res.statusCode == 404) {
|
} else if (message === '!lightsauth') {
|
||||||
console.log('Record not found');
|
if (from != user_jid) {
|
||||||
xmpp.send(from, 'Record not found');
|
xmpp.send(from, pc);
|
||||||
}
|
} else {
|
||||||
if (!error && res.statusCode == 500) {
|
// refresh tokens for Tuya API
|
||||||
console.log('Server error.');
|
request(tuya_reauth_url, (error, res, body) => {
|
||||||
xmpp.send(from, 'Server error. Please !ping');
|
if (error) {
|
||||||
|
return console.log(error)
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!error && res.statusCode == 200) {
|
||||||
|
xmpp.send(from, 'Refreshing Tuya API auth keys');
|
||||||
}
|
}
|
||||||
|
resError(error,res);
|
||||||
});
|
});
|
||||||
debug();
|
debug();
|
||||||
}
|
}
|
||||||
|
@ -290,18 +299,7 @@ try {
|
||||||
xmpp.send(from, stat);
|
xmpp.send(from, stat);
|
||||||
console.log(stat);
|
console.log(stat);
|
||||||
}
|
}
|
||||||
if (!error && res.statusCode == 401) {
|
resError(error,res);
|
||||||
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');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
debug();
|
debug();
|
||||||
}
|
}
|
||||||
|
@ -314,24 +312,12 @@ try {
|
||||||
if (error) {
|
if (error) {
|
||||||
return console.log(error)
|
return console.log(error)
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!error && res.statusCode == 200) {
|
if (!error && res.statusCode == 200) {
|
||||||
const stat = tuya_name + " has been turned off";
|
const stat = tuya_name + " has been turned off";
|
||||||
xmpp.send(from, stat);
|
xmpp.send(from, stat);
|
||||||
console.log(stat);
|
console.log(stat);
|
||||||
}
|
}
|
||||||
if (!error && res.statusCode == 401) {
|
resError(error,res);
|
||||||
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');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
debug();
|
debug();
|
||||||
}
|
}
|
||||||
|
@ -351,18 +337,7 @@ try {
|
||||||
xmpp.send(from, tuya_name + "set to " + per + "%");
|
xmpp.send(from, tuya_name + "set to " + per + "%");
|
||||||
console.log(tuya_name + "set to " + per + "%");
|
console.log(tuya_name + "set to " + per + "%");
|
||||||
}
|
}
|
||||||
if (!error && res.statusCode == 401) {
|
resError(error,res);
|
||||||
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');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
if (config.debug === '1') {
|
if (config.debug === '1') {
|
||||||
console.log('lights turned to ' + per + "%");
|
console.log('lights turned to ' + per + "%");
|
||||||
|
@ -390,18 +365,7 @@ try {
|
||||||
xmpp.send(from, tuya_name + "set to " + color);
|
xmpp.send(from, tuya_name + "set to " + color);
|
||||||
console.log(tuya_name + "set to " + color);
|
console.log(tuya_name + "set to " + color);
|
||||||
}
|
}
|
||||||
if (!error && res.statusCode == 401) {
|
resError(error,res);
|
||||||
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');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
if (config.debug === '1') {
|
if (config.debug === '1') {
|
||||||
console.log(tuya_name + "set to " + color);
|
console.log(tuya_name + "set to " + color);
|
||||||
|
|
Reference in New Issue
Block a user