diff --git a/example.js b/example.js index 3955d5f..b577c60 100644 --- a/example.js +++ b/example.js @@ -1,4 +1,4 @@ -const googlehome = require('./index') +const GoogleHome = require('./index') -googlehome.search(1000) - .then(resp => console.log(resp)) +let device = new GoogleHome.Connecter('192.168.11.13') +device.speak('Hello').then(console.log) diff --git a/index.js b/index.js index 4a28c2d..80de480 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ -const search = require('./lib/search') +const search = require('./lib/search'), + Connecter = require('./lib/connect') -module.exports = {search: search} +module.exports = { search, Connecter } diff --git a/lib/connect.js b/lib/connect.js new file mode 100644 index 0000000..8d1c984 --- /dev/null +++ b/lib/connect.js @@ -0,0 +1,50 @@ +const googletts = require('google-tts-api'), + Client = require('castv2-client').Client, + DefaultMediaReceiver = require('castv2-client').DefaultMediaReceiver + +const Connecter = (() => { + const playMethod = Symbol('playMethod') + + class Connecter { + + constructor(ip){ + this.ip = ip + this.lang = 'en' + } + + config(options = {}) { + this.lang = options.lang || this.lang + } + + async speak(message, speed = 1, timeout = 3000) { + const url = await googletts(message, this.lang, speed, timeout) + return await this[playMethod](url) + } + + async [playMethod](url) { + + const client = new Client() + const params = { + contentId: url, + contentType: 'audio/mp3', + streamType: 'BUFFERED' + } + + const status = await new Promise((resolve, reject) => client.connect(this.ip, () => { + client.launch(DefaultMediaReceiver, (err, player) => { + player.load(params, { autoplay: true }, (err, status) => { + err ? reject() : resolve(status) + }) + }) + })) + + client.close() + return status + } + } + + return Connecter; + +})() + +module.exports = Connecter diff --git a/package.json b/package.json index e1da4d6..5f7174f 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,8 @@ "author": "_Ghostrick_ ", "license": "MIT", "dependencies": { + "castv2-client": "^1.2.0", + "google-tts-api": "^0.0.2", "mdns": "^2.3.4" } } diff --git a/yarn.lock b/yarn.lock index 9f3c7e9..121e0f8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,10 +2,83 @@ # yarn lockfile v1 +ascli@~0.3: + version "0.3.0" + resolved "https://registry.yarnpkg.com/ascli/-/ascli-0.3.0.tgz#5e66230e5219fe3e8952a4efb4f20fae596a813a" + dependencies: + colour latest + optjs latest + bindings@~1.2.1: version "1.2.1" resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.2.1.tgz#14ad6113812d2d37d72e67b4cacb4bb726505f11" +bufferview@~1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/bufferview/-/bufferview-1.0.1.tgz#7afd74a45f937fa422a1d338c08bbfdc76cd725d" + +"bytebuffer@~3 >=3.5": + version "3.5.5" + resolved "https://registry.yarnpkg.com/bytebuffer/-/bytebuffer-3.5.5.tgz#7a6faf1a13514b083f1fcf9541c4c9bfbe7e7fd3" + dependencies: + bufferview "~1" + long "~2 >=2.2.3" + +castv2-client@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/castv2-client/-/castv2-client-1.2.0.tgz#a9193b1a5448b8cb9a0415bd021c8811ed7b0544" + dependencies: + castv2 "~0.1.4" + debug "^2.2.0" + +castv2@~0.1.4: + version "0.1.9" + resolved "https://registry.yarnpkg.com/castv2/-/castv2-0.1.9.tgz#d0b0fab1fd06b0d9cca636886716ec1293a5905a" + dependencies: + debug "^2.2.0" + protobufjs "^3.2.2" + +colour@latest: + version "0.7.1" + resolved "https://registry.yarnpkg.com/colour/-/colour-0.7.1.tgz#9cb169917ec5d12c0736d3e8685746df1cadf778" + +debug@^2.2.0: + version "2.6.9" + resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" + dependencies: + ms "2.0.0" + +encoding@^0.1.11: + version "0.1.12" + resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.12.tgz#538b66f3ee62cd1ab51ec323829d1f9480c74beb" + dependencies: + iconv-lite "~0.4.13" + +google-tts-api@^0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/google-tts-api/-/google-tts-api-0.0.2.tgz#169bc310778a4624146cbe26f367873a862c3894" + dependencies: + isomorphic-fetch "^2.2.1" + +iconv-lite@~0.4.13: + version "0.4.19" + resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b" + +is-stream@^1.0.1: + version "1.1.0" + resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44" + +isomorphic-fetch@^2.2.1: + version "2.2.1" + resolved "https://registry.yarnpkg.com/isomorphic-fetch/-/isomorphic-fetch-2.2.1.tgz#611ae1acf14f5e81f729507472819fe9733558a9" + dependencies: + node-fetch "^1.0.1" + whatwg-fetch ">=0.10.0" + +"long@~2 >=2.2.3": + version "2.4.0" + resolved "https://registry.yarnpkg.com/long/-/long-2.4.0.tgz#9fa180bb1d9500cdc29c4156766a1995e1f4524f" + mdns@^2.3.4: version "2.3.4" resolved "https://registry.yarnpkg.com/mdns/-/mdns-2.3.4.tgz#650380ac76089430cf6c8981a945dbd27d02f5b7" @@ -13,6 +86,32 @@ mdns@^2.3.4: bindings "~1.2.1" nan "~2.3.0" +ms@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" + nan@~2.3.0: version "2.3.5" resolved "https://registry.yarnpkg.com/nan/-/nan-2.3.5.tgz#822a0dc266290ce4cd3a12282ca3e7e364668a08" + +node-fetch@^1.0.1: + version "1.7.3" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef" + dependencies: + encoding "^0.1.11" + is-stream "^1.0.1" + +optjs@latest: + version "3.2.2" + resolved "https://registry.yarnpkg.com/optjs/-/optjs-3.2.2.tgz#69a6ce89c442a44403141ad2f9b370bd5bb6f4ee" + +protobufjs@^3.2.2: + version "3.8.2" + resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-3.8.2.tgz#bc826e34c3af4697e8d0af7a669e4d612aedcd17" + dependencies: + ascli "~0.3" + bytebuffer "~3 >=3.5" + +whatwg-fetch@>=0.10.0: + version "2.0.3" + resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-2.0.3.tgz#9c84ec2dcf68187ff00bc64e1274b442176e1c84"