summaryrefslogtreecommitdiff
path: root/Crossroads/nodes/(1645,6,8179).lua
blob: 33d3103581dfc01f1a1a4fde8e9718322304fb11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
local head = { "V  TRAIN  LINE    TIME", "ST  TRK STATION ", " " }
local length = 18
local height = 4
local sformat = string.format

if event.type=="digiline" or event.type=="punch" then
  if event.channel=="upd_main" or event.type=="punch" then
    local ts = os.time()
    local table = table
    local start_t = os.clock()
    if not S.lagrec then S.lagrec = {} end
    local ids = {}
    for i in pairs(S.trains) do ids[#ids+1] = i end
    table.sort(ids, function(a, b)
      local ta, tb = S.trains[a], S.trains[b]
      if ta.l == tb.l then
        return a < b
      else
        return ta.l < tb.l
      end 
    end)
    --do return end
    local disp = {{}, {}, {}}
    local percol = height*4-1
    for id = 1, #ids, 1 do
      local st = (S.trains[ids[id] or 0] or {})
      local i = math.floor((id-1)/percol)
      local j = math.floor(((id-1)%percol+1)/4)+1
      local stn = sformat("%-3s %-3s %s", st.mode or "", st.t or "N/A", F.stnlist[st.s] or st.s)
      stn = stn:gsub("ä","ae"):gsub("ö","oe"):gsub("ü","ue"):gsub("ß","ss")
      local dt = ts - (st.ts or -1)
      local td = ""
      if dt < 3600 then
        td = sformat("%02dm%02ds", math.floor(dt/60), dt%60)
      elseif dt < 86400 then
        td = sformat("%02dh%02dm", math.floor(dt/3600), math.floor(dt/60)%60)
      elseif dt < 8640000 then
        td = sformat("%02dd%02dh", math.floor(dt/86400), math.floor(dt/3600)%24)
      elseif st.ts then
        td = "LONG"
      end
      local line = {
        sformat("%02d %06d %-7s %s", st.v, ids[id], st.l, td),
        stn,
        stn:len() > 27 and stn:sub(28) or " ",
      }
      for df, fstr in pairs(line) do
        local ft = disp[df]
        local ct = ft[i]
        if not ct then
          ct = {{head[df]}}
          ft[i] = ct
        end
        local rt = ct[j]
        if not rt then
          rt = {}
          ct[j] = rt
        end
        table.insert(rt, fstr)
      end
    end
    for fi, ft in pairs(disp) do
      for fc, ct in pairs(ft) do
        for fr, rt in pairs(ct) do
          digiline_send(tostring((fi+fc*3)*10+fr), table.concat(rt or {}, "\n"))
        end
      end
    end
    local end_t = os.clock()
    local t = end_t - start_t
    local lagrec = S.lagrec
    if t > (lagrec.max or 0) then
      lagrec.max = t
    end
    if t < (lagrec.min or math.huge) then
      lagrec.min = t
    end
    avg = t
    if lagrec.avg then
      avg = (lagrec.avg+t)/2
    end
    lagrec.avg = avg
    digiline_send("lag1", sformat("DEBUG\nTRAINS: %d\nTS: %d", #ids, ts))
    digiline_send("lag2", sformat("CUR %f\nMIN %f\nMAX %f\nAVG %f", t, lagrec.min, lagrec.max, avg))
  end
end