nodes_status={} nodes_sta={} local function check_symbol(sn) if not sn then return nil end if not nodes_status[sn] then nodes_status[sn] = {} end if not nodes_status[sn].rx_cnt then nodes_status[sn].rx_cnt = 0 end if not nodes_status[sn].tx_cnt then nodes_status[sn].tx_cnt = 0 end if not nodes_status[sn].resp_cnt then nodes_status[sn].resp_cnt = 0 end if not nodes_status[sn].last_rcv then nodes_status[sn].last_rcv = 0 end if not nodes_status[sn].sn then nodes_status[sn].sn = sn end if not nodes_status[sn].loss_ratio then nodes_status[sn].loss_ratio = nil end if not nodes_status[sn].online then nodes_status[sn].online = nil end if not nodes_status[sn].last_online then nodes_status[sn].last_online = nil end return true end local function deepCopy(object) local lookup_table = {} local function _copy(object) if type(object) ~= "table" then return object elseif lookup_table[object] then return lookup_table[object] end local new_table = {} lookup_table[object] = new_table for key, value in pairs(object) do new_table[_copy(key)] = _copy(value) end return setmetatable(new_table, getmetatable(object)) end return _copy(object) end function nodes_sta.update(sn) if check_symbol(sn) then nodes_status[sn].online = false nodes_status[sn].last_online = nodes_status[sn].online end end function nodes_sta.tx_add(sn) if check_symbol(sn) then nodes_status[sn].tx_cnt = nodes_status[sn].tx_cnt + 1 end end function nodes_sta.rx_add(sn) if check_symbol(sn) then nodes_status[sn].rx_cnt = nodes_status[sn].rx_cnt + 1 nodes_status[sn].last_rcv = os.time() end end function nodes_sta.resp_add(sn) if check_symbol(sn) then nodes_status[sn].resp_cnt = nodes_status[sn].resp_cnt + 1 nodes_status[sn].last_rcv = os.time() end end function nodes_sta.info(timeout) local change = false for sn,status in pairs(nodes_status) do local offtime = timeout and timeout or 60*3 status.login_time = 0 if offtime == 0 or status.last_rcv and offtime and os.time() < (status.last_rcv + offtime) then status.online = true else status.online = false end -- if status.tx_cnt and status.tx_cnt == 0 then status.loss_ratio = 0 else status.loss_ratio = (status.tx_cnt - status.resp_cnt) * 100 / status.tx_cnt end -- 判断是否离线标记 if status.online ~= status.last_online then status.last_online = status.online change = true end end local report = {} report.status={} for sn,status in pairs(nodes_status) do local sta = deepCopy(status) -- 去掉不用的变量 sta.last_online = nil sta.resp_cnt = nil table.insert(report.status,sta) end return report,change end return nodes_sta