]> git.basschouten.com Git - openhab-addons.git/blob
aa3a97ab4cb2414308be97e7ad954aaf2105f07b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mybmw.internal.utils;
14
15 import java.time.ZonedDateTime;
16 import java.time.format.DateTimeFormatter;
17 import java.util.List;
18
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;
28
29 /**
30  * The {@link VehicleStatusUtils} Data Transfer Object
31  *
32  * @author Bernd Weymann - Initial contribution
33  * @author Martin Grassl - refactor for v2 and extract some methods to other classes
34  */
35 @NonNullByDefault
36 public class VehicleStatusUtils {
37     public static final Logger LOGGER = LoggerFactory.getLogger(VehicleStatusUtils.class);
38
39     /**
40      * the date can be empty
41      * 
42      * @param requiredServices
43      * @return
44      */
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)) {
52                     serviceDate = d;
53                 } // else skip
54             }
55         }
56         if (serviceDate.equals(farFuture)) {
57             return UnDefType.UNDEF;
58         } else {
59             return DateTimeType.valueOf(serviceDate.format(DateTimeFormatter.ISO_INSTANT));
60         }
61     }
62
63     /**
64      * the mileage can be empty
65      * 
66      * @param requiredServices
67      * @return
68      */
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();
75                 }
76             }
77         }
78         if (serviceMileage != Integer.MAX_VALUE) {
79             return QuantityType.valueOf(serviceMileage, Constants.KILOMETRE_UNIT);
80         } else {
81             return UnDefType.UNDEF;
82         }
83     }
84
85     /**
86      * calculates the mapping of thing type
87      *
88      * @param driveTrain
89      * @param model
90      * @return
91      */
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;
96             } else {
97                 return VehicleType.ELECTRIC;
98             }
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;
104         }
105         LOGGER.warn("Unknown Vehicle Type: {} | {}", model, driveTrain);
106         return VehicleType.UNKNOWN;
107     }
108 }