Advertisement
Pdevo

gestore_stati

Sep 30th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. -- configurazione sistema
  2.  
  3. app = "gestore"
  4. cod = "stati"
  5. pro = "scgrc"
  6.  
  7. -- configurazione periferiche
  8.  
  9. modem = nil
  10.  for n,l in pairs(peripheral.getNames()) do
  11.   if peripheral.getType(l) == "modem" then
  12.   modem = l
  13.   break
  14.   end
  15.  end
  16.  
  17. drive = nil
  18.  for n,l in pairs(peripheral.getNames()) do
  19.   if peripheral.getType(l) == "drive" then
  20.   drive = l
  21.   break
  22.   end
  23.  end
  24.  
  25. -- funzioni sistema
  26.  
  27. function sistema_inizializza(tipo,nome)
  28.  
  29. local path = disk.getMountPath(drive)
  30. local file = fs.open(path.."/"..tipo.."/"..nome,"r")
  31. local text = file.readAll()
  32. local data = textutils.unserialize(text)
  33. file.close()
  34. return data
  35.  
  36. end
  37.  
  38. -- funzioni applicativo
  39.  
  40. function tabella_salva(tabella,tipo,nome)
  41.  
  42. local path = disk.getMountPath(drive)
  43. local file = fs.open(path.."/"..tipo.."/"..nome,"w")
  44. local text = textutils.serialize(tabella)
  45. file.write(text)
  46. file.close()
  47.  
  48. end
  49.  
  50. function tabella_carica(tipo,nome)
  51.  
  52. local path = disk.getMountPath(drive)
  53. local file = fs.open(path.."/"..tipo.."/"..nome,"r")
  54. local text = file.readAll()
  55. local data = textutils.unserialize(text)
  56. file.close()
  57. return data
  58.  
  59. end
  60.  
  61. function tabella_posiziona(tabella,indice)
  62.  
  63. table.insert(tabella,indice,posizione)
  64.  
  65. end
  66.  
  67. function tabella_aggiungi(tabella,elemento)
  68.  
  69. table.insert(tabella,elemento)
  70.  
  71. end
  72.  
  73. function tabella_rimuovi(tabella,elemento)
  74.  
  75. table.remove(tabella,elemento)
  76.  
  77. end
  78.  
  79. -- codice sistema
  80.  
  81. sleep()
  82. print(app.."_"..cod)
  83. rednet.open(modem)
  84. rednet.host(pro,app.."_"..cod)
  85. rednet.broadcast({"avvio",app,cod})
  86.  
  87. -- codice tabella
  88.  
  89. S = tabella_carica("dati","stati")
  90.  
  91. -- codice applicativo
  92.  
  93. while true do
  94. local id, scgrc = rednet.receive()
  95.  
  96.  if scgrc[1] == "stato" then
  97.  
  98.  local d = scgrc[2]
  99.  local n = scgrc[3]
  100.  
  101.   if S[d] ~= nil then
  102.    if S[d][n] ~= nil then
  103.    rednet.broadcast({d,n,s[d][n]})
  104.    end
  105.    if S[d][n] == nil then
  106.    rednet.broadcast({d,n,"indefinito"})
  107.    end
  108.   end
  109.   if S[d] == nil then
  110.   rednet.broadcast({d,"dispositivo","indefinito"})
  111.   end
  112.  
  113.  end
  114.  
  115.  if scgrc[1] == "aggiornamento" then
  116.  
  117.  local d = scgrc[2]
  118.  local n = scgrc[3]
  119.  local s = scgrc[4]
  120.  
  121.   if S[d] ~= nil then
  122.   S[d][n] = s
  123.   tabella_salva(S,"dati","stati")
  124.   end
  125.   if S[d] == nil then
  126.   S[d] = {}
  127.   S[d][n] = s
  128.   tabella_salva(S,"dati","stati")
  129.   end
  130.  
  131.  end
  132.  
  133.  if scgrc[1] == "sistemi" then
  134.  
  135.   if scgrc[2] == app then
  136.  
  137.    if scgrc[3] == cod then
  138.  
  139.     if scgrc[4] == "spegnimento" then
  140.     rednet.broadcast({"spegnimento",app,cod})
  141.     os.shutdown()
  142.     end
  143.     if scgrc[4] == "riavvio" then
  144.     rednet.broadcast({"riavvio",app,cod})
  145.     os.reboot()
  146.     end
  147.  
  148.    end
  149.  
  150.   end
  151.  
  152.  end
  153.  
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement