Advertisement
meigrafd

OpenComputers: Nuclear Reactor Control - Computer (v0.2)

Aug 13th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.38 KB | None | 0 0
  1. --[[
  2.  
  3.  
  4. Der Computer sendet ständig ein PING.
  5. Antwortet jemand wird ein neuer Client mit seiner mac gespeichert und ein weiterer Reactor zur Anzeige hinzugefügt.
  6. Jeder Reactor kann Ein/Aus geschaltet werden. Der Client-Reactor ist per default aus, bis der Computer ihn einschaltet.
  7.  
  8.  
  9.  NOTE: Be sure that all your Robots have an unique Modem-Address! For that you must craft each Wireless Card yourself!
  10.  
  11. --]]
  12.  
  13.  
  14. --[[ Internal values - DONT change! ]]
  15.  
  16.  
  17. local component = require("component")  -- http://ocdoc.cil.li/api:component
  18. local computer = require("computer")
  19. local event = require("event")  -- http://ocdoc.cil.li/api:event
  20. local fs = require("filesystem")  -- http://ocdoc.cil.li/api:filesystem
  21. local math = require("math")
  22. local serial = require("serialization")
  23. local string = require("string")
  24. local text = require("text")
  25. local term = require("term")  -- http://ocdoc.cil.li/api:term
  26. local os = require("os")
  27. local gpu = component.gpu  -- http://ocdoc.cil.li/component:gpu
  28. local wlan = component.modem
  29.  
  30. local version = "16.08.2015"
  31. local wlanPort = 7331
  32. local PingEvery = 5 -- sec. Dont set too high! (but also not lower 2)
  33. local Clients = {}
  34. local textLength = {}
  35. local reasonLength = {}
  36. local verifyPONG = {}
  37. gpu.setResolution(130,40)
  38. local w,h = gpu.getResolution()
  39. local start_time=computer.uptime()
  40. wlan.open(wlanPort)
  41.  
  42.  
  43. -- Wireless Send signal strength/power. Default and Max is 400 meters (blocks)
  44. wlan.setStrength(200)
  45.  
  46.  
  47. ---[[ functions ]]
  48.  
  49.  
  50. -- RGB hexadecimal Color codes needed for GPU.
  51. --http://computercraft.info/wiki/Colors_(API)#Colors
  52. --http://www.w3schools.com/tags/ref_colorpicker.asp
  53. local rgb = {
  54.     black = 0x000000,
  55.     white = 0xFFFFFF,
  56.     orange = 0xFFCC33,
  57.     magenta = 0xCC66CC,
  58.     lightBlue = 0x6699FF,
  59.     yellow = 0xFFFF33,
  60.     lime = 0x33CC33,
  61.     pink = 0xFF6699,
  62.     gray = 0x333333,
  63.     lightGray = 0xCCCCCC,
  64.     cyan = 0x336699,
  65.     purple = 0x9933CC,
  66.     blue = 0x333399,
  67.     brown = 0x663300,
  68.     green = 0x336600,
  69.     red = 0xFF0000,
  70. }
  71.  
  72. -- This functions can be useful for sending/receiving messages via network.
  73. function encode(data)
  74.     return serial.serialize(data)
  75. end
  76. function decode(data)
  77.     status, result = pcall(serial.unserialize, data)
  78.     return status, result
  79. end
  80.  
  81.  
  82. -- http://stackoverflow.com/questions/1426954/split-string-in-lua
  83. function split(pString, pPattern)
  84.     local Table = {}    -- NOTE: use {n = 0} in Lua-5.0
  85.     local fpat = "(.-)" .. pPattern
  86.     local last_end = 1
  87.     local s, e, cap = pString:find(fpat, 1)
  88.     while s do
  89.         if s ~= 1 or cap ~= "" then
  90.             table.insert(Table, cap)
  91.         end
  92.         last_end = e+1
  93.         s, e, cap = pString:find(fpat, last_end)
  94.     end
  95.     if last_end <= #pString then
  96.         cap = pString:sub(last_end)
  97.         table.insert(Table, cap)
  98.     end
  99.     return Table
  100. end
  101.  
  102. function stringMatch(text, find)
  103.     if string.find(string.lower(text), string.lower(find)) then
  104.         return true
  105.     else
  106.         return false
  107.     end
  108. end
  109.  
  110. function round(num, idp)
  111.     return tonumber(string.format("%." .. (idp or 0) .. "f", num))
  112. end
  113.  
  114. function tableLength(T)
  115.     local count = 0
  116.     for k,v in pairs(T) do count = count + 1 end
  117.     return count
  118. end
  119.  
  120.  
  121. function findClient(adr)
  122.     for num,clientAddress in pairs(Clients) do
  123.         if clientAddress == adr then
  124.             return true, num, clientAddress
  125.         end
  126.     end
  127.     return false
  128. end
  129.  
  130. function addClient(remoteAddress, reactorPower)
  131.     local num = (tableLength(Clients) + 1)
  132.     Clients[num] = remoteAddress
  133.  
  134.     -- display notice: new reactor client added..
  135.     gpu.fill(1, h-3, w, 1, " ")
  136.     gpu.set(1, h-3, "Added new Reactor #"..num.." with max Power: "..reactorPower)
  137.  
  138.     local text = "Reactor #"..num.." Power: "..reactorPower.." EU/t Status:"
  139.     textLength[num] = (string.len(text) + 4)
  140.     textONbutton = (textLength[num] + 10)
  141.     textOFFbutton = (textONbutton + 6)
  142.     reasonLength[num] = (textOFFbutton + 10)
  143.  
  144.     gpu.setBackground(rgb.black)
  145.     gpu.setForeground(rgb.orange)
  146.     gpu.set(2, num, text)
  147.  
  148.     gpu.setForeground(rgb.lightBlue)
  149.     gpu.set(textLength[num], num, "Off")
  150.  
  151.     gpu.setBackground(rgb.green)
  152.     gpu.fill((textONbutton - 1), num, 5, 1, " ")
  153.     gpu.setForeground(rgb.black)
  154.     gpu.set(textONbutton, num, "ON")
  155.  
  156.     gpu.setBackground(rgb.red)
  157.     gpu.fill((textOFFbutton - 1), num, 5, 1, " ")
  158.     gpu.setForeground(rgb.black)
  159.     gpu.set(textOFFbutton, num, "OFF")
  160.  
  161.     -- reset colors to normal..
  162.     gpu.setForeground(rgb.white)
  163.     gpu.setBackground(rgb.black)
  164. end
  165.  
  166. function updateWarning(adr, text)
  167.     gpu.setForeground(rgb.black)
  168.     local check, num, adr = findClient(adr)
  169.     if (check == true) then
  170.         -- clear line
  171.         gpu.fill(reasonLength[num], num, w, 1, " ")
  172.         -- display warning message
  173.         gpu.setForeground(rgb.red)
  174.         gpu.set(reasonLength[num], num, text)
  175.     end
  176.     -- reset colors to normal..
  177.     gpu.setForeground(rgb.white)
  178.     gpu.setBackground(rgb.black)
  179. end
  180.  
  181. function updateReactorState(adr, state)
  182.     gpu.setForeground(rgb.black)
  183.     local check, num, adr = findClient(adr)
  184.     if (check == true) then
  185.         gpu.fill(textLength[num], num, 5, 1, " ")
  186.         gpu.setForeground(rgb.lightBlue)
  187.         if state == "ON" then
  188.             gpu.set(textLength[num], num, "On")
  189.         else
  190.             gpu.set(textLength[num], num, "Off")
  191.         end
  192.     end
  193.     -- reset colors to normal..
  194.     gpu.setForeground(rgb.white)
  195.     gpu.setBackground(rgb.black)
  196. end
  197.  
  198.  
  199. function onClick(event, screenAddress, x, y, mouseButton, playerName)
  200.     local mode = nil
  201.     if (x >= (textONbutton - 1)) and (x <= (textONbutton + 5)) then
  202.         mode = 'ON'
  203.     end
  204.     if (x >= (textOFFbutton - 1)) and (x <= (textOFFbutton + 5)) then
  205.         mode = 'OFF'
  206.     end
  207.     for num,clientAddress in pairs(Clients) do
  208.         if num == y then
  209.             wlan.send(clientAddress, wlanPort, 'POWER:'..mode)
  210.         end
  211.     end
  212. end
  213. event.listen("touch", onClick)
  214.  
  215. function messageReceived(event, localAddress, remoteAddress, port, senderDistance, message)
  216.     -- Determine if message contains ':'
  217.     if string.find(message, ":") then
  218.         -- split message into a table
  219.         msgTable = split(message, ":")
  220.         if msgTable[1] == "POWER" then
  221.             -- change state: ON/OFF
  222.             updateReactorState(remoteAddress, msgTable[2])
  223.         elseif msgTable[1] == "MISSING" then
  224.             -- Robot cant pick up coolant/uran..
  225.             updateWarning(remoteAddress, "MISSING: "..msgTable[2])
  226.         elseif msgTable[1] == "OVERHEAT" then
  227.             -- Thermal 1 or 2 triggered..
  228.             updateWarning(remoteAddress, "OVERHEAT: "..msgTable[2])
  229.  
  230.         elseif msgTable[1] == "PONG" then
  231.             -- an maybe unknown client answered..
  232.             local check, num, adr = findClient(remoteAddress)
  233.             if (check == false) then
  234.                 -- add new/unknown client..
  235.                 addClient(remoteAddress, msgTable[2])
  236.                 -- send PONG back to verify receive
  237.                 verifyPONG[num] = remoteAddress
  238.                 wlan.send(remoteAddress, wlanPort, 'PONG:OK')
  239.             end
  240.         end
  241.     -- message doesnt contain ':'
  242.     else
  243.         if message == "OK" then
  244.             -- PING/PONG verificated
  245.             local check, num, adr = findClient(remoteAddress)
  246.             if num ~= nil then
  247.                 table.remove(verifyPONG, num)
  248.             end
  249.         end
  250.     end
  251. end
  252. event.listen("modem_message", messageReceived)
  253.  
  254.  
  255. --[[ End functions ]]
  256.  
  257.  
  258. --[[ Main Script ]]
  259.  
  260.  
  261. term.clear()
  262. gpu.setBackground(rgb.black)
  263. gpu.setForeground(rgb.white)
  264. gpu.set(math.floor(w/3), (h-1), "Nuclear Reactor Control - Server")
  265. gpu.set(math.floor(w/3), h, "by meigrafd @ "..version)
  266.  
  267.  
  268. pingTimer = computer.uptime() + PingEvery
  269. while true do
  270.     local uptime = round(computer.uptime()-start_time, 0)
  271.     gpu.set(w-25, h, "Current uptime: "..uptime.." sec")
  272.     -- ping arround, maybe an unknown client answers and get added..
  273.     if (computer.uptime() >= pingTimer) then
  274.         pingTimer = computer.uptime() + PingEvery
  275.         wlan.broadcast(wlanPort, 'PING')
  276.     end
  277.    
  278.     -- verify PONG received..
  279.     if (tableLength(verifyPONG) > 0) then
  280.         for num,clientAdr in pairs(verifyPONG) do
  281.             wlan.send(clientAdr, wlanPort, 'PONG:OK')
  282.         end
  283.     end
  284.     os.sleep(1)
  285. end
  286.  
  287.  
  288. --
  289. -- EOF
  290. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement