Last active
August 9, 2018 15:25
-
-
Save aurimasniekis/4d47cc1fe0f9245dbfcba3dccaec7fa8 to your computer and use it in GitHub Desktop.
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 noble = require('noble'); | |
const { EventEmitter } = require('events'); | |
class PomoLight extends EventEmitter { | |
constructor(prefix, serviceUUID, writeUUID) { | |
super(); | |
this.prefix = prefix; | |
this.serviceUUID = serviceUUID; | |
this.writeUUID = writeUUID; | |
this.foundDevices = {}; | |
this.connected = false; | |
this.stream = null; | |
this.peripheral = null; | |
this.state = null; | |
this.foundDevice = false; | |
noble.on('discover', (peripheral) => { | |
this.onDiscover(peripheral); | |
}); | |
} | |
start() { | |
noble.on('stateChange', (state) => { | |
this.onStateChange(state); | |
}); | |
if (this.state === 'poweredOn') { | |
console.log('Scanning'); | |
noble.startScanning([], true); | |
} | |
} | |
onDiscover(peripheral) { | |
const name = peripheral.advertisement.localName || ''; | |
if (name.startsWith(this.prefix)) { | |
this.foundDevices[name] = peripheral; | |
} | |
this.emit('foundNewDevice', this.foundDevices); | |
} | |
onStateChange(state) { | |
this.state = state; | |
if (state === 'poweredOn') { | |
console.log('Scanning'); | |
noble.startScanning([], true); | |
} | |
} | |
connect(peripheral) { | |
if (this.foundDevice) { | |
return; | |
} | |
this.foundDevice = true; | |
noble.stopScanning(); | |
peripheral.connect(); | |
peripheral.once('disconnect', () => { | |
console.log('Disconnect'); | |
this.disconnect(); | |
this.start(); | |
}); | |
peripheral.once('connect', () => { | |
console.log('Connect'); | |
this.peripheral = peripheral; | |
this.connected = true; | |
peripheral.discoverSomeServicesAndCharacteristics([this.serviceUUID], [this.writeUUID], (err, services, characteristics) => { | |
if (characteristics.length === 1) { | |
console.log('Connected'); | |
this.stream = characteristics[0]; | |
this.emit('connected'); | |
} | |
}); | |
}); | |
} | |
disconnect() { | |
this.connected = false; | |
this.stream = null; | |
this.foundDevice = false; | |
} | |
changeColor(red, green, blue) { | |
if (this.connected) { | |
this.stream.write(Buffer.from([0, red, green, blue])); | |
return true; | |
} | |
return false; | |
} | |
} | |
const a = new PomoLight('Pomo Light', '6e400001b5a3f393e0a9e50e24dcca9e', '6e400002b5a3f393e0a9e50e24dcca9e'); | |
a.on('foundNewDevice', (devices) => { | |
const b = devices['Pomo Light #1']; | |
if (b) { | |
a.connect(b); | |
} | |
}); | |
const sleep = ms => new Promise(res => setTimeout(res, ms)); | |
a.on('connected', async () => { | |
while (true) { | |
a.changeColor(255, 0, 0); | |
await sleep(500); | |
a.changeColor(0, 255, 0); | |
await sleep(500); | |
a.changeColor(0, 0, 255); | |
await sleep(500); | |
} | |
}); | |
a.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment