-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnode-red-google-home-notify.js
148 lines (128 loc) · 3.88 KB
/
node-red-google-home-notify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
module.exports = function (RED) {
// Configuration node
function GoogleHomeConfig(n) {
RED.nodes.createNode(this, n);
this.ipaddress = n.ipaddress;
this.language = n.language;
this.name = n.name;
//Prepare language Select Box
var obj = require('./languages');
//map to Array:
var languages = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
languages.push({
key: key,
value: obj[key]
});
}
};
//Build an API for config node HTML to use
RED.httpAdmin.get('/languages', function (req, res) {
res.json(languages || []);
});
//Known issue: when 'language' is Default/Auto, this will fail & return undefined
this.googlehomenotifier = require('google-home-notify')(this.ipaddress, this.language, 1);
//Build another API to auto detect IP Addresses
discoverIpAddresses('googlecast', function (ipaddresses) {
RED.httpAdmin.get('/ipaddresses', function (req, res) {
res.json(ipaddresses);
});
});
};
function discoverIpAddresses(serviceType, discoveryCallback) {
var ipaddresses = [];
var bonjour = require('bonjour')();
var browser = bonjour.find({
type: serviceType
}, function (service) {
service.addresses.forEach(function (element) {
if (element.split(".").length == 4) {
var label = "" + service.txt.md + " (" + element + ")";
ipaddresses.push({
label: label,
value: element
});
}
});
//Add a bit of delay for all services to be discovered
if (discoveryCallback)
setTimeout(function () {
discoveryCallback(ipaddresses);
}, 2000);
});
}
RED.nodes.registerType("googlehome-config-node", GoogleHomeConfig);
//--------------------------------------------------------
function GoogleHomeNotifier(n) {
RED.nodes.createNode(this, n);
var node = this;
//Validate config node
var config = RED.nodes.getNode(n.server);
this.configname = config.name;
if (config === null || config === undefined) {
node.status({
fill: "red",
shape: "ring",
text: "please create & select a config node"
});
return;
}
//On Input
node.on('input', function (msg) {
//Validate config node
if (config === null || config === undefined) {
node.status({
fill: "red",
shape: "ring",
text: "please create & select a config node"
});
return;
}
//play music if it is a mp3 url
var expression = /^https?:\/\/[-a-zA-Z0-9@:%._\+~#=]{2,256}\/([-a-zA-Z0-9@:%_\+.~#?&//=]*).mp3$/;
var regex = new RegExp(expression);
if(msg.payload.toLowerCase().match(regex)){
config.googlehomenotifier.play(msg.payload, function (result) {
node.status({
fill: "green",
shape: "ring",
text: "Successfully played mp3"
});
});
return;
}
config.googlehomenotifier.notify(msg.payload, function (result) {
node.status({
fill: "green",
shape: "ring",
text: "Successfully sent voice command"
});
});
});
config.googlehomenotifier.on('error', function (error) {
node.status({
fill: "red",
shape: "ring",
text: error.message
});
});
//Workaround for a known issue
if (config.googlehomenotifier === null || config.googlehomenotifier === undefined) {
node.status({
fill: "red",
shape: "ring",
text: "please select a non-Default language"
});
return;
}
node.status({
fill: "blue",
shape: "dot",
text: "ready"
});
config.googlehomenotifier.setMaxListeners(Infinity);
};
RED.nodes.registerType("googlehome-notify", GoogleHomeNotifier);
};