2 * Copyright (c) 2010-2022 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.bmwconnecteddrive.internal.utils;
15 import static org.openhab.binding.bmwconnecteddrive.internal.utils.Constants.*;
17 import java.lang.reflect.Field;
18 import java.time.LocalDateTime;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.bmwconnecteddrive.internal.dto.status.CBSMessage;
22 import org.openhab.binding.bmwconnecteddrive.internal.dto.status.VehicleStatus;
25 * The {@link VehicleStatusUtils} Data Transfer Object
27 * @author Bernd Weymann - Initial contribution
30 public class VehicleStatusUtils {
32 public static String getNextServiceDate(VehicleStatus vStatus) {
33 if (vStatus.cbsData == null) {
34 return Constants.NULL_DATE;
36 if (vStatus.cbsData.isEmpty()) {
37 return Constants.NULL_DATE;
39 LocalDateTime farFuture = LocalDateTime.now().plusYears(100);
40 LocalDateTime serviceDate = farFuture;
41 for (int i = 0; i < vStatus.cbsData.size(); i++) {
42 CBSMessage entry = vStatus.cbsData.get(i);
43 if (entry.cbsDueDate != null) {
44 LocalDateTime d = LocalDateTime.parse(entry.cbsDueDate + Constants.UTC_APPENDIX);
45 if (d.isBefore(serviceDate)) {
50 if (serviceDate.equals(farFuture)) {
51 return Constants.NULL_DATE;
53 return serviceDate.format(Converter.DATE_INPUT_PATTERN);
58 public static int getNextServiceMileage(VehicleStatus vStatus) {
59 if (vStatus.cbsData == null) {
62 if (vStatus.cbsData.isEmpty()) {
65 int serviceMileage = Integer.MAX_VALUE;
66 for (int i = 0; i < vStatus.cbsData.size(); i++) {
67 CBSMessage entry = vStatus.cbsData.get(i);
68 if (entry.cbsRemainingMileage != -1) {
69 if (entry.cbsRemainingMileage < serviceMileage) {
70 serviceMileage = entry.cbsRemainingMileage;
74 if (serviceMileage != Integer.MAX_VALUE) {
75 return serviceMileage;
82 public static String checkControlActive(VehicleStatus vStatus) {
83 if (vStatus.checkControlMessages == null) {
86 if (vStatus.checkControlMessages.isEmpty()) {
93 public static String getUpdateTime(VehicleStatus vStatus) {
94 if (vStatus.internalDataTimeUTC != null) {
95 return vStatus.internalDataTimeUTC;
96 } else if (vStatus.updateTime != null) {
97 return vStatus.updateTime;
99 return Constants.NULL_DATE;
104 * Check for certain Windows or Doors DTO object the "Closed" Status
105 * INVALID values will be ignored
108 * @return Closed if all "Closed", "Open" otherwise
110 public static String checkClosed(Object dto) {
111 String overallState = Constants.UNDEF;
112 for (Field field : dto.getClass().getDeclaredFields()) {
114 Object d = field.get(dto);
116 String state = d.toString();
117 // skip invalid entries - they don't apply to this Vehicle
118 if (!state.equalsIgnoreCase(INVALID)) {
119 if (state.equalsIgnoreCase(OPEN)) {
121 // stop searching for more open items - overall Doors / Windows are OPEN
123 } else if (state.equalsIgnoreCase(INTERMEDIATE)) {
124 if (!overallState.equalsIgnoreCase(OPEN)) {
125 overallState = INTERMEDIATE;
126 // continue searching - maybe another Door / Window is OPEN
128 } else if (state.equalsIgnoreCase(CLOSED)) {
129 // at least one valid object needs to be found in order to reply "CLOSED"
130 if (overallState.equalsIgnoreCase(UNDEF)) {
131 overallState = CLOSED;
136 } catch (IllegalArgumentException | IllegalAccessException e) {
139 return Converter.toTitleCase(overallState);