Advertisement
sanovskiy

Reactor controller

Nov 12th, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.38 KB | None | 0 0
  1. version="Reactor control ver.1.0.3 by San"
  2. configFile="reactor.conf"
  3. local function saveConfig(table, file)
  4.   local fConfig = fs.open(file, "w") or error("Cannot open file "..file, 2)
  5.   fConfig.write(textutils.serialize(table))
  6.   fConfig.close()
  7. end
  8. local function loadConfig(file)
  9.   local fConfig = fs.open(file, "r")
  10.   local ret = textutils.unserialize(fConfig.readAll())
  11.   fConfig.close()
  12.   return ret
  13. end
  14. if fs.exists(configFile) then
  15.   cfg=loadConfig(configFile)
  16. else
  17.   cfg={}
  18.   cfg["SideReactor"] = "back"
  19.   cfg["SideMFSU"] = "bottom"
  20.   cfg["SideMonitor"] = "top"
  21.   cfg["RebootStatus"] = 1
  22.   saveConfig(cfg, configFile)
  23. end
  24. if cfg["invertSignal"]==nil then
  25.     cfg["invertSignal"] = false
  26.     saveConfig(cfg, configFile)
  27. end
  28. if not(fs.exists("startup")) then
  29.   local fStartup = fs.open("startup", "w") or error("Cannot open file startup", 2)
  30.   fStartup.write("shell.run(\"reactor\")")
  31.   fStartup.close()
  32. end
  33. if cfg["RebootStatus"]==nil then
  34.     cfg["RebootStatus"]=1
  35. end
  36. if (peripheral.getType(cfg["SideMonitor"])=="monitor") then
  37.   mon=peripheral.wrap(cfg["SideMonitor"])
  38. end
  39.  
  40. function showStatus()
  41.     term.clear()
  42.     term.setCursorPos(1,1)
  43.     print(version)
  44.     if STATUS==1 then
  45.         print("Press ''S'' to shut down reactor")
  46.     else
  47.         print("Press ''S'' to switch reactor on")
  48.     end
  49.     print("Press ''R'' to reboot")
  50.     print("Press ''U'' to update software and reboot")
  51. end
  52. STATUS=cfg["RebootStatus"] -- 0 - ON HOLD 1 - RUNNING
  53. showStatus()
  54. function pullEvents()
  55.     while true do
  56.         local sEvent, param = os.pullEvent("key")
  57.         if sEvent == "key" then
  58.             if param == 16 then
  59.                 error()
  60.             end
  61.             if param == 31 or param == 19 or param == 22 then
  62.                 if param == 31 then
  63.                     if STATUS~=1 then
  64.                         STATUS=1
  65.                     else
  66.                         STATUS=0
  67.                     end
  68.                     cfg["RebootStatus"] = STATUS
  69.                     saveConfig(cfg,configFile)
  70.                 end
  71.                 if param == 22 or param == 19 then
  72.                     redstone.setOutput(cfg["SideReactor"], false)
  73.                     if (STATUS) then
  74.                         print("Reactor shutted down")
  75.                     end
  76.                     if (mon ~= nil) then
  77.                         mon.setBackgroundColor(colors.lightBlue)
  78.                         mon.clear()
  79.                     end
  80.                     if param == 22 then
  81.                         shell.run("san","update reactor")
  82.                     end
  83.                     term.setTextColor(colors.red)
  84.                     print("REBOOTING")
  85.                     sleep(1)
  86.                     os.reboot()
  87.                 end
  88.                 showStatus()
  89.             end
  90.         end
  91.     end
  92. end
  93.  
  94. function round(num, idp)
  95.   local mult = 10^(idp or 0)
  96.   return math.floor(num * mult + 0.5) / mult
  97. end
  98.  
  99. function setMon(color)
  100.     if (mon ~= nil) then
  101.         mon.setBackgroundColor(color)
  102.         mon.clear()
  103.     end
  104. end
  105. shutdownTime = -1
  106. sleepTime=0.3
  107. function main()
  108.     while true do
  109.         local FullMFSU = redstone.getInput(cfg["SideMFSU"])
  110.         local ReactorOn = cfg["invertSignal"]
  111.         if STATUS==0 then
  112.             setMon(colors.lightBlue)
  113.         else
  114.             if FullMFSU then
  115.                 setMon(colors.red)
  116.                 shutdownTime = os.time()
  117.             else
  118.                 now = os.time()
  119.                 if now<shutdownTime then
  120.                     now=now+24
  121.                 end
  122.                 if now-sleepTime > shutdownTime then
  123.                     setMon(colors.lime)
  124.                     ReactorOn=not(cfg["invertSignal"])
  125.                 else
  126.                     mon.setCursorPos(1,1)
  127.                     mon.write( "Start in "..tostring(math.floor((sleepTime-(now-shutdownTime))*100)).."          ")
  128.                     --mon.write(round(round((-0.1)-shutdownTime,2)*-100))
  129.                 end
  130.             end
  131.         end
  132.         redstone.setOutput(cfg["SideReactor"], ReactorOn)
  133.         os.queueEvent("randomEvent")
  134.         os.pullEvent()
  135.     end
  136. end
  137.  
  138. parallel.waitForAny(pullEvents, main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement