-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-home-notifier.js
90 lines (75 loc) · 2.27 KB
/
google-home-notifier.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
'use strict';
var Client = require('castv2-client').Client;
const EventEmitter = require('events');
var DefaultMediaReceiver = require('castv2-client').DefaultMediaReceiver;
var Googletts = require('google-tts-api');
var net = require('net');
function GoogleHomeNotifier(deviceip, language, speed) {
this.deviceip = deviceip;
this.language = language;
this.speed = speed;
var emitter = this;
this.notify = function (message, callback) {
getSpeechUrl(message, this.deviceip, function (res) {
emitter.emit("speech", res);
});
};
this.play = function (mp3_url, callback) {
getPlayUrl(mp3_url, this.deviceip, function (res) {
emitter.emit("play", res)
});
};
var getSpeechUrl = function (text, host, callback) {
Googletts(text, language, 1).then(function (url) {
onDeviceUp(host, url, function (res) {
callback(res);
});
}).catch(function (err) {
emitter.emit("error", err);
});
};
var getPlayUrl = function (url, host, callback) {
onDeviceUp(host, url, function (res) {
callback(res);
});
};
var onDeviceUp = function (host, url, callback) {
var client = new Client();
var clienttcp = new net.Socket();
clienttcp.connect(8009, host, function () {
client.connect(host, function () {
client.launch(DefaultMediaReceiver, function (err, player) {
var media = {
contentId: url,
contentType: 'audio/mp3',
streamType: 'BUFFERED' // or LIVE
};
player.load(media, {
autoplay: true
}, function (err, status) {
client.close();
callback('Device notified');
});
});
});
});
clienttcp.on('error', function (error) {
emitter.emit("error", error);
callback('ERROR: Device not reachable');
});
client.on('error', function (err) {
console.log('Error: %s', err.message);
client.close();
emitter.emit("error", err)
});
};
};
GoogleHomeNotifier.prototype.__proto__ = EventEmitter.prototype // inherit from EventEmitter
module.exports = function (deviceip, language, speed) {
if (deviceip && language) {
if (!speed) {
speed = 1
};
return new GoogleHomeNotifier(deviceip, language, speed);
}
}