2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mybmw.internal.utils;
15 import java.time.ZonedDateTime;
16 import java.time.format.DateTimeFormatter;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.mybmw.internal.MyBMWConstants.VehicleType;
21 import org.openhab.binding.mybmw.internal.dto.vehicle.RequiredService;
22 import org.openhab.core.library.types.DateTimeType;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.UnDefType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * The {@link VehicleStatusUtils} Data Transfer Object
32 * @author Bernd Weymann - Initial contribution
33 * @author Martin Grassl - refactor for v2 and extract some methods to other classes
36 public class VehicleStatusUtils {
37 public static final Logger LOGGER = LoggerFactory.getLogger(VehicleStatusUtils.class);
40 * the date can be empty
42 * @param requiredServices
45 public static State getNextServiceDate(List<RequiredService> requiredServices) {
46 ZonedDateTime farFuture = ZonedDateTime.now().plusYears(100);
47 ZonedDateTime serviceDate = farFuture;
48 for (RequiredService requiredService : requiredServices) {
49 if (requiredService.getDateTime() != null && !requiredService.getDateTime().isEmpty()) {
50 ZonedDateTime d = ZonedDateTime.parse(requiredService.getDateTime());
51 if (d.isBefore(serviceDate)) {
56 if (serviceDate.equals(farFuture)) {
57 return UnDefType.UNDEF;
59 return DateTimeType.valueOf(serviceDate.format(DateTimeFormatter.ISO_INSTANT));
64 * the mileage can be empty
66 * @param requiredServices
69 public static State getNextServiceMileage(List<RequiredService> requiredServices) {
70 int serviceMileage = Integer.MAX_VALUE;
71 for (RequiredService requiredService : requiredServices) {
72 if (requiredService.getMileage() > 0) {
73 if (requiredService.getMileage() < serviceMileage) {
74 serviceMileage = requiredService.getMileage();
78 if (serviceMileage != Integer.MAX_VALUE) {
79 return QuantityType.valueOf(serviceMileage, Constants.KILOMETRE_UNIT);
81 return UnDefType.UNDEF;
86 * calculates the mapping of thing type
92 public static VehicleType vehicleType(String driveTrain, String model) {
93 if (Constants.DRIVETRAIN_BEV.equals(driveTrain)) {
94 if (model.endsWith(Constants.DRIVETRAIN_REX_EXTENSION)) {
95 return VehicleType.ELECTRIC_REX;
97 return VehicleType.ELECTRIC;
99 } else if (Constants.DRIVETRAIN_PHEV.equals(driveTrain)) {
100 return VehicleType.PLUGIN_HYBRID;
101 } else if (Constants.DRIVETRAIN_CONV.equals(driveTrain)
102 || Constants.DRIVETRAIN_MILD_HYBRID.equals(driveTrain)) {
103 return VehicleType.CONVENTIONAL;
105 LOGGER.warn("Unknown Vehicle Type: {} | {}", model, driveTrain);
106 return VehicleType.UNKNOWN;