Handle Alarms

This commit is contained in:
nemunaire 2022-10-05 22:33:31 +02:00
commit 6e54ad1a87
24 changed files with 1148 additions and 136 deletions

View file

@ -0,0 +1,91 @@
export class AlarmRepeated {
constructor(res) {
if (res) {
this.update(res);
}
}
update({ id, weekday, time, routines, ignore_exceptions, comment }) {
this.id = id;
this.weekday = weekday;
this.time = time;
this.routines = routines;
this.ignore_exceptions = ignore_exceptions;
this.comment = comment;
}
async delete() {
const res = await fetch(`api/alarms/repeated/${this.id}`, {
method: 'DELETE',
headers: {'Accept': 'application/json'}
});
if (res.status == 200) {
return true;
} else {
throw new Error((await res.json()).errmsg);
}
}
async save() {
const res = await fetch(this.id?`api/alarms/repeated/${this.id}`:'api/alarms/repeated', {
method: this.id?'PUT':'POST',
headers: {'Accept': 'application/json'},
body: JSON.stringify(this),
});
if (res.status == 200) {
const data = await res.json();
this.update(data);
return data;
} else {
throw new Error((await res.json()).errmsg);
}
}
}
export async function getAlarmsRepeated() {
const res = await fetch(`api/alarms/repeated`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
const data = await res.json();
if (data === null)
return [];
else
return data.map((t) => new AlarmRepeated(t));
} else {
throw new Error((await res.json()).errmsg);
}
}
export async function getAlarmRepeated(aid) {
const res = await fetch(`api/alarms/repeated/${aid}`, {headers: {'Accept': 'application/json'}})
if (res.status == 200) {
return new AlarmRepeated(await res.json());
} else {
throw new Error((await res.json()).errmsg);
}
}
export function weekdayStr(weekday) {
switch (weekday) {
case 0:
case "0":
return "dimanche";
case 1:
case "1":
return "lundi";
case 2:
case "2":
return "mardi";
case 3:
case "3":
return "mercredi";
case 4:
case "4":
return "jeudi";
case 5:
case "5":
return "vendredi";
case 6:
case "6":
return "samedi";
}
}