Advertisement
Shurhaian

ComputerCraft remote fluid graphing script

Feb 27th, 2013
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. local lComp=5 -- Lava sensor computer ID
  2. local lMax= 576000 -- Lava tank max capacity
  3. local crComp=6 -- Creosote sensor computer ID
  4. local crMax=576000 -- Creosote tank max capacity
  5. local oComp=8 -- Oil sensor computer ID
  6. local oMax=576000 -- Oil tank max capacity
  7. local fComp=7 -- Fuel sensor computer ID
  8. local fMax=576000 -- Fuel tank max capacity
  9.  
  10. local monpos="left" -- Monitor position
  11. local modempos="back" -- Modem position
  12.  
  13. local reqStr="DREQ" -- Data request keystring
  14.  
  15. local dbgr=false -- Debug Mode
  16.  
  17. local version="1.7" -- Version number, string in case of finer point releases
  18.  
  19. print ("Remote Fluid Graph v"..version.."\nBy Shurhaian\n")
  20.  
  21. mon=peripheral.wrap(monpos) -- Assign the monitor
  22.  
  23. print ("Modem position: "..modempos.."\nMonitor position: "..monpos.."\n")
  24. rednet.open(modempos) -- Open Rednet
  25. if dbgr then
  26.   print ("Rednet channel open")
  27. end
  28.  
  29. local function callData(sensID) -- Generic sensor poller function
  30.   myNet.send( reqStr, sensID ) -- Send the request string to the specified computer
  31.   local msg,id=myNet.receive(1) -- Wait up to 1 full second for a reply
  32.   if msg then return msg -- If a sensible value was received, return it
  33.   else return 0 end      -- Otherwise, just return 0
  34. end
  35.  
  36. local function callLava()
  37.   return callData(lComp)
  38. end
  39.  
  40. local function callCreo()
  41.   return callData(crComp)
  42. end
  43.  
  44. local function callOil()
  45.   return callData(oComp)
  46. end
  47.  
  48. local function callFuel()
  49.   return callData(fComp)
  50. end
  51.  
  52. local nameTable={"Lava", "Creosote", "Oil", "Fuel"} -- Table of fluid names
  53. local callTable={callLava, callCreo, callOil, callFuel} -- Table of update functions
  54. local colorTable={{colors.orange},{colors.green},{colors.white},{colors.yellow}} -- Table of gradient "tables"
  55. local minTable={0, 0, 0, 0} -- Table of minimum values - always 0 (for now)
  56. local maxTable={lMax, crMax, oMax, fMax} -- Table of maximum values
  57.  
  58.  
  59. print ("Sensor computers:")
  60. print ("Lava         : "..lComp)
  61. print ("Creosote oil : "..crComp)
  62. print ("Petroleum oil: "..oComp)
  63. print ("Refined fuel : "..fComp)
  64. local compID=os.getComputerID() -- Store local ID for next POST report
  65. print ("Running on computer: #"..compID.."\n")
  66.  
  67.  
  68. if dbgr then
  69.   print ("Test polls: ")
  70.   local testVal=callData(lComp) -- Fetch lava amount
  71.   print ("Lava         : "..testVal) -- Report lava
  72.   testVal=callData(crComp) -- Fetch creosote amount
  73.   print ("Creosote oil : "..testVal) -- Report creosote
  74.   testVal=callData(oComp) -- Fetch oil amount
  75.   print ("Petroleum oil: "..testVal) -- Report oil
  76.   testVal=callData(fComp) -- Fetch fuel amount
  77.   print ("Refined fuel : "..testVal) -- Report fuel
  78. else
  79.   os.loadAPI("ocs/apis/graph")
  80.   print ("Graph API loaded\n")
  81.   graphInstance = graph.new(mon, callTable, nameTable, colorTable, minTable, maxTable, true) -- Graph declaration
  82.   print ("Graph declared\n")
  83.   print ("Now polling...")
  84. end
  85.  
  86.  
  87. while not dbgr do
  88.   graphInstance:draw()
  89.   sleep(0.5)
  90. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement