Advertisement
Pdevo

gestore_treni

Feb 28th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.20 KB | None | 0 0
  1. -- configurazione sistema
  2.  
  3. app = "gestore"
  4. cod = "treni"
  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,elemento)
  62.  
  63. table.insert(tabella,indice,elemento)
  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. function configura_versori(v)
  80.  
  81. q = nil
  82. g = nil
  83. w = nil
  84.  if v == N.ve then
  85.  q = "esistente"
  86.  g = G.de
  87.  w = N.we
  88.  end
  89.  if v == N.vs then
  90.  q = "esistente"
  91.  g = G.ds
  92.  w = N.ws
  93.  end
  94.  if v == N.vw then
  95.  q = "esistente"
  96.  g = G.dw
  97.  w = N.ww
  98.  end
  99.  if v == N.vn then
  100.  q = "esistente"
  101.  g = G.dn
  102.  w = N.wn
  103.  end
  104.  if v ~= N.ve and v ~= N.vs and v ~= N.vw and v ~= N.vn then
  105.  q = "inesistente"
  106.  end
  107.  
  108. end
  109.  
  110. function configura_blocchi(q,g,v,d)
  111.  
  112. k = nil
  113.  if q == "esistente" and g == "ammessa" then
  114.  k = v*N.cd+d
  115.  end
  116.  
  117. end
  118.  
  119. function determina_orario()
  120.  
  121. h = os.time()
  122.  
  123. end
  124.  
  125. function determina_azione(n)
  126.  
  127.  if T[n].s.a ~= "convergente" then
  128.   if T[n].r.b == "indefinito" and T[n].r.x ~= "indefinito" then
  129.   T[n].s.a = "convergente"
  130.   T[n].s.r = E[n].ox
  131.   end
  132.  end
  133.  if T[n].s.a ~= "controvergente" then
  134.   if T[n].r.b == "indefinito" and T[n].r.y ~= "indefinito" then
  135.   T[n].s.a = "controvergente"
  136.   T[n].s.r = E[n].ox
  137.   end
  138.  end
  139.  if T[n].s.a ~= "originante" then
  140.   if T[n].r.b ~= "indefinito" and T[n].r.l == "indefinito" then
  141.   T[n].s.a = "originante"
  142.   T[n].s.r = E[n].oy
  143.   end
  144.  end
  145.  if T[n].s.a ~= "divergente" then
  146.   if T[n].r.b ~= "indefinito" and T[n].r.l ~= "indefinito" then
  147.   T[n].s.a = "divergente"
  148.   T[n].s.r = E[n].oy
  149.   end
  150.  end
  151.  
  152. end
  153.  
  154. function determina_stato(n,h)
  155.  
  156. local r = T[n].s.r
  157. local d = h - r
  158. T[n].s.d = d
  159.  if T[n].s.o ~= "anticipo" and (-O.fe < d and d < -O.fi) then
  160.  T[n].s.o = "anticipo"
  161.  end
  162.  if T[n].s.o ~= "puntuale" and (-O.fi < d and d < O.fi) then
  163.  T[n].s.o = "puntuale"
  164.  end
  165.  if T[n].s.o ~= "ritardo" and (O.fi < d and d < O.fe) then
  166.  T[n].s.o = "ritardo"
  167.  end
  168.  if T[n].s.o ~= "vincolo" and (O.fe < d or d < -O.fe) then
  169.  T[n].s.o = "escluso"
  170.  end
  171.  
  172. end
  173.  
  174. function determina_vincolo(n,h,i)
  175.  
  176. local r = T[n].r.b
  177. local d = h - r
  178.  if T[n].u.v ~= "blocco" and d > O.fi then
  179.  T[n].u.v = "blocco"
  180.  end
  181.  if T[n].u.v ~= "libero" and d < O.fi then
  182.  T[n].u.v = "libero"
  183.  end
  184.  
  185. end
  186.  
  187. function iterazione_inizializza(n,h)
  188.  
  189. T[n].i.e = h
  190. T[n].i.r = "sblocco"
  191. T[n].i.c = 0
  192.  
  193. end
  194.  
  195. function iterazione_ripetitore(n,h)
  196.  
  197. local e = T[n].i.e
  198. local d = h - e
  199.  if T[n].i.r ~= "blocco" and d > O.fc then
  200.  T[n].i.r = "blocco"
  201.  end
  202.  if T[n].i.r ~= "sblocco" and d < O.fc then
  203.  T[n].i.r = "sblocco"
  204.  end
  205.  
  206. end
  207.  
  208. function iterazione_contatore(n,h)
  209.  
  210. T[n].i.c = T[n].i.c + 1
  211.  
  212. end
  213.  
  214. function compito_iterazione(n,t,v,l,b)
  215.  
  216.  if T[n].c.t == "indefinito" then
  217.  T[n].c.t = 1
  218.  else
  219.  T[n].c.t = T[n].c.t + 1
  220.  end
  221.  if b ~= "elabora" and T[n].c.t <= O.fd then
  222.  compito_prepara(n,t,v,l,b)
  223.  end
  224.  if b == "elabora" or T[n].c.t > O.fd then
  225.  compito_prepara(n,t,v,l,"elabora")
  226.  end
  227.  
  228. end
  229.  
  230. function compito_annuncia(n,t,v,l,b)
  231.  
  232. rednet.broadcast({"annuncia",t,v,l,b})
  233.  --while true do
  234.  --local id, scgrc = rednet.receive()
  235.  -- if scgrc[1] == t and scgrc[2] == v and scgrc[3] == l and scgrc[4] == T[n].c.b then
  236.  -- T[n].c.a = scgrc[5]
  237.  -- break
  238.  -- end
  239.  --end
  240.  
  241. end
  242.  
  243. function compito_prepara(n,t,v,l,b)
  244.  
  245. rednet.broadcast({"prepara",t,v,l,b})
  246.  while true do
  247.  local id, scgrc = rednet.receive()
  248.   if scgrc[1] == t and scgrc[2] == v and scgrc[3] == l then
  249.    if scgrc[5] == "preparato" then
  250.    T[n].c.b = scgrc[4]
  251.    T[n].c.p = scgrc[5]
  252.    break
  253.    end
  254.    if scgrc[5] == "impossibile" or scgrc[5] == "errore" then
  255.    T[n].c.p = scgrc[5]
  256.    break
  257.    end
  258.   end
  259.  end
  260.  
  261. end
  262.  
  263. function compito_rettifica(n,t,v,l,b)
  264.  
  265. rednet.broadcast({"annuncia",t,v,l,b})
  266. -- while true do
  267. -- local id, scgrc = rednet.receive()
  268. --  if scgrc[1] == t and scgrc[2] == v and scgrc[3] == l and scgrc[4] == T[n].c.b then
  269. --  T[n].c.r = scgrc[5]
  270. --  break
  271. --  end
  272. -- end
  273.  
  274. end
  275.  
  276. function compito_esegui(n,t,v,l,b)
  277.  
  278. rednet.broadcast({"esegui",t,v,l,b})
  279.  while true do
  280.  local id, scgrc = rednet.receive()
  281.   if scgrc[1] == t and scgrc[2] == v and scgrc[3] == l then
  282.    if scgrc[5] == "preparato" then
  283.    T[n].c.b = scgrc[4]
  284.    T[n].c.p = scgrc[5]
  285.    break
  286.    end
  287.    if scgrc[5] == "impossibile" or scgrc[5] == "errore" then
  288.    T[n].c.p = scgrc[5]
  289.    break
  290.    end
  291.   end
  292.  end
  293.  
  294. end
  295.  
  296. function treno_genera(n)
  297.  
  298. T[n] = {}
  299. T[n].s = {}                     -- stato
  300. T[n].s.o = "indefinito"           -- orario
  301. T[n].s.r = "indefinito"           -- riferimento
  302. T[n].s.d = "indefinito"           -- differenziale
  303. T[n].s.a = "indefinito"           -- azione
  304. T[n].u = {}                     -- ubicazione
  305. T[n].u.p = "indefinito"           -- posizione
  306. T[n].u.i = "indefinito"           -- codice
  307. T[n].u.v = "indefinito"           -- vincolo
  308. T[n].i = {}                     -- iterazione
  309. T[n].i.e = "indefinito"           -- effettuato
  310. T[n].i.r = "indefinito"           -- ripetitore
  311. T[n].i.c = "indefinito"           -- contatore
  312. T[n].c = {}                     -- compito
  313. T[n].c.a = "indefinito"           -- annunciazione
  314. T[n].c.b = "indefinito"           -- binario
  315. T[n].c.p = "indefinito"           -- preparazione
  316. T[n].c.r = "indefinito"           -- rettifica
  317. T[n].c.e = "indefinito"           -- esecuzione
  318. T[n].r = {}                     -- registro
  319. T[n].r.l = "indefinito"           -- linea
  320. T[n].r.x = "indefinito"           -- blocco
  321. T[n].r.y = "indefinito"           -- blocco
  322. T[n].r.b = "indefinito"           -- binario
  323.  
  324. end
  325.  
  326. function treno_elimina(n)
  327.  
  328. tabella_rimuovi(T,n)
  329.  
  330. end
  331.  
  332. function treno_ubica(n,p,i)
  333.  
  334. T[n].u.p = p
  335. T[n].u.i = i
  336.  
  337. end
  338.  
  339. function treno_registra(n,h,p)
  340.  
  341. T[n].s.r = h
  342. T[n].r[p] = h
  343.  
  344. end
  345.  
  346. function treno_disponi(n,h,t,v,l,b)
  347.  
  348.  if T[n].c.p ~= "preparato" then
  349.   if T[n].c.p == "indefinito" then
  350.   iterazione_inizializza(n,h)
  351.   end
  352.   if T[n].i.r == "blocco" then
  353.   iterazione_ripetitore(n,h)
  354.   end
  355.   if T[n].i.r == "sblocco" then
  356.   iterazione_contatore(n,h)
  357.   compito_prepara(n,t,v,l,b)
  358.   end
  359.  end
  360.  if T[n].c.p == "preparato" then
  361.   if T[n].c.e ~= "eseguito" then
  362.   compito_esegui(n,t,v,l,b)
  363.   iterazione_inizializza(n,h)
  364.   treno_elimina(n)  
  365.   treno_genera(n)
  366.   end
  367.  end
  368.  
  369. end
  370.  
  371. function treno_compito(n,h,t,v,l,b)
  372.  
  373.  if T[n].c.p ~= "preparato" then
  374.   if T[n].c.p == "indefinito" then
  375.   iterazione_inizializza(n,h)
  376.   end
  377.   if T[n].i.r == "blocco" then
  378.   iterazione_ripetitore(n,h)
  379.   end
  380.   if T[n].i.r == "sblocco" then
  381.   iterazione_contatore(n,h)
  382.   compito_prepara(n,t,v,l,b)
  383.   end
  384.  end
  385.  if T[n].c.p == "preparato" then
  386.   if T[n].c.r ~= "annunciato" then
  387.   compito_rettifica(n,t,v,l,b)
  388.   end
  389.   if T[n].c.r == "annunciato" then
  390.    if T[n].c.e ~= "eseguito" then
  391.    compito_esegui(n,t,v,l,b)
  392.    end
  393.   end
  394.  end
  395.    
  396. end
  397.  
  398. function treno_linea(n,h,t,v,l,b)
  399.  
  400.  if T[n].c.a ~= "annunciato" then
  401.  compito_rettifica(n,t,v,l,E[n].tb)
  402.  end
  403.  
  404. end
  405.  
  406. function treno_blocco(n,h,t,v,l,b)
  407.  
  408.  if T[n].s.a == "convergente" then
  409.  treno_compito(n,h,t,v,l,b)
  410.  end
  411.  if T[n].s.a == "controvergente" then
  412.  treno_disponi(n,h,t,v,l,b)
  413.  end
  414.  if T[n].s.a == "divergente" then
  415.  treno_elimina(n)
  416.  end
  417.  
  418. end
  419.  
  420. function treno_binario(n,h,t,v,l,b)
  421.  
  422.  if T[n].s.o == "anticipo" then
  423.   if T[n].c.a ~= "annunciato" then
  424.   compito_annuncia(n,t,v,l,b)
  425.   end
  426.  end
  427.  if T[n].s.o == "puntuale" then
  428.  treno_compito(n,h,t,v,l,b)
  429.  end
  430.  if T[n].s.o == "ritardo" then
  431.  treno_compito(n,h,t,v,l,b)
  432.  end
  433.  
  434. end
  435.  
  436. function treno_controlla(h)
  437.  
  438.  for n in pairs(T) do
  439.   if T[n] ~= nil and E[n] ~= nil then
  440.   determina_azione(n)
  441.   determina_stato(n,h)
  442.    if T[n].u.p == "linea" then
  443.    treno_linea(n,E[n].tt,E[n].vx,E[n].tl,E[n].tb)  
  444.    end
  445.    if T[n].u.p == "blocco" then
  446.    treno_blocco(n,E[n].tt,E[n].vx,E[n].tl,E[n].tb)  
  447.    end
  448.    if T[n].u.p == "binario" then
  449.    determina_vincolo(n,h,T[n].u.i)
  450.    treno_binario(n,E[n].tt,E[n].vy,E[n].tl,E[n].tb)
  451.    end 
  452.   end  
  453.  end
  454.  
  455. end
  456.  
  457. function treno_rileva(n,h,p,i)
  458.  
  459.  if p == "linea" then
  460.   if i ~= nil then
  461.   configura_versori(i)
  462.    if q == "esistente" and g == "ammessa" then
  463.    treno_ubica(n,p,i)
  464.    treno_registra(n,h,"l",i)
  465.    end
  466.    if q == "esistente" and g == "vietata" then
  467.    rednet.broadcast({"rilevazione","linea",i,"cardinalita'",g})
  468.    end
  469.    if q ~= "esistente" then
  470.    rednet.broadcast({"rilevazione","linea",i,"cardinalita'",q})
  471.    end
  472.   end
  473.   if i == nil then
  474.   rednet.broadcast({"rilevazione","cardinalita'","indefinita"})
  475.   end
  476.  end
  477.  
  478.  if p == "blocco" then
  479.   if i ~= nil then
  480.   configura_versori(i)
  481.   configura_blocchi(q,g,i,d)
  482.    if q == "esistente" and g == "ammessa" then
  483.    treno_ubica(n,p,i)
  484.    treno_registra(n,h,"k",i)
  485.    end
  486.    if q == "esistente" and g == "vietata" then
  487.    rednet.broadcast({"rilevazione","linea",i,"cardinalita'",g})
  488.    end
  489.    if q ~= "esistente" then
  490.    rednet.broadcast({"rilevazione","linea",i,"cardinalita'",q})
  491.    end
  492.   end
  493.   if i == nil then
  494.   rednet.broadcast({"rilevazione","cardinalita'","indefinita"})
  495.   end
  496.  end   
  497.  
  498.  if p == "binario" then
  499.   if i ~= nil then
  500.    if i > P.ls then
  501.    rednet.broadcast({"rilevazione","binario","limite","sistemi"})
  502.    end
  503.    if i < P.ls then
  504.     if (V.xp <= i and i <= V.yp) or (V.xm <= i and i <= V.ym) or (V.xs <= i and i <= V.ys) then
  505.     treno_ubica(n,p,i)
  506.     treno_registra(n,h,"b",i)
  507.     end
  508.    else
  509.    rednet.broadcast({"rilevazione","binario","numero","inesistente"})
  510.    end
  511.   end
  512.   if i == nil then
  513.   rednet.broadcast({"rilevazione","binario","indefinito"})
  514.   end
  515.  end
  516.  
  517. end
  518.  
  519. -- codice sistema
  520.  
  521. sleep()
  522. print(app.."_"..cod)
  523. rednet.open(modem)
  524. rednet.host(pro,app.."_"..cod)
  525. rednet.broadcast({"avvio",app,cod})
  526.  
  527. -- codice inizializzazione
  528.  
  529. N = sistema_inizializza("settaggi","norme")
  530. O = sistema_inizializza("settaggi","operatori")
  531. P = sistema_inizializza("settaggi","parametri")
  532. G = sistema_inizializza("settaggi","gestioni")
  533. V = sistema_inizializza("settaggi","variabili")
  534. E = sistema_inizializza("dati","elenchi")
  535. T = sistema_inizializza("dati","treni")
  536. R = sistema_inizializza("dati","registri")
  537. C = sistema_inizializza("dati","compiti")
  538.  
  539. -- codice applicativo
  540.  
  541. while true do
  542.  
  543. tabella_salva(T,"dati","treni")
  544.  
  545. determina_orario()
  546. treno_controlla(h)
  547.  
  548. id, scgrc = rednet.receive(O.fc)
  549.  
  550.  if scgrc ~= nil then
  551.    
  552.   if scgrc[1] == "rilevazione" then
  553.   n = scgrc[2]
  554.   p = scgrc[3]
  555.   i = scgrc[4]
  556.  
  557.    if n == nil then
  558.    rednet.broadcast({"rilevazione","treno",v,"numero","sconosciuto"})
  559.    end
  560.  
  561.    if n ~= nil then
  562.  
  563.     if T[n] == nil then
  564.     treno_genera(n)
  565.     end
  566.  
  567.     if n == N.cs then
  568.     rednet.broadcast({"rilevazione","treno","numero","speciale"})
  569.     R[n].ts = "speciale"
  570.     else
  571.      if T[n] == nil then
  572.      rednet.broadcast({"rilevazione","treno","numero","sconosciuto"})
  573.      end
  574.      if T[n] ~= nil then
  575.      treno_rileva(n,h,p,i)
  576.      end
  577.     end
  578.    
  579.    end
  580.  
  581.   end
  582.  
  583.   if scgrc[1] == "sistemi" then
  584.  
  585.    if scgrc[2] == app then
  586.  
  587.     if scgrc[3] == cod then
  588.  
  589.      if scgrc[4] == "spegnimento" then
  590.      rednet.broadcast({"spegnimento",app,cod})
  591.      os.shutdown()
  592.      end
  593.      if scgrc[4] == "riavvio" then
  594.      rednet.broadcast({"riavvio",app,cod})
  595.      os.reboot()
  596.      end
  597.    
  598.     end
  599.    
  600.    end
  601.  
  602.   end
  603.  
  604.  end
  605.  
  606. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement