diff --git a/README.md b/README.md new file mode 100644 index 0000000..2e94af1 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# node-googlehome +It uses Node.js to communicate with Google Home. + + diff --git a/example.js b/example.js new file mode 100644 index 0000000..3955d5f --- /dev/null +++ b/example.js @@ -0,0 +1,4 @@ +const googlehome = require('./index') + +googlehome.search(1000) + .then(resp => console.log(resp)) diff --git a/index.js b/index.js new file mode 100644 index 0000000..4a28c2d --- /dev/null +++ b/index.js @@ -0,0 +1,3 @@ +const search = require('./lib/search') + +module.exports = {search: search} diff --git a/lib/search.js b/lib/search.js new file mode 100644 index 0000000..f66bbed --- /dev/null +++ b/lib/search.js @@ -0,0 +1,18 @@ +const mdns = require('mdns'), + browser = mdns.createBrowser(mdns.tcp('googlecast')) + +async function search(waitTime = 1000) { + + let services = [] + + browser.start() + browser.on('serviceUp', service => { services.push(service) }) + await new Promise(resolve => setTimeout(resolve, 1000)) + browser.stop() + + return services + .filter(service => { if(service.txtRecord.md === 'Google Home') return service }) + .map(service => { return Object.assign(service.txtRecord, {address: service.addresses[0]}) }) +} + +module.exports = search diff --git a/package.json b/package.json new file mode 100644 index 0000000..e1da4d6 --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "node-googlehome", + "version": "1.0.0", + "main": "index.js", + "repository": "https://github.com/Ghostrick/node-googlehome", + "author": "_Ghostrick_ ", + "license": "MIT", + "dependencies": { + "mdns": "^2.3.4" + } +} diff --git a/yarn.lock b/yarn.lock new file mode 100644 index 0000000..9f3c7e9 --- /dev/null +++ b/yarn.lock @@ -0,0 +1,18 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +bindings@~1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.2.1.tgz#14ad6113812d2d37d72e67b4cacb4bb726505f11" + +mdns@^2.3.4: + version "2.3.4" + resolved "https://registry.yarnpkg.com/mdns/-/mdns-2.3.4.tgz#650380ac76089430cf6c8981a945dbd27d02f5b7" + dependencies: + bindings "~1.2.1" + nan "~2.3.0" + +nan@~2.3.0: + version "2.3.5" + resolved "https://registry.yarnpkg.com/nan/-/nan-2.3.5.tgz#822a0dc266290ce4cd3a12282ca3e7e364668a08"