Handle Alarms
This commit is contained in:
parent
efdd3a29b3
commit
6e54ad1a87
24 changed files with 1148 additions and 136 deletions
63
ui/src/lib/alarmsingle.js
Normal file
63
ui/src/lib/alarmsingle.js
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
export class AlarmSingle {
|
||||
constructor(res) {
|
||||
if (res) {
|
||||
this.update(res);
|
||||
}
|
||||
}
|
||||
|
||||
update({ id, time, routines, comment }) {
|
||||
this.id = id;
|
||||
this.time = new Date(time);
|
||||
this.routines = routines;
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
async delete() {
|
||||
const res = await fetch(`api/alarms/single/${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/single/${this.id}`:'api/alarms/single', {
|
||||
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 getAlarmsSingle() {
|
||||
const res = await fetch(`api/alarms/single`, {headers: {'Accept': 'application/json'}})
|
||||
if (res.status == 200) {
|
||||
const data = await res.json();
|
||||
if (data === null)
|
||||
return [];
|
||||
else
|
||||
return data.map((t) => new AlarmSingle(t));
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAlarmSingle(aid) {
|
||||
const res = await fetch(`api/alarms/single/${aid}`, {headers: {'Accept': 'application/json'}})
|
||||
if (res.status == 200) {
|
||||
return new AlarmSingle(await res.json());
|
||||
} else {
|
||||
throw new Error((await res.json()).errmsg);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue