From 2144d7ef692aefe08485e796ee6057657309f162 Mon Sep 17 00:00:00 2001 From: ztimson Date: Tue, 29 Oct 2024 00:39:52 +0000 Subject: [PATCH] Fixes --- src/daemon.js | 2 +- src/main.js | 22 +++++++++++++++------- src/sensor-suite.js | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/daemon.js b/src/daemon.js index 5f7201a..ea9c600 100644 --- a/src/daemon.js +++ b/src/daemon.js @@ -7,7 +7,7 @@ export default class Daemon { apollo; express; - constructor(port = 80) { + constructor(port = 1969) { this.apollo = APOLLO; this.apollo.start(); diff --git a/src/main.js b/src/main.js index 59ce562..716e4fe 100644 --- a/src/main.js +++ b/src/main.js @@ -1,18 +1,19 @@ import {ask} from './misc.js'; import Daemon from './daemon.js'; -const command = process.argv[2]; -let remote = 'localhost'; +const command = process.argv.slice(2).join(' '); +let remote = 'localhost:1969'; function help() { return ` Apollo v0.0.0 +Remote: ${remote} Commands: exit - Exit CLI help - Display this manual reboot - Reboot System -remote - Connect to remote Apollo +remote - Get/Set Apollo address sensors - Display sensor data shutdown - Shutdown System start - Start Apollo server @@ -22,15 +23,22 @@ status - Apollo Subsystems status } function run(cmd) { + try { if(cmd.toLowerCase() === 'exit') process.exit(); - else if(cmd === 'help') return this.help(); - else if(cmd.startsWith('remote')) remote = cmd.split(' ').pop(); - else if(cmd.startsWith('start')) new Daemon(+cmd.split(' ').pop() || 80); + else if(cmd === 'help') return help(); + else if(cmd === 'remote') return remote; + else if(cmd.startsWith('remote')) { + remote = cmd.split(' ').pop(); + return `Remote Set: ${remote}`; + } else if(cmd.startsWith('start')) new Daemon(+cmd.split(' ').pop() || 1969); else return fetch(`${remote.startsWith('http') ? '' : 'http://'}${remote}/api/${cmd}`).then(resp => { - if(resp.ok && resp.headers['Content-Type'].includes('json')) + if(resp.ok && resp.headers['Content-Type']?.includes('json')) return resp.json(); else return resp.text(); }).catch(err => err.message); + } catch(err) { + return err.message || err; + } } if(command) console.log(run(command)); diff --git a/src/sensor-suite.js b/src/sensor-suite.js index 4c4b2ed..df0fd64 100644 --- a/src/sensor-suite.js +++ b/src/sensor-suite.js @@ -27,7 +27,7 @@ export default class SensorSuite { async start() { this.intervals.push(adjustedInterval(async () => { this.data.environment = await this.statusWrapper(bme(), 'bme280'); - if(this.data.environment.pressure != null) + if(this.data.environment?.pressure != null) this.data.environment.altitude = SensorSuite.#hPaToAltitude(this.data.environment.pressure); }, 1000)); await sleep(500); // Offset reading sensor data