Silo Simulation
The SiloSimulator
class provides a dynamic simulation of a silo's fill level. It models a continuous process where a silo gradually empties over a configurable period and then automatically triggers a rapid refill cycle once it reaches a low threshold (10% of capacity).
This class is an event-driven utility. You start the simulation and then listen for real-time level updates to power dashboards, trigger alerts, or test data pipelines. An instance must be created to represent a single silo.
create
Creates a new silo simulator instance with a specified capacity and emptying time.
Parameters
options
: An object for configuring the silo's properties.capacity
: The maximum fill level of the silo. Defaults to100
.timeToEmpty
: The approximate time, in seconds, for the silo to empty from 100% down to the 10% refill threshold. Defaults to60
.
Example
# options
capacity: 500 // e.g., in kilograms or tons
timeToEmpty: 300 // 5 minutes
start
Starts the simulation loop. Once started, the silo will begin its emptying/refilling cycle, and level updates will be emitted every second to any registered listeners.
Output
Returns true when the simulation has been started.
stop
Stops and pauses the simulation loop. No more level updates will be sent until start()
is called again.
Output
Returns true when the simulation has been stopped.
getLevel
Manually retrieves the current fill level of the silo at any given moment.
Output
A number representing the current fill level (e.g., 85.34).
onLevelUpdate
Registers a handler (listener) that is triggered every second while the simulation is running. This is the primary way to receive data from the simulator. The listener function receives the current level as its only argument.
Parameters
callback
: The callback function to execute on each level update.
Example
# callback
<callback>
Complete Usage Example
Here’s a step-by-step example showing how to use the SiloSimulator
.
Step 1: Create a silo instance
First, create the simulator with your desired parameters.
# (Call create)
# options
capacity: 200
timeToEmpty: 10
Step 2: Register a listener to receive data
Next, set up a handler to process the level updates as they are emitted.
# (Call onLevelUpdate)
# callback
<callback that receives the level and, for example, logs it to the console>
Step 3: Start the simulation
The silo will now begin emptying, and your listener from Step 2 will be called every second with the new level.
# (Call start)
After about 10 seconds, the silo level will drop to near 20 (10% of capacity). It will then automatically switch to "refilling" mode and quickly fill back up to 200 before starting the emptying cycle again.
Step 4: Manually check the level (optional)
At any point while it's running, you can get the current level on demand.
# (Call getLevel)
Step 5: Stop the simulation
When you no longer need the data, stop the simulation.
# (Call stop)
Last updated