Логика контроллера
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
package ru.spbstu.telematics.java;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
Room room = new Room();
|
||||
Settings settings = new Settings(28, 0.4);
|
||||
Controller controller = new Controller(room, settings);
|
||||
Thread controllerThread = new Thread(controller);
|
||||
controllerThread.start();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,62 @@ package ru.spbstu.telematics.java;
|
||||
* комнаты напрямую.
|
||||
*/
|
||||
public class Controller implements Runnable {
|
||||
private Room room;
|
||||
private Settings settings;
|
||||
private Sensor sensor;
|
||||
private Thread sensorThread;
|
||||
private Heater heater;
|
||||
private Thread heaterThread;
|
||||
private Fan fan;
|
||||
private Thread fanThread;
|
||||
|
||||
public Controller(Room room, Settings settings) {
|
||||
this.settings = settings;
|
||||
|
||||
sensor = new Sensor(room);
|
||||
sensorThread = new Thread(sensor);
|
||||
|
||||
heater = new Heater(room);
|
||||
heaterThread = new Thread(heater);
|
||||
|
||||
fan = new Fan(room);
|
||||
fanThread = new Thread(fan);
|
||||
}
|
||||
|
||||
private long updateIntervalMs = 500;
|
||||
private double tolerance = 0.01;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
sensorThread.start();
|
||||
heaterThread.start();
|
||||
fanThread.start();
|
||||
|
||||
while (!Thread.interrupted()) {
|
||||
if (sensor.getTemperature() < settings.getTemperature() * (1 - tolerance)) {
|
||||
heater.turnOn();
|
||||
} else {
|
||||
heater.turnOff();
|
||||
}
|
||||
|
||||
if (sensor.getHumidity() > settings.getHumidity() * (1 + tolerance)) {
|
||||
fan.turnOn();
|
||||
} else {
|
||||
fan.turnOff();
|
||||
}
|
||||
|
||||
Utils.sleep(updateIntervalMs);
|
||||
}
|
||||
|
||||
sensorThread.interrupt();
|
||||
heaterThread.interrupt();
|
||||
fanThread.interrupt();
|
||||
|
||||
try {
|
||||
sensorThread.join();
|
||||
heaterThread.join();
|
||||
fanThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user