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.ojelectronics.internal.services;
15 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.jetty.client.HttpClient;
20 import org.eclipse.jetty.client.api.Request;
21 import org.eclipse.jetty.client.api.Result;
22 import org.eclipse.jetty.client.util.BufferingResponseListener;
23 import org.eclipse.jetty.client.util.StringContentProvider;
24 import org.eclipse.jetty.http.HttpHeader;
25 import org.openhab.binding.ojelectronics.internal.ThermostatHandler;
26 import org.openhab.binding.ojelectronics.internal.common.OJGSonBuilder;
27 import org.openhab.binding.ojelectronics.internal.config.OJElectronicsBridgeConfiguration;
28 import org.openhab.binding.ojelectronics.internal.models.SimpleResponseModel;
29 import org.openhab.binding.ojelectronics.internal.models.Thermostat;
30 import org.openhab.binding.ojelectronics.internal.models.thermostat.UpdateThermostatRequestModel;
31 import org.openhab.core.thing.Thing;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.gson.Gson;
38 * Handles the update of the devices of a session
40 * @author Christian Kittel - Initial Contribution
43 public final class UpdateService {
45 private final Gson gson = OJGSonBuilder.getGSon();
46 private final Logger logger = LoggerFactory.getLogger(UpdateService.class);
48 private final String sessionId;
49 private final HttpClient httpClient;
50 private final OJElectronicsBridgeConfiguration configuration;
52 public UpdateService(OJElectronicsBridgeConfiguration configuration, HttpClient httpClient, String sessionId) {
53 this.configuration = configuration;
54 this.httpClient = httpClient;
55 this.sessionId = sessionId;
59 * Sends all changes of all {@link ThermostatHandler} to the API
63 public void updateAllThermostats(List<Thing> things) {
64 things.stream().filter(thing -> thing.getHandler() instanceof ThermostatHandler)
65 .map(thing -> (ThermostatHandler) thing.getHandler())
66 .map(handler -> handler.tryHandleAndGetUpdatedThermostat()).forEach(this::updateThermostat);
69 private void updateThermostat(@Nullable Thermostat thermostat) {
70 if (thermostat == null) {
73 Request request = httpClient.POST(configuration.apiUrl + "/Thermostat/UpdateThermostat")
74 .param("sessionid", sessionId).header(HttpHeader.CONTENT_TYPE, "application/json")
75 .content(new StringContentProvider(
76 gson.toJson(new UpdateThermostatRequestModel(thermostat).withApiKey(configuration.apiKey))));
78 request.send(new BufferingResponseListener() {
80 public void onComplete(@Nullable Result result) {
82 logger.trace("onComplete {}", result);
83 if (result.isFailed()) {
84 logger.warn("updateThermostat failed {}", thermostat);
86 SimpleResponseModel responseModel = gson.fromJson(getContentAsString(), SimpleResponseModel.class);
87 if (responseModel == null) {
88 logger.warn("updateThermostat failed with empty result {}", thermostat);
89 } else if (responseModel.errorCode != 0) {
90 logger.warn("updateThermostat failed with errorCode {} {}", responseModel.errorCode,