-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
180 lines (157 loc) · 6.57 KB
/
script.js
File metadata and controls
180 lines (157 loc) · 6.57 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
document.addEventListener('DOMContentLoaded', () => {
const speedEl = document.getElementById('speed');
const temperatureEl = document.getElementById('temperature');
const voltageEl = document.getElementById('voltage');
const currentEl = document.getElementById('current');
const distanceEl = document.getElementById('distance');
const powerEl = document.getElementById('power');
const wifiStatusEl = document.getElementById('wifi-status');
const loraStatusEl = document.getElementById('lora-status');
const emergencyBrakesStatusEl = document.getElementById('emergency-brakes-status');
const logsEl = document.getElementById('logs');
const startBtn = document.getElementById('start-btn');
const stopBtn = document.getElementById('stop-btn');
const speedSlider = document.getElementById('speed-slider');
const speedValue = document.getElementById('speed-value');
const resetBtn = document.getElementById('reset-btn');
const emergencyBrakeBtn = document.getElementById('emergency-brake-btn');
const modal = document.getElementById('confirmation-modal');
const modalMessage = document.getElementById('modal-message');
const modalConfirmBtn = document.getElementById('modal-confirm-btn');
const modalCancelBtn = document.getElementById('modal-cancel-btn');
let confirmCallback = null;
const showConfirmation = (message, callback) => {
modalMessage.textContent = message;
confirmCallback = callback;
modal.style.display = 'flex';
};
const hideConfirmation = () => {
modal.style.display = 'none';
confirmCallback = null;
};
modalConfirmBtn.addEventListener('click', () => {
if (confirmCallback) {
confirmCallback();
}
hideConfirmation();
});
modalCancelBtn.addEventListener('click', () => {
hideConfirmation();
});
const createChart = (ctx, label) => {
return new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: [{
label: label,
data: [],
borderColor: '#00ffff',
backgroundColor: 'rgba(0, 255, 255, 0.2)',
borderWidth: 1
}]
},
options: {
scales: {
x: {
ticks: { color: '#f0f0f0' }
},
y: {
ticks: { color: '#f0f0f0' }
}
},
plugins: {
legend: { labels: { color: '#f0f0f0' } }
}
}
});
};
const speedChart = createChart(document.getElementById('speedChart').getContext('2d'), 'Speed (km/h)');
const temperatureChart = createChart(document.getElementById('temperatureChart').getContext('2d'), 'Temperature (°C)');
const powerChart = createChart(document.getElementById('powerChart').getContext('2d'), 'Power (W)');
const addLog = (message) => {
const li = document.createElement('li');
li.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
logsEl.prepend(li);
if (logsEl.children.length > 20) {
logsEl.removeChild(logsEl.lastChild);
}
};
const updateChart = (chart, value) => {
const now = new Date().toLocaleTimeString();
chart.data.labels.push(now);
chart.data.datasets[0].data.push(value);
if (chart.data.labels.length > 10) {
chart.data.labels.shift();
chart.data.datasets[0].data.shift();
}
chart.update();
};
const simulateData = () => {
const getStatus = () => {
const rand = Math.random();
if (rand < 0.1) return 'error';
if (rand < 0.3) return 'warning';
return 'ok';
};
const data = {
speed: Math.random() * 1000,
temperature: Math.random() * 100 + 20,
voltage: Math.random() * 50 + 400,
current: Math.random() * 100,
distance: Math.random() * 1000,
power: Math.random() * 5000,
wifiStatus: getStatus(),
loraStatus: getStatus(),
emergencyBrakesStatus: getStatus(),
};
speedEl.textContent = `${data.speed.toFixed(2)} km/h`;
speedSlider.value = data.speed;
speedValue.textContent = `${data.speed.toFixed(2)} km/h`;
temperatureEl.textContent = `${data.temperature.toFixed(2)} °C`;
voltageEl.textContent = `${data.voltage.toFixed(2)} V`;
currentEl.textContent = `${data.current.toFixed(2)} A`;
distanceEl.textContent = `${data.distance.toFixed(2)} km`;
powerEl.textContent = `${data.power.toFixed(2)} W`;
wifiStatusEl.className = `status-indicator ${data.wifiStatus}`;
loraStatusEl.className = `status-indicator ${data.loraStatus}`;
emergencyBrakesStatusEl.className = `status-indicator ${data.emergencyBrakesStatus}`;
updateChart(speedChart, data.speed);
updateChart(temperatureChart, data.temperature);
updateChart(powerChart, data.power);
if (data.loraStatus === 'warning') addLog('Warning: Lora signal weak.');
if (data.emergencyBrakesStatus === 'error') addLog('Alert: Emergency Brakes engaged!');
};
startBtn.addEventListener('click', () => {
showConfirmation('Are you sure you want to start?', () => {
addLog('Command: START');
});
});
stopBtn.addEventListener('click', () => {
showConfirmation('Are you sure you want to stop?', () => {
addLog('Command: STOP');
});
});
speedSlider.addEventListener('input', () => {
const speed = parseFloat(speedSlider.value).toFixed(2);
speedValue.textContent = `${speed} km/h`;
});
speedSlider.addEventListener('change', () => {
showConfirmation('Are you sure you want to set the speed?', () => {
const speed = parseFloat(speedSlider.value).toFixed(2);
addLog(`Command: SET SPEED to ${speed} km/h`);
});
});
resetBtn.addEventListener('click', () => {
showConfirmation('Are you sure you want to reset?', () => {
addLog('Command: RESET');
});
});
emergencyBrakeBtn.addEventListener('click', () => {
showConfirmation('Are you sure you want to engage the emergency brake?', () => {
addLog('Command: EMERGENCY BRAKE');
});
});
setInterval(simulateData, 2000);
addLog('Dashboard initialized.');
});