cyber_Ahn

Ficsit-Networks-Storage_Monitor

May 14th, 2022 (edited)
1,565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.08 KB | None | 0 0
  1. screen_name = "empty"
  2. --------------main functions-----------
  3. function tableConcat( t1, t2 )
  4.     for i=1, #t2 do
  5.        t1[#t1+1] = t2[i]
  6.     end
  7.     return t1
  8. end
  9.  
  10. function tableHasValue( t, value )
  11.     if t == nil or value == nil then
  12.         return false
  13.     end
  14.  
  15.     for _,v in pairs( t ) do
  16.         if v == value then
  17.             return true
  18.         end
  19.     end
  20.  
  21.     return false
  22. end
  23.  
  24. function getComponentsByClass( class, getOne )
  25.     local results = {}
  26.  
  27.     if ( getOne == nil ) then
  28.         getOne = false
  29.     end
  30.  
  31.     if type( class ) == "table" then
  32.  
  33.         for _, c in pairs( class ) do
  34.             local proxies = getComponentsByClass( c, getOne )
  35.             if not getOne then
  36.                 tableConcat( results, proxies )
  37.             else
  38.                 if( proxies ~= nil ) then
  39.                     return proxies
  40.                 end
  41.             end
  42.         end
  43.  
  44.     elseif type( class ) == "string" then
  45.  
  46.         local ctype = classes[ class ]
  47.         if ctype ~= nil then
  48.             local comps = component.findComponent( ctype )
  49.             for _, c in pairs( comps ) do
  50.                 local proxy = component.proxy( c )
  51.                 if getOne and proxy ~= nil then
  52.                     return proxy
  53.                 elseif not tableHasValue( results, proxy ) then
  54.                     table.insert( results, proxy )
  55.                 end
  56.             end
  57.         end
  58.  
  59.     end
  60.  
  61.     if ( getOne ) then
  62.         return {}
  63.     end
  64.  
  65.     return results
  66. end
  67.  
  68. function getComponentsByClassAndNick( class, nickParts )
  69.     if type( nickParts ) == 'string' then
  70.         nickParts = { nickParts }
  71.     end
  72.  
  73.     local classComponents = getComponentsByClass( class )
  74.     local results = {}
  75.  
  76.     for _, component in pairs( classComponents ) do
  77.         for _, nickPart in pairs( nickParts ) do
  78.             if component.nick:find( nickPart, 1, true ) == nil then
  79.                 goto nextComponent
  80.             end
  81.         end
  82.  
  83.         table.insert( results, component )
  84.  
  85.         ::nextComponent::
  86.     end
  87.  
  88.     return results
  89. end
  90.  
  91. function settingsFromString( str, lowerKeys )
  92.     lowerKeys = lowerKeys or false
  93.     local results = {}
  94.     if str == nil or type( str ) ~= "string" then return results end
  95.     for key, value in string.gmatch( str, '(%w+)=(%b"")' ) do
  96.         if lowerKeys then key = string.lower( key ) end
  97.         results[ key ] = string.sub( value, 2, string.len( value ) - 1 )
  98.     end
  99.     return results
  100. end
  101.  
  102. function settingsFromComponentNickname( proxy, lowerKeys )
  103.     if proxy == nil then return nil end
  104.     return settingsFromString( proxy[ "nick" ], lowerKeys )
  105. end
  106.  
  107. Vector2d = {
  108.     x = 0,
  109.     y = 0,
  110.     pattern = '{x=%d,y=%d}',
  111. }
  112. Vector2d.__index = Vector2d
  113. function Vector2d.new( x, y )
  114.     if x == nil or type( x ) ~= "number" then return nil end
  115.     if y == nil or type( y ) ~= "number" then return nil end
  116.     local o = { x = math.floor( x ), y = math.floor( y ) }
  117.     setmetatable( o, { __index = Vector2d } )
  118.     return o
  119. end
  120.  
  121. function split(s, delimiter)
  122.  result = {};
  123.  for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  124.   table.insert(result, match);
  125.  end
  126.  return result;
  127. end
  128. --------------GPU----------------------
  129. local gpu = computer.getPCIDevices( classes.GPU_T2_C )[1]
  130. if gpu == nil then
  131.     computer.panic( "No GPU T2 found. Cannot continue." )
  132. end
  133. print(gpu)
  134. --------------Screen-------------------
  135. local computerSettings = settingsFromComponentNickname( computer.getInstance() )
  136. local screens = getComponentsByClassAndNick( {
  137.     "ModuleScreen_C",
  138.     "Build_Screen_C",
  139. }, computerSettings.screen or '' )
  140. if #screens == 0 then
  141.     computer.panic( "No screen found. Cannot continue." )
  142. end
  143.  
  144. if screen_name ~= "empty" then
  145.     for i=1, #screens do
  146.         screen_data = screens[i]
  147.         screen_table_data = split(tostring(screen_data),'"')
  148.         if screen_name == screen_table_data[2] then
  149.             gpu:bindScreen( screens[i] )
  150.             screenSize = gpu:getScreenSize()
  151.             print( 'Screen resolution: ' .. screenSize.x .. 'x' .. screenSize.y )
  152.         end
  153.     end
  154. else
  155.     gpu:bindScreen( screens[1] )
  156.     screenSize = gpu:getScreenSize()
  157.     print( 'Screen resolution: ' .. screenSize.x .. 'x' .. screenSize.y )
  158. end
  159. --------------line calc----------------
  160. function get_line(line)
  161.     spacer = (size + 10) * line
  162.     return spacer
  163. end
  164. --------------row calc-----------------
  165. function get_row(row)
  166.     spacer = (size - 25) * row
  167.     return spacer
  168. end
  169. ---------------------------------------
  170.  
  171. -------------------------------------------------------------------------------------------------------------------------------------
  172. function draw_frame(x,y,le,dire,size_d,color_d)
  173.     for i=0, le do
  174.         if dire == "right" then
  175.             gpu:drawText(Vector2d.new(get_row(x+i),get_line(y)),"_",size_d,color_d,TRUE)
  176.         end
  177.         if dire == "down" then
  178.             gpu:drawText(Vector2d.new(get_row(x),get_line(y+i)),"|",size_d,color_d,TRUE)
  179.         end
  180.     end
  181. end
  182.  
  183. function round(x)
  184.   return math.floor(x + 0.5)
  185. end
  186. ----------Parameters------------------
  187. size = 48
  188. color_white = {1.000, 1.000, 1.000, 1.0}
  189. color_red = {1.000, 0.000, 0.000, 1.0}
  190. color_yellow = {1.000, 1.000, 0.000, 1.0}
  191. color_green = {0.000, 1.000, 0.000, 1.0}
  192. w = screenSize.x
  193. h = screenSize.y
  194. ---------------------------------------
  195. function main()
  196.     while true do
  197.     draw_frame(0,1,129,"right",size,color_white)
  198.     draw_frame(0,2,40,"down",size,color_white)
  199.     draw_frame(129,2,40,"down",size,color_white)
  200.     draw_frame(68,2,40,"down",size,color_white)
  201.     draw_frame(0,40,129,"right",size,color_white)
  202.     gpu:drawText(Vector2d.new(get_row(50),get_line(0)),"Storage Monitor V3.2",size,color_white,TRUE)
  203.     local storage = component.proxy(component.findComponent("Storage"))
  204.     local fluid = component.proxy(component.findComponent("F_Storage"))
  205.     print("Container:"..#storage)
  206.     print("Tanks:"..#fluid)
  207.     for i=1, #storage do
  208.         inv = storage[i]:getInventories()[1]
  209.         inv:sort()
  210.         amount = inv.itemCount
  211.         itemName = "Empty"
  212.         max = 0
  213.         if amount > 1 then
  214.             max = inv:getStack(0).count * inv.size
  215.             itemName = inv:getStack(0).item.type.name
  216.         end
  217.         gpu:drawText(Vector2d.new(get_row(2),get_line(i+1)),"Container_"..i,size,color_white,TRUE)
  218.         gpu:drawText(Vector2d.new(get_row(20),get_line(i+1)),itemName,size,color_white,TRUE)
  219.         if (max/2) > amount then
  220.             gpu:drawText(Vector2d.new(get_row(51),get_line(i+1)),amount.."/"..max,size,color_red,TRUE)
  221.         end
  222.         if (max/2) < amount then
  223.             gpu:drawText(Vector2d.new(get_row(51),get_line(i+1)),amount.."/"..max,size,color_yellow,TRUE)
  224.         end
  225.         if amount == max then
  226.             gpu:drawText(Vector2d.new(get_row(51),get_line(i+1)),amount.."/"..max,size,color_green,TRUE)
  227.         end
  228.         if amount == 0 then
  229.             gpu:drawText(Vector2d.new(get_row(51),get_line(i+1)),amount.."/"..max,size,color_white,TRUE)
  230.         end
  231.     end
  232.     for i=1, #fluid do
  233.         itemName = "Empty"
  234.         isF = fluid[i].fluidContent
  235.         maxF = fluid[i].maxFluidContent
  236.         if isF > 1 then
  237.             itemName = fluid[i]:getFluidType().name
  238.         end
  239.         gpu:drawText(Vector2d.new(get_row(70),get_line(i+1)),"Fluid_"..i,size,color_white,TRUE)
  240.         gpu:drawText(Vector2d.new(get_row(80),get_line(i+1)),itemName,size,color_white,TRUE)       
  241.         if (maxF/2) > isF then
  242.             gpu:drawText(Vector2d.new(get_row(114),get_line(i+1)),round(isF).."/"..round(maxF),size,color_red,TRUE)
  243.         end
  244.         if (maxF/2) < isF then
  245.             gpu:drawText(Vector2d.new(get_row(114),get_line(i+1)),round(isF).."/"..round(maxF),size,color_yellow,TRUE)
  246.         end
  247.         if isF == maxF then
  248.             gpu:drawText(Vector2d.new(get_row(114),get_line(i+1)),round(isF).."/"..round(maxF),size,color_green,TRUE)
  249.         end
  250.         if isF > maxF then
  251.             gpu:drawText(Vector2d.new(get_row(114),get_line(i+1)),round(isF).."/"..round(maxF),size,color_green,TRUE)
  252.         end
  253.         if isF == 0 then
  254.             gpu:drawText(Vector2d.new(get_row(114),get_line(i+1)),round(isF).."/"..round(maxF),size,color_white,TRUE)
  255.         end
  256.     end
  257.     gpu:flush()
  258.     event.pull(10)
  259.     end
  260. end
  261. ---------------------------------------
  262. gpu:flush()
  263. gpu:drawText(Vector2d.new(get_row(0),get_line(0)),"Start Terminal......",size,color_white,TRUE)
  264. gpu:drawText(Vector2d.new(get_row(0),get_line(1)),"Please wait......",size,color_red,TRUE)
  265. gpu:flush()
  266. event.pull(10)
  267. gpu:flush()
  268. main()
Advertisement
Add Comment
Please, Sign In to add comment