feat(core): Centralize timeouts/constants, add CLI prompt helpers, and introduce webhook/script actions with safety and SNMP refactors

This commit is contained in:
2026-01-29 17:04:12 +00:00
parent d0e3a4ae74
commit 07648b4880
24 changed files with 1019 additions and 590 deletions

118
ts/constants.ts Normal file
View File

@@ -0,0 +1,118 @@
/**
* NUPST Constants
*
* Central location for all timeout, interval, and threshold values.
* This makes configuration easier and code more self-documenting.
*/
/**
* Default timing values in milliseconds
*/
export const TIMING = {
/** Default interval between UPS status checks (30 seconds) */
CHECK_INTERVAL_MS: 30000,
/** Interval for idle monitoring mode (60 seconds) */
IDLE_CHECK_INTERVAL_MS: 60000,
/** Interval for checking config file changes (60 seconds) */
CONFIG_CHECK_INTERVAL_MS: 60000,
/** Interval for logging periodic status updates (5 minutes) */
LOG_INTERVAL_MS: 5 * 60 * 1000,
/** Maximum time to monitor during shutdown (5 minutes) */
MAX_SHUTDOWN_MONITORING_MS: 5 * 60 * 1000,
/** Interval for UPS checks during shutdown (30 seconds) */
SHUTDOWN_CHECK_INTERVAL_MS: 30000,
} as const;
/**
* SNMP-related constants
*/
export const SNMP = {
/** Default SNMP port */
DEFAULT_PORT: 161,
/** Default SNMP timeout (5 seconds) */
DEFAULT_TIMEOUT_MS: 5000,
/** Number of SNMP retries */
RETRIES: 2,
/** Timeout for noAuthNoPriv security level (5 seconds) */
TIMEOUT_NO_AUTH_MS: 5000,
/** Timeout for authNoPriv security level (10 seconds) */
TIMEOUT_AUTH_MS: 10000,
/** Timeout for authPriv security level (15 seconds) */
TIMEOUT_AUTH_PRIV_MS: 15000,
/** Maximum timeout for connection tests (10 seconds) */
MAX_TEST_TIMEOUT_MS: 10000,
} as const;
/**
* Default threshold values
*/
export const THRESHOLDS = {
/** Default battery capacity threshold for shutdown (60%) */
DEFAULT_BATTERY_PERCENT: 60,
/** Default runtime threshold for shutdown (20 minutes) */
DEFAULT_RUNTIME_MINUTES: 20,
/** Emergency runtime threshold during shutdown (5 minutes) */
EMERGENCY_RUNTIME_MINUTES: 5,
} as const;
/**
* Webhook action constants
*/
export const WEBHOOK = {
/** Default webhook request timeout (10 seconds) */
DEFAULT_TIMEOUT_MS: 10000,
} as const;
/**
* Script action constants
*/
export const SCRIPT = {
/** Default script execution timeout (60 seconds) */
DEFAULT_TIMEOUT_MS: 60000,
} as const;
/**
* Shutdown action constants
*/
export const SHUTDOWN = {
/** Default shutdown delay (5 minutes) */
DEFAULT_DELAY_MINUTES: 5,
} as const;
/**
* HTTP Server constants
*/
export const HTTP_SERVER = {
/** Default HTTP server port */
DEFAULT_PORT: 8080,
/** Default URL path for UPS status endpoint */
DEFAULT_PATH: '/ups-status',
} as const;
/**
* UI/Display constants
*/
export const UI = {
/** Default width for log boxes */
DEFAULT_BOX_WIDTH: 45,
/** Wide box width for status displays */
WIDE_BOX_WIDTH: 60,
/** Extra wide box width for detailed info */
EXTRA_WIDE_BOX_WIDTH: 70,
} as const;