Логика контроллера
This commit is contained in:
@@ -1,13 +1,11 @@
|
|||||||
package ru.spbstu.telematics.java;
|
package ru.spbstu.telematics.java;
|
||||||
|
|
||||||
/**
|
public class App {
|
||||||
* Hello world!
|
public static void main(String[] args) {
|
||||||
*
|
Room room = new Room();
|
||||||
*/
|
Settings settings = new Settings(28, 0.4);
|
||||||
public class App
|
Controller controller = new Controller(room, settings);
|
||||||
{
|
Thread controllerThread = new Thread(controller);
|
||||||
public static void main( String[] args )
|
controllerThread.start();
|
||||||
{
|
|
||||||
System.out.println( "Hello World!" );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,14 +7,62 @@ package ru.spbstu.telematics.java;
|
|||||||
* комнаты напрямую.
|
* комнаты напрямую.
|
||||||
*/
|
*/
|
||||||
public class Controller implements Runnable {
|
public class Controller implements Runnable {
|
||||||
private Room room;
|
|
||||||
private Settings settings;
|
private Settings settings;
|
||||||
private Sensor sensor;
|
private Sensor sensor;
|
||||||
|
private Thread sensorThread;
|
||||||
private Heater heater;
|
private Heater heater;
|
||||||
|
private Thread heaterThread;
|
||||||
private Fan fan;
|
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
|
@Override
|
||||||
public void run() {
|
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