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