feat: Room elapsed time processing
Questo commit è contenuto in:
parent
fb1ab7247d
commit
b69c01243c
3 ha cambiato i file con 67 aggiunte e 12 eliminazioni
|
@ -42,8 +42,8 @@ public class RoomsServiceController {
|
||||||
return rooms.affectDevice(idRoom, type, idDevice);
|
return rooms.affectDevice(idRoom, type, idDevice);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}/devices")
|
@DeleteMapping("/{id}/devices/{idDevice}")
|
||||||
public boolean removeDevice(@PathVariable("id") int idRoom, @RequestParam String type, @RequestBody Integer idDevice) {
|
public boolean removeDevice(@PathVariable("id") int idRoom, @RequestParam String type, @PathVariable("idDevice") Integer idDevice) {
|
||||||
return rooms.removeDevice(idRoom, type, idDevice);
|
return rooms.removeDevice(idRoom, type, idDevice);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,14 +57,19 @@ public class RoomsServiceController {
|
||||||
return rooms.getPersons(id);
|
return rooms.getPersons(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/{id}/persons")
|
@PostMapping("/{id}/persons/add")
|
||||||
public Integer addPersons(@PathVariable("id") int idRoom, @RequestBody Integer nbPersons) {
|
public Integer addPersons(@PathVariable("id") int idRoom, @RequestBody Integer nbPersons) {
|
||||||
return rooms.addPersons(idRoom, nbPersons);
|
return rooms.addPersons(idRoom, nbPersons);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}/persons")
|
@PostMapping("/{id}/persons/remove")
|
||||||
public Integer removePersons(@PathVariable("id") int idRoom, @RequestBody Integer nbPersons) {
|
public Integer removePersons(@PathVariable("id") int idRoom, @RequestBody Integer nbPersons) {
|
||||||
return rooms.removePersons(idRoom, nbPersons);
|
return rooms.removePersons(idRoom, nbPersons);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}/elapsedTime")
|
||||||
|
public Long getElapsedTime(@PathVariable("id") int idRoom) {
|
||||||
|
return rooms.getElapsedTime(idRoom);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -53,15 +53,14 @@ public class INSA {
|
||||||
public Integer getPersons(int idRoom) {
|
public Integer getPersons(int idRoom) {
|
||||||
Room room = rooms.get(idRoom);
|
Room room = rooms.get(idRoom);
|
||||||
if (room == null) return null;
|
if (room == null) return null;
|
||||||
else return room.countPeople;
|
else return room.getPersons();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer addPersons(int idRoom, int nbPersons) {
|
public Integer addPersons(int idRoom, int nbPersons) {
|
||||||
Room room = rooms.get(idRoom);
|
Room room = rooms.get(idRoom);
|
||||||
if (room == null) return null;
|
if (room == null) return null;
|
||||||
else {
|
else {
|
||||||
room.countPeople += nbPersons;
|
return room.addPersons(nbPersons);
|
||||||
return room.countPeople;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,8 +68,16 @@ public class INSA {
|
||||||
Room room = rooms.get(idRoom);
|
Room room = rooms.get(idRoom);
|
||||||
if (room == null) return null;
|
if (room == null) return null;
|
||||||
else {
|
else {
|
||||||
room.countPeople = Math.max(room.countPeople-nbPersons, 0); // Prevent having -1 person in a room
|
return room.removePersons(nbPersons);
|
||||||
return room.countPeople;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getElapsedTime(int idRoom) {
|
||||||
|
Room room = rooms.get(idRoom);
|
||||||
|
if (room == null) return null;
|
||||||
|
else {
|
||||||
|
return room.getElapsedTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,25 @@ import java.util.ArrayList;
|
||||||
|
|
||||||
public class Room {
|
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;
|
static int compteur = 0;
|
||||||
private final int id;
|
private final int id;
|
||||||
private final String name;
|
private final String name;
|
||||||
|
@ -18,7 +37,8 @@ public class Room {
|
||||||
private final ArrayList<Integer> heatingActuator;
|
private final ArrayList<Integer> heatingActuator;
|
||||||
private final ArrayList<Integer> climActuator;
|
private final ArrayList<Integer> climActuator;
|
||||||
private final ArrayList<Integer> lightActuator;
|
private final ArrayList<Integer> lightActuator;
|
||||||
public Integer countPeople = 0;
|
private Integer countPeople = 0;
|
||||||
|
private final MyTimer timer = new MyTimer();
|
||||||
|
|
||||||
public Room(String name) {
|
public Room(String name) {
|
||||||
this.id = Room.compteur;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Caricamento…
Crea riferimento in una nuova segnalazione