All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jentity.example.FlowAgent Maven / Gradle / Ivy

The newest version!
package org.jentity.example;

import javax.swing.SwingUtilities;

import org.jentity.datamodel.Container;
import org.jentity.datamodel.Valve;

/**
 * Simulates a flow by listening to the Valve setting and updating the waterlevel in the container accordingly
 */
public class FlowAgent {
	private final Container container;
	private final  Valve valve;
	private Thread thread;

	public FlowAgent(Container container, Valve valve) {
		this.container = container;
		this.valve = valve;
	}

	public void start() {
		thread = new Thread(new Worker());
		thread.start();
	}

	public void stop() {
		thread.interrupt();
	}

	private class Worker implements Runnable {
		public void run() {
			try {
				while (true) {
					SwingUtilities.invokeLater(new Runnable() {	public void run() {
						if (valve.getOpen()) {
							final Container update = new Container();
							int currentLevel = container.getWaterVolume();
							update.setWaterVolume(currentLevel-valve.getFlow());

							container.update(update);
						}		
					}					
					});
					Thread.sleep(1000);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}			
		}		
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy