Last active
February 14, 2019 19:35
-
-
Save blia/138432f29df0517c95c4be9b884b683e 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 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