idasen-control
Advanced tools
Comparing version 1.0.3 to 1.1.0
{ | ||
"name": "idasen-control", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "IKEA Idasen Desk Control", | ||
@@ -31,3 +31,5 @@ "bin": "./src/index.js", | ||
"commander": "^5.1.0", | ||
"node-schedule": "^1.3.2" | ||
"desktop-idle": "^1.3.0", | ||
"node-schedule": "^1.3.2", | ||
"pretty-ms": "^7.0.0" | ||
}, | ||
@@ -34,0 +36,0 @@ "devDependencies": { |
@@ -156,3 +156,3 @@ const EventEmitter = require("events"); | ||
const isMovingUp = targetPosition > this.position; | ||
const stopThreshold = 1; | ||
const stopThreshold = 1.0; | ||
@@ -162,2 +162,3 @@ let lastPosition = this.position; | ||
let shouldStopCounter = 0; | ||
let lastCommand = 0; | ||
@@ -170,9 +171,13 @@ try { | ||
) { | ||
await this.ensureConnection(); | ||
await this.controlChar.writeAsync( | ||
isMovingUp ? Desk.control().up : Desk.control().down, | ||
false | ||
); | ||
if (lastCommand == 0 || lastCommand < +new Date() - 300) { | ||
await this.ensureConnection(); | ||
await this.controlChar.writeAsync( | ||
isMovingUp ? Desk.control().up : Desk.control().down, | ||
false | ||
); | ||
lastCommand = +new Date(); | ||
} | ||
await new Promise((resolve) => setTimeout(resolve, 50)); | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
await this.readPosition(); | ||
@@ -179,0 +184,0 @@ |
@@ -9,2 +9,4 @@ #!/usr/bin/env node | ||
const { promisify } = require("util"); | ||
const { getIdleTime } = require("desktop-idle"); | ||
const prettyMilliseconds = require("pretty-ms"); | ||
@@ -17,2 +19,5 @@ const readFile = promisify(fs.readFile); | ||
const PIDFILE = "/tmp/idasen-control.pid"; | ||
const CHECK_INTERVAL = 5.0; | ||
const BREAK_TIME = 2 * 60; | ||
const STAND_THRESHOLD = 30; | ||
@@ -27,3 +32,5 @@ program | ||
) | ||
.option("-p, --get-pos", "Get the current position") | ||
.option("-s, --status", "Get the current status") | ||
.option("--prompt-fragment", "Render a prompt fragment") | ||
.option("--json", "Output as JSON") | ||
.parse(process.argv); | ||
@@ -37,9 +44,13 @@ | ||
if (!(await readPid())) { | ||
const env = { ...process.env, IDASEN_START_SERVER: "1" }; | ||
const [_first, ...argv] = process.argv; | ||
spawn(process.execPath, argv, { | ||
env, | ||
detached: true, | ||
stdio: "ignore", | ||
}); | ||
if (process.env.IDASEN_NO_DAEMON === "1") { | ||
runServer(); | ||
} else { | ||
const env = { ...process.env, IDASEN_START_SERVER: "1" }; | ||
const [_first, ...argv] = process.argv; | ||
spawn(process.execPath, argv, { | ||
env, | ||
detached: true, | ||
stdio: "ignore", | ||
}); | ||
} | ||
await sleep(100); | ||
@@ -49,5 +60,35 @@ } | ||
if (program.moveTo) { | ||
console.log(await sendCommand({ op: "moveTo", pos: program.moveTo })); | ||
} else if (program.getPos) { | ||
console.log(await sendCommand({ op: "getPos" })); | ||
await sendCommand({ op: "moveTo", pos: program.moveTo }); | ||
} else if (program.status) { | ||
const status = await sendCommand({ op: "getStatus" }); | ||
if (program.json) { | ||
console.log(JSON.stringify(status)); | ||
} else { | ||
console.log(`height: ${status.height} (${status.pos})`); | ||
console.log( | ||
`time sitting: ${prettyMilliseconds(status.sittingTime * 1000)}` | ||
); | ||
} | ||
} else if (program.promptFragment) { | ||
const template = | ||
process.env.IDASEN_PROMPT_TEMPLATE || | ||
"%(sittingWarning)s %(standingHint)s"; | ||
const status = await sendCommand({ op: "getStatus" }); | ||
let vars = { | ||
sittingTime: prettyMilliseconds(status.sittingTime * 1000), | ||
sittingWarning: | ||
status.sittingTime >= 30 * 60 | ||
? `sitting for ${prettyMilliseconds(status.sittingTime * 1000)}` | ||
: "", | ||
positionHint: status.pos, | ||
standingHint: status.pos === "standing" ? "standing" : "", | ||
sittingHint: status.pos === "sitting" ? "sitting" : "", | ||
}; | ||
console.log( | ||
template | ||
.replace(/%\((.*?)\)s/g, (_, group) => { | ||
return vars[group] || ""; | ||
}) | ||
.trim() | ||
); | ||
} | ||
@@ -116,2 +157,6 @@ } | ||
function describePosition(desk) { | ||
return desk.position >= STAND_THRESHOLD ? "standing" : "sitting"; | ||
} | ||
async function sendCommand(cmd) { | ||
@@ -131,3 +176,3 @@ return new Promise((resolve) => { | ||
if (process.env.IDASEN_START_SERVER === "1") { | ||
async function runServer() { | ||
let resolveReadyPromise = null; | ||
@@ -137,2 +182,3 @@ let readyPromise = new Promise((resolve) => { | ||
}); | ||
let sittingTime = 0; | ||
@@ -147,2 +193,14 @@ const manager = new DeskManager({ | ||
setInterval(() => { | ||
readyPromise.then((desk) => { | ||
// someone did something | ||
const idleTime = getIdleTime(); | ||
if (idleTime < CHECK_INTERVAL && desk.position < STAND_THRESHOLD) { | ||
sittingTime += CHECK_INTERVAL; | ||
} else if (desk.position >= STAND_THRESHOLD || idleTime >= BREAK_TIME) { | ||
sittingTime = 0; | ||
} | ||
}); | ||
}, CHECK_INTERVAL * 1000); | ||
ensureServer(async (message) => { | ||
@@ -156,5 +214,9 @@ if (message.op === "moveTo") { | ||
return true; | ||
} else if (message.op === "getPos") { | ||
} else if (message.op === "getStatus") { | ||
const desk = await readyPromise; | ||
return desk.position; | ||
return { | ||
height: desk.position, | ||
pos: describePosition(desk), | ||
sittingTime, | ||
}; | ||
} else { | ||
@@ -179,4 +241,8 @@ return false; | ||
}); | ||
} | ||
if (process.env.IDASEN_START_SERVER === "1") { | ||
runServer().then(() => process.exit(0)); | ||
} else { | ||
runClient().then(() => process.exit(0)); | ||
} |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
16326
494
5
6
+ Addeddesktop-idle@^1.3.0
+ Addedpretty-ms@^7.0.0
+ Addeddesktop-idle@1.3.0(transitive)
+ Addedparse-ms@2.1.0(transitive)
+ Addedpretty-ms@7.0.1(transitive)