try different aproch for status

This commit is contained in:
SebClem 2024-04-10 15:13:41 +02:00
parent 17a6f1cbec
commit 35aaae183b
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
4 changed files with 65 additions and 28 deletions

View File

@ -9,3 +9,5 @@
- `7` => Fail to download snap - `7` => Fail to download snap
- `8` => Fail to stop addon - `8` => Fail to stop addon
- `9` => Fail to start addon - `9` => Fail to start addon

View File

@ -10,6 +10,7 @@ import type { WebdavBackup } from "../types/services/webdav.js";
import type { WebdavConfig } from "../types/services/webdavConfig.js"; import type { WebdavConfig } from "../types/services/webdavConfig.js";
import { templateToRegexp } from "./backupConfigService.js"; import { templateToRegexp } from "./backupConfigService.js";
import { getEndpoint } from "./webdavConfigService.js"; import { getEndpoint } from "./webdavConfigService.js";
import { WebdabStatus } from "../types/status.js";
const PROPFIND_BODY = const PROPFIND_BODY =
'<?xml version="1.0" encoding="utf-8" ?>\ '<?xml version="1.0" encoding="utf-8" ?>\
@ -38,6 +39,13 @@ export function checkWebdavLogin(config: WebdavConfig) {
}, },
(reason) => { (reason) => {
messageManager.error("Fail to connect to Webdav", reason?.message); messageManager.error("Fail to connect to Webdav", reason?.message);
const status = statusTools.getStatus();
status.webdav = {
state: WebdabStatus.LOGIN_FAIL,
blocked: true,
last_check: DateTime.now()
}
statusTools.setStatus(status);
logger.error(`Fail to connect to Webdav`); logger.error(`Fail to connect to Webdav`);
logger.error(reason); logger.error(reason);
return Promise.reject(reason); return Promise.reject(reason);
@ -61,6 +69,13 @@ export async function createBackupFolder(conf: WebdavConfig) {
messageManager.error("Fail to create webdav root folder"); messageManager.error("Fail to create webdav root folder");
logger.error("Fail to create webdav root folder"); logger.error("Fail to create webdav root folder");
logger.error(error); logger.error(error);
const status = statusTools.getStatus();
status.webdav = {
state: WebdabStatus.MK_FOLDER_FAIL,
blocked: true,
last_check: DateTime.now()
}
statusTools.setStatus(status);
return Promise.reject(error); return Promise.reject(error);
} }
} }

View File

@ -1,40 +1,46 @@
import { publish_state } from "../services/homeAssistantService.js"; import { publish_state } from "../services/homeAssistantService.js";
import logger from "../config/winston.js" import logger from "../config/winston.js";
import type { Status } from "../types/status.js"; import { type Status, WebdabStatus } from "../types/status.js";
import { DateTime } from "luxon";
let status: Status = { let status: Status = {
status: "idle", status: "idle",
last_backup: undefined, last_backup: undefined,
next_backup: undefined, next_backup: undefined,
progress: -1, progress: -1,
webdav: {
state: WebdabStatus.INIT,
last_check: DateTime.now(),
blocked: true,
},
}; };
export function init() { export function init() {
if (status.status !== "idle") { if (status.status !== "idle") {
status.status = "idle"; status.status = "idle";
status.message = undefined; status.message = undefined;
status.progress = -1; status.progress = -1;
} }
} }
export function getStatus() { export function getStatus() {
return status; return status;
} }
export function setStatus(new_state: Status) { export function setStatus(new_state: Status) {
const old_state_str = JSON.stringify(status); const old_state_str = JSON.stringify(status);
if(old_state_str !== JSON.stringify(new_state)){ if (old_state_str !== JSON.stringify(new_state)) {
status = new_state; status = new_state;
publish_state(status); publish_state(status);
} }
} }
export function setError(message: string, error_code: number){ export function setError(message: string, error_code: number) {
// Check if we don't have another error stored // Check if we don't have another error stored
if (status.status != "error") { if (status.status != "error") {
status.status = "error" status.status = "error";
status.message = message status.message = message;
status.error_code = error_code; status.error_code = error_code;
} }
logger.error(message); logger.error(message);
} }

View File

@ -1,3 +1,13 @@
import type { DateTime } from "luxon";
export enum WebdabStatus {
OK = "OK",
LOGIN_FAIL = "LOGIN_FAIL",
UPLOAD_FAIL = "UPLOAD_FAIL",
CON_ERROR = "CON_ERROR",
INIT = "INIT",
MK_FOLDER_FAIL = "MK_FOLDER_FAIL"
}
export interface Status { export interface Status {
status: string; status: string;
@ -6,5 +16,9 @@ export interface Status {
next_backup?: string; next_backup?: string;
message?: string; message?: string;
error_code?: number; error_code?: number;
webdav: {
state: WebdabStatus;
last_check: DateTime;
blocked: boolean;
};
} }