Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Aspect Monitor
- -- Description: Example ComputerCraft script that monitors certain aspect levels on an AE network via a connected Essentia Provider,
- -- and sets a redstone signal based on the amounts. In effect, this operates just like the Essentia Level Emitter, but with more flexibility.
- -- This script demonstrates using the watcher event system as an alternative to polling the provider for amounts.
- -- Author: Nividica
- -- Declare methods, because Lua
- local main = nil
- local processEvents = nil
- local processEssentiaEvent = nil
- local getIndexOf = nil
- local getAmountOf = nil
- local registerForEvents = nil
- local showDisplay = nil
- local onAspectAmountChanged = nil
- local resetToSafeState = nil
- -- Declare essentia event types:
- --- Sent when a watched essentia level changes
- local Ess_EssentiaChange = 1
- --- Sent when the provider goes online or offline
- local Ess_PowerChange = 2
- --- Sent when the computer detaches from the provider. (Most likely the network link was broken)
- local Ess_Detach = 3
- -- Declare variables
- local essentiaToWatch = { "Aqua", "Meto" }
- local essentiaAmount = {}
- local userWantsToExit = false
- local isRegistered = false
- local isOnline = false
- local provider = nil
- local status = ""
- -- This is where you would do something useful with the information
- onAspectAmountChanged = function()
- -- Make sure the provider is online, and that it is powered. Otherwise the amounts could be out of sync
- if( ( provider == nil ) or ( not isOnline ) ) then
- -- Stale
- return
- end
- -- If Aqua is less than 500, turn on redstone, else turn it off
- aquaAmount = getAmountOf( "Aqua" )
- if( aquaAmount ~= -1 ) then
- rs.setOutput( "back", ( aquaAmount < 500 ) );
- end
- -- If Meto is less than 200, turn on redstone, else turn it off
- metoAmount = getAmountOf( "Meto" )
- if( metoAmount ~= -1 ) then
- rs.setOutput( "top", ( metoAmount < 200 ) );
- end
- end
- -- Called automatically when the provider is no longer accessible due to a power loss, or removal.
- -- If you have a default or safe state, this is a good time to transition into it.
- -- For example, if you have it setup such that when you output a redstone signal an
- -- export bus does work, you should make sure that signal is turned off here.
- resetToSafeState = function()
- -- Turn redstone off
- rs.setOutput( "back", false )
- rs.setOutput( "top", false )
- end
- -- Main loop
- main = function()
- -- Init timeout
- recheckTimeout = nil
- -- Reset state
- resetToSafeState()
- -- Loop until the user elects to exit
- while( not userWantsToExit ) do
- -- Reset the timeout
- recheckTimeout = nil
- -- Is the provider nil?
- if( provider == nil ) then
- -- Try to locate the essentia provider
- result = {pcall( peripheral.find, "EssentiaProvider" )}
- -- Was a provider found?
- if( result[1] and ( result[2] ~= nil ) ) then
- -- Set the provider
- provider = result[2]
- else
- -- Set status to searching
- status = "Searching for provider..."
- -- Check again in 5 seconds
- recheckTimeout = 5
- end
- end
- -- Should the computer register for essentia events?
- if( ( provider ~= nil ) and ( not isRegistered ) )then
- -- Get the providers power state
- isOnline = provider.isOnline()
- -- Can the computer register for essentia events?
- if( isOnline ) then
- registerForEvents()
- else
- -- Set status
- status = "Waiting for provider to be ready..."
- -- Check again in 5 seconds
- recheckTimeout = 5
- end
- end
- -- Display the status screen
- showDisplay()
- -- Wait for an event
- processEvents( recheckTimeout )
- end
- -- Make sure to un-register
- if( ( provider ~= nil ) and isRegistered )then
- provider.unregisterAsWatcher()
- end
- -- Return to the safe state before exiting
- resetToSafeState()
- -- Done
- print( "Exiting" )
- end
- -- Event processor, optional timeout value
- processEvents = function( timeout )
- -- Is there a timeout?
- if( timeout ~= nil ) then
- -- Start a timer
- os.startTimer(timeout)
- end
- -- Pull an event
- event = { os.pullEvent() }
- -- Was the event a character key press?
- if( event[1] == "char" ) then
- -- Was the key a q?
- if( event[2] == "q" or event[2] == "Q" ) then
- -- User wants to exit
- userWantsToExit = true
- end
- -- Was the event from the essentia provider?
- elseif( event[1] == "essentia" ) then
- processEssentiaEvent( event )
- end
- end
- -- Processes an essentia event
- processEssentiaEvent = function( event )
- -- Has the providers power changed?
- if( event[2] == Ess_PowerChange ) then
- -- Get the new power state
- isOnline = event[3]
- -- Is the power going off?
- if( not isOnline ) then
- -- No longer registered
- isRegistered = false
- -- Update status
- status = "Provider offline, amounts shown may not be correct."
- -- Reset state
- resetToSafeState()
- end
- -- Essentia amount update?
- elseif( event[2] == Ess_EssentiaChange ) then
- -- Get the index of the essentia changed
- index = getIndexOf( essentiaToWatch, event[3] )
- if( index ~= -1 ) then
- -- Update the amount
- essentiaAmount[index] = event[4] + ( essentiaAmount[index] or 0 )
- -- Call the changed event handler
- onAspectAmountChanged()
- end
- -- Detached?
- elseif( event[2] == Ess_Detach ) then
- -- Reset everything
- provider = nil
- isOnline = false
- isRegistered = false
- resetToSafeState()
- end
- end
- -- Returns the index of the item in the table
- -- If the item is not in the table, returns -1
- getIndexOf = function( t, item )
- -- Search the table
- for index,v in ipairs(t) do
- -- Do the items match?
- if v == item then
- -- Return the index
- return index
- end
- end
- -- Item not found
- return -1
- end
- -- Helper function that returns how much of the aspect is stored
- -- Returns -1 if the aspect is not being tracked
- getAmountOf = function( aspect )
- -- Get the index
- index = getIndexOf( essentiaToWatch, aspect )
- -- Is the essentia not being tracked
- if( index == -1 ) then
- return -1
- end
- -- Return the amount
- return essentiaAmount[index] or 0
- end
- -- Registers the computer to receive essentia updates from the provider
- registerForEvents = function()
- -- Start by getting the essentia amounts
- for index,aspect in ipairs( essentiaToWatch ) do
- essentiaAmount[index] = provider.getAmount( aspect )
- end
- -- Register for essentia events
- isRegistered = provider.registerAsWatcher( unpack( essentiaToWatch ) )
- if( isRegistered ) then
- -- Set status
- status = "Watching Network For Changes."
- -- Call the changed event handler
- onAspectAmountChanged()
- end
- end
- -- Displays what the program is doing
- showDisplay = function()
- -- Reset the terminal
- term.clear()
- term.setCursorPos( 1, 1 )
- -- Display each essentia and amount
- for index,aspect in ipairs( essentiaToWatch ) do
- print( aspect , ": " , essentiaAmount[index] )
- end
- -- Divider line
- print( "----------" )
- -- Display the status
- print( "Status: ".. status )
- -- Display how to quit
- print( "Press Q to exit" )
- end
- -- Call main
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement