]> git.basschouten.com Git - openhab-addons.git/blob
1985d9beb5f665fb37c1752a79815a35afe9b767
[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.transform.vat.internal.profile;
14
15 import static org.openhab.transform.vat.internal.VATTransformationConstants.*;
16
17 import java.math.BigDecimal;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.core.i18n.LocaleProvider;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.thing.profiles.ProfileCallback;
24 import org.openhab.core.thing.profiles.ProfileContext;
25 import org.openhab.core.thing.profiles.ProfileTypeUID;
26 import org.openhab.core.thing.profiles.StateProfile;
27 import org.openhab.core.transform.TransformationException;
28 import org.openhab.core.transform.TransformationHelper;
29 import org.openhab.core.transform.TransformationService;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.State;
32 import org.openhab.core.types.Type;
33 import org.openhab.transform.vat.internal.config.VATConfig;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Profile to offer the {@link VATTransformationService} on an ItemChannelLink.
39  *
40  * @author Jacob Laursen - Initial contribution
41  */
42 @NonNullByDefault
43 public class VATTransformationProfile implements StateProfile {
44
45     private final Logger logger = LoggerFactory.getLogger(VATTransformationProfile.class);
46
47     private final ProfileCallback callback;
48     private final TransformationService service;
49     private final LocaleProvider localeProvider;
50
51     private VATConfig configuration;
52
53     public VATTransformationProfile(final ProfileCallback callback, final TransformationService service,
54             final ProfileContext context, LocaleProvider localeProvider) {
55         this.callback = callback;
56         this.service = service;
57         this.localeProvider = localeProvider;
58         this.configuration = context.getConfiguration().as(VATConfig.class);
59     }
60
61     @Override
62     public ProfileTypeUID getProfileTypeUID() {
63         return PROFILE_TYPE_UID;
64     }
65
66     @Override
67     public void onCommandFromItem(Command command) {
68         callback.handleCommand(command);
69     }
70
71     @Override
72     public void onStateUpdateFromItem(State state) {
73     }
74
75     @Override
76     public void onCommandFromHandler(Command command) {
77         callback.sendCommand((Command) transformState(command));
78     }
79
80     @Override
81     public void onStateUpdateFromHandler(State state) {
82         callback.sendUpdate((State) transformState(state));
83     }
84
85     private Type transformState(Type state) {
86         String result = state.toFullString();
87         try {
88             result = TransformationHelper.transform(service, getVATPercentage(), "%s", result);
89         } catch (TransformationException e) {
90             logger.warn("Could not apply '{}' transformation on state '{}' with value '{}'.", PROFILE_TYPE_UID.getId(),
91                     state, getVATPercentage());
92         }
93         Type resultType = state;
94         if (result != null) {
95             if (state instanceof DecimalType) {
96                 resultType = DecimalType.valueOf(result);
97             } else if (state instanceof QuantityType) {
98                 resultType = QuantityType.valueOf(result);
99             }
100             logger.debug("Transformed '{}' into '{}'", state, resultType);
101         }
102         return resultType;
103     }
104
105     private String getVATPercentage() {
106         if (!configuration.percentage.isBlank()) {
107             return getOverriddenVAT();
108         }
109
110         String country = localeProvider.getLocale().getCountry();
111         String rate = RATES.get(country);
112         if (rate == null) {
113             logger.warn("No VAT rate for country {}", country);
114             return "0";
115         }
116         return rate;
117     }
118
119     private String getOverriddenVAT() {
120         String percentage = configuration.percentage.trim();
121         if (percentage.endsWith("%")) {
122             percentage = percentage.substring(0, percentage.length() - 1).trim();
123         }
124         try {
125             return new BigDecimal(percentage).toString();
126         } catch (NumberFormatException e) {
127             logger.warn("{} is not a valid percentage", percentage);
128             return "0";
129         }
130     }
131 }