]> git.basschouten.com Git - openhab-addons.git/blob
7beb926a0f2157e9154cbf18a3bc7854bcb7730e
[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.ojelectronics.internal.services;
14
15 import java.util.List;
16
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;
34
35 import com.google.gson.Gson;
36
37 /**
38  * Handles the update of the devices of a session
39  *
40  * @author Christian Kittel - Initial Contribution
41  */
42 @NonNullByDefault
43 public final class UpdateService {
44
45     private final Gson gson = OJGSonBuilder.getGSon();
46     private final Logger logger = LoggerFactory.getLogger(UpdateService.class);
47
48     private final String sessionId;
49     private final HttpClient httpClient;
50     private final OJElectronicsBridgeConfiguration configuration;
51
52     public UpdateService(OJElectronicsBridgeConfiguration configuration, HttpClient httpClient, String sessionId) {
53         this.configuration = configuration;
54         this.httpClient = httpClient;
55         this.sessionId = sessionId;
56     }
57
58     /**
59      * Sends all changes of all {@link ThermostatHandler} to the API
60      *
61      * @param things
62      */
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);
67     }
68
69     private void updateThermostat(@Nullable Thermostat thermostat) {
70         if (thermostat == null) {
71             return;
72         }
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))));
77
78         request.send(new BufferingResponseListener() {
79             @Override
80             public void onComplete(@Nullable Result result) {
81                 if (result != null) {
82                     logger.trace("onComplete {}", result);
83                     if (result.isFailed()) {
84                         logger.warn("updateThermostat failed {}", thermostat);
85                     }
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,
91                                 thermostat);
92                     }
93                 }
94             }
95         });
96     }
97 }