Advertisement
pepeknamornik

EPS v2

Jun 26th, 2022 (edited)
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.34 KB | None | 0 0
  1. verze = "22.0625.R1"
  2. local idPC = os.getComputerID()
  3. local IDserver = 142
  4. local w,h = term.getSize()
  5. local modem
  6. local testJednotky = false
  7. local casTestu = 0
  8. local function bgc(barvaP) term.setBackgroundColor(barvaP) end
  9. local function txc(barvaP) term.setTextColor(barvaP) end
  10. local function curs(xi,yi) term.setCursorPos(xi,yi) end
  11. if not fs.exists ("lastECUD.txt") then local data = fs.open("lastECUD.txt", "w") data.writeLine("0") data.close() end
  12. local data = fs.open("lastECUD.txt", "r")   local ECUD = tonumber(data.readLine())+1    data.close()
  13. local historielinka = false
  14.  
  15. local jePoplach = false
  16.  
  17.  
  18. local function prc(text, y)
  19.   local w = term.getSize()
  20.   local _, cy = term.getCursorPos()
  21.   curs(math.ceil((w-#text)/2), y or cy)
  22.   write(text)
  23. end
  24.  
  25. function bar(styl, text)
  26.     if (styl == "OK") then                  -- Uspech
  27.         term.setTextColor(colors.green)
  28.     elseif (styl == "FAIL") then            -- Neuspesne
  29.         term.setTextColor(colors.red)
  30.     elseif (styl == "INFO") then            -- Informativni
  31.         term.setTextColor(colors.lightBlue)
  32.     elseif (styl == "def") then             -- defaultni
  33.         term.setTextColor(colors.white)
  34.     elseif (styl == "CONST") then             -- Progress
  35.         term.setTextColor(colors.yellow)
  36.     else
  37.         styl = "def"
  38.         term.setTextColor(colors.white)
  39.     end
  40.    
  41.     if not (styl == "def") then
  42.         zprava = ("["..textutils.formatTime(os.time(), true).."] ["..styl.."] "..text)
  43.         else
  44.         zprava = text
  45.     end
  46.    
  47.     if logs then
  48.     logy(zprava)
  49.     end
  50.     print (zprava)
  51. end
  52.  
  53. function boot()
  54.     term.setBackgroundColor(colors.black) term.clear() term.setCursorPos(1,1)
  55.     while not modem do
  56.     bar("INFO","Finding a modem...")
  57.     modem = peripheral.find("modem")
  58.     sleep(1)
  59.     end
  60.     modem.open(idPC)
  61.     bar("OK","Modem is connected!")
  62.     bar("INFO","Finding a configuration file...")
  63.    
  64.     if fs.exists ("config.cfg") then
  65.         bar("OK","Configuration file found! Reading...")
  66.         local data = fs.open("config.cfg", "r")
  67.         IDserver = tonumber(data.readLine())
  68.         Linka = data.readLine()
  69.         if (data.readLine() == "true") then testJednotky = true else testJednotky = false end
  70.         Adresa = data.readLine()
  71.         Udalost = data.readLine()
  72.         Upresneni = data.readLine()
  73.         data.close()
  74.         bar("CONST","Server:   "..IDserver)
  75.         bar("CONST","Signal:   "..Linka)
  76.         if (testJednotky) then bar("CONST","T. Alarm: Enable") casTestu = math.random(10000, 32000) else bar("CONST","T. Alarm: Disanable") end
  77.         bar("CONST","Address:  "..Adresa)
  78.         bar("CONST","Event:    "..Udalost)
  79.         bar("CONST","Spec.:    "..Upresneni)
  80.     else
  81.     bar("FAIL","The configuration file is missing, the system will shut down!")
  82.     local data = fs.open("config.cfg", "w")
  83.     data.writeLine("Server ID")
  84.     data.writeLine("Strana signalu")
  85.     data.writeLine("true / false -- test jednotky")
  86.     data.writeLine("Adresa")
  87.     data.writeLine("Udalost")
  88.     data.writeLine("Upresneni")
  89.     data.close()
  90.     error("Missing config folder!")
  91.     end
  92.    
  93.     bar("INFO","Test the connection to the server...")
  94.    
  95.     if testPing() then
  96.     bar("OK","The server is available!")
  97.     else
  98.     bar("FAIL","The server did not respond, the message may not arrive!")
  99.     end
  100.    
  101.     bar("OK","System boot completed successfully.")
  102.     bar("INFO","The system will start, press any key to stop the start!")
  103.     local myTimer = os.startTimer(1)
  104.  
  105.     while true do
  106.             local event, param1 = os.pullEvent()
  107.  
  108.             if event == "timer" and param1 == myTimer then
  109.                     --casTestu = 10
  110.                     visual()
  111.             elseif event == "key" then
  112.                     error("Stopped by user!")
  113.             end
  114.     end
  115. end
  116.  
  117.  
  118.  
  119.     function testPing()
  120.     modem.transmit(IDserver,990,idPC)
  121.     modem.transmit(IDserver,57610,"ping")
  122.     local myTimer = os.startTimer(2)
  123.    
  124.         while true do
  125.              local event, side, freq, rfreq, message = os.pullEvent()
  126.          
  127.         if event == "modem_message" and rfreq == 57610 then
  128.                 return true
  129.         elseif event == "timer" and side == myTimer then
  130.                 return false
  131.         end
  132.         end
  133.     end
  134.  
  135. function setting()
  136.     local plocha = window.create(term.current(),1,3,w-10,h)
  137.     plocha.setBackgroundColor(colors.white)
  138.     plocha.setTextColor(colors.black)
  139.     plocha.clear()
  140.     plocha.setCursorPos(1,2)
  141.     plocha.write("Server: ")
  142.     plocha.setCursorPos(1,4)
  143.     plocha.write("Signal input: ")
  144.     plocha.setCursorPos(1,6)
  145.     plocha.write("Training alarm: ")
  146.     plocha.setCursorPos(1,8)
  147.     plocha.write("Adress: ")
  148.     plocha.setCursorPos(1,10)
  149.     plocha.write("Event: ")
  150.     plocha.setCursorPos(1,12)
  151.     plocha.write("Spec.: ")
  152.    
  153.     plocha.setCursorPos(9,2)
  154.     plocha.write("_____")
  155.     plocha.setCursorPos(9,8)
  156.     plocha.write("________________________________")
  157.     plocha.setCursorPos(9,10)
  158.     plocha.write("________________________________")
  159.     plocha.setCursorPos(9,12)
  160.     plocha.write("________________________________")
  161.    
  162.     plocha.setCursorPos(9,2)
  163.     plocha.write(IDserver)
  164.     plocha.setCursorPos(9,8)
  165.     plocha.write(Adresa)
  166.     plocha.setCursorPos(9,10)
  167.     plocha.write(Udalost)
  168.     plocha.setCursorPos(9,12)
  169.     plocha.write(Upresneni)
  170.    
  171.     local myTimer = os.startTimer(1)
  172.     local menuTimer = os.startTimer(3)
  173.    
  174.     menu()
  175.    
  176.     while true do
  177.             local event, param1, x, y = os.pullEvent()
  178.            
  179.             checkLinka()
  180.            
  181.             if event == "timer" and param1 == myTimer and testJednotky then
  182.                
  183.                 local HT = os.date("%H")*1
  184.                 local MT = os.date("%M")*1
  185.                 local ST = os.date("%S")*1
  186.            
  187.                 if (H == HT and M == MT and S < ST) then
  188.                     poplach("TC")
  189.                 else
  190.                     myTimer = os.startTimer(1)
  191.                 end
  192.             elseif event == "timer" and param1 == menuTimer then
  193.                     menu()
  194.             elseif  event == "mouse_click" and param1 == 1 then
  195.                     menuClick(x,y) 
  196.             elseif event == "key" and param1 == keys.q then
  197.                     os.shutdown()
  198.             elseif event == "key" and param1 == keys.p then
  199.                     LastProtocol()
  200.             elseif event == "key" and param1 == keys.r then
  201.                     os.reboot()
  202.             elseif event == "key" and param1 == keys.t then
  203.                     testPing()
  204.             end
  205.         end
  206.     end
  207.  
  208.     function visual()
  209.     term.clear()
  210.     term.setTextColor(colors.white)
  211.     paintutils.drawLine(1,1,w,1,colors.red)
  212.     paintutils.drawLine(1,2,w,2,colors.red)
  213.     term.setCursorPos(1,1)
  214.     print("FireAlarm, by Pepek@Soft")
  215.     write("Version "..verze)
  216.     term.setBackgroundColor(colors.white)
  217.     local plocha = window.create(term.current(),1,3,w-10,h)
  218.     plocha.setBackgroundColor(colors.white)
  219.     plocha.setTextColor(colors.black)
  220.     plocha.clear()
  221.     plocha.setCursorPos(1,2)
  222.     plocha.write("Adress: "..Adresa)
  223.    
  224.    
  225.     local testJ = "Disabled"
  226.     if (casTestu < 11600 and testJednotky) then
  227.     testJ = "Enabled"
  228.     casPoplachuH = math.floor(casTestu/60/60)
  229.     casPoplachuM = math.floor((casTestu-casPoplachuH*3600)/60)
  230.     casPoplachuS = casTestu-casPoplachuH*3600-casPoplachuM*60
  231.     S = (os.date("%S")+casPoplachuS)
  232.     M = (os.date("%M")+casPoplachuM)
  233.     H = (os.date("%H")+casPoplachuH)
  234.     if (S>59) then S = S - 60 M = M+1 end
  235.     if (M>59) then M = M - 60 H = H+1 end
  236.     if (H>24) then H = H-24 end
  237.     plocha.setCursorPos(1,4)
  238.     plocha.write("Training alarm time: ")
  239.     if (H<10) then plocha.write("0"..H) else plocha.write(H) end
  240.     plocha.write(":")
  241.     if (M<10) then plocha.write("0"..M) else plocha.write(M) end
  242.     plocha.write(":")
  243.     if (S<10) then plocha.write("0"..S) else plocha.write(S) end
  244.  
  245.     else testJednotky = false
  246.     end
  247.     plocha.setCursorPos(1,3)
  248.     plocha.write("Training alarm: "..testJ)
  249.     term.setCursorPos(1,8)
  250.    
  251.     local myTimer = os.startTimer(1)
  252.     local menuTimer = os.startTimer(3)
  253.     menu()
  254.    
  255.     while true do
  256.             local event, param1, x, y = os.pullEvent()
  257.            
  258.             checkLinka()
  259.            
  260.             if event == "timer" and param1 == myTimer and testJednotky then
  261.                
  262.                 local HT = os.date("%H")*1
  263.                 local MT = os.date("%M")*1
  264.                 local ST = os.date("%S")*1
  265.            
  266.                 if (H == HT and M == MT and S < ST) then
  267.                     poplach("TC")
  268.                 else
  269.                     myTimer = os.startTimer(1)
  270.                 end
  271.             elseif event == "timer" and param1 == menuTimer then
  272.                     menu()
  273.             elseif  event == "mouse_click" and param1 == 1 then
  274.                     menuClick(x,y)
  275.             elseif event == "key" and param1 == keys.enter then
  276.                     poplach()      
  277.             elseif event == "key" and param1 == keys.q then
  278.                     os.shutdown()
  279.             elseif event == "key" and param1 == keys.p then
  280.                     LastProtocol()
  281.             elseif event == "key" and param1 == keys.r then
  282.                     os.reboot()
  283.             end
  284.         end
  285.     end
  286.        
  287.     function LastProtocol()
  288.     local plocha = window.create(term.current(),1,3,w-10,h)
  289.     plocha.setBackgroundColor(colors.white)
  290.     plocha.setTextColor(colors.black)
  291.     plocha.clear()
  292.    
  293.     local data = fs.open("lastECUD.txt", "r") ECUD = tonumber(data.readLine()) data.close()
  294.    
  295.     local data = fs.open("protocol/"..ECUD..".txt", "r")
  296.    
  297.     local i = 2
  298.     repeat
  299.         local dataProtocol = data.readLine()
  300.         plocha.setCursorPos(1,i)
  301.         plocha.write(dataProtocol)
  302.         i = i+1
  303.     until dataProtocol == nil
  304.     plocha.setCursorPos(1,i-1)
  305.     plocha.write("     ")
  306.        
  307.     data.close()
  308.    
  309.     menu()
  310.    
  311.     local myTimer = os.startTimer(1)
  312.     local menuTimer = os.startTimer(3)
  313.    
  314.     while true do
  315.             local event, param1, x, y = os.pullEvent()
  316.            
  317.             checkLinka()
  318.            
  319.             if event == "timer" and param1 == myTimer and testJednotky then
  320.                
  321.                 local HT = os.date("%H")*1
  322.                 local MT = os.date("%M")*1
  323.                 local ST = os.date("%S")*1
  324.            
  325.                 if (H == HT and M == MT and S < ST) then
  326.                     poplach("TC")
  327.                 else
  328.                     myTimer = os.startTimer(1)
  329.                 end
  330.             elseif event == "timer" and param1 == menuTimer then
  331.                     menu()
  332.             elseif  event == "mouse_click" and param1 == 1 then
  333.                     menuClick(x,y)
  334.             elseif event == "key" and param1 == keys.q then
  335.                     os.shutdown()
  336.             elseif event == "key" and (param1 == keys.b or param1 == keys.enter) then
  337.                     jePoplach = false
  338.                     visual()
  339.             elseif event == "key" and param1 == keys.r then
  340.                     os.reboot()
  341.             end
  342.         end
  343.     end
  344.    
  345.     function menuClick(x,y)
  346.     if x >= (w-9) and x<=(w) and y >= 16 and y<= 19 then
  347.         mplocha.setBackgroundColor(colors.red)
  348.         mplocha.setTextColor(colors.white)
  349.         mplocha.setCursorPos(1,13)
  350.         mplocha.write("          ")
  351.         mplocha.setCursorPos(1,16)
  352.         mplocha.write("          ")
  353.         mplocha.setCursorPos(1,14)
  354.         mplocha.write("   RUN    ")
  355.         mplocha.setCursorPos(1,15)
  356.         mplocha.write("  ALARM!  ")
  357.         poplach()
  358.         elseif x >= (w-9) and x<=(w) and y == 7 then
  359.         mplocha.setBackgroundColor(colors.gray)
  360.         mplocha.setCursorPos(1,5)
  361.         mplocha.write("   Home   ")
  362.         sleep(0.1)
  363.         visual()
  364.         elseif x >= (w-9) and x<=(w) and y == 9 then
  365.         mplocha.setBackgroundColor(colors.gray)
  366.         mplocha.setCursorPos(1,7)
  367.         mplocha.write(" Setting  ")
  368.         sleep(0.1)
  369.         setting()
  370.         elseif x >= (w-9) and x<=(w) and y == 11 then
  371.         mplocha.setBackgroundColor(colors.gray)
  372.         mplocha.setCursorPos(1,9)
  373.         mplocha.write(" Protocol ")
  374.         sleep(0.1)
  375.         LastProtocol()
  376.         elseif x >= (w-9) and x<=(w) and y == 13 then
  377.         mplocha.setBackgroundColor(colors.gray)
  378.         mplocha.setCursorPos(1,11)
  379.         mplocha.write("   Ping   ")
  380.         sleep(0.1)
  381.         mplocha.setCursorPos(1,11)
  382.        
  383.         if testPing() then
  384.             mplocha.setBackgroundColor(colors.green)
  385.             mplocha.write(" Success  ")
  386.         else
  387.             mplocha.setBackgroundColor(colors.red)
  388.             mplocha.write("   Fail   ")
  389.         end
  390.    
  391.     end
  392.    
  393.     end
  394.    
  395.     function menu()
  396.     mplocha = window.create(term.current(),w-9,3,w,h)
  397.     mplocha.setBackgroundColor(colors.white)
  398.     mplocha.setTextColor(colors.black)
  399.     mplocha.clear()
  400.    
  401.     mplocha.setBackgroundColor(colors.lightGray)
  402.     mplocha.setCursorPos(1,5)
  403.     mplocha.write("   Home   ")
  404.     mplocha.setCursorPos(1,7)
  405.     mplocha.write(" Setting  ")
  406.     mplocha.setCursorPos(1,9)
  407.     mplocha.write(" Protocol ")
  408.     mplocha.setCursorPos(1,11)
  409.     mplocha.write("   Ping   ")
  410.    
  411.     mplocha.setBackgroundColor(colors.orange)
  412.     mplocha.setTextColor(colors.white)
  413.     mplocha.setCursorPos(1,13)
  414.     mplocha.write("          ")
  415.     mplocha.setCursorPos(1,16)
  416.     mplocha.write("          ")
  417.     mplocha.setCursorPos(1,14)
  418.     mplocha.write("   RUN    ")
  419.     mplocha.setCursorPos(1,15)
  420.     mplocha.write("  ALARM!  ")
  421.    
  422.     if jePoplach then
  423.     mplocha.setBackgroundColor(colors.red)
  424.     mplocha.setTextColor(colors.white)
  425.     mplocha.setCursorPos(1,2)
  426.     mplocha.write("  ALARM!  ")
  427.     mplocha.setCursorPos(1,1)
  428.     mplocha.write("          ")
  429.     mplocha.setCursorPos(1,3)
  430.     mplocha.write("          ")
  431.     else
  432.     mplocha.setBackgroundColor(colors.green)
  433.     mplocha.setTextColor(colors.white)
  434.     mplocha.setCursorPos(1,2)
  435.     mplocha.write("    OK    ")
  436.     mplocha.setCursorPos(1,1)
  437.     mplocha.write("          ")
  438.     mplocha.setCursorPos(1,3)
  439.     mplocha.write("          ")
  440.     end
  441.  
  442.     end
  443.    
  444.    
  445.     function checkLinka()
  446.    
  447.     local tedlinka = redstone.getInput(Linka)
  448.        
  449.     if not (historielinka == tedlinka) then
  450.         if tedlinka then
  451.         poplach()
  452.         end
  453.     end
  454.    
  455.     historielinka = tedlinka
  456.    
  457.     end
  458.    
  459.     function poplach(typ)
  460.     jePoplach = true
  461.         if typ == "TC" then Upresneni = (" TC - "..Upresneni) protokol () else protokol () end
  462.         poslatStatus()
  463.         testJednotky = false
  464.         sleep(1)
  465.         LastProtocol()
  466.     end
  467.    
  468.     function poslatStatus()
  469.         modem.transmit(IDserver,990,idPC)
  470.         modem.transmit(IDserver,57600,Adresa)
  471.         modem.transmit(IDserver,57601,Udalost)
  472.         modem.transmit(IDserver,57602,Upresneni)
  473.     end
  474.    
  475.    
  476.     function protokol (tc)
  477.     data = fs.open("lastECUD.txt", "r")
  478.     ECUD = tonumber(data.readLine())+1
  479.     data.close()
  480.     data = fs.open("protocol/"..ECUD..".txt", "w")
  481.     data.writeLine("Protokol c. "..ECUD)
  482.     data.writeLine("Adresa zarizeni: "..idPC)
  483.     data.writeLine("Ustredna: "..IDserver)
  484.     data.writeLine("Adresa Udalosti: "..Adresa)
  485.     data.writeLine("Co se stalo?: "..Udalost)
  486.     data.writeLine("Upresneni: "..Upresneni)
  487.     data.writeLine("Cas odeslani: "..os.date())
  488.     data.close()
  489.  
  490.     data = fs.open("lastECUD.txt", "w")
  491.     data.writeLine(ECUD)
  492.     data.close()
  493. end
  494.  
  495.  
  496. boot()
  497.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement