Skip to content

Instantly share code, notes, and snippets.

@blia
Last active February 14, 2019 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blia/138432f29df0517c95c4be9b884b683e to your computer and use it in GitHub Desktop.
Save blia/138432f29df0517c95c4be9b884b683e to your computer and use it in GitHub Desktop.
const miio = require('miio');
const ws2812b = require('rpi-ws281x-native');
const { discover } = require('@w4f/lgtv/src/discover');
const { connect } = require('@w4f/lgtv/src/connect');
const { showFloat } = require('@w4f/lgtv/src/commands');
const { Strip, initStrip } = require('./Strip');
const { findCube, findDoorbell } = require('./utils');
async function initTV() {
let { address } = await discover(2000);
let tv = await connect(address);
await tv.send(showFloat('Connected to Raspberry Pi'));
return [tv, () => tv.disconnect()];
}
async function initMiHome() {
let hub = await miio.device({ address: '192.168.31.37', token: '6e1e7' });
return [hub, () => null];
}
async function swithPowerOfStripeOnCubeFreeFall({ miHome, strip }) {
let cube = findCube(miHome);
cube.on('action:free_fall', async () => {
await strip.switchPover();
})
}
async function changeBrightnessOfStripeOnCubeRotate({ miHome, strip }) {
let cube = findCube(miHome);
cube.on('action:rotate', async ({ amount }) => {
await strip.fadeTo(amount);
})
}
async function showFloatOnDoorbellPress({ miHome, strip, tv }) {
let doorbell = findDoorbell(miHome);
doorbell.on('action:click', async () => {
await tv.send(showFloat('Someone rings the doorbell'));
})
}
async function changeHueOfStripeOnCubeRotate({ miHome, strip }) {
let cube = findCube(miHome);
cube.on('action:rotate', async ({ amount }) => {
await strip.rotateHue(amount);
})
}
async function run() {
let [tv, disconnectFromTV] = await initTV();
let [miHome, disconnectFromMiHome] = await initMiHome();
let [strip, disconnectFromStrip] = await initStrip(ws2812b, 144);
let home = { tv, miHome, strip };
process.on('SIGINT', _ => {
disconnectFromTV();
disconnectFromStrip();
disconnectFromMiHome();
process.nextTick(() => process.exit(0));
});
await swithPowerOfStripeOnCubeFreeFall(home);
// await changeBrightnessOfStripeOnCubeRotate(home);
await changeHueOfStripeOnCubeRotate(home);
await showFloatOnDoorbellPress(home);
};
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment