-
Notifications
You must be signed in to change notification settings - Fork 7
/
settings.js
153 lines (127 loc) · 4.7 KB
/
settings.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
$(document).ready(async function() {
await getCurrentSettings();
// Activate tooltips.
var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
return new bootstrap.Tooltip(tooltipTriggerEl)
});
// Activate toasts.
var toastElList = [].slice.call(document.querySelectorAll('.toast'));
var toastList = toastElList.map(function (toastEl) {
return new bootstrap.Toast(toastEl)
});
$('#settings-form').on('input change', function() {
$('#save-settings-btn').attr('disabled', false);
});
await loadLastUser();
});
var currentUserId;
async function getCurrentSettings() {
// Get user's settings from db.
currentUserId = parseInt(localStorage.getItem('lastLoginUserID'));
const currentUserObj = {userId: currentUserId},
userCreds = await getDbRowWhere('device', currentUserObj),
xMax = userCreds[0].xMax,
yMax = userCreds[0].yMax,
lightPinNum = userCreds[0].lightPinNum,
zScanHeight = userCreds[0].zScanHeight;
// Set inputs to these values.
document.getElementById("inputXAxis").value = xMax;
document.getElementById("inputYAxis").value = yMax;
document.getElementById("inputLightPinNum").value = lightPinNum;
document.getElementById("inputZScanHeight").value = zScanHeight;
}
async function autoDetectFarmDetails() {
const farmSizeXInput = document.getElementById("inputXAxis"),
farmSizeYInput = document.getElementById("inputYAxis"),
lightPinInput = document.getElementById("inputLightPinNum");
// Check if can connect to API.
if (!apiConnected) {
const errModal = new bootstrap.Modal(document.getElementById('connection-error-modal'));
errModal.show();
return;
}
// Get the farm details.
const farmSize = await getFarmSize();
const lightPin = await findLightPin();
// Update form with new values.
farmSizeXInput.value = farmSize[0];
farmSizeYInput.value = farmSize[1];
lightPinInput.value = lightPin;
// Save to database new values.
saveSettings();
// Disable save button again if it isn't already.
document.getElementById("save-settings-btn").setAttribute("disabled", "");
}
async function clearMeshroomCache() {
const { ipcRenderer } = require('electron');
const successToastEle = document.getElementById('delSuccessToast'),
successToast = bootstrap.Toast.getInstance(successToastEle),
failToastEle = document.getElementById('delFailToast'),
failToast = bootstrap.Toast.getInstance(failToastEle);
const tempPath = await ipcRenderer.invoke('get-temp-path'),
meshroomCachePath = path.join(tempPath, 'MeshroomCache');
// If there, delete the cache.
if (fs.existsSync(meshroomCachePath)) {
fs.rmdirSync(meshroomCachePath, { recursive: true });
}
// Check if deleted and notify user on results.
if (!fs.existsSync(meshroomCachePath)) {
// Success.
successToast.show();
} else {
// Failure.
failToast.show();
}
}
async function saveSettings() {
const successToastEle = document.getElementById('autoDetectSuccessToast'),
successToast = bootstrap.Toast.getInstance(successToastEle);
// Get the values to save.
const farmSizeX = Number(document.getElementById("inputXAxis").value),
farmSizeY = Number(document.getElementById("inputYAxis").value),
lightPin = Number(document.getElementById("inputLightPinNum").value),
zHeight = Number(document.getElementById("inputZScanHeight").value);
// Check if values are valid.
let invalidValues = false;
if (!(farmSizeX > 0)) {
document.getElementById("inputXAxis").classList.add("is-invalid");
invalidValues = true;
} else {
document.getElementById("inputXAxis").classList.remove("is-invalid");
}
if (!(farmSizeY > 0)) {
document.getElementById("inputYAxis").classList.add("is-invalid");
invalidValues = true;
} else {
document.getElementById("inputYAxis").classList.remove("is-invalid");
}
if (document.getElementById("inputLightPinNum").value.length == 0) {
document.getElementById("inputLightPinNum").classList.add("is-invalid");
invalidValues = true;
} else {
document.getElementById("inputLightPinNum").classList.remove("is-invalid");
}
if (document.getElementById("inputZScanHeight").value.length == 0) {
document.getElementById("inputZScanHeight").classList.add("is-invalid");
invalidValues = true;
} else {
document.getElementById("inputZScanHeight").classList.remove("is-invalid");
}
if (invalidValues) {
return;
}
// Save them to db.
const currentUserObj = {userId: currentUserId},
newValuesObj = {
xMax: farmSizeX,
yMax: farmSizeY,
lightPinNum: lightPin,
zScanHeight: zHeight
};
await updateDbRowWhere('device', currentUserObj, newValuesObj);
// Disable save button again.
document.getElementById("save-settings-btn").setAttribute("disabled", "");
// Notify user.
successToast.show();
}