]> git.basschouten.com Git - openhab-addons.git/blob
951d3eb66fe88493e9d8a9005fe9c0db1877d9e8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.boschshc.internal.devices;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.*;
16
17 import java.util.List;
18
19 import javax.measure.quantity.Energy;
20 import javax.measure.quantity.Power;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
24 import org.openhab.binding.boschshc.internal.services.powermeter.PowerMeterService;
25 import org.openhab.binding.boschshc.internal.services.powermeter.dto.PowerMeterServiceState;
26 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchService;
27 import org.openhab.binding.boschshc.internal.services.powerswitch.PowerSwitchState;
28 import org.openhab.binding.boschshc.internal.services.powerswitch.dto.PowerSwitchServiceState;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.State;
36
37 /**
38  * Abstract handler implementation for devices with power switches and energy monitoring.
39  * <p>
40  * This implementation provides the functionality to
41  * <ul>
42  * <li>Switch the device on and off using the <code>PowerSwitch</code> service</li>
43  * <li>Measuring the current power consumption and the overall energy consumption using the <code>PowerMeter</code>
44  * service</li>
45  * </ul>
46  *
47  * @author David Pace - Initial contribution (extracted from LightControlHandler)
48  */
49 @NonNullByDefault
50 public abstract class AbstractPowerSwitchHandler extends BoschSHCDeviceHandler {
51
52     /**
53      * Service for switching the device on and off
54      */
55     private final PowerSwitchService powerSwitchService;
56
57     protected AbstractPowerSwitchHandler(Thing thing) {
58         super(thing);
59         this.powerSwitchService = new PowerSwitchService();
60     }
61
62     @Override
63     protected void initializeServices() throws BoschSHCException {
64         super.initializeServices();
65
66         this.registerService(this.powerSwitchService, this::updateChannels, List.of(CHANNEL_POWER_SWITCH), true);
67         this.createService(PowerMeterService::new, this::updateChannels,
68                 List.of(CHANNEL_POWER_CONSUMPTION, CHANNEL_ENERGY_CONSUMPTION), true);
69     }
70
71     @Override
72     public void handleCommand(ChannelUID channelUID, Command command) {
73         super.handleCommand(channelUID, command);
74
75         switch (channelUID.getId()) {
76             case CHANNEL_POWER_SWITCH:
77                 if (command instanceof OnOffType onOffCommand) {
78                     updatePowerSwitchState(onOffCommand);
79                 }
80                 break;
81         }
82     }
83
84     /**
85      * Updates the channels which are linked to the {@link PowerMeterService} of the device.
86      *
87      * @param state Current state of {@link PowerMeterService}.
88      */
89     private void updateChannels(PowerMeterServiceState state) {
90         super.updateState(CHANNEL_POWER_CONSUMPTION, new QuantityType<Power>(state.powerConsumption, Units.WATT));
91         super.updateState(CHANNEL_ENERGY_CONSUMPTION,
92                 new QuantityType<Energy>(state.energyConsumption, Units.WATT_HOUR));
93     }
94
95     /**
96      * Updates the channels which are linked to the {@link PowerSwitchService} of the device.
97      *
98      * @param state Current state of {@link PowerSwitchService}.
99      */
100     private void updateChannels(PowerSwitchServiceState state) {
101         State powerState = OnOffType.from(state.switchState.toString());
102         super.updateState(CHANNEL_POWER_SWITCH, powerState);
103     }
104
105     private void updatePowerSwitchState(OnOffType command) {
106         PowerSwitchServiceState state = new PowerSwitchServiceState();
107         state.switchState = PowerSwitchState.valueOf(command.toFullString());
108         this.updateServiceState(this.powerSwitchService, state);
109     }
110 }