Last active
February 14, 2020 18:12
-
-
Save Deses/bf2355075ac38137923129c2da136da0 to your computer and use it in GitHub Desktop.
A little script to turn off a Tuya plug,
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 off!'); | |
} else { | |
console.log('The printer is on. Turning it off.'); | |
if (!stateHasChanged) { | |
device.set({set: false}); | |
stateHasChanged = true; | |
} | |
} | |
}); | |
setTimeout(() => { device.disconnect(); }, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment