Before cleanup
This commit is contained in:
89
src/index.js
89
src/index.js
@@ -1,11 +1,54 @@
|
||||
#!/bin/env node
|
||||
const { execSync, spawnSync } = require("child_process");
|
||||
const debug = require("debug");
|
||||
const { spawnSync } = require("child_process");
|
||||
const yargs = require("yargs");
|
||||
const suncalc = require("suncalc");
|
||||
const dbus = require("./dbus");
|
||||
const pulse = require("./pulse");
|
||||
const mpd = require("./mpd");
|
||||
const { colors, brightness } = require("./colors");
|
||||
const DANGEROUS_VOLUME = 40;
|
||||
const AUDIO_NAME = "analog-surround-51";
|
||||
const MIC_NAME = "C-Media_Electronics_Inc._USB_PnP_Sound_Device";
|
||||
|
||||
const o = yargs.options({
|
||||
a: {
|
||||
alias: "audio",
|
||||
default: "analog-surround-51",
|
||||
describe: "String used to match the pulse audio device",
|
||||
type: "string",
|
||||
},
|
||||
m: {
|
||||
alias: ["mic", "record"],
|
||||
default: "C-Media_Electronics_Inc._USB_PnP_Sound_Device",
|
||||
describe: "String used to match the pulse microphone device",
|
||||
type: "string",
|
||||
},
|
||||
v: {
|
||||
alias: "verbose",
|
||||
default: 0,
|
||||
describe: "Verbosity level, add more -v for more verbosity",
|
||||
type: "count",
|
||||
},
|
||||
mpdport: {
|
||||
alias: "p",
|
||||
default: 6600,
|
||||
describe: "MPD port",
|
||||
type: "number",
|
||||
},
|
||||
mpdhost: {
|
||||
alias: "h",
|
||||
default: "localhost",
|
||||
describe: "MPD Host",
|
||||
type: "string",
|
||||
},
|
||||
}).argv;
|
||||
const AUDIO_NAME = o.a;
|
||||
const MIC_NAME = o.m;
|
||||
|
||||
const levels = ["*:error", "*:warn", "*:info", "*:log", "*:debug"].slice(
|
||||
0,
|
||||
o.v + 1,
|
||||
);
|
||||
|
||||
debug.enable(levels.join(","));
|
||||
|
||||
function isNight() {
|
||||
const now = new Date();
|
||||
@@ -14,18 +57,6 @@ function isNight() {
|
||||
return isnight;
|
||||
}
|
||||
|
||||
function setPlaying(matrix) {
|
||||
const playing = !spawnSync("mpc | grep playing", { shell: true }).status;
|
||||
[18, 19, 20].forEach(x =>
|
||||
dbus.setKey(
|
||||
matrix,
|
||||
0,
|
||||
x,
|
||||
...(playing ? colors.ALERT_COLOR.rgb() : colors.BASE_COLOR.rgb()),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function getBrightness() {
|
||||
return isNight() ? 60 : 100;
|
||||
}
|
||||
@@ -79,7 +110,7 @@ async function setAudio(matrix) {
|
||||
volume < DANGEROUS_VOLUME
|
||||
? colors.BASE_COLOR.rgb()
|
||||
: colors.ALERT_COLOR.rgb(),
|
||||
volume / 50,
|
||||
(volume / 50) * 100,
|
||||
);
|
||||
keys.forEach(x => dbus.setKey(matrix, 0, x, ...c.rgb()));
|
||||
}
|
||||
@@ -96,4 +127,26 @@ async function run() {
|
||||
dbus.disconnect();
|
||||
}
|
||||
|
||||
run();
|
||||
async function daemon() {
|
||||
const matrix = dbus.staticMatrix(...colors.BASE_COLOR.rgb(), 6, 22);
|
||||
const setKey = (...args) => {
|
||||
dbus.setKey(matrix, ...args);
|
||||
};
|
||||
const interfaces = await dbus.connect();
|
||||
const draw = async () => await interfaces.chroma.setKeyRow(matrix);
|
||||
|
||||
const mpdDisconnect = mpd(
|
||||
{ host: o.mpdhost, port: o.mpdport },
|
||||
setKey,
|
||||
draw,
|
||||
)();
|
||||
const p = pulse.connect({ out: AUDIO_NAME, mic: MIC_NAME, setKey, draw });
|
||||
|
||||
process.on("SIGINT", () => {
|
||||
mpdDisconnect();
|
||||
dbus.disconnect();
|
||||
p.disconnect();
|
||||
});
|
||||
}
|
||||
|
||||
daemon();
|
||||
|
||||
Reference in New Issue
Block a user