-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensorSimulator.java
More file actions
42 lines (36 loc) · 1.48 KB
/
SensorSimulator.java
File metadata and controls
42 lines (36 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.concurrent.BlockingQueue;
import java.util.Random;
import java.util.Date;
public class SensorSimulator extends Thread {
private final BlockingQueue<DataRecord> queue;
private final int intervalMs;
private volatile boolean running = true;
private final Random rnd = new Random();
public SensorSimulator(BlockingQueue<DataRecord> queue, int intervalMs) {
this.queue = queue;
this.intervalMs = intervalMs;
}
@Override
public void run() {
String[] locations = {"Field A", "Field B", "North Plot", "Greenhouse 1"};
while (running) {
try {
// Simulate realistic ranges
double moisture = 10 + rnd.nextDouble() * 80; // 10..90%
double pH = 4.5 + rnd.nextDouble() * 4.5; // 4.5..9.0
double temp = 15 + rnd.nextDouble() * 25; // 15..40 C
double hum = 20 + rnd.nextDouble() * 70; // 20..90%
double light = 100 + rnd.nextDouble() * 900; // 100..1000 lux
String loc = locations[rnd.nextInt(locations.length)];
DataRecord r = new DataRecord(moisture, pH, temp, hum, light, loc, new Date());
queue.offer(r);
Thread.sleep(intervalMs);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public void stopSimulator() {
running = false;
}
}