Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ALTPROTO_PORT = 16660
- local function Log(text)
- print(text)
- end
- local modem = peripheral.find("modem", function(_, p) return p.isWireless() end)
- if not modem then
- Log("No modem found")
- return
- end
- modem.open(ALTPROTO_PORT)
- local RECIPIENT_ALL = 0
- local function SendMessage(id, recipient, data)
- modem.transmit(ALTPROTO_PORT, 0, {id = id, data = data, recipient = recipient, sender = 0})
- end
- local lbase = math.log(10)
- local si_prefixes = {
- [-4] = "p";
- [-3] = "n";
- [-2] = "u";
- [-1] = "m";
- [0] = "WTF";
- [1] = "k";
- [2] = "M";
- [3] = "G";
- [4] = "T";
- [5] = "P";
- [6] = "E";
- [7] = "Z";
- [8] = "Y";
- }
- local si_low = -4
- local si_high = 8
- local function Round(number, decimals)
- return math.floor((number * 10^decimals) + 0.5) / 10^decimals
- end
- local function FormatSI(number, unit)
- local mult = 1
- if number < 0 then
- mult = -1
- number = -number
- end
- local brack = math.floor((math.log(number) / lbase) / 3)
- brack = math.min(math.max(brack, si_low), si_high)
- if brack == 0 then
- return tostring(mult * Round(number, 3)) .. " " .. unit
- end
- number = number / 10^(brack * 3)
- return tostring(Round(mult * number, 3)) .. " " .. si_prefixes[brack] .. unit
- end
- local args = {...}
- if args[1] == "ping" then
- SendMessage("GPING", RECIPIENT_ALL, math.random())
- local timer = os.startTimer(1)
- while true do
- local event = {os.pullEvent()}
- if event[1] == "timer" and event[2] == timer then
- break
- end
- if event[1] == "modem_message" then
- local event, side, sender, reply, message, distance = unpack(event)
- if sender == ALTPROTO_PORT then
- if type(message) ~= "table" then
- Log("ERROR: Message was not a table")
- return
- end
- local recipient = message.recipient
- if not recipient then
- Log("ERROR: Message has no recipient")
- return
- end
- local sender = message.sender
- if not sender then
- Log("ERROR: Message has no sender")
- return
- end
- if recipient == proto_id or recipient == RECIPIENT_ALL then
- local id = message.id
- if not id then
- Log("ERROR: Message has no ID")
- return
- end
- if id == "GPONG" then
- Log("Got pong from: " .. sender .. " (" .. (message.data or "no label") .. ")")
- end
- end
- end
- end
- end
- elseif args[1] == "reactor" then
- if args[2] == "on" then
- SendMessage("REACT_SET", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, true)
- Log("Enabled reactor")
- elseif args[2] == "off" then
- SendMessage("REACT_SET", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, false)
- Log("Disabled reactor")
- else
- Log("Usage: con reactor <on/off>")
- end
- elseif args[1] == "update" then
- local filename = args[2]
- if not filename then
- Log("Usage: con update <file> [id]")
- return
- end
- local id = tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL
- local file = fs.open(filename, "r")
- if not file then
- Log("Invalid file")
- return
- end
- local data = file.readAll()
- file.close()
- local func, err = loadstring(data)
- if not func then
- Log("Syntax error: " .. err)
- return
- end
- SendMessage("UPDATE", id, data)
- Log("Sent update file to " .. tostring(id))
- elseif args[1] == "updatemod" then
- local filename = args[2]
- if not filename then
- Log("Usage: con updatemod <file> [id]")
- return
- end
- local id = tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL
- local file = fs.open(filename, "r")
- if not file then
- Log("Invalid file")
- return
- end
- local data = file.readAll()
- file.close()
- local func, err = loadstring(data)
- if not func then
- Log("Syntax error: " .. err)
- return
- end
- SendMessage("UPDATEMOD", id, {name = filename, code = data})
- Log("Sent module file to " .. tostring(id))
- elseif args[1] == "lights" then
- if args[2] == "on" then
- SendMessage("LIGHTS", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, true)
- Log("Enabled lights")
- elseif args[2] == "off" then
- SendMessage("LIGHTS", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, false)
- Log("Disabled lights")
- else
- Log("Usage: con lights <on/off> [id]")
- end
- elseif args[1] == "spawner" then
- if args[2] == "on" then
- SendMessage("SETSPAWNER", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, true)
- Log("Enabled spawner")
- elseif args[2] == "off" then
- SendMessage("SETSPAWNER", tonumber(args[3] or RECIPIENT_ALL) or RECIPIENT_ALL, false)
- Log("Disabled spawner")
- else
- Log("Usage: con spawner <on/off> [id]")
- end
- else
- Log("Unknown command")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement