Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/services/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local websocket = require "http.websocket"
local cjson = require "cjson.safe"
local op = require "fibers.op"
local sleep = require "fibers.sleep"
-- -@type { get_box_reports: fun(): table, get_box_logs: fun(): table }
---@type { get_box_reports: fun(config: table, gsm_stats: table|nil): table, get_box_logs: fun(): table }
local diagnostics = require "services.ui.diagnostics"
require "services.ui.fibers_cqueues"

Expand Down Expand Up @@ -207,7 +207,7 @@ local function onstream(self, stream)

if req_method == "GET" then
---@diagnostic disable-next-line: need-check-nil
local diagnostics_reports = diagnostics.get_box_reports(config)
local diagnostics_reports = diagnostics.get_box_reports(config, stats_messages_cache)
---@diagnostic disable-next-line: need-check-nil
local diagnostics_logs = diagnostics.get_box_logs()
local resp = { diagnostics_logs = diagnostics_logs, diagnostics = diagnostics_reports }
Expand Down
62 changes: 55 additions & 7 deletions src/services/ui/diagnostics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -351,20 +351,60 @@ local function check_expected_cloud_services_reachable(box_type, config)
end
end

return {
expected = expected,
installed = installed,
missing = missing
}
return new_report(expected, installed, missing)
end

---Check which modems are active (SIM present and state machine running)
---@param box_type string Box model type
---@param stats_cache table|nil Flat stats messages cache
---@return table<string, number|string[]>|nil report
---@return string|nil error
local function get_modems_sim_active(box_type, stats_cache)
local expected_modems, err = expected.get_expected_modem_names(box_type)
if err ~= nil then
return nil, err
end

local installed = 0
local missing = {}

if not stats_cache then
return new_report(#expected_modems, 0, expected_modems), nil
end

for _, modem_name in ipairs(expected_modems) do
local sim_key = "gsm.modem." .. modem_name .. ".sim"
local sim_entry = stats_cache[sim_key]
local sim_present = sim_entry and sim_entry.payload == "present"

local state_key = "gsm.modem." .. modem_name .. ".state"
local state_entry = stats_cache[state_key]
local has_active_state = state_entry
and state_entry.payload
and state_entry.payload.curr_state
and state_entry.payload.curr_state ~= ""

local is_active = sim_present and has_active_state

if is_active then
installed = installed + 1
else
table.insert(missing, modem_name)
end
end

return new_report(#expected_modems, installed, missing), nil
end

---Fetches diagnostics stats from the box
---@return table<string, table> diagnostics
---@param config table<string, string> The configuration
local function get_box_reports(config)
---@param stats_cache table|nil Flat stats messages cache
---@return table<string, table> diagnostics
local function get_box_reports(config, stats_cache)
local diagnostics = {
packages_installed = {},
modems_installed = {},
modems_sim_active = {},
services_running = {},
packages_running = {},
bootstrap_installed = {},
Expand Down Expand Up @@ -404,6 +444,14 @@ local function get_box_reports(config)
else
diagnostics.cloud_services_reachable = cloud_services
end

-- Check modem SIM active (present + state machine running)
local modems_sim_active, sim_check_err = get_modems_sim_active(hardware_info.model, stats_cache)
if sim_check_err ~= nil then
log.error("UI - error checking modem SIM active", sim_check_err)
else
diagnostics.modems_sim_active = modems_sim_active
end
else
log.error("UI - error getting hardware info", hardware_info_err)
end
Expand Down
21 changes: 21 additions & 0 deletions src/services/ui/diagnostics_expected.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ local GETBOX_MODEMS = 1
local BIGBOX_SS_MODEMS = 2
local BIGBOX_V1_CM_MODEMS = 2

local GETBOX_MODEM_NAMES = {"primary"}
local BIGBOX_SS_MODEM_NAMES = {"primary", "secondary"}
local BIGBOX_V1_CM_MODEM_NAMES = {"primary", "secondary"}

local BOOTSTRAP_INSTALLED = {
"/data/configs/hawkbit.cfg",
"/data/configs/mainflux.cfg",
Expand Down Expand Up @@ -301,11 +305,28 @@ local function get_expected_modem_count(box_type)
end
end

---Get the expected modem names for the box model
---@param box_type string getbox|bigbox-ss|bigbox-v1-cm
---@return string[] expected_modem_names
---@return string|nil error
local function get_expected_modem_names(box_type)
if box_type == GETBOX then
return GETBOX_MODEM_NAMES, nil
elseif box_type == BIGBOX_SS then
return BIGBOX_SS_MODEM_NAMES, nil
elseif box_type == BIGBOX_V1_CM then
return BIGBOX_V1_CM_MODEM_NAMES, nil
else
return {}, "Unknown box_type: " .. box_type
end
end

return {
packages_running = PACKAGES_RUNNING,
bootstrap_installed = BOOTSTRAP_INSTALLED,
get_expected_packages_installed = get_expected_packages_installed,
get_expected_services_running = get_expected_services_running,
get_expected_modem_count = get_expected_modem_count,
get_expected_modem_names = get_expected_modem_names,
get_expected_connectivity_tests = get_expected_connectivity_tests,
}