Asioron

reactor_control

Jan 31st, 2021
767
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local c = require('component')
  2. local event = require("event")
  3. local term = require('term')
  4. local srl = require('serialization')
  5. local mod = c.modem
  6. local inv = c.invoke
  7.  
  8. local setting = {}
  9. local temp = {}
  10.  
  11. local function readReactorBase()
  12.   setting = {}
  13.   print('Загрузка базы реакторов')
  14.   local f = io.open('/etc/reactor/setting.cfg', 'r')
  15.   if f then
  16.     setting = srl.unserialize(f:read())
  17.     f:close()
  18.     print('Загрузка завершена')
  19.   else
  20.     print('ОШИБКА! Нет файла конфигурации! Выполните настройку!')
  21.   end
  22. end
  23.  
  24. local function reactorOnOff(tupe)
  25.   local tmp = 0
  26.   if inv(setting[tupe].redstone, 'getOutput', setting.side) == 0 then
  27.     tmp = true
  28.     print('Врубаю реактор '..tupe)
  29.     inv(setting[tupe].redstone, 'setOutput', setting.side, 255)
  30.   else
  31.     tmp = false
  32.     print('Вырубаю реактор '..tupe)
  33.     inv(setting[tupe].redstone, 'setOutput', setting.side, 0)
  34.   end
  35.   return tmp
  36. end
  37.  
  38. local function returnStatus()
  39.   local status = {}
  40.   local tmp = 0
  41.   for i = 1, #setting do
  42.     status[#status+1] = {['Act'] = inv(setting[i].reactor, 'isActive'), ['Heat'] = math.ceil(100*inv(setting[i].reactor, 'getHeat')/inv(setting[i].reactor, 'getMaxHeat')),['Out'] = inv(setting[i].reactor, 'getReactorEUOutput')}
  43.   end
  44.   mod.broadcast(setting.port, 'INFO', srl.serialize(status))
  45. end
  46.  
  47. local function pause()
  48.   print('Включен режим паузы')
  49.   event.pull('modem_message')
  50.   print('Выключен режим паузы')
  51. end
  52.  
  53. term.clear()
  54. readReactorBase()
  55. mod.open(setting.port)
  56.  
  57. while true do
  58.   tmp = {event.pull(3, 'modem_message')}
  59.   if tmp[1] then
  60.     if tmp[6] == 'ON_OFF' then
  61.       reactorOnOff(tmp[7])
  62.       returnStatus()
  63.     elseif tmp[6] == 'PAUSE' then
  64.       pause()
  65.     end
  66.   end
  67.   returnStatus()
  68. end
  69.  
RAW Paste Data