Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:ckhmer1/node-red-contrib-alexa-v…
Browse files Browse the repository at this point in the history
…irtual-smarthome
  • Loading branch information
claudiochimera committed Nov 26, 2022
2 parents 1051b89 + fa3d2cb commit e54f068
Show file tree
Hide file tree
Showing 12 changed files with 620 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def lambda_handler(event, context):
response = http.request(
'POST',
'{}/alexa/smarthome'.format(base_url),
base_url,
headers={
'Authorization': 'Bearer {}'.format(token),
'Content-Type': 'application/json',
Expand All @@ -197,7 +197,7 @@ def lambda_handler(event, context):
* Click on the "Edit" button.
* Click on the "Add environment variable" button.
* Enter "BASE_URL" as the "Key".
* Enter "https://YOUR_DOMAIN" as the Value.
* Enter "https://YOUR_DOMAIN/alexa/smarthome" as the Value.
* Click on the "Add environment variable" button.
* Enter "DEBUG" as the "Key".
* Enter "True" as the Value.
Expand Down
9 changes: 8 additions & 1 deletion alexa/alexa-device.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
name: { value: "", required: true },
display_categories: { value: [], required: true, validate: function (dc) { return dc && dc.length > 0 } },
topic: { value: "", required: false },
topic_filter: { value: false, required: false },
passthru: { value: false, required: false },
a_inputs: { value: ['AUX_1', 'AUX_2', 'AUX_3', 'AUX_4', 'AUX_5', 'AUX_6', 'AUX_7', 'BLURAY', 'CABLE', 'CD', 'COAX_1', 'COAX_2', 'COMPOSITE_1', 'DVD', 'GAME', 'HD_RADIO', 'HDMI_1', 'HDMI_2', 'HDMI_3', 'HDMI_4', 'HDMI_5', 'HDMI_6', 'HDMI_7', 'HDMI_8', 'HDMI_9', 'HDMI_10', 'HDMI_ARC', 'INPUT_1', 'INPUT_2', 'INPUT_3', 'INPUT_4', 'INPUT_5', 'INPUT_6', 'INPUT_7', 'INPUT_8', 'INPUT_9', 'INPUT_10', 'IPOD', 'LINE_1', 'LINE_2', 'LINE_3', 'LINE_4', 'LINE_5', 'LINE_6', 'LINE_7', 'MEDIA_PLAYER', 'OPTICAL_1', 'OPTICAL_2', 'PHONO', 'PLAYSTATION', 'PLAYSTATION_3', 'PLAYSTATION_4', 'SATELLITE', 'SMARTCAST', 'TUNER', 'TV', 'USB_DAC', 'VIDEO_1', 'VIDEO_2', 'VIDEO_3', 'XBOX'], required: false },
i_automation_management: { value: false, required: false },
Expand Down Expand Up @@ -1291,9 +1292,15 @@
</select>
</div>

<!-- Filter by Topic -->
<div class="form-row">
<label style="width:auto" for="node-input-topic_filter"><i class="fa fa-arrow-right"></i> <span data-i18n="alexa-device.label.topic_filter"></span></label>
<input type="checkbox" id="node-input-topic_filter" style="display:inline-block; width:auto; vertical-align:top;">
</div>

<div class="form-row">
<label style="width:auto" for="node-input-passthru"><i class="fa fa-arrow-right"></i> <span data-i18n="alexa-device.label.passthru"></span></label>
<input type="checkbox" checked id="node-input-passthru" style="display:inline-block; width:auto; vertical-align:top;">
<input type="checkbox" id="node-input-passthru" style="display:inline-block; width:auto; vertical-align:top;">
</div>

<div class="form-row red-ui-editableList-border div_trait" hidden id="i_automation_management" style="background: #fbfbfb; padding: 5px;">
Expand Down
7 changes: 6 additions & 1 deletion alexa/alexa-device.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ module.exports = function (RED) {
//
onInput(msg) {
const node = this;
const topicArr = String(msg.topic || '').split('/');
const topic_str = String(msg.topic || '');
const topicArr = topic_str.split('/');
const topic = topicArr[topicArr.length - 1].toUpperCase();
if (node.isVerbose()) node._debug("onInput " + JSON.stringify(msg));
if (node.isVerbose()) node._debug("onInput " + topic);
Expand Down Expand Up @@ -366,6 +367,10 @@ module.exports = function (RED) {
}
} else {
let msg1 = msg;
console.log("CCHI topic " + node.config.topic + " msg.topic " + topic_str);
if (node.config.topic_filter && !topic_str.startsWith(node.config.topic)) {
return;
}
Object.keys(node.state_types).forEach(function (key) {
if (topic == key.toUpperCase()) {
msg1 = {
Expand Down
1 change: 1 addition & 0 deletions alexa/locales/en-US/alexa-device.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"name": "Name",
"topic": "Out topic",
"passthru": "If msg arrives on input, pass through to output:",
"topic_filter": "Filter incoming messages by topic",
"username": "Username",
"password": "Password",
"secret": "Secret",
Expand Down
1 change: 1 addition & 0 deletions alexa/locales/it_IT/alexa-device.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"name": "Nome",
"topic": "Topic in uscita",
"passthru": "Se un messaggio arriva in ingresso, invialo in uscita:",
"topic_filter": "Filtra i messaggi in ingresso",
"username": "Nome utente",
"password": "Password",
"secret": "Segreto",
Expand Down
87 changes: 87 additions & 0 deletions docs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* NodeRED Alexa SmartHome
* Copyright 2022 Claudio Chimera <Claudio.Chimera at gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/


const https = require('https')
const base_url = process.env.BASE_URL
const d = 'True' == process.env.DEBUG

const { hostname = "localhost", pathname = "/", s_port, protocol } = new URL(base_url);
const port = s_port ? parseInt(s_port) : protocol === 'https:' ? 443 : 80

if (d) {
console.log('url: ' + base_url)
console.log('hostname: ' + hostname)
console.log('pathname: ' + pathname)
console.log('port: ' + port)
console.log('protocol: ' + protocol)
}

const options = {
hostname: hostname,
path: pathname,
method: 'POST',
port: port,
headers: {
'Content-Type': 'application/json',
},
};

function getError(err) {
return {
'event': {
'payload': {
'type': 'INTERNAL_ERROR',
'message': err,
}
}
};
}

exports.handler = async (event) => {
if (d) {
console.log("event: " + JSON.stringify(event))
}

return new Promise((resolve, reject) => {
const req = https.request(options, res => {
let rawData = '';

res.on('data', chunk => {
rawData += chunk;
});

res.on('end', () => {
try {
resolve(JSON.parse(rawData));
} catch (err) {
if (d) console.log("rawData: " + rawData)
resolve(getError(rawData));
}
});
});

req.on('error', err => {
if (d) console.log("reject: " + err)
resolve(getError(err));
});

req.write(JSON.stringify(event));
req.end();
});
};
Loading

0 comments on commit e54f068

Please sign in to comment.