-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.lua
More file actions
163 lines (132 loc) · 4.91 KB
/
dashboard.lua
File metadata and controls
163 lines (132 loc) · 4.91 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
local Grid = require("grid")
local Dashboard = {}
Dashboard.__index = Dashboard
local TEXT_SCALE = 1
function Dashboard:new(UtilsInstance)
local self = setmetatable({}, Dashboard)
self.utils = UtilsInstance
self.colors = {
background = colors.black,
title = colors.yellow,
text = colors.white,
progressFull = colors.lime,
progressEmpty = colors.gray,
warning = colors.red,
info = colors.lightBlue,
success = colors.green,
}
return self
end
function Dashboard:clearMonitor(monitor)
monitor.setBackgroundColor(self.colors.background)
monitor.setTextColor(self.colors.text)
monitor.clear()
monitor.setCursorPos(1, 1)
end
function Dashboard:drawProgressBar(grid, row, col, percentage, width)
width = math.max(width, 3)
local filled = math.floor((percentage / 100) * (width - 2)) -- -2 para los corchetes
local empty = (width - 2) - filled
grid.monitor.setCursorPos(col, row)
grid.monitor.setTextColor(self.colors.text)
grid.monitor.write("[")
grid.monitor.setTextColor(self.colors.progressFull)
grid.monitor.write(string.rep("=", filled))
grid.monitor.setTextColor(self.colors.progressEmpty)
grid.monitor.write(string.rep("-", empty))
grid.monitor.setTextColor(self.colors.text)
grid.monitor.write("]")
end
function Dashboard:formatNumber(num)
if num >= 1000000 then
return string.format("%.1fM", num / 1000000)
elseif num >= 1000 then
return string.format("%.1fK", num / 1000)
end
return tostring(num)
end
function Dashboard:getSystemStatus(percentUsed)
if percentUsed >= 90 then
return "CRÍTICO", self.colors.warning
elseif percentUsed >= 75 then
return "ALTO", self.colors.warning
elseif percentUsed >= 50 then
return "MEDIO", self.colors.info
else
return "NORMAL", self.colors.success
end
end
function Dashboard:formatUptime()
local uptime = os.day() * 24 + os.time()
local hours = math.floor(uptime)
local minutes = math.floor((uptime - hours) * 60)
return string.format("%dh %dm", hours, minutes)
end
function Dashboard:updateClock(grid)
while true do
local monitor = grid.monitor
monitor.setTextColor(self.colors.title)
-- Escribe el título en la primera fila
local title = "=== VaultOS v1.0 ==="
local timeStr = textutils.formatTime(os.time(), true)
local dayStr = "Dia " .. os.day()
grid:writeInCell(1, 1, title, "center", true, 5)
grid:writeInCell(2, 1, string.format("%s | %s", timeStr, dayStr), "center", true, 5)
sleep(1) -- Actualización cada segundo
end
end
function Dashboard:formatLastUpdated(epochTime)
local seconds = math.floor(epochTime / 1000)
local minutes = math.floor(seconds / 60) % 60
local hours = math.floor(seconds / 3600) % 24
local days = math.floor(seconds / 86400)
local dayPart = string.format("Dia %d", days)
local timePart = string.format("%02d:%02d", hours, minutes)
return dayPart .. " - " .. timePart
end
function Dashboard:drawGeneralTab(grid, startRow)
while true do
local storageInfo = self.utils:getStorageInfo()
local totalSlots = storageInfo.totalSlots
local usedSlots = storageInfo.usedSlots
local percentUsed = math.floor((usedSlots / totalSlots) * 100)
local chestCount = storageInfo.chestCount
local status, statusColor = self:getSystemStatus(percentUsed)
grid.monitor.setTextColor(statusColor)
grid:writeInCell(startRow, 1, string.format("Estado: %s", status), "center", true, 5)
grid.monitor.setTextColor(self.colors.text)
local statsText = string.format(
"Slots: %s/%s | Cofres conectados: %d\nUso: %d%% | Uptime: %s",
self:formatNumber(usedSlots),
self:formatNumber(totalSlots),
chestCount,
percentUsed,
self:formatUptime()
)
grid:writeInCell(startRow + 1, 1, statsText, "center", true, 5)
-- Dibujar barra de progreso
self:drawProgressBar(grid, startRow + 2, 1, percentUsed, math.min(grid.width, grid.cellWidth * 5))
if percentUsed >= 90 then
grid.monitor.setTextColor(self.colors.warning)
grid:writeInCell(startRow + 3, 1, "! ALERTA !", "center", true, 5)
end
sleep(5) -- Actualizar cada 5 segundos
end
end
function Dashboard:updateDisplay(grid)
self:updateClock(grid)
self:drawGeneralTab(grid, 3)
end
function Dashboard:updateDashboard()
local config = self.utils:loadData("config.txt")
local monitor = peripheral.wrap(config.monitor)
self:clearMonitor(monitor)
monitor.setTextScale(TEXT_SCALE)
monitor.setCursorBlink(false)
local grid = Grid:new(monitor, 5, 5, 0)
parallel.waitForAny(
function() self:updateClock(grid) end,
function() self:drawGeneralTab(grid, 3) end
)
end
return Dashboard