Skip to content

Instantly share code, notes, and snippets.

@deanshub
Last active January 4, 2019 21:39
Show Gist options
  • Save deanshub/73370b4b5aafcd0d4e47eb11f0d5a45b to your computer and use it in GitHub Desktop.
Save deanshub/73370b4b5aafcd0d4e47eb11f0d5a45b to your computer and use it in GitHub Desktop.
import fs from 'fs-extra'
import path from 'path'
import Broadlink from './Broadlink'
import CONSTS from './consts'
import logger from './logger'
const b = new Broadlink()
import {LIVINGROOM_KEY, BEDROOM_KEY, WORKROOM_KEY} from './keys'
export const devicesReady = new Promise(resolve => {
const devices = {};
b.on('deviceReady', dev => {
dev.checkData();
if (dev.key === LIVINGROOM_KEY) {
dev.name = 'livingroom';
} else if (dev.key === BEDROOM_KEY) {
dev.name = 'bedroom';
} else if (dev.key === WORKROOM_KEY) {
dev.name = 'workroom';
}
devices[dev.name] = dev;
setTimeout(() => {
resolve(devices);
}, 200);
});
b.discover();
});
export const getDeviceByName = name => {
return devicesReady.then(devices => devices[name]);
};
export const sendSignal = async (signalFile, deviceName) => {
const dev = await getDeviceByName(deviceName);
const signalData = await fs.readFile(path.join(__dirname, '../signals', signalFile));
return dev.sendData(signalData);
};
export const learnSignal = async (deviceName, signalName) => {
const dev = await getDeviceByName(deviceName);
return new Promise((resolve, reject) => {
dev.on('rawData', signalData => {
const signalFilePath = path.join(__dirname, '../signals', `${signalName}.deg`);
fs.writeFile(signalFilePath, signalData)
.then(() => resolve(signalName))
.catch(reject);
});
dev.checkData();
dev.enterLearning();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment