-
Notifications
You must be signed in to change notification settings - Fork 5
/
checklist.js
90 lines (69 loc) · 1.9 KB
/
checklist.js
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
const fs = require('fs');
const path = __dirname + '/server-data/checklist.json';
let checklistdb = undefined;
function load() {
fs.readFile(path, (err, data) => {
if (err) {
console.error(err);
return;
}
checklistdb = JSON.parse(data);
// console.log(JSON.parse(data));
console.log("checklistdb Loaded")
});
};
load();
async function save(){
if(checklistdb == undefined) {
console.log("checklistdb undefined");
return ;
}
let droneJson = JSON.stringify(checklistdb, null, 2);
fs.writeFile(path, droneJson, err => {
if (err) {
console.error(err);
// return false;
} else {
console.log("Saved checklistdb to file");
}
});
};
exports.save = save;
// exports.editDrone = (req,res,next)=>{
//
// };
exports.get_checklistdbdb = function() {
return checklistdb;
};
exports.add = function(){
let d = {
"lid": checklistdb.next_lid++,
"label": "",
"items": []
};
checklistdb.lists.push(d);
save();
return d;
}
exports.del = function(did){
drochecklistdbnedb.lists = checklistdb.lists.filter(d=> d.did != did);
save();
console.log("Drone Removed: "+did);
}
exports.update = function(newdrone) {
let i = checklistdb.lists.findIndex(drone=>drone.did == newdrone.did);
if(i < 0 ){
console.error("Drone To be updated not found no updates mades to checklist.json");
return false;
}
// console.log(dronedb);
console.log(i);
// console.log(dronedb.lists[i].did);
if(checklistdb.lists[i].did != newdrone.did) throw "You Cant Change drone ID";
checklistdb.lists[i] = newdrone;
console.log("Updated Drone(did):" + checklistdb.lists[i].did);
save();
};
exports.get_drone_by_did = function(did){
return checklistdb.lists.find(drone=>drone.did == did);
};