Search in sources :

Example 1 with ElectricCar

use of com.adja.evchargerappserver.api.electriccar.ElectricCar in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class ChargerService method carAttemptsCharging.

@Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = NotValidUpdateException.class)
public boolean carAttemptsCharging(Long chargerId, Long carId) throws NotValidUpdateException {
    Optional<Charger> chargerById = this.repository.findById(chargerId);
    if (chargerById.isEmpty()) {
        throw new NotValidUpdateException("");
    } else {
        Charger charger = chargerById.get();
        if (charger.getCurrentlyChargingCar() != null) {
            return false;
        } else {
            Optional<ElectricCar> carOptional = this.electricCarRepository.findById(carId);
            if (carOptional.isEmpty())
                throw new NotValidUpdateException("");
            ElectricCar car = carOptional.get();
            if (!car.getCarType().getCompatibleChargerTypes().contains(charger.getChargerType())) {
                return false;
            }
            car.setCharger(charger);
            this.electricCarRepository.save(car);
            charger.setCurrentlyChargingCar(car);
            this.repository.save(charger);
            return true;
        }
    }
}
Also used : ElectricCar(com.adja.evchargerappserver.api.electriccar.ElectricCar) NotValidUpdateException(com.adja.evchargerappserver.api.NotValidUpdateException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ElectricCar

use of com.adja.evchargerappserver.api.electriccar.ElectricCar in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class MockElectricCarHandler method onBatteryPercentageChanged.

private void onBatteryPercentageChanged(ElectricCarRepresentation car) {
    this.electricCarService.persistBatteryPercentageChanges(car.getID(), car.getBatteryPercentage());
    ElectricCar persistedCar = this.electricCarService.getById(car.getID());
    if (persistedCar.getCharger() != null) {
        this.websocket.sendChargingStationUpdateFromJava(new ChargingStationStateChange(persistedCar.getCharger().getChargingStation().getId()));
        if (persistedCar.getBatteryPercentage() == 80) {
            this.notificationService.carReached80PercentBattery(persistedCar);
        }
    }
    this.websocket.sendCarBatteryPercentageUpdateFromJava(new CarBatteryStateChange(persistedCar.getId(), (long) persistedCar.getBatteryPercentage()));
}
Also used : ElectricCar(com.adja.evchargerappserver.api.electriccar.ElectricCar) CarBatteryStateChange(com.adja.evchargerappserver.socket.CarBatteryStateChange) ChargingStationStateChange(com.adja.evchargerappserver.socket.ChargingStationStateChange)

Example 3 with ElectricCar

use of com.adja.evchargerappserver.api.electriccar.ElectricCar in project iet-hf-2022-k-k-k-k-k-k by BME-MIT-IET.

the class PersonService method setCarToPerson.

@Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = NotValidUpdateException.class)
public void setCarToPerson(Long id, Long carId) throws NotValidUpdateException {
    try {
        Person person = this.getById(id);
        ElectricCar car = this.electricCarService.getById(carId);
        if (car.getDriver() != null)
            throw new NotValidUpdateException("");
        if (person.getCar() != null) {
            person.getCar().setDriver(null);
        }
        car.setDriver(person);
        person.setCar(car);
        this.put(id, person);
    } catch (EntityNotFoundException e) {
        throw new NotValidUpdateException("");
    }
}
Also used : ElectricCar(com.adja.evchargerappserver.api.electriccar.ElectricCar) NotValidUpdateException(com.adja.evchargerappserver.api.NotValidUpdateException) EntityNotFoundException(javax.persistence.EntityNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ElectricCar (com.adja.evchargerappserver.api.electriccar.ElectricCar)3 NotValidUpdateException (com.adja.evchargerappserver.api.NotValidUpdateException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 CarBatteryStateChange (com.adja.evchargerappserver.socket.CarBatteryStateChange)1 ChargingStationStateChange (com.adja.evchargerappserver.socket.ChargingStationStateChange)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1