feat: Room elapsed time processing

This commit is contained in:
Jean-Remy Hok 2022-01-11 16:54:36 +01:00
parent fb1ab7247d
commit b69c01243c
3 changed files with 67 additions and 12 deletions

View file

@ -42,8 +42,8 @@ public class RoomsServiceController {
return rooms.affectDevice(idRoom, type, idDevice);
}
@DeleteMapping("/{id}/devices")
public boolean removeDevice(@PathVariable("id") int idRoom, @RequestParam String type, @RequestBody Integer idDevice) {
@DeleteMapping("/{id}/devices/{idDevice}")
public boolean removeDevice(@PathVariable("id") int idRoom, @RequestParam String type, @PathVariable("idDevice") Integer idDevice) {
return rooms.removeDevice(idRoom, type, idDevice);
}
@ -57,14 +57,19 @@ public class RoomsServiceController {
return rooms.getPersons(id);
}
@PostMapping("/{id}/persons")
@PostMapping("/{id}/persons/add")
public Integer addPersons(@PathVariable("id") int idRoom, @RequestBody Integer nbPersons) {
return rooms.addPersons(idRoom, nbPersons);
}
@DeleteMapping("/{id}/persons")
@PostMapping("/{id}/persons/remove")
public Integer removePersons(@PathVariable("id") int idRoom, @RequestBody Integer nbPersons) {
return rooms.removePersons(idRoom, nbPersons);
}
@GetMapping("/{id}/elapsedTime")
public Long getElapsedTime(@PathVariable("id") int idRoom) {
return rooms.getElapsedTime(idRoom);
}
}

View file

@ -53,15 +53,14 @@ public class INSA {
public Integer getPersons(int idRoom) {
Room room = rooms.get(idRoom);
if (room == null) return null;
else return room.countPeople;
else return room.getPersons();
}
public Integer addPersons(int idRoom, int nbPersons) {
Room room = rooms.get(idRoom);
if (room == null) return null;
else {
room.countPeople += nbPersons;
return room.countPeople;
return room.addPersons(nbPersons);
}
}
@ -69,8 +68,16 @@ public class INSA {
Room room = rooms.get(idRoom);
if (room == null) return null;
else {
room.countPeople = Math.max(room.countPeople-nbPersons, 0); // Prevent having -1 person in a room
return room.countPeople;
return room.removePersons(nbPersons);
}
}
public Long getElapsedTime(int idRoom) {
Room room = rooms.get(idRoom);
if (room == null) return null;
else {
return room.getElapsedTime();
}
}
}

View file

@ -4,6 +4,25 @@ import java.util.ArrayList;
public class Room {
private class MyTimer {
private long start = 0L;
public MyTimer() { }
public void start() {
start = System.currentTimeMillis();
}
public void stop() {
start = 0L;
}
public long getElapsedTime() {
long end = System.currentTimeMillis();
return end - start;
}
}
static int compteur = 0;
private final int id;
private final String name;
@ -18,7 +37,8 @@ public class Room {
private final ArrayList<Integer> heatingActuator;
private final ArrayList<Integer> climActuator;
private final ArrayList<Integer> lightActuator;
public Integer countPeople = 0;
private Integer countPeople = 0;
private final MyTimer timer = new MyTimer();
public Room(String name) {
this.id = Room.compteur;
@ -145,4 +165,27 @@ public class Room {
}
}
public Integer getPersons() {
return countPeople;
}
public Integer addPersons(int nbPersons) {
// If there was nobody before => starts the timer
if (countPeople == 0 && nbPersons > 0) timer.start();
countPeople = Math.max(countPeople+nbPersons, 0); // Prevent having -1 person in a room
return countPeople;
}
public Integer removePersons(int nbPersons) {
countPeople = Math.max(countPeople-nbPersons, 0); // Prevent having -1 person in a room
// If there is nobody left => stops the timer
if (countPeople == 0) timer.stop();
return countPeople;
}
public long getElapsedTime() {
if (countPeople > 0) return timer.getElapsedTime();
else return 0L;
}
}