diff --git a/station_broadcast.lua b/station_broadcast.lua index 8f0155a..932544f 100644 --- a/station_broadcast.lua +++ b/station_broadcast.lua @@ -1,6 +1,6 @@ -- 1. Configuration & Constants -local APP_VERSION_TEXT = "v1.0.54" +local APP_VERSION_TEXT = "v1.0.55" local CONFIG = { CLOUD_URL = "ws://stabadmin.fse-media.group", @@ -778,35 +778,31 @@ function Utils.getStopTime(schedule, stationName) for i = 1, maxIdx do local entry = schedule.entries[i] if entry then - local function scanRecursive(obj, depth) - if depth > 5 or not obj then return false end - for k, v in pairs(obj) do - if type(v) == "string" then - local entryText = v:lower() - local entryCode = Utils.getStationCode(entryText) - local entryAlpha = entryText:gsub("[^%w%d]", "") - local entryBase = Utils.getBaseName(entryText) - - Utils.log("DEBUG", string.format(" [%d] SCAN '%s': '%s' (Code: %s)", i, k, entryText, tostring(entryCode))) + local isStationEntry = false + if entry.instruction and (entry.instruction.id == "create:station" or entry.instruction.id == "create:destination") then + local text = (entry.instruction.data and entry.instruction.data.text) or entry.instruction.text + if type(text) == "string" then + local entryText = text:lower() + local entryCode = Utils.getStationCode(entryText) + local entryAlpha = entryText:gsub("[^%w%d]", "") + local entryBase = Utils.getBaseName(entryText) - local isMatch = (target == entryText) - or (targetCode and entryCode and targetCode == entryCode) - or (targetAlpha ~= "" and entryAlpha ~= "" and (targetAlpha == entryAlpha or targetAlpha:find(entryAlpha, 1, true) or entryAlpha:find(targetAlpha, 1, true))) - or (targetBase ~= "" and entryBase ~= "" and (targetBase == entryBase or targetBase:find(entryBase, 1, true) or entryBase:find(targetBase, 1, true))) - or Utils.isFuzzyMatch(target, entryText) + Utils.log("DEBUG", string.format(" [%d] SCAN STATION: '%s' (Code: %s)", i, entryText, tostring(entryCode))) - if isMatch then - Utils.log("DEBUG", string.format(" >>> MATCH FOUND AT ENTRY %d (via field '%s')", i, k)) - return true - end - elseif type(v) == "table" then - if scanRecursive(v, depth + 1) then return true end + local isMatch = (target == entryText) + or (targetCode and entryCode and targetCode == entryCode) + or (targetAlpha ~= "" and entryAlpha ~= "" and (targetAlpha == entryAlpha or targetAlpha:find(entryAlpha, 1, true) or entryAlpha:find(targetAlpha, 1, true))) + or (targetBase ~= "" and entryBase ~= "" and (targetBase == entryBase or targetBase:find(entryBase, 1, true) or entryBase:find(targetBase, 1, true))) + or Utils.isFuzzyMatch(target, entryText) + + if isMatch then + Utils.log("DEBUG", string.format(" >>> MATCH FOUND AT ENTRY %d", i)) + isStationEntry = true end end - return false end - if scanRecursive(entry, 0) then + if isStationEntry then matchFoundIndex = i local s = findTimeInConditions(entry.conditions) if s then @@ -833,19 +829,10 @@ function Utils.getStopTime(schedule, stationName) local entry = schedule.entries[i] if entry then local isOtherStation = false - local function checkIsStation(obj, depth) - if depth > 2 or not obj then return false end - for k, v in pairs(obj) do - if type(v) == "string" then - local lk = tostring(k):lower() - if lk:find("text") or lk:find("station") or lk:find("destination") then return true end - elseif type(v) == "table" then - if checkIsStation(v, depth + 1) then return true end - end - end - return false + if entry.instruction and (entry.instruction.id == "create:station" or entry.instruction.id == "create:destination") then + isOtherStation = true end - if checkIsStation(entry, 0) then break end + if isOtherStation then break end local s = findTimeInConditions(entry.conditions) if s then @@ -887,30 +874,26 @@ function Utils.getNextStation(schedule, currentStationName) for i = 1, maxIdx do local entry = schedule.entries[i] if entry then - local function scanRecursive(obj, depth) - if depth > 5 or not obj then return false end - for k, v in pairs(obj) do - if type(v) == "string" then - local entryText = v:lower() - local entryCode = Utils.getStationCode(entryText) - local entryAlpha = entryText:gsub("[^%w%d]", "") - local entryBase = Utils.getBaseName(entryText) - - local isMatch = (target == entryText) - or (targetCode and entryCode and targetCode == entryCode) - or (targetAlpha ~= "" and entryAlpha ~= "" and (targetAlpha == entryAlpha or targetAlpha:find(entryAlpha, 1, true) or entryAlpha:find(targetAlpha, 1, true))) - or (targetBase ~= "" and entryBase ~= "" and (targetBase == entryBase or targetBase:find(entryBase, 1, true) or entryBase:find(targetBase, 1, true))) - or Utils.isFuzzyMatch(target, entryText) + local isStationEntry = false + if entry.instruction and (entry.instruction.id == "create:station" or entry.instruction.id == "create:destination") then + local text = (entry.instruction.data and entry.instruction.data.text) or entry.instruction.text + if type(text) == "string" then + local entryText = text:lower() + local entryCode = Utils.getStationCode(entryText) + local entryAlpha = entryText:gsub("[^%w%d]", "") + local entryBase = Utils.getBaseName(entryText) - if isMatch then return true end - elseif type(v) == "table" then - if scanRecursive(v, depth + 1) then return true end - end + local isMatch = (target == entryText) + or (targetCode and entryCode and targetCode == entryCode) + or (targetAlpha ~= "" and entryAlpha ~= "" and (targetAlpha == entryAlpha or targetAlpha:find(entryAlpha, 1, true) or entryAlpha:find(targetAlpha, 1, true))) + or (targetBase ~= "" and entryBase ~= "" and (targetBase == entryBase or targetBase:find(entryBase, 1, true) or entryBase:find(targetBase, 1, true))) + or Utils.isFuzzyMatch(target, entryText) + + if isMatch then isStationEntry = true end end - return false end - if scanRecursive(entry, 0) then + if isStationEntry then matchFoundIndex = i break end @@ -2751,7 +2734,8 @@ function Main.loop() end end - if etaSeconds > 0 and etaSeconds <= 45 then + local isAtStation = State.currentTrain and State.currentTrain.id == trainName and State.currentTrain.dest == destination + if not isAtStation and etaSeconds > 0 and etaSeconds <= 45 then if not seenApproachKeys[announceKey] then seenApproachKeys[announceKey] = true local pending = State.pendingApproaching[announceKey] @@ -2888,7 +2872,8 @@ function Main.loop() end end - if etaSeconds > 0 and etaSeconds <= 45 then + local isAtStation = State.currentTrain and State.currentTrain.id == tName and State.currentTrain.dest == dest + if not isAtStation and etaSeconds > 0 and etaSeconds <= 45 then if not seenApproachKeys[announceKey] then seenApproachKeys[announceKey] = true local pending = State.pendingApproaching[announceKey]