]> git.basschouten.com Git - openhab-addons.git/blob
7b2e36ebb3c181a364d391f792f6f81292cb6006
[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.millheat.internal.handler;
14
15 import java.util.Optional;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.millheat.internal.model.MillheatModel;
19 import org.openhab.core.thing.Bridge;
20 import org.openhab.core.thing.Channel;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.binding.BaseThingHandler;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Base class for heater and room handlers
31  *
32  * @author Arne Seime - Initial contribution
33  */
34 @NonNullByDefault
35 public abstract class MillheatBaseThingHandler extends BaseThingHandler {
36     private final Logger logger = LoggerFactory.getLogger(MillheatBaseThingHandler.class);
37
38     public MillheatBaseThingHandler(final Thing thing) {
39         super(thing);
40     }
41
42     public void updateState(final MillheatModel model) {
43         for (final Channel channel : getThing().getChannels()) {
44             handleCommand(channel.getUID(), RefreshType.REFRESH, model);
45         }
46     }
47
48     protected MillheatModel getMillheatModel() {
49         final Optional<MillheatAccountHandler> accountHandler = getAccountHandler();
50         if (accountHandler.isPresent()) {
51             return accountHandler.get().getModel();
52         } else {
53             logger.warn(
54                     "Thing {} cannot exist without a bridge and account handler - returning empty model. No heaters or rooms will be found",
55                     getThing().getUID());
56             return new MillheatModel(0);
57         }
58     }
59
60     protected Optional<MillheatAccountHandler> getAccountHandler() {
61         final Bridge bridge = getBridge();
62         if (bridge != null) {
63             MillheatAccountHandler handler = (MillheatAccountHandler) bridge.getHandler();
64             if (handler != null) {
65                 return Optional.of(handler);
66             }
67         }
68         return Optional.empty();
69     }
70
71     protected abstract void handleCommand(ChannelUID uid, Command command, MillheatModel model);
72 }