2022-04-11 18:47:26 +02:00
|
|
|
require("common.class")
|
|
|
|
local Util = require("common.util")
|
2022-06-29 00:35:52 +02:00
|
|
|
local ServiceField = require("api.page.servicefield")
|
2022-04-11 18:47:26 +02:00
|
|
|
|
2022-06-22 02:32:27 +02:00
|
|
|
---@class SelfTestStatus
|
|
|
|
SelfTestStatus = {
|
2022-04-11 18:47:26 +02:00
|
|
|
IDLE = 1,
|
|
|
|
INPROGRESS = 2,
|
|
|
|
OK = 3,
|
|
|
|
PASS = 4,
|
|
|
|
ERROR = 5
|
|
|
|
}
|
|
|
|
|
2022-04-24 01:39:48 +02:00
|
|
|
local function statusToString(status)
|
|
|
|
local statusName = {"IDLE", "INPROGRESS", "OK", "PASS", "ERROR"}
|
|
|
|
return statusName[status]
|
|
|
|
end
|
|
|
|
|
2022-04-11 18:47:26 +02:00
|
|
|
---@class SelfTestField: ServiceField
|
2022-06-22 02:32:27 +02:00
|
|
|
---@field checkTask nil|fun(): SelfTestStatus # a function that will run asynchronously on activating the Field
|
|
|
|
---@field status SelfTestStatus
|
2022-04-24 01:39:48 +02:00
|
|
|
---@field _thread thread
|
2022-06-29 00:35:52 +02:00
|
|
|
---@field _timer number
|
2022-04-11 18:47:26 +02:00
|
|
|
local SelfTestField = {
|
2022-05-06 01:02:30 +02:00
|
|
|
__name = "SelfTestField",
|
2022-04-24 01:39:48 +02:00
|
|
|
COLOR_INPROGRESS = {255, 255, 255, 255},
|
|
|
|
COLOR_OK = {0, 255, 0, 255},
|
|
|
|
COLOR_PASS = {255, 255, 0, 255},
|
|
|
|
COLOR_ERROR = {255, 0, 0, 255},
|
|
|
|
INPROGRESS_FREQ = 1 / 20, --20Hz
|
2022-04-11 18:47:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
---Create a new SelfTestField instance
|
2022-05-06 01:02:30 +02:00
|
|
|
---@param params? SelfTestField
|
2022-04-11 18:47:26 +02:00
|
|
|
---@return SelfTestField
|
2022-05-06 01:02:30 +02:00
|
|
|
function SelfTestField.new(params)
|
|
|
|
params = params or {}
|
2022-04-11 18:47:26 +02:00
|
|
|
|
2022-06-22 02:32:27 +02:00
|
|
|
params.status = params.status or SelfTestStatus.IDLE
|
2022-04-11 18:47:26 +02:00
|
|
|
|
2022-05-06 01:02:30 +02:00
|
|
|
local self = CreateInstance(SelfTestField, params, ServiceField)
|
|
|
|
|
2022-06-29 00:35:52 +02:00
|
|
|
self._timer = 0.0
|
2022-05-06 01:02:30 +02:00
|
|
|
self._thread = nil
|
|
|
|
|
|
|
|
return self
|
2022-04-11 18:47:26 +02:00
|
|
|
end
|
|
|
|
|
2022-06-22 02:32:27 +02:00
|
|
|
function SelfTestField:init()
|
|
|
|
self._thread = coroutine.create(self.checkTask)
|
|
|
|
end
|
|
|
|
|
|
|
|
function SelfTestField.checkTask()
|
|
|
|
return SelfTestStatus.PASS
|
|
|
|
end
|
|
|
|
|
2022-04-24 01:39:48 +02:00
|
|
|
function SelfTestField:_closeThread()
|
|
|
|
if self._thread and coroutine.status(self._thread) ~= "dead" then
|
2022-06-29 00:35:52 +02:00
|
|
|
--coroutine.close(self._thread) --only god knows why this crashes lmao
|
2022-04-24 01:39:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function SelfTestField:_resumeThread()
|
|
|
|
if self._thread and coroutine.status(self._thread) == "suspended" then
|
|
|
|
local success, status = coroutine.resume(self._thread)
|
|
|
|
game.Log(self.label .. ": success: " .. tostring(success) ..
|
|
|
|
", status: " .. status .. " (" .. statusToString(status) .. ")",
|
|
|
|
game.LOGGER_DEBUG
|
|
|
|
)
|
|
|
|
if success and status ~= self.status then
|
|
|
|
self.status = status
|
|
|
|
if self.onStatusChange then
|
|
|
|
game.Log("SKIN CONFIG: onStatusChange(" .. status .. ") (" ..
|
|
|
|
statusToString(status) .. ")",
|
|
|
|
game.LOGGER_DEBUG
|
|
|
|
)
|
2022-06-22 02:32:27 +02:00
|
|
|
self:onStatusChange(status)
|
2022-04-24 01:39:48 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-15 04:54:30 +02:00
|
|
|
function SelfTestField:activate(obj)
|
2022-04-24 01:39:48 +02:00
|
|
|
self:_closeThread()
|
2022-06-22 02:32:27 +02:00
|
|
|
self:_resumeThread()
|
2022-04-15 04:54:30 +02:00
|
|
|
end
|
|
|
|
|
2022-04-24 01:39:48 +02:00
|
|
|
function SelfTestField:deactivate(obj)
|
|
|
|
self:_closeThread()
|
|
|
|
end
|
|
|
|
|
|
|
|
function SelfTestField:tick(deltaTime)
|
|
|
|
self:_resumeThread()
|
2022-06-29 00:35:52 +02:00
|
|
|
|
|
|
|
self._timer = self._timer + deltaTime
|
2022-04-24 01:39:48 +02:00
|
|
|
end
|
|
|
|
|
2022-06-22 02:32:27 +02:00
|
|
|
---@param status SelfTestStatus
|
|
|
|
function SelfTestField:onStatusChange(status) end
|
|
|
|
|
2022-04-11 18:47:26 +02:00
|
|
|
function SelfTestField:drawValue(deltaTime)
|
2022-04-15 04:54:30 +02:00
|
|
|
gfx.Translate(self.VALUE_OFFSETX, 0)
|
|
|
|
|
2022-04-11 18:47:26 +02:00
|
|
|
gfx.TextAlign(gfx.TEXT_ALIGN_RIGHT | gfx.TEXT_ALIGN_TOP)
|
2022-04-15 04:54:30 +02:00
|
|
|
gfx.FillColor(table.unpack(self.FONT_COLOR))
|
|
|
|
gfx.Text(": ", 0, 0)
|
2022-04-11 18:47:26 +02:00
|
|
|
|
|
|
|
local color, text
|
2022-06-22 02:32:27 +02:00
|
|
|
if self.status == SelfTestStatus.IDLE then
|
2022-04-15 04:54:30 +02:00
|
|
|
color = self.FONT_COLOR
|
2022-04-11 18:47:26 +02:00
|
|
|
text = ""
|
2022-06-22 02:32:27 +02:00
|
|
|
elseif self.status == SelfTestStatus.INPROGRESS then
|
2022-04-24 01:39:48 +02:00
|
|
|
local progress = math.ceil(Util.lerp(self._timer % self.INPROGRESS_FREQ,
|
|
|
|
0, 0, self.INPROGRESS_FREQ, 4
|
|
|
|
))
|
|
|
|
color = self.COLOR_INPROGRESS
|
2022-04-11 18:47:26 +02:00
|
|
|
text = string.rep(".", progress)
|
2022-06-22 02:32:27 +02:00
|
|
|
elseif self.status == SelfTestStatus.OK then
|
2022-04-24 01:39:48 +02:00
|
|
|
color = self.COLOR_OK
|
2022-04-11 18:47:26 +02:00
|
|
|
text = "OK"
|
2022-06-22 02:32:27 +02:00
|
|
|
elseif self.status == SelfTestStatus.PASS then
|
2022-04-24 01:39:48 +02:00
|
|
|
color = self.COLOR_PASS
|
2022-04-11 18:47:26 +02:00
|
|
|
text = "PASS"
|
2022-06-22 02:32:27 +02:00
|
|
|
elseif self.status == SelfTestStatus.ERROR then
|
2022-04-24 01:39:48 +02:00
|
|
|
color = self.COLOR_ERROR
|
2022-04-11 18:47:26 +02:00
|
|
|
text = "ERROR"
|
|
|
|
end
|
2022-04-15 04:54:30 +02:00
|
|
|
|
2022-04-11 18:47:26 +02:00
|
|
|
gfx.TextAlign(gfx.TEXT_ALIGN_LEFT | gfx.TEXT_ALIGN_TOP)
|
|
|
|
gfx.FillColor(table.unpack(color))
|
2022-04-15 04:54:30 +02:00
|
|
|
gfx.Text(text, 0, 0)
|
2022-04-11 18:47:26 +02:00
|
|
|
end
|
|
|
|
|
2022-04-24 01:39:48 +02:00
|
|
|
function SelfTestField:render(deltaTime)
|
|
|
|
self:tick(deltaTime)
|
|
|
|
ServiceField.render(self, deltaTime)
|
|
|
|
end
|
|
|
|
|
2022-04-11 18:47:26 +02:00
|
|
|
return SelfTestField
|