aboutsummaryrefslogtreecommitdiff
path: root/src/sys.js
blob: 8b19f225c78c399103155a2563d838c7e59b76f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const { invoke } = window.__TAURI__.tauri;
const { message } = window.__TAURI__.dialog;

let passwordOldEl;
let passwordNewEl;
let passwordRepeatEl;
let passwordSubmitEl;
let passwordStatusEl;

async function reboot() {
  const error = await invoke("reboot", {});

  if (error !== "") {
    await message("Befehl konnte nicht erteilt werden: " + error, {
      kind: "error",
      title: "Neustart nicht erfolgt"
    });
  }

  const successTime = Date.now();

  while ((Date.now() - successTime) < 60) {
    await message("Neustart läuft. Bitte ca. 1 Minute warten.", {
      kind: "info",
      title: "Neustart im Gange"
    });
  }
}

async function shutdown() {
  const error = await invoke("shutdown", {});

  if (error !== "") {
    await message("Befehl konnte nicht erteilt werden: " + error, {
      kind: "error",
      title: "Herunterfahren nicht erfolgt"
    });
  }

  await message("Router erfolgreich heruntergefahren. Bitte Stromkabel abziehen.", {
    kind: "info",
    title: "Herunterfahren erfolgreich"
  });

  window.location = "index.html";
}

function showPassword() {
  switch (passwordNewEl.type) {
    case "password":
      passwordNewEl.type = "text";
      passwordRepeatEl.type = "text";
      break;
    case "text":
      passwordNewEl.type = "password";
      passwordRepeatEl.type = "password";
      break;
  }
}

async function changePassword() {
  passwordOldEl.disabled = true;
  passwordNewEl.disabled = true;
  passwordRepeatEl.disabled = true;
  passwordSubmitEl.disabled = true;
  passwordStatusEl.innerText = "Änderungsanfrage...";
  document.body.style.cursor = "progress";

  passwordStatusEl.innerText = await invoke("change_sys_password", {
    old: passwordOldEl.value,
    to: passwordNewEl.value,
    repeat: passwordRepeatEl.value,
  });

  passwordOldEl.disabled = false;
  passwordNewEl.disabled = false;
  passwordRepeatEl.disabled = false;
  passwordSubmitEl.disabled = false;
  document.body.style.cursor = "default";

  if (passwordStatusEl.innerText === "Änderung erfolgreich") {
    await message("Passwort erfolgreich geändert. Melden Sie sich neu an, um das Verwaltungswerkzeug weiter benutzen zu können.", {
      kind: "info",
      title: "Neuanmeldung erforderlich",
    });

    window.location = "index.html";
  }
}

window.addEventListener("DOMContentLoaded", () => {
  document.querySelector("#power-reboot").addEventListener("click", (e) => {
    e.preventDefault();
    reboot();
  });
  document.querySelector("#power-shutdown").addEventListener("click", (e) => {
    e.preventDefault();
    shutdown();
  });

  passwordOldEl = document.querySelector("#password-old");
  passwordNewEl = document.querySelector("#password-new");
  passwordRepeatEl = document.querySelector("#password-repeat");
  passwordSubmitEl = document.querySelector("#password-submit");
  passwordStatusEl = document.querySelector("#password-status");

  document.querySelector("#password-show").addEventListener("click", (e) => {
    e.preventDefault();
    showPassword();
  });
  document.querySelector("#password-form").addEventListener("submit", (e) => {
    e.preventDefault();
    changePassword();
  });
});