Last active
September 23, 2021 19:40
-
-
Save dmattia/99c85c5968e1e06adf6beb5adb300655 to your computer and use it in GitHub Desktop.
PetTutor Feed dispense
This file contains hidden or 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
// external | |
import noble from '@abandonware/noble'; | |
noble.on('stateChange', async (state) => { | |
if (state === 'poweredOn') { | |
await noble.startScanningAsync(); | |
} | |
}); | |
noble.on('scanStart', () => { | |
logger.info(`Began scanning.`); | |
setTimeout(async () => { | |
await noble.stopScanningAsync(); | |
}, 1000 * 100); | |
}); | |
noble.on('discover', async (peripheral) => { | |
if (!peripheral.advertisement.localName === 'PTFeeder') { | |
return; | |
} | |
await peripheral.connectAsync(); | |
const { services } = | |
await peripheral.discoverAllServicesAndCharacteristicsAsync(); | |
// Find the service to talk to | |
const service = services.find( | |
({ uuid }) => uuid === 'b0e6a4bfccccffff330c0000000000f0', | |
); | |
if (!service) { | |
throw Error('Could not find service'); | |
} | |
console.info('Found service!'); | |
// Find the characteristic to talk to | |
const characteristic = service.characteristics.find( | |
({ uuid }) => uuid === 'b0e6a4bfccccffff330c0000000000f1', | |
); | |
if (!characteristic) { | |
throw Error('Could not find service'); | |
} | |
console.info('Found characteristic!'); | |
// Dispense a treat for a good dog | |
await characteristic.writeAsync(Buffer.from([0]), true); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment