Advertisement
melzneni

monitor_mManager

Apr 22nd, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. local args = { ... }
  2. os.loadAPI("vislib")
  3.  
  4. if #args < 1 then error("expected at least one argument") end
  5. local screenName = args[1]
  6.  
  7. local monitors = {}
  8. local xMax = 0
  9. local yMax = 0
  10. local xPixMax = 0
  11. local yPixMax = 0
  12.  
  13. function rearangeMonitors()
  14. local rows = {}
  15. local cols = {}
  16. for i, m in ipairs(monitors) do
  17. if rows[m.cX] == nil or m.w > rows[m.cX] then
  18. rows[m.cX] = m.w
  19. end
  20. if cols[m.cY] == nil or m.h > cols[m.cY] then
  21. cols[m.cY] = m.h
  22. end
  23. end
  24. local w = 0
  25. local h = 0
  26.  
  27. local xCords = {}
  28. local yCords = {}
  29.  
  30. for x = 1, xMax do
  31. xCords[x] = w
  32. if rows[x] ~= nil then
  33. w = w + rows[x] + 4
  34. end
  35. end
  36.  
  37. for y = 1, yMax do
  38. yCords[y] = h
  39. if cols[y] ~= nil then
  40. h = h + cols[y] + 4
  41. end
  42. end
  43.  
  44. xPixMax = w - 4
  45. yPixMax = h - 4
  46. for i, m in ipairs(monitors) do
  47. vislib.sendRednet(m.id, "@mon:" .. screenName .. ",setPos," .. xCords[m.cX] .. "," .. yCords[m.cY])
  48. end
  49. end
  50.  
  51. rednet.open("top")
  52.  
  53. vislib.broadcast("@mon:" .. screenName .. ",sendData")
  54. while true do
  55. local id, msg = vislib.receiveRednet()
  56. local tag, pts = vislib.getMsgData(msg)
  57.  
  58. if tag == "@monman" and pts[1] == screenName then
  59. if pts[2] == "init" then
  60. monitors = {}
  61. xMax = 0
  62. yMax = 0
  63. print("init")
  64. vislib.broadcast("@mon:" .. screenName .. ",sendData")
  65. elseif pts[2] == "monData" then
  66. print("monData:", msg)
  67. local label = pts[3]
  68. local screenCX = tonumber(pts[4])
  69. local screenCY = tonumber(pts[5])
  70. local w = tonumber(pts[6])
  71. local h = tonumber(pts[7])
  72. if screenCX > xMax then xMax = screenCX end
  73. if screenCY > yMax then yMax = screenCY end
  74. table.insert(monitors, {
  75. id = id,
  76. label = label,
  77. cX = screenCX,
  78. cY = screenCY,
  79. w = w,
  80. h = h
  81. })
  82. rearangeMonitors()
  83. elseif pts[2] == "reboot" then
  84. print("reboot")
  85. vislib.broadcast("@mon:" .. screenName .. ",reboot")
  86. sleep(3)
  87. os.reboot()
  88. elseif pts[2] == "get" then
  89. if pts[3] == "ping" then
  90. vislib.sendRednet(id, "@monAnsw:pong")
  91. elseif pts[3] == "size" then
  92. vislib.sendRednet(id, "@monAnsw:" .. xPixMax .. "," .. yPixMax)
  93. end
  94. else print("unknown message: " .. msg)
  95. end
  96. end
  97. end
  98.  
  99. --pastebin run 0YB9PsQV startup={files={vislib=<pb:GYvMFCY5>,monman=<pb:R7pDQu5D>},cmds={{'monman',<input:'name'>}}} label=<input:'Label'> reboot=true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement