Created
September 13, 2019 09:21
-
-
Save yaeda/fc2c3c22954f448194b4abd9c2ec4ec8 to your computer and use it in GitHub Desktop.
utility modules for toio.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(cube) { | |
return new Promise((resolve, reject) => { | |
cube.peripheral.disconnect(error => { | |
if (error !== null) { | |
reject(error) | |
} else { | |
resolve() | |
} | |
}) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Cube } = require('@toio/cube') | |
const { Scanner } = require('@toio/scanner') | |
/** | |
* Scan cubes which has certain id or address | |
*/ | |
class IdAddressScanner extends Scanner { | |
/** | |
* Constructor of IdAddressScanner class | |
* | |
* @param targets - list of id or address | |
* @param fuzzy - use fuzzy matching or not (first match or perfect match) | |
* @param timeoutMs - timeout duration in millisecond. 0 means no timeout | |
*/ | |
constructor(targets, fuzzy = true, timeoutMs = Scanner.DEFAULT_TIMEOUT_MS) { | |
super(timeoutMs) | |
this.targets = targets.map(target => target.toLowerCase()) | |
this.fuzzy = fuzzy | |
} | |
onDiscover(peripheral) { | |
if (this.match(peripheral.address) || this.match(peripheral.id)) { | |
this.eventEmitter.emit('discover', new Cube(peripheral)) | |
} | |
} | |
executor() { | |
return | |
} | |
match(candidate) { | |
if (this.fuzzy) { | |
return this.targets.some(target => { | |
return candidate.startsWith(target) | |
}) | |
} else { | |
return this.targets.includes(candidate) | |
} | |
} | |
} | |
module.exports = IdAddressScanner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment