Socket now sends motion sensor updates

This commit is contained in:
Claudio Maggioni 2020-03-15 13:51:14 +01:00
parent 5a441a6992
commit cf940df6b2

View file

@ -6,6 +6,8 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
import java.security.Principal;
import java.util.List; import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -19,6 +21,8 @@ public class MotionSensorController {
@Autowired private MotionSensorRepository motionSensorService; @Autowired private MotionSensorRepository motionSensorService;
@Autowired private SensorSocketEndpoint sensorSocketEndpoint;
@GetMapping @GetMapping
public List<MotionSensor> findAll() { public List<MotionSensor> findAll() {
return toList(motionSensorService.findAll()); return toList(motionSensorService.findAll());
@ -38,6 +42,25 @@ public class MotionSensorController {
return motionSensorService.save(newMS); return motionSensorService.save(newMS);
} }
@PutMapping("/{id}/detect")
public MotionSensor updateDetection(
@PathVariable("id") Long sensorId,
@RequestParam("detected") boolean detected,
final Principal principal)
throws NotFoundException {
final MotionSensor sensor =
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;
}
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) { public void delete(@PathVariable("id") long id) {
motionSensorService.deleteById(id); motionSensorService.deleteById(id);