]> git.basschouten.com Git - openhab-addons.git/blob
1a11ee69643b3918776abec13fc6c6b43a7902f0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.bmwconnecteddrive.internal.utils;
14
15 import static org.openhab.binding.bmwconnecteddrive.internal.utils.Constants.*;
16
17 import java.lang.reflect.Field;
18 import java.time.LocalDateTime;
19
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;
23
24 /**
25  * The {@link VehicleStatusUtils} Data Transfer Object
26  *
27  * @author Bernd Weymann - Initial contribution
28  */
29 @NonNullByDefault
30 public class VehicleStatusUtils {
31
32     public static String getNextServiceDate(VehicleStatus vStatus) {
33         if (vStatus.cbsData == null) {
34             return Constants.NULL_DATE;
35         }
36         if (vStatus.cbsData.isEmpty()) {
37             return Constants.NULL_DATE;
38         } else {
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)) {
46                         serviceDate = d;
47                     }
48                 }
49             }
50             if (serviceDate.equals(farFuture)) {
51                 return Constants.NULL_DATE;
52             } else {
53                 return serviceDate.format(Converter.DATE_INPUT_PATTERN);
54             }
55         }
56     }
57
58     public static int getNextServiceMileage(VehicleStatus vStatus) {
59         if (vStatus.cbsData == null) {
60             return -1;
61         }
62         if (vStatus.cbsData.isEmpty()) {
63             return -1;
64         } else {
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;
71                     }
72                 }
73             }
74             if (serviceMileage != Integer.MAX_VALUE) {
75                 return serviceMileage;
76             } else {
77                 return -1;
78             }
79         }
80     }
81
82     public static String checkControlActive(VehicleStatus vStatus) {
83         if (vStatus.checkControlMessages == null) {
84             return UNDEF;
85         }
86         if (vStatus.checkControlMessages.isEmpty()) {
87             return NOT_ACTIVE;
88         } else {
89             return ACTIVE;
90         }
91     }
92
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;
98         } else {
99             return Constants.NULL_DATE;
100         }
101     }
102
103     /**
104      * Check for certain Windows or Doors DTO object the "Closed" Status
105      * INVALID values will be ignored
106      *
107      * @param dto
108      * @return Closed if all "Closed", "Open" otherwise
109      */
110     public static String checkClosed(Object dto) {
111         String overallState = Constants.UNDEF;
112         for (Field field : dto.getClass().getDeclaredFields()) {
113             try {
114                 Object d = field.get(dto);
115                 if (d != null) {
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)) {
120                             overallState = OPEN;
121                             // stop searching for more open items - overall Doors / Windows are OPEN
122                             break;
123                         } else if (state.equalsIgnoreCase(INTERMEDIATE)) {
124                             if (!overallState.equalsIgnoreCase(OPEN)) {
125                                 overallState = INTERMEDIATE;
126                                 // continue searching - maybe another Door / Window is OPEN
127                             }
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;
132                             }
133                         }
134                     }
135                 }
136             } catch (IllegalArgumentException | IllegalAccessException e) {
137             }
138         }
139         return Converter.toTitleCase(overallState);
140     }
141 }