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.handler.backend;
15 import java.io.BufferedReader;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.util.ArrayList;
20 import java.util.List;
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;
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
43 * You have to set the environment variable "ENVIRONMENT" to the value "test"
45 * @author Bernd Weymann - Initial contribution
46 * @author Martin Grassl - refactoring
49 public class MyBMWFileProxy implements MyBMWProxy {
50 private final Logger logger = LoggerFactory.getLogger(MyBMWFileProxy.class);
51 private String vehicleToBeTested;
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";
61 public MyBMWFileProxy(HttpClientFactory httpClientFactory, MyBMWBridgeConfiguration bridgeConfiguration) {
62 logger.trace("MyBMWFileProxy - initialize");
63 vehicleToBeTested = bridgeConfiguration.password;
66 public void setBridgeConfiguration(MyBMWBridgeConfiguration bridgeConfiguration) {
67 logger.trace("MyBMWFileProxy - update bridge");
68 vehicleToBeTested = bridgeConfiguration.password;
71 public List<@NonNull Vehicle> requestVehicles() throws NetworkException {
72 List<@NonNull Vehicle> vehicles = new ArrayList<>();
73 List<@NonNull VehicleBase> vehiclesBase = requestVehiclesBase();
75 for (VehicleBase vehicleBase : vehiclesBase) {
76 VehicleStateContainer vehicleState = requestVehicleState(vehicleBase.getVin(),
77 vehicleBase.getAttributes().getBrand());
79 Vehicle vehicle = new Vehicle();
80 vehicle.setVehicleBase(vehicleBase);
81 vehicle.setVehicleState(vehicleState);
82 vehicles.add(vehicle);
89 * request all vehicles for one specific brand and their state
93 public List<VehicleBase> requestVehiclesBase(String brand) throws NetworkException {
94 String vehicleResponseString = requestVehiclesBaseJson(brand);
95 return JsonStringDeserializer.getVehicleBaseList(vehicleResponseString);
98 public String requestVehiclesBaseJson(String brand) throws NetworkException {
99 String vehicleResponseString = fileToString(VEHICLES_BASE);
100 return vehicleResponseString;
104 * request vehicles for all possible brands
108 public List<VehicleBase> requestVehiclesBase() throws NetworkException {
109 List<VehicleBase> vehicles = new ArrayList<>();
111 for (String brand : BimmerConstants.REQUESTED_BRANDS) {
112 vehicles.addAll(requestVehiclesBase(brand));
119 * request the vehicle image
125 public byte[] requestImage(String vin, String brand, ImageProperties props) throws NetworkException {
126 return "".getBytes();
130 * request the state for one specific vehicle
135 public VehicleStateContainer requestVehicleState(String vin, String brand) throws NetworkException {
136 String vehicleStateResponseString = requestVehicleStateJson(vin, brand);
137 return JsonStringDeserializer.getVehicleState(vehicleStateResponseString);
140 public String requestVehicleStateJson(String vin, String brand) throws NetworkException {
141 String vehicleStateResponseString = fileToString(VEHICLES_STATE);
142 return vehicleStateResponseString;
146 * request charge statistics for electric vehicles
149 public ChargingStatisticsContainer requestChargeStatistics(String vin, String brand) throws NetworkException {
150 String chargeStatisticsResponseString = requestChargeStatisticsJson(vin, brand);
151 return JsonStringDeserializer.getChargingStatistics(new String(chargeStatisticsResponseString));
154 public String requestChargeStatisticsJson(String vin, String brand) throws NetworkException {
155 String chargeStatisticsResponseString = fileToString(CHARGING_STATISTICS);
156 return chargeStatisticsResponseString;
160 * request charge sessions for electric vehicles
163 public ChargingSessionsContainer requestChargeSessions(String vin, String brand) throws NetworkException {
164 String chargeSessionsResponseString = requestChargeSessionsJson(vin, brand);
165 return JsonStringDeserializer.getChargingSessions(chargeSessionsResponseString);
168 public String requestChargeSessionsJson(String vin, String brand) throws NetworkException {
169 String chargeSessionsResponseString = fileToString(CHARGING_SESSIONS);
170 return chargeSessionsResponseString;
173 public ExecutionStatusContainer executeRemoteServiceCall(String vin, String brand, RemoteService service)
174 throws NetworkException {
175 return JsonStringDeserializer.getExecutionStatus(fileToString(REMOTE_SERVICES_CALL));
178 public ExecutionStatusContainer executeRemoteServiceStatusCall(String brand, String eventId)
179 throws NetworkException {
180 return JsonStringDeserializer.getExecutionStatus(fileToString(REMOTE_SERVICES_STATE));
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),
188 StringBuilder buf = new StringBuilder();
191 while ((sCurrentLine = br.readLine()) != null) {
192 buf.append(sCurrentLine);
194 logger.trace("successful");
195 return buf.toString();
196 } catch (IOException e) {
197 logger.error("file {} could not be loaded: {}", filename, e.getMessage());