2022-08-28 16:12:30 +00:00
|
|
|
local log = require("scada-common.log")
|
|
|
|
local types = require("scada-common.types")
|
2022-10-07 15:43:18 +00:00
|
|
|
local util = require("scada-common.util")
|
2022-06-04 15:11:35 +00:00
|
|
|
|
|
|
|
local unit_session = require("supervisor.session.rtu.unit_session")
|
|
|
|
|
|
|
|
local envd = {}
|
|
|
|
|
2023-02-21 17:27:16 +00:00
|
|
|
local RTU_UNIT_TYPE = types.RTU_UNIT_TYPE
|
2022-06-04 15:11:35 +00:00
|
|
|
local MODBUS_FCODE = types.MODBUS_FCODE
|
|
|
|
|
|
|
|
local TXN_TYPES = {
|
|
|
|
RAD = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
local TXN_TAGS = {
|
|
|
|
"envd.radiation"
|
|
|
|
}
|
|
|
|
|
|
|
|
local PERIODICS = {
|
|
|
|
RAD = 500
|
|
|
|
}
|
|
|
|
|
|
|
|
-- create a new environment detector rtu session runner
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2024-08-16 19:53:43 +00:00
|
|
|
---@param session_id integer RTU gateway session ID
|
|
|
|
---@param unit_id integer RTU ID
|
|
|
|
---@param advert rtu_advertisement RTU advertisement table
|
|
|
|
---@param out_queue mqueue RTU message out queue
|
2022-06-04 15:11:35 +00:00
|
|
|
function envd.new(session_id, unit_id, advert, out_queue)
|
2023-11-12 16:54:47 +00:00
|
|
|
-- checks
|
2023-02-21 16:05:57 +00:00
|
|
|
if advert.type ~= RTU_UNIT_TYPE.ENV_DETECTOR then
|
2023-11-12 16:54:47 +00:00
|
|
|
log.error("attempt to instantiate envd RTU for type " .. types.rtu_type_to_string(advert.type))
|
|
|
|
return nil
|
|
|
|
elseif not util.is_int(advert.index) then
|
|
|
|
log.error("attempt to instantiate envd RTU without index")
|
2022-06-04 15:11:35 +00:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2023-11-12 16:54:47 +00:00
|
|
|
local log_tag = util.c("session.rtu(", session_id, ").envd(", advert.index, ")[@", unit_id, "]: ")
|
2022-06-04 15:11:35 +00:00
|
|
|
|
|
|
|
local self = {
|
2022-11-12 06:35:31 +00:00
|
|
|
session = unit_session.new(session_id, unit_id, advert, out_queue, log_tag, TXN_TAGS),
|
2022-06-04 15:11:35 +00:00
|
|
|
periodics = {
|
|
|
|
next_rad_req = 0
|
|
|
|
},
|
|
|
|
---@class envd_session_db
|
|
|
|
db = {
|
2022-10-07 15:43:18 +00:00
|
|
|
last_update = 0,
|
2023-02-14 20:15:34 +00:00
|
|
|
radiation = types.new_zero_radiation_reading(),
|
2022-06-04 15:11:35 +00:00
|
|
|
radiation_raw = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
local public = self.session.get()
|
|
|
|
|
|
|
|
-- PRIVATE FUNCTIONS --
|
|
|
|
|
|
|
|
-- query the radiation readings of the device
|
|
|
|
local function _request_radiation()
|
|
|
|
-- read input registers 1 and 2 (start = 1, count = 2)
|
|
|
|
self.session.send_request(TXN_TYPES.RAD, MODBUS_FCODE.READ_INPUT_REGS, { 1, 2 })
|
|
|
|
end
|
|
|
|
|
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
|
|
|
-- handle a packet
|
|
|
|
---@param m_pkt modbus_frame
|
|
|
|
function public.handle_packet(m_pkt)
|
2022-09-19 02:25:59 +00:00
|
|
|
local txn_type = self.session.try_resolve(m_pkt)
|
2022-06-04 15:11:35 +00:00
|
|
|
if txn_type == false then
|
|
|
|
-- nothing to do
|
|
|
|
elseif txn_type == TXN_TYPES.RAD then
|
|
|
|
-- radiation status response
|
|
|
|
if m_pkt.length == 2 then
|
2022-10-07 15:43:18 +00:00
|
|
|
self.db.last_update = util.time_ms()
|
|
|
|
self.db.radiation = m_pkt.data[1]
|
2022-06-04 15:11:35 +00:00
|
|
|
self.db.radiation_raw = m_pkt.data[2]
|
|
|
|
else
|
2022-06-04 15:17:54 +00:00
|
|
|
log.debug(log_tag .. "MODBUS transaction reply length mismatch (" .. TXN_TAGS[txn_type] .. ")")
|
2022-06-04 15:11:35 +00:00
|
|
|
end
|
|
|
|
elseif txn_type == nil then
|
|
|
|
log.error(log_tag .. "unknown transaction reply")
|
|
|
|
else
|
|
|
|
log.error(log_tag .. "unknown transaction type " .. txn_type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- update this runner
|
|
|
|
---@param time_now integer milliseconds
|
|
|
|
function public.update(time_now)
|
2022-06-04 19:00:50 +00:00
|
|
|
if self.periodics.next_rad_req <= time_now then
|
2022-06-04 15:11:35 +00:00
|
|
|
_request_radiation()
|
|
|
|
self.periodics.next_rad_req = time_now + PERIODICS.RAD
|
|
|
|
end
|
|
|
|
|
|
|
|
self.session.post_update()
|
|
|
|
end
|
|
|
|
|
2022-11-11 19:59:53 +00:00
|
|
|
-- invalidate build cache
|
|
|
|
function public.invalidate_cache()
|
|
|
|
-- no build cache for this device
|
|
|
|
end
|
|
|
|
|
2022-06-04 15:11:35 +00:00
|
|
|
-- get the unit session database
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-06-04 15:11:35 +00:00
|
|
|
function public.get_db() return self.db end
|
|
|
|
|
|
|
|
return public
|
|
|
|
end
|
|
|
|
|
|
|
|
return envd
|