]> git.basschouten.com Git - openhab-addons.git/blob
286b6d31115ce764b68fbf5a5cf0c4f3751e7cd1
[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.mqtt.homeassistant.internal;
14
15 import java.io.IOException;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.mqtt.generic.tools.JsonReaderDelegate;
21
22 import com.google.gson.stream.JsonReader;
23
24 /**
25  * JsonReader which will replace specific names.
26  *
27  * @author Jochen Klein - Initial contribution
28  */
29 @NonNullByDefault
30 public class MappingJsonReader extends JsonReaderDelegate {
31
32     private static final Map<String, String> ABBREVIATIONS = new HashMap<>();
33     private static final Map<String, String> DEVICE_ABBREVIATIONS = new HashMap<>();
34
35     static {
36         ABBREVIATIONS.put("act_t", "action_topic");
37         ABBREVIATIONS.put("act_tpl", "action_template");
38         ABBREVIATIONS.put("atype", "automation_type");
39         ABBREVIATIONS.put("aux_cmd_t", "aux_command_topic");
40         ABBREVIATIONS.put("aux_stat_tpl", "aux_state_template");
41         ABBREVIATIONS.put("aux_stat_t", "aux_state_topic");
42         ABBREVIATIONS.put("avty", "availability");
43         ABBREVIATIONS.put("avty_t", "availability_topic");
44         ABBREVIATIONS.put("away_mode_cmd_t", "away_mode_command_topic");
45         ABBREVIATIONS.put("away_mode_stat_tpl", "away_mode_state_template");
46         ABBREVIATIONS.put("away_mode_stat_t", "away_mode_state_topic");
47         ABBREVIATIONS.put("b_tpl", "blue_template");
48         ABBREVIATIONS.put("bri_cmd_t", "brightness_command_topic");
49         ABBREVIATIONS.put("bri_scl", "brightness_scale");
50         ABBREVIATIONS.put("bri_stat_t", "brightness_state_topic");
51         ABBREVIATIONS.put("bri_tpl", "brightness_template");
52         ABBREVIATIONS.put("bri_val_tpl", "brightness_value_template");
53         ABBREVIATIONS.put("clr_temp_cmd_tpl", "color_temp_command_template");
54         ABBREVIATIONS.put("bat_lev_t", "battery_level_topic");
55         ABBREVIATIONS.put("bat_lev_tpl", "battery_level_template");
56         ABBREVIATIONS.put("chrg_t", "charging_topic");
57         ABBREVIATIONS.put("chrg_tpl", "charging_template");
58         ABBREVIATIONS.put("clr_temp_cmd_t", "color_temp_command_topic");
59         ABBREVIATIONS.put("clr_temp_stat_t", "color_temp_state_topic");
60         ABBREVIATIONS.put("clr_temp_tpl", "color_temp_template");
61         ABBREVIATIONS.put("clr_temp_val_tpl", "color_temp_value_template");
62         ABBREVIATIONS.put("cln_t", "cleaning_topic");
63         ABBREVIATIONS.put("cln_tpl", "cleaning_template");
64         ABBREVIATIONS.put("cmd_off_tpl", "command_off_template");
65         ABBREVIATIONS.put("cmd_on_tpl", "command_on_template");
66         ABBREVIATIONS.put("cmd_t", "command_topic");
67         ABBREVIATIONS.put("cmd_tpl", "command_template");
68         ABBREVIATIONS.put("cod_arm_req", "code_arm_required");
69         ABBREVIATIONS.put("cod_dis_req", "code_disarm_required");
70         ABBREVIATIONS.put("curr_temp_t", "current_temperature_topic");
71         ABBREVIATIONS.put("curr_temp_tpl", "current_temperature_template");
72         ABBREVIATIONS.put("dev", "device");
73         ABBREVIATIONS.put("dev_cla", "device_class");
74         ABBREVIATIONS.put("dock_t", "docked_topic");
75         ABBREVIATIONS.put("dock_tpl", "docked_template");
76         ABBREVIATIONS.put("err_t", "error_topic");
77         ABBREVIATIONS.put("err_tpl", "error_template");
78         ABBREVIATIONS.put("fanspd_t", "fan_speed_topic");
79         ABBREVIATIONS.put("fanspd_tpl", "fan_speed_template");
80         ABBREVIATIONS.put("fanspd_lst", "fan_speed_list");
81         ABBREVIATIONS.put("flsh_tlng", "flash_time_long");
82         ABBREVIATIONS.put("flsh_tsht", "flash_time_short");
83         ABBREVIATIONS.put("fx_cmd_t", "effect_command_topic");
84         ABBREVIATIONS.put("fx_list", "effect_list");
85         ABBREVIATIONS.put("fx_stat_t", "effect_state_topic");
86         ABBREVIATIONS.put("fx_tpl", "effect_template");
87         ABBREVIATIONS.put("fx_val_tpl", "effect_value_template");
88         ABBREVIATIONS.put("exp_aft", "expire_after");
89         ABBREVIATIONS.put("fan_mode_cmd_t", "fan_mode_command_topic");
90         ABBREVIATIONS.put("fan_mode_stat_tpl", "fan_mode_state_template");
91         ABBREVIATIONS.put("fan_mode_stat_t", "fan_mode_state_topic");
92         ABBREVIATIONS.put("frc_upd", "force_update");
93         ABBREVIATIONS.put("g_tpl", "green_template");
94         ABBREVIATIONS.put("hold_cmd_t", "hold_command_topic");
95         ABBREVIATIONS.put("hold_stat_tpl", "hold_state_template");
96         ABBREVIATIONS.put("hold_stat_t", "hold_state_topic");
97         ABBREVIATIONS.put("hs_cmd_t", "hs_command_topic");
98         ABBREVIATIONS.put("hs_stat_t", "hs_state_topic");
99         ABBREVIATIONS.put("hs_val_tpl", "hs_value_template");
100         ABBREVIATIONS.put("ic", "icon");
101         ABBREVIATIONS.put("init", "initial");
102         ABBREVIATIONS.put("json_attr", "json_attributes");
103         ABBREVIATIONS.put("json_attr_t", "json_attributes_topic");
104         ABBREVIATIONS.put("json_attr_tpl", "json_attributes_template");
105         ABBREVIATIONS.put("max_mirs", "max_mireds");
106         ABBREVIATIONS.put("min_mirs", "min_mireds");
107         ABBREVIATIONS.put("max_temp", "max_temp");
108         ABBREVIATIONS.put("min_temp", "min_temp");
109         ABBREVIATIONS.put("mode_cmd_t", "mode_command_topic");
110         ABBREVIATIONS.put("mode_stat_tpl", "mode_state_template");
111         ABBREVIATIONS.put("mode_stat_t", "mode_state_topic");
112         ABBREVIATIONS.put("name", "name");
113         ABBREVIATIONS.put("off_dly", "off_delay");
114         ABBREVIATIONS.put("on_cmd_type", "on_command_type");
115         ABBREVIATIONS.put("opt", "optimistic");
116         ABBREVIATIONS.put("osc_cmd_t", "oscillation_command_topic");
117         ABBREVIATIONS.put("osc_stat_t", "oscillation_state_topic");
118         ABBREVIATIONS.put("osc_val_tpl", "oscillation_value_template");
119         ABBREVIATIONS.put("pl", "payload");
120         ABBREVIATIONS.put("pl_arm_away", "payload_arm_away");
121         ABBREVIATIONS.put("pl_arm_home", "payload_arm_home");
122         ABBREVIATIONS.put("pl_arm_custom_b", "payload_arm_custom_bypass");
123         ABBREVIATIONS.put("pl_arm_nite", "payload_arm_night");
124         ABBREVIATIONS.put("pl_avail", "payload_available");
125         ABBREVIATIONS.put("pl_cln_sp", "payload_clean_spot");
126         ABBREVIATIONS.put("pl_cls", "payload_close");
127         ABBREVIATIONS.put("pl_disarm", "payload_disarm");
128         ABBREVIATIONS.put("pl_hi_spd", "payload_high_speed");
129         ABBREVIATIONS.put("pl_home", "payload_home");
130         ABBREVIATIONS.put("pl_lock", "payload_lock");
131         ABBREVIATIONS.put("pl_loc", "payload_locate");
132         ABBREVIATIONS.put("pl_lo_spd", "payload_low_speed");
133         ABBREVIATIONS.put("pl_med_spd", "payload_medium_speed");
134         ABBREVIATIONS.put("pl_not_avail", "payload_not_available");
135         ABBREVIATIONS.put("pl_not_home", "payload_not_home");
136         ABBREVIATIONS.put("pl_off", "payload_off");
137         ABBREVIATIONS.put("pl_off_spd", "payload_off_speed");
138         ABBREVIATIONS.put("pl_on", "payload_on");
139         ABBREVIATIONS.put("pl_open", "payload_open");
140         ABBREVIATIONS.put("pl_osc_off", "payload_oscillation_off");
141         ABBREVIATIONS.put("pl_osc_on", "payload_oscillation_on");
142         ABBREVIATIONS.put("pl_paus", "payload_pause");
143         ABBREVIATIONS.put("pl_stop", "payload_stop");
144         ABBREVIATIONS.put("pl_strt", "payload_start");
145         ABBREVIATIONS.put("pl_stpa", "payload_start_pause");
146         ABBREVIATIONS.put("pl_ret", "payload_return_to_base");
147         ABBREVIATIONS.put("pl_toff", "payload_turn_off");
148         ABBREVIATIONS.put("pl_ton", "payload_turn_on");
149         ABBREVIATIONS.put("pl_unlk", "payload_unlock");
150         ABBREVIATIONS.put("pos_clsd", "position_closed");
151         ABBREVIATIONS.put("pos_open", "position_open");
152         ABBREVIATIONS.put("pow_cmd_t", "power_command_topic");
153         ABBREVIATIONS.put("pow_stat_t", "power_state_topic");
154         ABBREVIATIONS.put("pow_stat_tpl", "power_state_template");
155         ABBREVIATIONS.put("r_tpl", "red_template");
156         ABBREVIATIONS.put("ret", "retain");
157         ABBREVIATIONS.put("rgb_cmd_tpl", "rgb_command_template");
158         ABBREVIATIONS.put("rgb_cmd_t", "rgb_command_topic");
159         ABBREVIATIONS.put("rgb_stat_t", "rgb_state_topic");
160         ABBREVIATIONS.put("rgb_val_tpl", "rgb_value_template");
161         ABBREVIATIONS.put("send_cmd_t", "send_command_topic");
162         ABBREVIATIONS.put("send_if_off", "send_if_off");
163         ABBREVIATIONS.put("set_fan_spd_t", "set_fan_speed_topic");
164         ABBREVIATIONS.put("set_pos_tpl", "set_position_template");
165         ABBREVIATIONS.put("set_pos_t", "set_position_topic");
166         ABBREVIATIONS.put("pos_t", "position_topic");
167         ABBREVIATIONS.put("spd_cmd_t", "speed_command_topic");
168         ABBREVIATIONS.put("spd_stat_t", "speed_state_topic");
169         ABBREVIATIONS.put("spd_val_tpl", "speed_value_template");
170         ABBREVIATIONS.put("spds", "speeds");
171         ABBREVIATIONS.put("src_type", "source_type");
172         ABBREVIATIONS.put("stat_clsd", "state_closed");
173         ABBREVIATIONS.put("stat_closing", "state_closing");
174         ABBREVIATIONS.put("stat_off", "state_off");
175         ABBREVIATIONS.put("stat_on", "state_on");
176         ABBREVIATIONS.put("stat_open", "state_open");
177         ABBREVIATIONS.put("stat_opening", "state_opening");
178         ABBREVIATIONS.put("stat_locked", "state_locked");
179         ABBREVIATIONS.put("stat_unlocked", "state_unlocked");
180         ABBREVIATIONS.put("stat_t", "state_topic");
181         ABBREVIATIONS.put("stat_tpl", "state_template");
182         ABBREVIATIONS.put("stat_val_tpl", "state_value_template");
183         ABBREVIATIONS.put("stype", "subtype");
184         ABBREVIATIONS.put("sup_feat", "supported_features");
185         ABBREVIATIONS.put("swing_mode_cmd_t", "swing_mode_command_topic");
186         ABBREVIATIONS.put("swing_mode_stat_tpl", "swing_mode_state_template");
187         ABBREVIATIONS.put("swing_mode_stat_t", "swing_mode_state_topic");
188         ABBREVIATIONS.put("temp_cmd_t", "temperature_command_topic");
189         ABBREVIATIONS.put("temp_hi_cmd_t", "temperature_high_command_topic");
190         ABBREVIATIONS.put("temp_hi_stat_tpl", "temperature_high_state_template");
191         ABBREVIATIONS.put("temp_hi_stat_t", "temperature_high_state_topic");
192         ABBREVIATIONS.put("temp_lo_cmd_t", "temperature_low_command_topic");
193         ABBREVIATIONS.put("temp_lo_stat_tpl", "temperature_low_state_template");
194         ABBREVIATIONS.put("temp_lo_stat_t", "temp_lo_stat_t");
195         ABBREVIATIONS.put("temp_stat_tpl", "temperature_state_template");
196         ABBREVIATIONS.put("temp_stat_t", "temperature_state_topic");
197         ABBREVIATIONS.put("temp_unit", "temperature_unit");
198         ABBREVIATIONS.put("tilt_clsd_val", "tilt_closed_value");
199         ABBREVIATIONS.put("tilt_cmd_t", "tilt_command_topic");
200         ABBREVIATIONS.put("tilt_inv_stat", "tilt_invert_state");
201         ABBREVIATIONS.put("tilt_max", "tilt_max");
202         ABBREVIATIONS.put("tilt_min", "tilt_min");
203         ABBREVIATIONS.put("tilt_opnd_val", "tilt_opened_value");
204         ABBREVIATIONS.put("tilt_opt", "tilt_optimistic");
205         ABBREVIATIONS.put("tilt_status_t", "tilt_status_topic");
206         ABBREVIATIONS.put("tilt_status_tpl", "tilt_status_template");
207         ABBREVIATIONS.put("t", "topic");
208         ABBREVIATIONS.put("uniq_id", "unique_id");
209         ABBREVIATIONS.put("unit_of_meas", "unit_of_measurement");
210         ABBREVIATIONS.put("val_tpl", "value_template");
211         ABBREVIATIONS.put("whit_val_cmd_t", "white_value_command_topic");
212         ABBREVIATIONS.put("whit_val_scl", "white_value_scale");
213         ABBREVIATIONS.put("whit_val_stat_t", "white_value_state_topic");
214         ABBREVIATIONS.put("whit_val_tpl", "white_value_template");
215         ABBREVIATIONS.put("xy_cmd_t", "xy_command_topic");
216         ABBREVIATIONS.put("xy_stat_t", "xy_state_topic");
217         ABBREVIATIONS.put("xy_val_tpl", "xy_value_template");
218
219         DEVICE_ABBREVIATIONS.put("cns", "connections");
220         DEVICE_ABBREVIATIONS.put("ids", "identifiers");
221         DEVICE_ABBREVIATIONS.put("name", "name");
222         DEVICE_ABBREVIATIONS.put("mf", "manufacturer");
223         DEVICE_ABBREVIATIONS.put("mdl", "model");
224         DEVICE_ABBREVIATIONS.put("sw", "sw_version");
225     }
226
227     private final Map<String, String> mapping;
228
229     /**
230      *
231      * @param delegate
232      * @return return a JsonReader which replaces all config abbreviations
233      */
234     public static MappingJsonReader getConfigMapper(JsonReader delegate) {
235         return new MappingJsonReader(JsonReaderDelegate.getDelegate(delegate), ABBREVIATIONS);
236     }
237
238     /**
239      *
240      * @param delegate
241      * @return return a JsonReader which replaces all config.device abbreviations
242      */
243     public static MappingJsonReader getDeviceMapper(JsonReader delegate) {
244         return new MappingJsonReader(JsonReaderDelegate.getDelegate(delegate), DEVICE_ABBREVIATIONS);
245     }
246
247     private MappingJsonReader(JsonReader delegate, Map<String, String> mapping) {
248         super(delegate);
249         this.mapping = mapping;
250     }
251
252     @Override
253     public String nextName() throws IOException {
254         String name = super.nextName();
255         return mapping.getOrDefault(name, name);
256     }
257 }