package vs;

import java.io.IOException;
import java.util.Random;

import jakarta.websocket.RemoteEndpoint;
import jakarta.websocket.Session;

public class SensorSimulator extends Thread {
	private Session session;
	private Random random;
	private double messwert;

	public SensorSimulator(Session session) {
		super();
		this.session = session;
		random = new Random();
		messwert = 100.0;
	}

	public void notifyClient() throws IOException {
		RemoteEndpoint.Basic client = session.getBasicRemote();
		String message = String.format("%.2f", messwert);
		client.sendText(message);
	}

	@Override
	public void run() {
		try {
			while (session != null && session.isOpen()) {
				sleep(random.nextInt(1000));
				if (random.nextBoolean()) {
					messwert += 0.1;
				} else {
					messwert -= 0.1;
				}
				notifyClient();
			}
		} catch (InterruptedException | IOException e) {
			e.printStackTrace();
		}
	}
}
