From 620c196393ff56fdb8ea1ffafa4d850549802ddc Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Sun, 15 Mar 2020 14:49:51 +0100 Subject: [PATCH] Implemented fake updates --- .../smarthut/SmarthutApplication.java | 2 + .../controller/MotionSensorController.java | 27 +++++--- .../smarthut/controller/SensorController.java | 30 +++++---- .../sanmarinoes/smarthut/models/Sensor.java | 8 +-- .../smarthut/scheduled/SensorUpdateTasks.java | 62 +++++++++++++++++++ .../socket/AuthenticationMessageListener.java | 2 - .../smarthut/socket/SensorSocketEndpoint.java | 1 - .../sa4/sanmarinoes/smarthut/utils/Utils.java | 1 - 8 files changed, 106 insertions(+), 27 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/SensorUpdateTasks.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java index 242f03f..57f7b42 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java @@ -3,8 +3,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling @EnableJpaRepositories("ch.usi.inf.sa4.sanmarinoes.smarthut.models") public class SmarthutApplication { public static void main(String[] args) { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java index 96ba755..59c0343 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java @@ -42,6 +42,22 @@ public class MotionSensorController { return motionSensorService.save(newMS); } + /** + * Updates detection status of given motion sensor and propagates update throgh socket + * + * @param sensor the motion sensor to update + * @param detected the new detection status + * @return the updated motion sensor + */ + public MotionSensor updateDetectionFromMotionSensor(MotionSensor sensor, boolean detected) { + sensor.setDetected(detected); + final MotionSensor toReturn = motionSensorService.save(sensor); + + sensorSocketEndpoint.broadcast(sensor, motionSensorService.findUser(sensor.getId())); + + return toReturn; + } + @PutMapping("/{id}/detect") public MotionSensor updateDetection( @PathVariable("id") Long sensorId, @@ -49,16 +65,11 @@ public class MotionSensorController { final Principal principal) throws NotFoundException { - final MotionSensor sensor = + return updateDetectionFromMotionSensor( motionSensorService .findByIdAndUsername(sensorId, principal.getName()) - .orElseThrow(NotFoundException::new); - sensor.setDetected(detected); - final MotionSensor toReturn = motionSensorService.save(sensor); - - sensorSocketEndpoint.broadcast(sensor, motionSensorService.findUser(sensorId)); - - return toReturn; + .orElseThrow(NotFoundException::new), + detected); } @DeleteMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java index 400d06f..ee1be81 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java @@ -45,25 +45,33 @@ public class SensorController { return sensorRepository.save(newSensor); } + /** + * Updates the sensor with new measurement and propagates update through websocket + * + * @param sensor the sensor to update + * @param value the new measurement + * @return the updated sensor + */ + public Sensor updateValueFromSensor(Sensor sensor, BigDecimal value) { + sensor.setValue(value); + final Sensor toReturn = sensorRepository.save(sensor); + + sensorSocketEndpoint.broadcast(sensor, sensorRepository.findUser(sensor.getId())); + + return toReturn; + } + @PutMapping("/{id}/value") public Sensor updateValue( @PathVariable("id") Long sensorId, @RequestParam("value") BigDecimal value, final Principal principal) throws NotFoundException { - - final Sensor sensor = + return updateValueFromSensor( sensorRepository .findByIdAndUsername(sensorId, principal.getName()) - .orElseThrow(NotFoundException::new); - sensor.setValue(value); - final Sensor toReturn = sensorRepository.save(sensor); - - System.out.println("sensor: " + sensor); - - sensorSocketEndpoint.broadcast(sensor, sensorRepository.findUser(sensorId)); - - return toReturn; + .orElseThrow(NotFoundException::new), + value); } @DeleteMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java index 6dd634a..525ceb3 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java @@ -13,11 +13,11 @@ import javax.validation.constraints.NotNull; @Entity public class Sensor extends InputDevice { - private static final Map TYPICAL_VALUES = + public static final Map TYPICAL_VALUES = Map.of( - SensorType.TEMPERATURE, 17, - SensorType.HUMIDITY, 40, - SensorType.LIGHT, 1000); + SensorType.TEMPERATURE, new BigDecimal(17.0), + SensorType.HUMIDITY, new BigDecimal(40.0), + SensorType.LIGHT, new BigDecimal(1000)); /** Type of sensor, i.e. of the thing the sensor measures. */ public enum SensorType { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/SensorUpdateTasks.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/SensorUpdateTasks.java new file mode 100644 index 0000000..b1614aa --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/SensorUpdateTasks.java @@ -0,0 +1,62 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.scheduled; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.controller.MotionSensorController; +import ch.usi.inf.sa4.sanmarinoes.smarthut.controller.SensorController; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SensorRepository; +import java.math.BigDecimal; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.stream.StreamSupport; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +/** Generates fake sensor (and motion sensor) updates as required by milestone one */ +@Component +public class SensorUpdateTasks { + + @Autowired private SensorRepository sensorRepository; + + @Autowired private MotionSensorRepository motionSensorRepository; + + @Autowired private SensorController sensorController; + + @Autowired private MotionSensorController motionSensorController; + + /** Generates fake sensor updates every two seconds with a +/- 1.25% error */ + @Scheduled(fixedRate = 2000) + public void sensorFakeUpdate() { + StreamSupport.stream(sensorRepository.findAll().spliterator(), true) + .forEach( + sensor -> + sensorController.updateValueFromSensor( + sensor, + Sensor.TYPICAL_VALUES + .get(sensor.getSensor()) + .multiply( + new BigDecimal( + 0.9875 + Math.random() / 40)))); + } + + /** + * Generate fake motion detections in all motion detectors every 20 seconds for 2 seconds at + * most + */ + @Scheduled(fixedDelay = 20000) + public void motionSensorFakeUpdate() { + StreamSupport.stream(motionSensorRepository.findAll().spliterator(), true) + .forEach( + sensor -> { + motionSensorController.updateDetectionFromMotionSensor(sensor, true); + CompletableFuture.delayedExecutor( + (long) (Math.random() * 2000), TimeUnit.MILLISECONDS) + .execute( + () -> + motionSensorController + .updateDetectionFromMotionSensor( + sensor, false)); + }); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/AuthenticationMessageListener.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/AuthenticationMessageListener.java index e3d97ea..89415aa 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/AuthenticationMessageListener.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/AuthenticationMessageListener.java @@ -44,8 +44,6 @@ public class AuthenticationMessageListener { return new MessageHandler.Whole<>() { @Override public void onMessage(final String message) { - System.out.println(message); - if (message == null) { acknowledge(false); return; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java index ee7ff56..bc2f90e 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/socket/SensorSocketEndpoint.java @@ -59,7 +59,6 @@ public class SensorSocketEndpoint extends Endpoint { */ public long broadcast(Object message, User u) { final Collection sessions = authorizedClients.get(u); - System.out.println(authorizedClients + " " + sessions + " " + u); return sessions.stream() .parallel() .filter(didThrow(s -> s.getBasicRemote().sendText(gson.toJson(message)))) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java index 3f24d4c..99d363b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java @@ -22,7 +22,6 @@ public final class Utils { return (t) -> { try { consumer.apply(t); - System.out.println("successful"); return true; } catch (Throwable e) { System.err.println(e.getMessage());