-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtraffic.js
More file actions
73 lines (63 loc) · 2.44 KB
/
traffic.js
File metadata and controls
73 lines (63 loc) · 2.44 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
// Traffic monitoring page script
document.addEventListener('DOMContentLoaded', function() {
initializeTraffic();
// 初始化 Material UI 交互
if (typeof initMaterialUI === 'function') {
initMaterialUI();
}
});
function initializeTraffic() {
const now = new Date();
document.getElementById('cycleStart').value = formatLocal(now);
updateTrafficRule();
}
function formatLocal(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
return `${year}-${month}-${day}T${hours}:${minutes}`;
}
function updateTrafficRule() {
const type = document.getElementById('ruleType').value;
const min = document.getElementById('minValue').value;
const minUnit = Number(document.getElementById('minUnit').value);
const max = document.getElementById('maxValue').value;
const maxUnit = Number(document.getElementById('maxUnit').value);
const start = document.getElementById('cycleStart').value;
const interval = document.getElementById('cycleInterval').value;
const unit = document.getElementById('cycleUnit').value;
const cover = document.getElementById('cover').checked ? 1 : 0;
const ignoreInput = document.getElementById('ignoreList').value;
const ignore = {};
if (ignoreInput) {
ignoreInput.split(',').forEach(id => {
const t = id.trim();
if (t) ignore[t] = true;
});
}
const obj = {
type: type,
};
if (min) obj.min = Number(min) * minUnit;
if (max) obj.max = Number(max) * maxUnit;
if (start) obj.cycle_start = new Date(start).toISOString();
obj.cycle_interval = Number(interval);
obj.cycle_unit = unit;
obj.cover = cover;
if (Object.keys(ignore).length) obj.ignore = ignore;
const json = JSON.stringify([obj], null, 2);
document.getElementById('trafficJson').value = json;
// 更新输入框状态
document.querySelectorAll('.md-text-field input').forEach(input => {
if (input.value) input.parentElement.classList.add('has-value');
});
}
function copyTrafficCode(event) {
if (event) event.stopPropagation();
const textarea = document.getElementById('trafficJson');
textarea.select();
document.execCommand('copy');
showToast();
}