Advertisement
melzneni

monitor_visLib

Apr 11th, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. if fs.exists("isrlib") then
  2. os.loadAPI("isrlib")
  3. end
  4.  
  5. function tableContainsValue(table, value)
  6. for i, v in pairs(table) do
  7. if v == value then return true end
  8. end
  9. return false
  10. end
  11.  
  12. function initRednet(side)
  13. rednet.open(side)
  14. end
  15.  
  16. function printTable(tbl)
  17. printTableIndex(tbl, "", {})
  18. end
  19.  
  20. function printTableIndex(tbl, before, alreadyUsed)
  21. for i, v in pairs(tbl) do
  22. if type(v) == "table" then
  23. if tableContainsValue(alreadyUsed, v) then
  24. print(before .. i .. ":", "alreadyUsed");
  25. else
  26. print(before .. i .. ": [");
  27. table.insert(alreadyUsed, v)
  28. printTableIndex(v, before .. "\t", alreadyUsed)
  29. print(before .. "]")
  30. end
  31. else
  32. print(before .. i .. ":", v);
  33. end
  34. end
  35. end
  36.  
  37. function printKeys(t)
  38. local keys = ""
  39. local c = 0
  40. for i, v in pairs(t) do
  41. keys = keys .. ((c ~= 0 and ",") or "") .. i
  42. c = 1
  43. end
  44. print(keys)
  45. end
  46.  
  47. function split(s, delimiter)
  48. local result = {};
  49. for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
  50. table.insert(result, match);
  51. end
  52. return result;
  53. end
  54.  
  55. function getMsgData(msg)
  56. local i, j = string.find(msg, ":")
  57. local tag = string.sub(msg, 1, i - 1)
  58. local pts = split(string.sub(msg, i + 1, -1), ",")
  59. return tag, pts
  60. end
  61.  
  62. local thisComputerID
  63.  
  64. function receiveRednet()
  65. while true do
  66. local id, msg = rednet.receive()
  67. if type(msg) == "string" then
  68. if thisComputerID == nil then findComputerId() end
  69. local cid
  70. if #msg > 8 and string.sub(msg, 1, 8) == "<global>" then
  71. msg = string.sub(msg, 9)
  72. cid = id
  73. elseif #msg > 8 and string.sub(msg, 1, 8) == "<direct>" then
  74. msg = string.sub(msg, 9)
  75. cid = -1
  76. end
  77. if cid ~= nil and (thisComputerID == cid or cid == -1) then
  78. if string.find(msg, ":") ~= nil then
  79. if string.sub(msg, 1, 1) == "[" then
  80. local ind = string.find(msg, "]")
  81. id = tonumber(string.sub(msg, 2, ind - 1))
  82. msg = string.sub(msg, ind + 1)
  83. end
  84. return id, msg
  85. end
  86. end
  87. end
  88. end
  89. end
  90.  
  91. function sendRednet(id, msg)
  92. if isrlib ~= nil then
  93. rednet.send(id, "<direct>[" .. isrlib.getCId() .. "]" .. msg)
  94. return
  95. end
  96. rednet.send(id, "<direct>" .. msg)
  97. end
  98.  
  99. function findComputerId()
  100. rednet.broadcast("@globalNet:access")
  101. while true do
  102. local id, msg = rednet.receive()
  103. if type(msg) == "string" and #msg >= 12 and string.sub(msg, 1, 12) == "@gNetAnsw:hu" then
  104. thisComputerID = id;
  105. break
  106. end
  107. rednet.broadcast("@globalNet:access")
  108. end
  109. end
  110.  
  111. function broadcast(msg)
  112. if thisComputerID == nil then findComputerId() end
  113. if isrlib ~= nil then
  114. rednet.send(thisComputerID, "@globalNet:broadcast,[" .. isrlib.getCId() .. "]" .. msg)
  115. --rednet.broadcast("[" .. isrlib.getCId() .. "]" .. msg)
  116. return
  117. end
  118. rednet.send(thisComputerID, "@globalNet:broadcast," .. msg)
  119. end
  120.  
  121. monitor={
  122. getSize= function (monitor)
  123. local answ = getAnswer("@monman:" .. monitor.name .. ",get,size","@monAnsw", monitor.id)
  124. local ind = string.find(answ.msg, ",")
  125. return tonumber(string.sub(answ.msg, 1, ind - 1)), tonumber(string.sub(answ.msg, ind + 1))
  126. end,
  127. connect=function (name)
  128. local event = getAnswer("@monman:" .. name .. ",get,ping", "@monAnsw")
  129. return { id = event.id, name = name }
  130. end
  131.  
  132. }
  133.  
  134.  
  135.  
  136. function getAnswer(msg,key, id, timeout)
  137. if timeout == nil then timeout = 3 end
  138. if id == nil then
  139. broadcast(msg)
  140. else
  141. sendRednet(id, msg)
  142. end
  143. for i = 1, timeout * 20 do
  144. for _, e in ipairs(isrlib.getEvents()) do
  145. if e.type == isrlib.ETYPE_RNET then
  146. if #e.msg > 9 and string.sub(e.msg, 1, 9) == key .. ":" then
  147. e.msg = string.sub(e.msg, 10)
  148. return e
  149. end
  150. end
  151. end
  152. sleep(0.05)
  153. end
  154. error("answer-timeout")
  155. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement