Socket now sends motion sensor updates
This commit is contained in:
parent
5a441a6992
commit
cf940df6b2
1 changed files with 23 additions and 0 deletions
|
@ -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.models.MotionSensor;
|
||||
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 javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -19,6 +21,8 @@ public class MotionSensorController {
|
|||
|
||||
@Autowired private MotionSensorRepository motionSensorService;
|
||||
|
||||
@Autowired private SensorSocketEndpoint sensorSocketEndpoint;
|
||||
|
||||
@GetMapping
|
||||
public List<MotionSensor> findAll() {
|
||||
return toList(motionSensorService.findAll());
|
||||
|
@ -38,6 +42,25 @@ public class MotionSensorController {
|
|||
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}")
|
||||
public void delete(@PathVariable("id") long id) {
|
||||
motionSensorService.deleteById(id);
|
||||
|
|
Loading…
Reference in a new issue