Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not require("component").isAvailable("modem") then
- io.stderr:write("This program requires a wireless network card!")
- os.exit(1)
- end
- local modem = require("component").modem
- local event = require("event")
- local term = require("term")
- local ser = require("serialization")
- local shell = require("shell")
- local INP_PATH = "/etc/nano.cfg"
- local AINP_PATH = "/etc/nano_state.cfg"
- local MAXINPUTS = 10
- local PORT = 443
- local inputs = {}
- local activeInputs = {}
- if not modem.isWireless() then
- io.stderr:write("Your network card isn't wireless!")
- os.exit(1)
- end
- if not modem.isOpen(PORT) then
- modem.open(PORT)
- end
- function send(command, ...)
- modem.broadcast(PORT, "nanomachines", command, ...)
- end
- local _, ops = shell.parse(...)
- if ops.h then
- io.write("-h shows this and -c rescan your nanomachines")
- os.exit(1)
- end
- send("setResponsePort", PORT)
- event.pull("modem_message")
- -------------------------------------------------------------------------------
- message = ""
- function handleMessage(tbl)
- local head = tbl[6]
- if head ~= "nanomachines" then return end
- local title = tbl[7]
- local msg1 = tbl[8]
- if title == "effects" then message = msg1
- elseif title == "totalInputCount" then MAXINPUTS = tonumber(msg1)
- end
- end
- send("getTotalInputCount")
- handleMessage({event.pull("modem_message")})
- function tableElementCheck(tbl, element)
- for key, val in pairs(tbl) do
- if val == element then
- return true, key
- end
- end
- return false
- end
- function getEffects()
- io.write("Getting your nanomachines' config.\n")
- for i = 1, MAXINPUTS do
- send("setInput", i, true)
- event.pull("modem_message")
- send("getActiveEffects")
- handleMessage({event.pull("modem_message")})
- send("setInput", i, false)
- event.pull("modem_message")
- io.write("ID: "..i.." = "..message.."\n")
- if message ~= "{}" then
- inputs[i] = message
- end
- end
- local file = io.open(INP_PATH, "w")
- file:write(ser.serialize(inputs))
- file:close()
- end
- function turnEffect(inp)
- if not tableElementCheck(activeInputs, inp) then
- send("setInput", inp, true)
- event.pull("modem_message")
- activeInputs[inp] = inp
- else
- send("setInput", inp, false)
- event.pull("modem_message")
- activeInputs[inp] = nil
- end
- end
- function saveAndQuit()
- if activeInputs == {} and io.open(AINP_PATH, "r") then
- os.execute("rm "..AINP_PATH)
- return
- elseif activeInputs == {} then
- return
- end
- local file = io.open(AINP_PATH, "w")
- file:write(ser.serialize(activeInputs))
- file:close()
- modem.close(PORT)
- end
- function display(inp, ainp)
- term.clear()
- io.write("Inputs: \n")
- for key, val in pairs(inp) do
- io.write("ID: "..key.." = "..val.."\n")
- end
- io.write("Active inputs: ")
- for _, val in pairs(ainp) do
- io.write(val.." ")
- end
- io.write("\n")
- end
- if ops.c or not io.open(INP_PATH, "r") then
- getEffects()
- else
- file = io.open(INP_PATH, "r")
- inputs = ser.unserialize(file:read("*a"))
- file:close()
- if io.open(AINP_PATH, "r") then
- file = io.open(AINP_PATH, "r")
- activeInputs = ser.unserialize(file:read("*a"))
- file:close()
- end
- end
- --------------------------------------------------------------------------------
- while true do
- display(inputs, activeInputs)
- io.write("Print 0 to exit\nID: ")
- id = tonumber(term.read())
- if id == 0 then
- saveAndQuit()
- break
- elseif not id then
- io.stderr:write("\nInvalid input")
- os.sleep(1)
- else
- turnEffect(id)
- end
- end
- modem.close(PORT)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement