Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- verze = "23.0821.R1"
- local idPC = os.getComputerID()
- local IDserver=0
- local w,h = term.getSize()
- local monitor = peripheral.find("monitor")
- local modem
- local suma = 0
- local cykl = 0
- local cykly = 0
- local cena = 4.5
- local cas1=0
- local cas2=0
- local historie = false
- local function bgc(barvaP) term.setBackgroundColor(barvaP) end
- local function txc(barvaP) term.setTextColor(barvaP) end
- local function curs(xi,yi) term.setCursorPos(xi,yi) end
- local function prc(text, y)
- local w = term.getSize()
- local _, cy = term.getCursorPos()
- curs(math.ceil((w-#text)/2), y or cy)
- write(text)
- end
- function logy(text)
- local data = fs.open("/log.txt", "w")
- data.writeLine(text)
- data.close()
- end
- function bar(styl, text)
- if (styl == "OK") then -- Uspech
- term.setTextColor(colors.green)
- elseif (styl == "FAIL") then -- Neuspesne
- term.setTextColor(colors.red)
- elseif (styl == "INFO") then -- Informativni
- term.setTextColor(colors.lightBlue)
- elseif (styl == "def") then -- defaultni
- term.setTextColor(colors.white)
- elseif (styl == "CONST") then -- Progress
- term.setTextColor(colors.yellow)
- else
- styl = "def"
- term.setTextColor(colors.white)
- end
- if not (styl == "def") then
- zprava = ("["..textutils.formatTime(os.time(), true).."] ["..styl.."] "..text)
- else
- zprava = text
- end
- if logs then
- logy(zprava)
- end
- print (zprava)
- end
- function boot()
- term.setBackgroundColor(colors.black) term.clear() term.setCursorPos(1,1)
- while not modem do
- bar("INFO","Finding a modem...")
- modem = peripheral.find("modem")
- sleep(1)
- end
- modem.open(idPC)
- bar("OK","Modem is connected!")
- bar("INFO","Finding a configuration file...")
- if fs.exists ("/config.cfg") then
- bar("OK","Configuration file found! Reading...")
- local data = fs.open("/config.cfg", "r")
- IDserver = tonumber(data.readLine())
- Linka = data.readLine()
- Adresa = data.readLine()
- watty = tonumber(data.readLine())
- data.close()
- bar("CONST","Server: "..IDserver)
- bar("CONST","Signal: "..Linka)
- bar("CONST","Address: "..Adresa)
- bar("CONST","Tariff: "..watty)
- else
- bar("FAIL","The configuration file is missing, the system will shut down!")
- local data = fs.open("config.cfg", "w")
- data.writeLine("Server ID")
- data.writeLine("Strana signalu")
- data.writeLine("Adresa")
- data.writeLine("maximalni_kw_pripojky")
- data.close()
- error("Missing config folder!")
- end
- bar("INFO","Finding a status file...")
- if fs.exists ("/stav") then
- bar("OK","Status file found! Reading...")
- local f = fs.open("/stav", "r")
- suma = tonumber(f.readLine())
- f.close()
- bar("CONST","Spotreba: "..suma)
- else
- bar("FAIL","The status file is missing!")
- end
- bar("INFO","Finding a status file...")
- if fs.exists ("/stavCelkem") then
- bar("OK","Status file found! Reading...")
- local f = fs.open("/stavCelkem", "r")
- Celkovy_stav = tonumber(f.readLine())
- f.close()
- bar("CONST","Celkem: "..Celkovy_stav)
- else
- Celkovy_stav = 0
- bar("FAIL","The status file is missing!")
- end
- monitor = peripheral.find("monitor")
- if not (monitor == nil) then
- bar("OK","Monitor is connected.")
- else
- bar("FAIL","Monitor is noc connected!")
- end
- bar("INFO","Test the connection to the server...")
- if testPing() then
- bar("OK","The server is available!")
- else
- bar("FAIL","The server did not respond, the message may not arrive!")
- end
- bar("OK","System boot completed successfully.")
- bar("INFO","The system will start, press any key to stop the start!")
- local myTimer = os.startTimer(2)
- while true do
- local event, param1 = os.pullEvent()
- if event == "timer" and param1 == myTimer then
- --casTestu = 10
- visual()
- elseif event == "key" then
- error("Stopped by user!")
- end
- end
- end
- function testPing()
- modem.transmit(IDserver,990,idPC)
- modem.transmit(IDserver,57610,Adresa)
- local CasPing = os.startTimer(2)
- while true do
- local event, side, freq, rfreq, message = os.pullEvent()
- if event == "modem_message" and rfreq == 57610 then
- return true
- elseif event == "timer" and side == CasPing then
- return false
- end
- end
- end
- function visual()
- term.clear()
- term.setTextColor(colors.white)
- paintutils.drawLine(1,1,w,1,colors.red)
- paintutils.drawLine(1,2,w,2,colors.red)
- term.setCursorPos(1,1)
- print("EU meter, by Pepek@Soft")
- write("Version "..verze)
- term.setBackgroundColor(colors.white)
- plocha = window.create(term.current(),1,3,w-10,h)
- plocha.setBackgroundColor(colors.white)
- plocha.setTextColor(colors.black)
- plocha.clear()
- plocha.setCursorPos(1,2)
- plocha.write("Adress: "..Adresa)
- plocha.setCursorPos(1,8)
- plocha.write("Spotreba: "..suma.." ")
- plocha.setCursorPos(1,10)
- plocha.write("Nedoplatek: ")
- plocha.setCursorPos(1,12)
- plocha.write("Celkova: ")
- local myTimer = os.startTimer(1)
- local menuTimer = os.startTimer(3)
- menu()
- while true do
- local event, param1, x, y, message = os.pullEvent()
- if (message) then
- if (y == 8082) then
- modem.transmit(IDserver,990,idPC)
- modem.transmit(IDserver,8082,suma)
- modem.transmit(IDserver,8083,Celkovy_stav)
- end
- if (y == 8083) then
- modem.transmit(IDserver,990,idPC)
- sleep(0.1)
- modem.transmit(IDserver,8081,suma)
- fs.delete("/stav")
- suma = 0
- vypis()
- visual()
- end
- end
- checkLinka()
- plocha.setCursorPos(13,8)
- if (suma<1000) then
- plocha.write(suma.." W ")
- elseif (suma >= 1000 and suma < 1000000) then
- plocha.write((math.floor(suma/1000)).." kW ")
- elseif (suma>= 1000000 and suma <1000000000) then
- plocha.write((math.floor(suma/1000/1000)).." MW ")
- elseif (suma>= 1000000000 and suma <1000000000000) then
- plocha.write((math.floor(suma/1000/1000)).." GW ")
- elseif (suma>= 1000000000000 and suma <1000000000000000) then
- plocha.write((math.floor(suma/1000/1000)).." TW ")
- end
- plocha.setCursorPos(13,10)
- plocha.write(suma/1000*cena.." MC ")
- plocha.setCursorPos(13,12)
- if (Celkovy_stav<1000) then
- plocha.write(Celkovy_stav.." W ")
- elseif (Celkovy_stav >= 1000 and Celkovy_stav < 1000000) then
- plocha.write((math.floor(Celkovy_stav/1000)).." kW ")
- elseif (Celkovy_stav>= 1000000 and Celkovy_stav <1000000000) then
- plocha.write((math.floor(Celkovy_stav/1000/1000)).." MW ")
- elseif (Celkovy_stav>= 1000000000 and Celkovy_stav <1000000000000) then
- plocha.write((math.floor(Celkovy_stav/1000/1000)).." GW ")
- elseif (Celkovy_stav>= 1000000000000 and Celkovy_stav <1000000000000000) then
- plocha.write((math.floor(Celkovy_stav/1000/1000)).." TW ")
- end
- if event == "timer" and param1 == myTimer then
- elseif event == "timer" and param1 == menuTimer then
- menu()
- elseif event == "mouse_click" and param1 == 1 then
- menuClick(x,y)
- elseif event == "timer" and param1 == casSpotreby then
- --checkLinka()
- elseif event == "key" and param1 == keys.s then
- setting()
- elseif event == "key" and param1 == keys.q then
- os.shutdown()
- elseif event == "key" and param1 == keys.r then
- os.reboot()
- end
- end
- end
- function menu()
- mplocha = window.create(term.current(),w-9,3,w,h)
- mplocha.setBackgroundColor(colors.white)
- mplocha.setTextColor(colors.black)
- mplocha.clear()
- mplocha.setBackgroundColor(colors.lightGray)
- mplocha.setCursorPos(1,5)
- mplocha.write(" Home ")
- mplocha.setCursorPos(1,7)
- mplocha.write(" Setting ")
- mplocha.setCursorPos(1,9)
- mplocha.write(" Reset m. ")
- mplocha.setCursorPos(1,11)
- mplocha.write(" Ping ")
- mplocha.setBackgroundColor(colors.green)
- mplocha.setTextColor(colors.white)
- mplocha.setCursorPos(1,2)
- mplocha.write(" WAIT ")
- mplocha.setCursorPos(1,1)
- mplocha.write(" ")
- mplocha.setCursorPos(1,3)
- mplocha.write(" ")
- end
- function menuClick(x,y)
- if x >= (w-9) and x<=(w) and y == 7 then
- mplocha.setBackgroundColor(colors.gray)
- mplocha.setCursorPos(1,5)
- mplocha.write(" Home ")
- sleep(0.1)
- visual()
- elseif x >= (w-9) and x<=(w) and y == 9 then
- mplocha.setBackgroundColor(colors.gray)
- mplocha.setCursorPos(1,7)
- mplocha.write(" Setting ")
- sleep(0.1)
- setting()
- elseif x >= (w-9) and x<=(w) and y == 11 then
- mplocha.setBackgroundColor(colors.gray)
- mplocha.setCursorPos(1,9)
- mplocha.write(" Reset m. ")
- sleep(0.1)
- if testPing() then
- mplocha.setBackgroundColor(colors.green)
- mplocha.write(" Success ")
- modem.transmit(IDserver,8081,suma)
- fs.delete("/stav")
- suma = 0
- visual()
- else
- mplocha.setBackgroundColor(colors.red)
- mplocha.write(" Fail ")
- end
- elseif x >= (w-9) and x<=(w) and y == 13 then
- mplocha.setBackgroundColor(colors.gray)
- mplocha.setCursorPos(1,11)
- mplocha.write(" Ping ")
- sleep(0.1)
- mplocha.setCursorPos(1,11)
- if testPing() then
- mplocha.setBackgroundColor(colors.green)
- mplocha.write(" Success ")
- else
- mplocha.setBackgroundColor(colors.red)
- mplocha.write(" Fail ")
- end
- end
- end
- function checkLinka()
- if (redstone.getInput(Linka)) then
- casSpotreby = os.startTimer(1)
- stav = true
- mplocha.setBackgroundColor(colors.orange)
- mplocha.setTextColor(colors.white)
- mplocha.setCursorPos(1,2)
- mplocha.write(" READ ")
- mplocha.setCursorPos(1,1)
- mplocha.write(" ")
- mplocha.setCursorPos(1,3)
- mplocha.write(" ")
- else
- stav = false
- mplocha.setBackgroundColor(colors.green)
- mplocha.setTextColor(colors.white)
- mplocha.setCursorPos(1,2)
- mplocha.write(" WAIT ")
- mplocha.setCursorPos(1,1)
- mplocha.write(" ")
- mplocha.setCursorPos(1,3)
- mplocha.write(" ")
- end
- if not (historie == stav) then
- if stav then
- cas1 = (os.time() * 1000 + 18000)%24000
- else
- cas2 = (os.time() * 1000 + 18000)%24000
- dcas = cas2-cas1
- if (dcas<0) then dcas = 0 end
- vypocet()
- end
- historie = stav
- elseif cykl >= 20 and stav then
- cas2 = (os.time() * 1000 + 18000)%24000
- dcas = cas2-cas1
- if (dcas<0) then dcas = 0 end
- vypocet()
- cas1 = (os.time() * 1000 + 18000)%24000
- end
- cykl = cykl+1
- end
- function vypocet()
- cykl = 0
- dsuma = suma
- suma = math.floor((suma +watty*dcas))
- cykly = cykly + 1
- local f = fs.open("/stav", "w")
- f.writeLine(suma)
- f.writeLine(cykly)
- f.close()
- Celkovy_stav = Celkovy_stav+suma-dsuma
- f = fs.open("/stavCelkem", "w")
- f.writeLine(Celkovy_stav)
- f.close()
- if (monitor) then vypis() end
- end
- function setting()
- plocha = window.create(term.current(),1,3,w-10,h)
- plocha.setBackgroundColor(colors.white)
- plocha.setTextColor(colors.black)
- plocha.clear()
- plocha.setCursorPos(1,2)
- plocha.write("Server: ")
- plocha.setCursorPos(1,4)
- plocha.write("Signal input: ")
- plocha.setCursorPos(1,6)
- plocha.write("Adress: ")
- plocha.setCursorPos(1,8)
- plocha.write("Tariff: ")
- plocha.setCursorPos(1,10)
- plocha.write("Invoice: ")
- plocha.setCursorPos(1,12)
- plocha.write("Spec.: ")
- plocha.setCursorPos(1,14)
- plocha.write("Monitor: ")
- plocha.setCursorPos(9,2)
- plocha.write("_____")
- plocha.setCursorPos(9,6)
- plocha.write("________________________________")
- plocha.setCursorPos(9,8)
- plocha.write("_____")
- plocha.setCursorPos(9,10)
- plocha.write("________________________________")
- plocha.setCursorPos(9,12)
- plocha.write("________________________________")
- plocha.setCursorPos(9,14)
- plocha.write("________________________________")
- plocha.setCursorPos(9,2)
- plocha.write(IDserver)
- plocha.setCursorPos(15,4)
- plocha.write(Linka)
- plocha.setCursorPos(9,6)
- plocha.write(Adresa)
- plocha.setCursorPos(9,8)
- plocha.write(watty)
- plocha.setCursorPos(9,10)
- plocha.write(suma)
- plocha.setCursorPos(9,12)
- plocha.write(Celk_stav)
- plocha.setCursorPos(9,14)
- plocha.write(Celk_stav)
- local myTimer = os.startTimer(1)
- local menuTimer = os.startTimer(3)
- menu()
- while true do
- local event, param1, x, y = os.pullEvent()
- checkLinka()
- if event == "timer" and param1 == menuTimer then
- menu()
- elseif event == "timer" and param1 == casSpotreby then
- checkLinka()
- elseif event == "mouse_click" and param1 == 1 then
- menuClick(x,y)
- elseif event == "key" and param1 == keys.q then
- os.shutdown()
- elseif event == "key" and param1 == keys.r then
- os.reboot()
- end
- end
- end
- function vypis()
- monitor.setTextScale(0.5)
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.write("Tar: "..watty.."kW/t")
- monitor.setCursorPos(1,3)
- monitor.write("+ "..((watty*dcas)).." W")
- monitor.setCursorPos(1,5)
- monitor.write("Tick: "..dcas)
- monitor.setCursorPos(1,7)
- monitor.write("Sum: ")
- if (suma<1000) then
- monitor.write(suma.." W")
- elseif (suma >= 1000 and suma < 1000000) then
- monitor.write((math.floor(suma/1000)).." kW")
- elseif (suma>= 1000000 and suma <1000000000) then
- monitor.write((math.floor(suma/1000/1000)).." MW")
- elseif (suma>= 1000000000 and suma <1000000000000) then
- monitor.write((math.floor(suma/1000/1000)).." GW")
- elseif (suma>= 1000000000000 and suma <1000000000000000) then
- monitor.write((math.floor(suma/1000/1000)).." TW")
- end
- monitor.setCursorPos(1,9)
- monitor.write("Cena: "..(math.floor(suma/1000*cena)).."MC")
- end
- boot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement