backend/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatRepository.java

31 lines
1.0 KiB
Java
Raw Normal View History

2020-04-09 09:30:05 +00:00
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
2020-04-13 14:42:48 +00:00
import java.math.BigDecimal;
2020-04-15 15:55:04 +00:00
import java.util.List;
2020-04-13 14:42:48 +00:00
import java.util.Optional;
2020-04-15 15:55:04 +00:00
import javax.transaction.Transactional;
2020-04-13 14:42:48 +00:00
import org.springframework.data.jpa.repository.Query;
public interface ThermostatRepository extends DeviceRepository<Thermostat> {
2020-04-15 15:55:04 +00:00
/**
* Finds all devices belonging to a user
*
* @param username the User's username
* @return all devices of that user
*/
@Transactional
@Query("SELECT t FROM Thermostat t JOIN t.room r JOIN r.user u WHERE u.username = ?1")
List<Thermostat> findAllByUsername(String username);
2020-04-13 14:42:48 +00:00
/**
* Computes the average temperature of all temperature sensors in the room
*
* @param thermostatRoomId room ID of the thermostat
* @return an optional big decimal, empty if none found
*/
@Query(
2020-04-14 11:24:36 +00:00
"SELECT AVG(s.value) FROM Sensor s JOIN s.room r WHERE s.sensor = 'TEMPERATURE' AND r.id = ?1")
2020-04-13 14:42:48 +00:00
Optional<BigDecimal> getAverageTemperature(Long thermostatRoomId);
}