Advertisement
ben_mkiv

wirelessRedstone.lua

Apr 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.74 KB | None | 0 0
  1. component = require("component")
  2. modem = component.modem
  3. event = require("event")
  4. sides = require("sides")
  5. serialization = require("serialization")
  6. local range = 128
  7.  
  8. modem.open(42001) -- Redstone network
  9. modem.setStrength(range)
  10.  
  11. config = {}
  12. devices = {}
  13.  
  14. function modemMessageEvent(a, receiver_address, sender_address, port, e, msg)
  15.     if not msg or msg == nil then return false end
  16.  
  17.     data = serialization.unserialize(msg)[1]
  18.     return data
  19. end
  20.  
  21. function sendC(ad, cmd, waitReply)
  22.     local data = {}
  23.     data.a = ad
  24.     data.c = cmd
  25.  
  26.     modem.send(ad, 42001, serialization.serialize(data))
  27.  
  28.     if waitReply ~= false then
  29.         return modemMessageEvent(event.pull(3, "modem_message"))
  30.     end
  31. end
  32.  
  33. function sendCB(cmd, waitReply)
  34.     local data = {}
  35.     data.a = "all"
  36.     data.c = cmd
  37.     modem.broadcast(42001, serialization.serialize(data))
  38.     if waitReply ~= false then
  39.         return modemMessageEvent(event.pull(3, "modem_message"))
  40.     end
  41. end
  42.  
  43.  
  44. function sendCBM(cmd, waitReply)
  45.     local data = {}
  46.     data.a = "all"
  47.     data.c = cmd
  48.     modem.broadcast(42001, serialization.serialize(data))
  49.  
  50.     local messages = {}
  51.  
  52.     if waitReply ~= false then
  53.         while true do
  54.             local msg = modemMessageEvent(event.pull(3, "modem_message"))
  55.             if msg ~= nil and msg then
  56.                 messages[#messages+1] = msg
  57.             else
  58.                 return messages
  59.             end
  60.         end
  61.     end
  62.  
  63.     return false
  64. end
  65.  
  66.  
  67. function netRS_toggle(i, side, state)
  68.     if not state then
  69.         if netRS_get(i) > 0 then
  70.             state = 0
  71.         else
  72.             state = 15
  73.         end
  74.     end
  75.     return tonumber(sendC(machines[i].address, "return r.setOutput(".. side ..", ".. state ..")"))
  76. end
  77.  
  78. function getOutput(device, side)
  79.     return tonumber(sendC(device, "return r.getOutput(".. side ..")", 2))
  80. end
  81.  
  82. function setOutput(device, side, state)
  83.     return tonumber(sendC(device, "return r.setOutput(" .. side .. ", " .. state .. ")", 2))
  84. end
  85.  
  86. function wakeup()
  87.     modem.broadcast(42001, "initRSNetwork")
  88. end
  89.  
  90.  
  91. function compareNames(a, b)
  92.     return a.name < b.name
  93. end
  94.  
  95. function sortOutputs()
  96.     if #config.outputs > 1 then
  97.         table.sort(config.outputs, compareNames)
  98.     end
  99. end
  100.  
  101. function loadConfig(cFile)
  102.     local ser = require("serialization")
  103.     local fs = require("filesystem")
  104.     if not fs.exists(cFile) then
  105.         return {}
  106.     end
  107.  
  108.     local cf = io.open(cFile, "r")
  109.     local serData = cf:read("*a")
  110.     cf:close()
  111.  
  112.     return ser.unserialize(serData)
  113. end
  114.  
  115. function saveConfig(cFile, cData)
  116.     local ser = require("serialization")
  117.     local cf = io.open(cFile, "w")
  118.     cf:write(ser.serialize(cData))
  119.     cf:close()
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement