domian844

[OpenComputers] Miner Telegram 2.0

Jan 25th, 2021
521
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. comp = require("component")
  2. miner = comp.gt_machine
  3. screen = require("term")
  4. GPU1 = comp.gpu
  5. computer = require("computer")
  6. event = require("event")
  7.  
  8. screenWidth = 160
  9. screenHeight = 40
  10. errorColor = 0xCC0000
  11. idleColor = 0xc0cc00
  12. goodColor = 0x00cc0e
  13. local state = "off"
  14.  
  15. local internet = comp.internet
  16. local web = require("internet")
  17.  
  18. Telegram = {
  19.     "ТвойТокенБота", "ТвойIdТелеграмма"
  20. }
  21.  
  22. function SendTelega(Text)
  23.     local url ="https://api.telegram.org/bot"..Telegram[1].."/sendMessage?chat_id="..Telegram[2].."&text="..Text
  24.     internet.request(url)
  25. end
  26.  
  27. local parser = "off"
  28.  
  29. function parserTG()
  30.  
  31.     local req = web.request("https://api.telegram.org/botТвойТокен/getUpdates?offset=-1")
  32.     for line in req do
  33.         if string.match(line, "StartMiner") then
  34.             if parser == "off" then
  35.                 SendTelega("Starting..")
  36.                 miner.setWorkAllowed(true)
  37.                 parser = "on"
  38.             end
  39.         else if string.match(line, "StopMiner") then
  40.             if parser == "on" then
  41.                 SendTelega("Stoping..")
  42.                 miner.setWorkAllowed(false)
  43.                 parser = "off"
  44.             end
  45.         end
  46.         end
  47.     end
  48. end
  49.  
  50. function write(x, y, text, color)
  51.     color = color
  52.     screen.setCursor(x, y)
  53.     oldColor = GPU1.setForeground(color)
  54.     screen.write(text)
  55.     GPU1.setForeground(oldColor)
  56. end
  57.  
  58. function box(x, y, w, h, color)
  59.     local oldColor = GPU1.setBackground(color)
  60.     GPU1.fill(x, y, w, h, " ")
  61.     GPU1.setBackground(oldColor)
  62. end
  63.  
  64. function MachineGT(step)
  65.     parserTG()
  66.     x = 0
  67.     y = 0
  68.     maintenanceIndex = 0
  69.     uses = 0
  70.     for i = 1, #miner.getSensorInformation() do
  71.         if string.match(miner.getSensorInformation()[i], "Problems") then
  72.             maintenanceIndex = i
  73.         end
  74.     end
  75.     for i = 1, #miner.getSensorInformation() do
  76.         if string.match(miner.getSensorInformation()[i], "Probably") then
  77.             uses = i
  78.         end
  79.     end
  80.  
  81.     if (miner.isMachineActive()) then
  82.         write(x + 1, y + 2, "                             ", goodColor)
  83.         write(x + 1, y + 2, "Miner Worked!", goodColor)
  84.         if step % 1800 == 0 then
  85.             SendTelega("Miner worked: ".. step/60 .. " min")
  86.         end
  87.     else
  88.       write(x + 1, y + 2, "                             ", errorColor)
  89.       write(x + 1, y + 2, "Miner pause", errorColor)
  90.       if step % 60 == 0 then
  91.         SendTelega("Miner Pause!".. step .." sec")
  92.       end
  93.     end
  94.  
  95.     if (maintenanceIndex == 0 or string.match(miner.getSensorInformation()[maintenanceIndex], "c0")) and miner.isWorkAllowed() then
  96.     else
  97.         if (miner.isWorkAllowed()) then
  98.             write(x + 1, y + 2, "                             ", errorColor)
  99.             write(x + 1, y + 2, "Needs maintenance!", errorColor)
  100.             if step % 60 == 0 then
  101.                 SendTelega("Needs maintenance!")
  102.             end
  103.         else
  104.             write(x + 1, y + 2, "                             ", errorColor)
  105.             write(x + 1, y + 2, "Miner disabled!", idleColor)
  106.             if step % 60 == 0 then
  107.                 SendTelega("Miner Not Worked")
  108.            end
  109.         end
  110.     end
  111. end
  112.  
  113. screen.clear()
  114. step = 1
  115. i = 1
  116.  
  117. while (true) do
  118.    GPU1.setResolution(15, 5)
  119.  
  120.     step = step + 1
  121.  
  122.     MachineGT(step)
  123.     write(1, 3, "[ "..(step).." sec ]", idleColor)
  124.     if step % 60 == 0 then
  125.         write(1, 4, "[ "..(i).." min ]", idleColor)
  126.         i = i + 1
  127.     end
  128.     -- Выключить программу ctrl+c
  129.     os.sleep(.5)
  130.    if event.pull(.5, "interrupted") then
  131.         screen.clear()
  132.         break
  133.     end
  134. end
RAW Paste Data