]> git.basschouten.com Git - openhab-addons.git/blob
4dd16cf400647ef9d6cb38f7dd21e62cad1b0a19
[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.handler.backend;
14
15 import java.io.BufferedReader;
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.mybmw.internal.MyBMWBridgeConfiguration;
25 import org.openhab.binding.mybmw.internal.dto.charge.ChargingSessionsContainer;
26 import org.openhab.binding.mybmw.internal.dto.charge.ChargingStatisticsContainer;
27 import org.openhab.binding.mybmw.internal.dto.remote.ExecutionStatusContainer;
28 import org.openhab.binding.mybmw.internal.dto.vehicle.Vehicle;
29 import org.openhab.binding.mybmw.internal.dto.vehicle.VehicleBase;
30 import org.openhab.binding.mybmw.internal.dto.vehicle.VehicleStateContainer;
31 import org.openhab.binding.mybmw.internal.handler.enums.RemoteService;
32 import org.openhab.binding.mybmw.internal.utils.BimmerConstants;
33 import org.openhab.binding.mybmw.internal.utils.ImageProperties;
34 import org.openhab.core.io.net.http.HttpClientFactory;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * This class is for local testing. You have to configure a connected account with username = "testuser" and password =
40  * vehicle to be tested (e.g. BEV, ICE, BEV2, MILD_HYBRID,...)
41  * The respective files are loaded from the resources folder
42  * 
43  * You have to set the environment variable "ENVIRONMENT" to the value "test"
44  * 
45  * @author Bernd Weymann - Initial contribution
46  * @author Martin Grassl - refactoring
47  */
48 @NonNullByDefault
49 public class MyBMWFileProxy implements MyBMWProxy {
50     private final Logger logger = LoggerFactory.getLogger(MyBMWFileProxy.class);
51     private String vehicleToBeTested;
52
53     private static final String RESPONSES = "responses" + File.separator;
54     private static final String VEHICLES_BASE = File.separator + "vehicles_base.json";
55     private static final String VEHICLES_STATE = File.separator + "vehicles_state.json";
56     private static final String CHARGING_SESSIONS = File.separator + "charging_sessions.json";
57     private static final String CHARGING_STATISTICS = File.separator + "charging_statistics.json";
58     private static final String REMOTE_SERVICES_CALL = File.separator + "remote_service_call.json";
59     private static final String REMOTE_SERVICES_STATE = File.separator + "remote_service_status.json";
60
61     public MyBMWFileProxy(HttpClientFactory httpClientFactory, MyBMWBridgeConfiguration bridgeConfiguration) {
62         logger.trace("MyBMWFileProxy - initialize");
63         vehicleToBeTested = bridgeConfiguration.password;
64     }
65
66     public void setBridgeConfiguration(MyBMWBridgeConfiguration bridgeConfiguration) {
67         logger.trace("MyBMWFileProxy - update bridge");
68         vehicleToBeTested = bridgeConfiguration.password;
69     }
70
71     public List<@NonNull Vehicle> requestVehicles() throws NetworkException {
72         List<@NonNull Vehicle> vehicles = new ArrayList<>();
73         List<@NonNull VehicleBase> vehiclesBase = requestVehiclesBase();
74
75         for (VehicleBase vehicleBase : vehiclesBase) {
76             VehicleStateContainer vehicleState = requestVehicleState(vehicleBase.getVin(),
77                     vehicleBase.getAttributes().getBrand());
78
79             Vehicle vehicle = new Vehicle();
80             vehicle.setVehicleBase(vehicleBase);
81             vehicle.setVehicleState(vehicleState);
82             vehicles.add(vehicle);
83         }
84
85         return vehicles;
86     }
87
88     /**
89      * request all vehicles for one specific brand and their state
90      *
91      * @param brand
92      */
93     public List<VehicleBase> requestVehiclesBase(String brand) throws NetworkException {
94         String vehicleResponseString = requestVehiclesBaseJson(brand);
95         return JsonStringDeserializer.getVehicleBaseList(vehicleResponseString);
96     }
97
98     public String requestVehiclesBaseJson(String brand) throws NetworkException {
99         String vehicleResponseString = fileToString(VEHICLES_BASE);
100         return vehicleResponseString;
101     }
102
103     /**
104      * request vehicles for all possible brands
105      *
106      * @param callback
107      */
108     public List<VehicleBase> requestVehiclesBase() throws NetworkException {
109         List<VehicleBase> vehicles = new ArrayList<>();
110
111         for (String brand : BimmerConstants.REQUESTED_BRANDS) {
112             vehicles.addAll(requestVehiclesBase(brand));
113         }
114
115         return vehicles;
116     }
117
118     /**
119      * request the vehicle image
120      *
121      * @param config
122      * @param props
123      * @return
124      */
125     public byte[] requestImage(String vin, String brand, ImageProperties props) throws NetworkException {
126         return "".getBytes();
127     }
128
129     /**
130      * request the state for one specific vehicle
131      *
132      * @param baseVehicle
133      * @return
134      */
135     public VehicleStateContainer requestVehicleState(String vin, String brand) throws NetworkException {
136         String vehicleStateResponseString = requestVehicleStateJson(vin, brand);
137         return JsonStringDeserializer.getVehicleState(vehicleStateResponseString);
138     }
139
140     public String requestVehicleStateJson(String vin, String brand) throws NetworkException {
141         String vehicleStateResponseString = fileToString(VEHICLES_STATE);
142         return vehicleStateResponseString;
143     }
144
145     /**
146      * request charge statistics for electric vehicles
147      *
148      */
149     public ChargingStatisticsContainer requestChargeStatistics(String vin, String brand) throws NetworkException {
150         String chargeStatisticsResponseString = requestChargeStatisticsJson(vin, brand);
151         return JsonStringDeserializer.getChargingStatistics(new String(chargeStatisticsResponseString));
152     }
153
154     public String requestChargeStatisticsJson(String vin, String brand) throws NetworkException {
155         String chargeStatisticsResponseString = fileToString(CHARGING_STATISTICS);
156         return chargeStatisticsResponseString;
157     }
158
159     /**
160      * request charge sessions for electric vehicles
161      *
162      */
163     public ChargingSessionsContainer requestChargeSessions(String vin, String brand) throws NetworkException {
164         String chargeSessionsResponseString = requestChargeSessionsJson(vin, brand);
165         return JsonStringDeserializer.getChargingSessions(chargeSessionsResponseString);
166     }
167
168     public String requestChargeSessionsJson(String vin, String brand) throws NetworkException {
169         String chargeSessionsResponseString = fileToString(CHARGING_SESSIONS);
170         return chargeSessionsResponseString;
171     }
172
173     public ExecutionStatusContainer executeRemoteServiceCall(String vin, String brand, RemoteService service)
174             throws NetworkException {
175         return JsonStringDeserializer.getExecutionStatus(fileToString(REMOTE_SERVICES_CALL));
176     }
177
178     public ExecutionStatusContainer executeRemoteServiceStatusCall(String brand, String eventId)
179             throws NetworkException {
180         return JsonStringDeserializer.getExecutionStatus(fileToString(REMOTE_SERVICES_STATE));
181     }
182
183     private String fileToString(String filename) {
184         logger.trace("reading file {}", RESPONSES + vehicleToBeTested + filename);
185         try (BufferedReader br = new BufferedReader(new InputStreamReader(
186                 MyBMWFileProxy.class.getClassLoader().getResourceAsStream(RESPONSES + vehicleToBeTested + filename),
187                 "UTF-8"))) {
188             StringBuilder buf = new StringBuilder();
189             String sCurrentLine;
190
191             while ((sCurrentLine = br.readLine()) != null) {
192                 buf.append(sCurrentLine);
193             }
194             logger.trace("successful");
195             return buf.toString();
196         } catch (IOException e) {
197             logger.error("file {} could not be loaded: {}", filename, e.getMessage());
198             return "";
199         }
200     }
201 }