Advertisement
Pdevo

scambio

Nov 12th, 2015
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 0 0
  1. -- configurazione numero
  2.  
  3. n = 0
  4.  
  5. -- configurazione lato
  6.  
  7. l = " "
  8.  
  9. -- configurazione archivio
  10.  
  11. a = "stato"
  12.  
  13. -- fattore attivita'
  14.  
  15. h = 1
  16.  
  17. -- fattore gerarchico
  18.  
  19. g = 1/10
  20.  
  21. -- operatore gerarchico
  22.  
  23. w = n*g
  24.  
  25. -- inizializzazione periferiche
  26.  
  27. modem = nil
  28.  for _, side in pairs(rs.getSides()) do
  29.   if peripheral.getType(side) == "modem" then
  30.   modem = side
  31.   break
  32.   end
  33.  end
  34.  
  35. -- codice avvio
  36.  
  37. sleep(1)
  38. print("< "..n.." >")
  39. sleep(1)
  40. print("scambio")
  41. sleep(1)
  42. print("assegnazione")
  43. sleep(1)
  44. print("inizializzazione")
  45. sleep(1)
  46. print("connessione")
  47. rednet.open(modem)
  48. rednet.broadcast("avvio_scambio_"..n)
  49. sleep(1)
  50. print("funzionamento")
  51.  
  52. -- codice funzioni
  53.  
  54. function genera_tabella()
  55.  
  56. scambio = {}
  57. scambio[stato] = {}
  58. scambio[tempo] = {}
  59.  
  60. end
  61.  
  62. function salva_tabella(table,name)
  63.  
  64. local file = fs.open(name,"w")
  65. local text = textutils.serialize(table)
  66. file.write(text)
  67. file.close()
  68.  
  69. end
  70.  
  71. function carica_tabella(table,name)
  72.  
  73. local file = fs.open(name,"r")
  74. local text = file.readAll()
  75. local data = textutils.unserialize(text)
  76. table = data
  77. file.close()
  78.  
  79. end
  80.  
  81. -- codice archiviazione
  82.  
  83. local archivio = fs.exists(a)
  84.  
  85.  if archivio == true then
  86.  carica_tabella(scambio,a)
  87.  end
  88.  
  89.  if archivio == false then
  90.  genera_tabella()
  91.  end
  92.  
  93. -- codice stato
  94.  
  95. local s = scambio[stato]
  96. local t = scambio[tempo]
  97.  
  98.  if s == "attivo" then
  99.  redstone.setOutput(l, true)
  100.  end
  101.  
  102.  if s == "inattivo" then
  103.  redstone.setOutput(l, false)
  104.  end
  105.  
  106.  if s == "temporizzato" then
  107.   redstone.setOutput(l, true)
  108.   sleep(t)
  109.   local s = "inattivo"
  110.   local t = nil
  111.   redstone.setOutput(l, false)
  112.   scambio[stato] = s
  113.   scambio[tempo] = t
  114.   rednet.broadcast("aggiornamento_scambio_"..n.."_"..s)
  115.  end
  116.  
  117. -- codice sistema
  118.  
  119. while true do
  120. local message = rednet.receive()
  121. local words = {}
  122.  
  123. for word in string.gmatch(message, "%w+") do words[#words + 1] = word end
  124.  
  125.  if words[1] == "comando" then
  126.  
  127.   if words[2] == "scambio" then
  128.  
  129.    if tonumber(words[3]) == n then
  130.    
  131.     if words[4] == "attivo" then
  132.     local s = "attivo"
  133.     redstone.setOutput(l, true)
  134.     scambio[stato] = s
  135.     rednet.broadcast("aggiornamento_scambio_"..n.."_"..s)
  136.     salva_tabella(scambio,a)
  137.     end  
  138.    
  139.     if words[4] == "inattivo" then
  140.     local s = "inattivo"
  141.     redstone.setOutput(l, false)
  142.     scambio[stato] = s
  143.     rednet.broadcast("aggiornamento_scambio_"..n.."_"..s)
  144.     salva_tabella(scambio,a)
  145.     end
  146.    
  147.     if words[4] == "temporizzato" then
  148.      if words[5] == nil then
  149.      local s = "temporizzato"
  150.      local t = h
  151.      redstone.setOutput(l, true)
  152.      scambio[stato] = s
  153.      scambio[tempo] = t
  154.      rednet.broadcast("aggiornamento_scambio_"..n.."_"..s.."_predefinito")
  155.      salva_tabella(scambio,a)
  156.      sleep(t)
  157.      local s = "inattivo"
  158.      local t = nil
  159.      redstone.setOutput(l, false)
  160.      scambio[stato] = s
  161.      scambio[tempo] = t
  162.      rednet.broadcast("aggiornamento_scambio_"..n.."_"..s)
  163.      salva_tabella(scambio,a)
  164.      end
  165.      if words[5] ~= nil then
  166.      local s = "temporizzato"
  167.      local t = tonumber(words[5])
  168.      redstone.setOutput(l, true)
  169.      scambio[stato] = s
  170.      scambio[tempo] = t
  171.      rednet.broadcast("aggiornamento_scambio_"..n.."_"..s.."_"..t)
  172.      salva_tabella(scambio,a)
  173.      sleep(t)
  174.      local s = "inattivo"
  175.      local t = nil
  176.      scambio[stato] = s
  177.      scambio[tempo] = t
  178.      redstone.setOutput(l, false)
  179.      rednet.broadcast("aggiornamento_scambio_"..n.."_"..s)
  180.      salva_tabella(scambio,a)
  181.      end
  182.     end
  183.    
  184.    end
  185.    
  186.   end
  187.  
  188.  end
  189.  
  190.  if words[1] == "sistemi" then
  191.  
  192.   if words[2] == "scambio" then
  193.  
  194.    if tonumber(words[3]) == n then
  195.    
  196.     if words[4] == "spegnimento" then
  197.     rednet.broadcast("spegnimento_scambio_"..n)
  198.     os.shutdown()
  199.     end
  200.     if words[4] == "riavvio" then
  201.     rednet.broadcast("riavvio_scambio_"..n)
  202.     os.reboot()
  203.     end
  204.    
  205.    end
  206.    
  207.   end
  208.  
  209.  end
  210.  
  211. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement