2022-04-30 16:19:31 +02:00
|
|
|
import * as hassioApiTools from "./hassioApiTools.js";
|
|
|
|
import logger from "../config/winston.js"
|
2022-09-21 17:24:02 +02:00
|
|
|
import { type Status } from "../types/status.js";
|
2019-12-19 15:08:47 +01:00
|
|
|
|
2022-09-21 17:24:02 +02:00
|
|
|
let status: Status = {
|
2019-12-19 15:08:47 +01:00
|
|
|
status: "idle",
|
2022-09-21 17:24:02 +02:00
|
|
|
last_backup: undefined,
|
|
|
|
next_backup: undefined,
|
|
|
|
progress: -1,
|
2020-02-15 14:28:48 +01:00
|
|
|
};
|
2019-12-19 15:08:47 +01:00
|
|
|
|
2022-04-30 16:19:31 +02:00
|
|
|
export function init() {
|
|
|
|
if (status.status !== "idle") {
|
|
|
|
status.status = "idle";
|
2022-09-21 17:24:02 +02:00
|
|
|
status.message = undefined;
|
|
|
|
status.progress = -1;
|
2019-12-19 15:08:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 16:19:31 +02:00
|
|
|
export function getStatus() {
|
|
|
|
return status;
|
2019-12-19 15:08:47 +01:00
|
|
|
}
|
|
|
|
|
2022-09-21 17:24:02 +02:00
|
|
|
export function setStatus(new_state: Status) {
|
|
|
|
const old_state_str = JSON.stringify(status);
|
2022-04-30 16:19:31 +02:00
|
|
|
if(old_state_str !== JSON.stringify(new_state)){
|
|
|
|
status = new_state;
|
|
|
|
hassioApiTools.publish_state(status);
|
2021-01-11 00:01:02 +01:00
|
|
|
}
|
2019-12-19 15:08:47 +01:00
|
|
|
}
|
|
|
|
|
2022-09-21 17:24:02 +02:00
|
|
|
export function setError(message: string, error_code: number){
|
2022-04-30 16:19:31 +02:00
|
|
|
// Check if we don't have another error stored
|
|
|
|
if (status.status != "error") {
|
|
|
|
status.status = "error"
|
|
|
|
status.message = message
|
|
|
|
status.error_code = error_code;
|
|
|
|
}
|
|
|
|
logger.error(message);
|
|
|
|
}
|