2022-11-02 18:47:18 +00:00
|
|
|
-- Power Indicator Graphics Element
|
|
|
|
|
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
|
|
|
local element = require("graphics.element")
|
|
|
|
|
|
|
|
---@class power_indicator_args
|
|
|
|
---@field label string indicator label
|
|
|
|
---@field format string power format override (lua string format)
|
2022-12-10 20:44:11 +00:00
|
|
|
---@field rate boolean? whether to append /t to the end (power per tick)
|
2022-11-02 18:47:18 +00:00
|
|
|
---@field lu_colors? cpair label foreground color (a), unit foreground color (b)
|
2023-09-29 23:34:10 +00:00
|
|
|
---@field value number default value
|
2022-11-02 18:47:18 +00:00
|
|
|
---@field parent graphics_element
|
|
|
|
---@field id? string element id
|
|
|
|
---@field x? integer 1 if omitted
|
2023-07-10 03:42:44 +00:00
|
|
|
---@field y? integer auto incremented if omitted
|
2022-11-02 18:47:18 +00:00
|
|
|
---@field width integer length
|
|
|
|
---@field fg_bg? cpair foreground/background colors
|
2023-05-25 21:40:16 +00:00
|
|
|
---@field hidden? boolean true to hide on initial draw
|
2022-11-02 18:47:18 +00:00
|
|
|
|
|
|
|
-- new power indicator
|
2023-02-25 00:50:01 +00:00
|
|
|
---@nodiscard
|
2022-11-02 18:47:18 +00:00
|
|
|
---@param args power_indicator_args
|
|
|
|
---@return graphics_element element, element_id id
|
|
|
|
local function power(args)
|
2023-10-04 02:52:13 +00:00
|
|
|
element.assert(type(args.value) == "number", "value is a required field")
|
2023-09-30 15:46:47 +00:00
|
|
|
element.assert(util.is_int(args.width), "width is a required field")
|
2022-11-02 18:47:18 +00:00
|
|
|
|
|
|
|
args.height = 1
|
|
|
|
|
|
|
|
-- create new graphics element base object
|
|
|
|
local e = element.new(args)
|
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
e.value = args.value
|
2022-11-02 18:47:18 +00:00
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
local data_start = 0
|
2022-11-02 18:47:18 +00:00
|
|
|
|
|
|
|
-- on state change
|
|
|
|
---@param value any new value
|
|
|
|
function e.on_update(value)
|
|
|
|
e.value = value
|
|
|
|
|
|
|
|
local data_str, unit = util.power_format(value, false, args.format)
|
|
|
|
|
|
|
|
-- write data
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_cur(data_start, 1)
|
|
|
|
e.w_set_fgd(e.fg_bg.fgd)
|
|
|
|
e.w_write(util.comma_format(data_str))
|
2022-11-02 18:47:18 +00:00
|
|
|
|
|
|
|
-- write unit
|
|
|
|
if args.lu_colors ~= nil then
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_set_fgd(args.lu_colors.color_b)
|
2022-11-02 18:47:18 +00:00
|
|
|
end
|
2022-12-10 20:44:11 +00:00
|
|
|
|
|
|
|
-- append per tick if rate is set
|
|
|
|
-- add space to FE so we don't end up with FEE (after having kFE for example)
|
|
|
|
if args.rate == true then
|
|
|
|
unit = unit .. "/t"
|
|
|
|
if unit == "FE/t" then unit = "FE/t " end
|
|
|
|
else
|
|
|
|
if unit == "FE" then unit = "FE " end
|
|
|
|
end
|
|
|
|
|
2023-08-31 01:11:57 +00:00
|
|
|
e.w_write(" " .. unit)
|
2022-11-02 18:47:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- set the value
|
|
|
|
---@param val any new value
|
|
|
|
function e.set_value(val) e.on_update(val) end
|
|
|
|
|
2023-09-29 23:34:10 +00:00
|
|
|
-- element redraw
|
|
|
|
function e.redraw()
|
|
|
|
if args.lu_colors ~= nil then e.w_set_fgd(args.lu_colors.color_a) end
|
|
|
|
e.w_set_cur(1, 1)
|
|
|
|
e.w_write(args.label)
|
|
|
|
|
|
|
|
data_start = string.len(args.label) + 2
|
|
|
|
if string.len(args.label) == 0 then data_start = 1 end
|
|
|
|
|
|
|
|
e.on_update(e.value)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- initial draw
|
|
|
|
e.redraw()
|
2022-11-02 18:47:18 +00:00
|
|
|
|
2023-05-30 23:51:10 +00:00
|
|
|
return e.complete()
|
2022-11-02 18:47:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return power
|