Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'miio' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
async connect() {
const address = this.ip;
this.ref = await miio.device({ address });
if (!this.ref.matches('type:air-purifier')) {
throw new Error(`Not an air purifier given: ${address}`);
}
// patch for usages
this.ref.aqi = this.ref.pm2_5;
return true;
}
discover: function() {
var log = this.log;
var that = this;
miio.device({
address: this.ip,
token: this.token
})
.then(device => {
if (device.matches('type:air-purifier')) {
that.device = device;
console.log('Discovered Mi Air Purifier (%s) at %s', device.miioModel, this.ip);
log.debug('Model : ' + device.miioModel);
log.debug('Power : ' + device.property('power'));
log.debug('Mode : ' + device.property('mode'));
log.debug('Temperature : ' + device.property('temperature'));
log.debug('Humidity : ' + device.property('humidity'));
log.debug('Air Quality : ' + device.property('aqi'));
log.debug('LED : ' + device.property('led'));
_search(thisIp, thisToken, index) {
if (!this.platform.syncLock._enterSyncState(() => {
this._search(thisIp, thisToken, index);
})) {
return;
}
this.retryCount[index]++;
//Try to connect to miio device
this.log.info("[INFO]Device %s -> Connecting", thisIp);
miio.device({
address: thisIp,
token: thisToken
}).then((retDevice) => {
this.platform.devices[index] = retDevice;
this.log.info("[INFO]Device %s -> Connected", thisIp);
this.platform.startEvent.emit(index);
}).catch((err) => {
this.log.error("[ERROR]Device %s -> %s", thisIp, err);
setTimeout((() => {
if (this.retryCount[index] <= 3) {
this._search(thisIp, thisToken, index);
} else {
this.log.warn("[WARN]Cannot connect to device %s!", thisIp);
this.retryCount[index] = 0;
}
}), 30 * 1000);
browser.on('available', (reg) => {
if (!reg.token) { //power strip support Auto-token
return;
}
miio.device(reg).then((device) => {
if (device.type != this.model) {
return;
}
this.devices[reg.id] = device;
this.mijia.log.debug('find model->%s with hostname->%s id->%s @ %s:%s.', device.model, reg.hostname, device.id, device.address, device.port);
this.setPowerStrip(reg, 0, device);
});
});
browser.on('available', (reg) => {
if (!reg.token) { //support Auto-token
return;
}
miio.device(reg).then((device) => {
this.devices[reg.id] = device;
this.mijia.log.debug('find model->%s with hostname->%s id->%s @ %s:%s.', device.model, reg.hostname, device.id, device.address, device.port);
this.setLightbulb(reg, device);
});
});
browser.on('available', (reg) => {
if (!reg.token) { //airpurifier support Auto-token
return;
}
miio.device(reg).then((device) => {
if (device.type != this.model) {
return;
}
this.devices[reg.id] = device;
this.mijia.log.debug('find model->%s with hostname->%s id->%s @ %s:%s.', device.model, reg.hostname, device.id, device.address, device.port);
this.setAirPurifier(reg, device);
});
});
//Init
this.log = log;
this.config = config;
this.Accessory = Accessory;
this.PlatformAccessory = PlatformAccessory;
this.Service = Service;
this.Characteristic = Characteristic;
this.UUIDGen = UUIDGen;
var that = this;
if(null != this.config['ip'] && null != this.config['token']){
this.device = 1;
miio.device({ address: this.config['ip'], token: this.config['token'] })
.then(function(device){
that.device = device;
that.log("[XiaoMiAcPartner][INFO]Discovered Device(Global)!");
}).catch(function(err){
that.log.error("[XiaoMiAcPartner][ERROR]Cannot connect to AC Partner. " + err);
})
}
this.log.info("[XiaoMiAcPartner][INFO]Plugin start successful");
}
createDevice() {
miio.device({
address: this.getSetting('address'),
token: this.getSetting('token')
}).then(miiodevice => {
if (!this.getAvailable()) {
this.setAvailable();
}
this.miio = miiodevice;
var interval = this.getSetting('polling') || 60;
this.pollDevice(interval);
}).catch((error) => {
this.log(error);
this.setUnavailable(Homey.__('unreachable'));
setTimeout(() => {
this.createDevice();
}, 10000);
createDevice() {
miio.device({
address: this.getSetting('address'),
token: this.getSetting('token')
}).then(miiodevice => {
if (!this.getAvailable()) {
this.setAvailable();
}
this.miio = miiodevice;
this.miio.on('colorChanged', c => {
var colortemp = util.normalize(c.values[0], 3000, 5700);
if (this.getCapabilityValue('light_temperature') != colortemp) {
this.setCapabilityValue('light_temperature', colortemp);
}
});
var interval = this.getSetting('polling') || 60;
createDevice() {
miio.device({
address: this.getSetting('address'),
token: this.getSetting('token')
}).then(miiodevice => {
if (!this.getAvailable()) {
this.setAvailable();
}
this.miio = miiodevice;
var interval = this.getSetting('polling') || 30;
this.pollDevice(interval);
}).catch((error) => {
this.log(error);
this.setUnavailable(Homey.__('unreachable'));
setTimeout(() => {
this.createDevice();
}, 10000);