Skip to content

Commit

Permalink
add search device function
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostrick committed Dec 25, 2017
1 parent 28182b6 commit 6a5dfbb
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# node-googlehome
It uses Node.js to communicate with Google Home.


4 changes: 4 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const googlehome = require('./index')

googlehome.search(1000)
.then(resp => console.log(resp))
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const search = require('./lib/search')

module.exports = {search: search}
18 changes: 18 additions & 0 deletions lib/search.js
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "node-googlehome",
"version": "1.0.0",
"main": "index.js",
"repository": "https://github.com/Ghostrick/node-googlehome",
"author": "_Ghostrick_ <[email protected]>",
"license": "MIT",
"dependencies": {
"mdns": "^2.3.4"
}
}
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 6a5dfbb

Please sign in to comment.