Advertisement
pepeknamornik

elektromer_server_V2

Aug 21st, 2023
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.40 KB | None | 0 0
  1. ModemPos = "top"
  2. MyID = os.getComputerID()
  3. modem = peripheral.wrap(ModemPos)
  4. modem.open(MyID)
  5.  
  6. function bar(styl, text)
  7.     if (styl == "OK") then                  -- Uspech
  8.         term.setTextColor(colors.green)
  9.     elseif (styl == "FAIL") then            -- Neuspesne
  10.         term.setTextColor(colors.red)
  11.     elseif (styl == "INFO") then            -- Informativni
  12.         term.setTextColor(colors.lightBlue)
  13.     elseif (styl == "def") then             -- defaultni
  14.         term.setTextColor(colors.white)
  15.     elseif (styl == "PRO") then             -- Progress
  16.         term.setTextColor(colors.yellow)
  17.     else
  18.         styl = "def"
  19.         term.setTextColor(colors.white)
  20.     end
  21.    
  22.     if not (styl == "def") then
  23.         zprava = ("["..textutils.formatTime(os.time(), true).."] ["..styl.."] "..text)
  24.         else
  25.         zprava = text
  26.     end
  27.    
  28.     if logs then
  29.     logy(zprava)
  30.     end
  31.     print (zprava)
  32. end
  33.  
  34.  
  35. function loop()
  36.  
  37. local function defaultHod()
  38. idComp = nil
  39. hodnota = nil
  40. bar("INFO", "Pripraveno pro novy zapis")       
  41.        
  42. end
  43.  
  44. local _, side, freq, rfreq, message = os.pullEvent('modem_message')
  45.  
  46.     if (rfreq == 990) then
  47.         bar("INFO", "Zadost o zpracovani udaju.")
  48.         idComp = message
  49.         bar("PRO", "Adresa zarizeni: "..idComp)
  50.         if not fs.exists("/database/"..idComp) then
  51.         bar("INFO", "Provedena nova Registrace zarizeni "..idComp)
  52.         fs.makeDir("/database/"..idComp)
  53.         end
  54.     end
  55.    
  56.     if (idComp) then
  57.             if (rfreq == 57610) then
  58.                 bar("OK", "Funkcni pojeni s Elektromerem ID: "..idComp)
  59.                 modem.transmit(idComp,57610,true)
  60.                
  61.                 if not fs.exists("/database/"..idComp.."/info.txt") then
  62.                     bar("INFO", "Zapsani fyzicke adresy elektromeru.")
  63.                     local data = fs.open("/database/"..idComp.."/info.txt", "w")
  64.                     data.writeLine(message)
  65.                     data.close()
  66.                    
  67.                 end
  68.             end
  69.  
  70.             if (rfreq == 8080) then
  71.                 idComp = message    --login to domain
  72.                 bar("INFO", "Prijem informaci ze zarizeni "..idComp)
  73.             end
  74.            
  75.             if (rfreq == 8081) then
  76.                 hodnota = message
  77.                 bar("PRO", "Prichozi hodnota: "..hodnota)
  78.                    
  79.                     if tonumber(idComp) >= 0 then
  80.                             if fs.exists("/database/"..idComp.."/nevyuctovano.txt") then
  81.                             bar("INFO", "Nalezen zaznam s puvodni hodnotou.")
  82.                             local data = fs.open("/database/"..idComp.."/nevyuctovano.txt", "r")
  83.                             local puv_hodnota = tonumber(data.readLine())
  84.                             data.close()
  85.                             hodnota = hodnota + puv_hodnota
  86.                             end
  87.                    
  88.                         local zapis = fs.open("/database/"..idComp.."/nevyuctovano.txt", "w")
  89.                         zapis.writeLine(hodnota)
  90.                         bar("PRO", "Nova hodnota: "..hodnota)
  91.                         bar("OK", "Prepsano")
  92.                         zapis.close()
  93.                         defaultHod()
  94.            
  95.                     end
  96.             end
  97.            
  98.             if (rfreq == 8082) then
  99.                 bar("INFO", "Doruceni vyzadane hodnoty.")
  100.                 hodnota = message
  101.                
  102.                 if tonumber(idComp) >= 0 then
  103.                     local zapis = fs.open("/database/"..idComp.."/info_stav.txt", "w")
  104.                     zapis.writeLine(hodnota)
  105.                     bar("OK", "Zapsana aktualni hodnota.")
  106.                     zapis.close()
  107.                 end
  108.             end
  109.            
  110.             if (rfreq == 8083) then
  111.                 bar("INFO", "Doruceni vyzadane hodnoty.")
  112.                 hodnota = message
  113.                
  114.                 if tonumber(idComp) >= 0 then
  115.                     local zapis = fs.open("/database/"..idComp.."/info_Celk_stav.txt", "w")
  116.                     zapis.writeLine(hodnota)
  117.                     bar("OK", "Zapsana celkova hodnota.")
  118.                     zapis.close()
  119.                     defaultHod()
  120.                 end
  121.             end
  122.  
  123.            
  124.        
  125.            
  126.    
  127. end
  128.  
  129. loop()
  130. end
  131.  
  132. bar("INFO", "Pripraveno k pouziti")
  133. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement