Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Der Computer sendet ständig ein PING.
- Antwortet jemand wird ein neuer Client mit seiner mac gespeichert und ein weiterer Reactor zur Anzeige hinzugefügt.
- Jeder Reactor kann Ein/Aus geschaltet werden. Der Client-Reactor ist per default aus, bis der Computer ihn einschaltet.
- NOTE: Be sure that all your Robots have an unique Modem-Address! For that you must craft each Wireless Card yourself!
- --]]
- --[[ Internal values - DONT change! ]]
- local component = require("component") -- http://ocdoc.cil.li/api:component
- local computer = require("computer")
- local event = require("event") -- http://ocdoc.cil.li/api:event
- local fs = require("filesystem") -- http://ocdoc.cil.li/api:filesystem
- local math = require("math")
- local serial = require("serialization")
- local string = require("string")
- local text = require("text")
- local term = require("term") -- http://ocdoc.cil.li/api:term
- local os = require("os")
- local gpu = component.gpu -- http://ocdoc.cil.li/component:gpu
- local wlan = component.modem
- local version = "16.08.2015"
- local wlanPort = 7331
- local PingEvery = 5 -- sec. Dont set too high! (but also not lower 2)
- local Clients = {}
- local textLength = {}
- local reasonLength = {}
- local verifyPONG = {}
- gpu.setResolution(130,40)
- local w,h = gpu.getResolution()
- local start_time=computer.uptime()
- wlan.open(wlanPort)
- -- Wireless Send signal strength/power. Default and Max is 400 meters (blocks)
- wlan.setStrength(200)
- ---[[ functions ]]
- -- RGB hexadecimal Color codes needed for GPU.
- --http://computercraft.info/wiki/Colors_(API)#Colors
- --http://www.w3schools.com/tags/ref_colorpicker.asp
- local rgb = {
- black = 0x000000,
- white = 0xFFFFFF,
- orange = 0xFFCC33,
- magenta = 0xCC66CC,
- lightBlue = 0x6699FF,
- yellow = 0xFFFF33,
- lime = 0x33CC33,
- pink = 0xFF6699,
- gray = 0x333333,
- lightGray = 0xCCCCCC,
- cyan = 0x336699,
- purple = 0x9933CC,
- blue = 0x333399,
- brown = 0x663300,
- green = 0x336600,
- red = 0xFF0000,
- }
- -- This functions can be useful for sending/receiving messages via network.
- function encode(data)
- return serial.serialize(data)
- end
- function decode(data)
- status, result = pcall(serial.unserialize, data)
- return status, result
- end
- -- http://stackoverflow.com/questions/1426954/split-string-in-lua
- function split(pString, pPattern)
- local Table = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table, cap)
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end
- function stringMatch(text, find)
- if string.find(string.lower(text), string.lower(find)) then
- return true
- else
- return false
- end
- end
- function round(num, idp)
- return tonumber(string.format("%." .. (idp or 0) .. "f", num))
- end
- function tableLength(T)
- local count = 0
- for k,v in pairs(T) do count = count + 1 end
- return count
- end
- function findClient(adr)
- for num,clientAddress in pairs(Clients) do
- if clientAddress == adr then
- return true, num, clientAddress
- end
- end
- return false
- end
- function addClient(remoteAddress, reactorPower)
- local num = (tableLength(Clients) + 1)
- Clients[num] = remoteAddress
- -- display notice: new reactor client added..
- gpu.fill(1, h-3, w, 1, " ")
- gpu.set(1, h-3, "Added new Reactor #"..num.." with max Power: "..reactorPower)
- local text = "Reactor #"..num.." Power: "..reactorPower.." EU/t Status:"
- textLength[num] = (string.len(text) + 4)
- textONbutton = (textLength[num] + 10)
- textOFFbutton = (textONbutton + 6)
- reasonLength[num] = (textOFFbutton + 10)
- gpu.setBackground(rgb.black)
- gpu.setForeground(rgb.orange)
- gpu.set(2, num, text)
- gpu.setForeground(rgb.lightBlue)
- gpu.set(textLength[num], num, "Off")
- gpu.setBackground(rgb.green)
- gpu.fill((textONbutton - 1), num, 5, 1, " ")
- gpu.setForeground(rgb.black)
- gpu.set(textONbutton, num, "ON")
- gpu.setBackground(rgb.red)
- gpu.fill((textOFFbutton - 1), num, 5, 1, " ")
- gpu.setForeground(rgb.black)
- gpu.set(textOFFbutton, num, "OFF")
- -- reset colors to normal..
- gpu.setForeground(rgb.white)
- gpu.setBackground(rgb.black)
- end
- function updateWarning(adr, text)
- gpu.setForeground(rgb.black)
- local check, num, adr = findClient(adr)
- if (check == true) then
- -- clear line
- gpu.fill(reasonLength[num], num, w, 1, " ")
- -- display warning message
- gpu.setForeground(rgb.red)
- gpu.set(reasonLength[num], num, text)
- end
- -- reset colors to normal..
- gpu.setForeground(rgb.white)
- gpu.setBackground(rgb.black)
- end
- function updateReactorState(adr, state)
- gpu.setForeground(rgb.black)
- local check, num, adr = findClient(adr)
- if (check == true) then
- gpu.fill(textLength[num], num, 5, 1, " ")
- gpu.setForeground(rgb.lightBlue)
- if state == "ON" then
- gpu.set(textLength[num], num, "On")
- else
- gpu.set(textLength[num], num, "Off")
- end
- end
- -- reset colors to normal..
- gpu.setForeground(rgb.white)
- gpu.setBackground(rgb.black)
- end
- function onClick(event, screenAddress, x, y, mouseButton, playerName)
- local mode = nil
- if (x >= (textONbutton - 1)) and (x <= (textONbutton + 5)) then
- mode = 'ON'
- end
- if (x >= (textOFFbutton - 1)) and (x <= (textOFFbutton + 5)) then
- mode = 'OFF'
- end
- for num,clientAddress in pairs(Clients) do
- if num == y then
- wlan.send(clientAddress, wlanPort, 'POWER:'..mode)
- end
- end
- end
- event.listen("touch", onClick)
- function messageReceived(event, localAddress, remoteAddress, port, senderDistance, message)
- -- Determine if message contains ':'
- if string.find(message, ":") then
- -- split message into a table
- msgTable = split(message, ":")
- if msgTable[1] == "POWER" then
- -- change state: ON/OFF
- updateReactorState(remoteAddress, msgTable[2])
- elseif msgTable[1] == "MISSING" then
- -- Robot cant pick up coolant/uran..
- updateWarning(remoteAddress, "MISSING: "..msgTable[2])
- elseif msgTable[1] == "OVERHEAT" then
- -- Thermal 1 or 2 triggered..
- updateWarning(remoteAddress, "OVERHEAT: "..msgTable[2])
- elseif msgTable[1] == "PONG" then
- -- an maybe unknown client answered..
- local check, num, adr = findClient(remoteAddress)
- if (check == false) then
- -- add new/unknown client..
- addClient(remoteAddress, msgTable[2])
- -- send PONG back to verify receive
- verifyPONG[num] = remoteAddress
- wlan.send(remoteAddress, wlanPort, 'PONG:OK')
- end
- end
- -- message doesnt contain ':'
- else
- if message == "OK" then
- -- PING/PONG verificated
- local check, num, adr = findClient(remoteAddress)
- if num ~= nil then
- table.remove(verifyPONG, num)
- end
- end
- end
- end
- event.listen("modem_message", messageReceived)
- --[[ End functions ]]
- --[[ Main Script ]]
- term.clear()
- gpu.setBackground(rgb.black)
- gpu.setForeground(rgb.white)
- gpu.set(math.floor(w/3), (h-1), "Nuclear Reactor Control - Server")
- gpu.set(math.floor(w/3), h, "by meigrafd @ "..version)
- pingTimer = computer.uptime() + PingEvery
- while true do
- local uptime = round(computer.uptime()-start_time, 0)
- gpu.set(w-25, h, "Current uptime: "..uptime.." sec")
- -- ping arround, maybe an unknown client answers and get added..
- if (computer.uptime() >= pingTimer) then
- pingTimer = computer.uptime() + PingEvery
- wlan.broadcast(wlanPort, 'PING')
- end
- -- verify PONG received..
- if (tableLength(verifyPONG) > 0) then
- for num,clientAdr in pairs(verifyPONG) do
- wlan.send(clientAdr, wlanPort, 'PONG:OK')
- end
- end
- os.sleep(1)
- end
- --
- -- EOF
- --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement