From 5ec19de979f91acf966e033c6832861be19f4185 Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 27 Jan 2022 10:48:21 -0500 Subject: [PATCH 1/2] Adding Tuya for lights (Uses Tuya API) Removed app_port from config, as it's depreciated. --- config.ini.dist | 6 ++- index.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 100 insertions(+), 5 deletions(-) diff --git a/config.ini.dist b/config.ini.dist index f1e127e..8d14b27 100644 --- a/config.ini.dist +++ b/config.ini.dist @@ -1,7 +1,6 @@ api_pass = api_url = api_user = -app_port = asterisk_callfrom = asterisk_callto = asterisk_callerID = @@ -29,8 +28,11 @@ mqtt_server = mqtt_topic = mqtt_user = rss_url = +tuya_name = +tuya_status_url = +tuya_toggle_url = user_jid = user_name = vfc_url = weather_api_key = -weather_units = \ No newline at end of file +weather_units = diff --git a/index.js b/index.js index afb6cde..596c23a 100755 --- a/index.js +++ b/index.js @@ -44,6 +44,9 @@ const mqtt_topic = config.mqtt_topic; const mqtt_user = config.mqtt_user; const rpc_url = config.rpc_url; const rss_url = config.rss_url; +const tuya_name = config.tuya_name; +const tuya_status_url = config.tuya_status_url; +const tuya_toggle_url = config.tuya_toggle_url; const user_jid = config.user_jid; const user_name = config.user_name; const vfc_url = config.vfc_url; @@ -121,6 +124,95 @@ xmpp.on('chat', function(from, message) { }); console.log('unlocks'); } + // Tuya light control + else if (message === 'lights' || message === 'Lights') { + request(tuya_status_url,(error, res, body) => { + if (error) { + return console.log(error) + }; + + if (!error && res.statusCode == 200) { + if (res.body === "true"){ + const stat = tuya_name + ": On"; + xmpp.send(from, stat); + console.log(stat); + } else if (res.body === "false") { + const stat = tuya_name + ": Off"; + xmpp.send(from, stat); + console.log(stat); + } else { + xmpp.send(from, "Unknown reply. Please reply ping to reach an admin."); + console.log("Tuya API: Unknown reply!"); + } + } + 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 status'); + } + + else if (message === tuya_name + ' on') { + request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => { + if (error) { + return console.log(error) + }; + + if (!error && res.statusCode == 200) { + const stat = tuya_name + " has been turned on"; + xmpp.send(from, stat); + console.log(stat); + } + 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 on'); + } + else if (message === tuya_name + ' off') { + request(tuya_toggle_url + "?stat=false&devname=" + tuya_name,(error, res, body) => { + if (error) { + return console.log(error) + }; + + if (!error && res.statusCode == 200) { + const stat = tuya_name + " has been turned off"; + xmpp.send(from, stat); + console.log(stat); + } + 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 on'); + } // Check VFC balance else if (message === "vfc" || message === "Vfc") { @@ -236,10 +328,11 @@ xmpp.on('chat', function(from, message) { console.log(vcard) }) } - + // If the user sends something we don't have logic for, send them info about the project - else { xmpp.send(from, "This is an auto replying bot. Reply \"help\" for more info, or https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start"); - console.log('autoreply'); + else { + xmpp.send(from, "This is an auto replying bot. Reply \"help\" for more info, or https://wiki.vanderwarker.family/doku.php?id=code:vxmppb:start"); + console.log('autoreply'); } }); From 530b6bdeaeaf8233dbe432cc7e28f0157bbc8cbc Mon Sep 17 00:00:00 2001 From: stephen Date: Thu, 27 Jan 2022 20:27:43 -0500 Subject: [PATCH 2/2] Removing RSS, and VFC dependencies, and code. Adding XMPP presence --- config.ini.dist | 2 -- index.js | 55 ++----------------------------------------------- package.json | 2 -- 3 files changed, 2 insertions(+), 57 deletions(-) diff --git a/config.ini.dist b/config.ini.dist index 8d14b27..734c1b5 100644 --- a/config.ini.dist +++ b/config.ini.dist @@ -27,12 +27,10 @@ mqtt_password = mqtt_server = mqtt_topic = mqtt_user = -rss_url = tuya_name = tuya_status_url = tuya_toggle_url = user_jid = user_name = -vfc_url = weather_api_key = weather_units = diff --git a/index.js b/index.js index 596c23a..52d1079 100755 --- a/index.js +++ b/index.js @@ -3,8 +3,6 @@ const ini = require('ini'); const mastodon = require('mastodon'); const mqtt = require('mqtt'); const request = require('request'); -let Parser = require('rss-parser'); -let parser = new Parser(); const xmpp = require('simple-xmpp'); try { @@ -42,14 +40,11 @@ const mqtt_password = config.mqtt_password; const mqtt_server = config.mqtt_server; const mqtt_topic = config.mqtt_topic; const mqtt_user = config.mqtt_user; -const rpc_url = config.rpc_url; -const rss_url = config.rss_url; const tuya_name = config.tuya_name; const tuya_status_url = config.tuya_status_url; const tuya_toggle_url = config.tuya_toggle_url; const user_jid = config.user_jid; const user_name = config.user_name; -const vfc_url = config.vfc_url; const weather_api_key = config.weather_api_key; const weather_units = config.weather_units; @@ -160,7 +155,6 @@ xmpp.on('chat', function(from, message) { }); console.log('lights status'); } - else if (message === tuya_name + ' on') { request(tuya_toggle_url + "?stat=true&devname=" + tuya_name,(error, res, body) => { if (error) { @@ -214,43 +208,6 @@ xmpp.on('chat', function(from, message) { console.log('lights on'); } - // Check VFC balance - else if (message === "vfc" || message === "Vfc") { - ami.action({ - 'action':'originate', - 'channel':'SIP/' + asterisk_callfrom, - 'context': asterisk_context, - 'callerID' : asterisk_callerID, - 'exten' : asterisk_callto, - 'priority' : '1', - }); - request(vfc_url, (error, res, body) => { - if (error) { - return console.log(error) - }; - - if (!error && res.statusCode == 200) { - const balance = user_name + " has " + res.body + " VFC"; - xmpp.send(from, balance); - console.log(balance); - } - }); - console.log('vfc'); - } - - // Get latest RSS item from feed - else if (message === "rss" || message === "Rss") { - (async () => { - let feed = await parser.parseURL(rss_url); - // console.log(feed.title); - feed.items.forEach(item => { - console.log(item.title + ':' + item.link) - xmpp.send(from, item.title) - }); - xmpp.send(from, 'See https://vanderwarker.social/@stephen for full entries.') - })(); - } - // Ping a user to let them know you would like to communitcate else if (message === "ping" || message === 'Ping') { xmpp.send(from, "Pinging " + user_name + "! Please stand by."); @@ -341,16 +298,8 @@ xmpp.on('error', function(err) { console.error(err); }); -// EXTENDING XEPS (WIP) -/* -xmpp.on('chatstate', function(from, state) { -console.log(state); -}); - -xmpp.on('buddy', function(jid, state, statusText, resource) { -console.log(state + statusText + resource); -}); -*/ +// Set XMPP status +xmpp.setPresence('chat', 'Asterisk,Mastodon, and MQTT. Oh my!'); // Alas, we connect to XMPP! xmpp.connect({ diff --git a/package.json b/package.json index bcb8598..94838a3 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ "sms", "autoreply", "asterisk", - "rss", "mqtt", "gotify", "push notifications" @@ -26,7 +25,6 @@ "mastodon": "1.2.2", "mqtt": "4.2.6", "request": "2.88.0", - "rss-parser": "3.12.0", "simple-xmpp": "2.3.1" }, "bugs": {