Advertisement
pepeknamornik

elektromer_client_V2

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