Created
February 14, 2020 18:11
-
-
Save Deses/b0d36e3431137c00b0200b954621c462 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 TuyAPI = require('tuyapi'); | |
const device = new TuyAPI({ | |
id: 'XXXXXXXXXXXXXXXXXXXX', | |
key: 'XXXXXXXXXXXXXXXX'}); | |
let stateHasChanged = false; | |
// Find device on network | |
device.find().then(() => { | |
device.connect(); | |
}); | |
// Add event listeners | |
device.on('connected', () => { | |
console.log('Connected to device!'); | |
}); | |
device.on('disconnected', () => { | |
console.log('Disconnected from device.'); | |
}); | |
device.on('error', error => { | |
console.log('Error!', error); | |
}); | |
device.on('data', data => { | |
//console.log('Data from device:', data.dps['1']); | |
if (data.dps['1']) { | |
console.log('The printer is already on!'); | |
} else { | |
console.log('The printer is off. Turning it on.'); | |
if (!stateHasChanged) { | |
device.set({set: true}); | |
stateHasChanged = true; | |
} | |
} | |
}); | |
setTimeout(() => { device.disconnect(); }, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment