]> git.basschouten.com Git - openhab-addons.git/blob
52f0e398b259063fa01f8e66ca63e1262511ad0c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.lang.reflect.Field;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 import com.google.gson.Gson;
23 import com.google.gson.TypeAdapter;
24 import com.google.gson.TypeAdapterFactory;
25 import com.google.gson.reflect.TypeToken;
26 import com.google.gson.stream.JsonReader;
27 import com.google.gson.stream.JsonWriter;
28
29 /**
30  * This a Gson type adapter factory.
31  *
32  * It will create a type adapter for every class derived from {@link BaseChannelConfiguration} and ensures,
33  * that abbreviated names are replaces with their long versions during the read.
34  *
35  * In elements, whose name end in'_topic' '~' replacement is performed.
36  *
37  * The adapters also handle {@link BaseChannelConfiguration.Device}
38  *
39  * @author Jochen Klein - Initial contribution
40  */
41 @NonNullByDefault
42 public class ChannelConfigurationTypeAdapterFactory implements TypeAdapterFactory {
43
44     @Override
45     @Nullable
46     public <T> TypeAdapter<T> create(@Nullable Gson gson, @Nullable TypeToken<T> type) {
47         if (gson == null || type == null) {
48             return null;
49         }
50         if (BaseChannelConfiguration.class.isAssignableFrom(type.getRawType())) {
51             return createHAConfig(gson, type);
52         }
53         if (BaseChannelConfiguration.Device.class.isAssignableFrom(type.getRawType())) {
54             return createHADevice(gson, type);
55         }
56         return null;
57     }
58
59     /**
60      * Handle {@link BaseChannelConfiguration}
61      *
62      * @param gson
63      * @param type
64      * @return
65      */
66     private <T> TypeAdapter<T> createHAConfig(Gson gson, TypeToken<T> type) {
67         /* The delegate is the 'default' adapter */
68         final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
69
70         return new TypeAdapter<T>() {
71             @Override
72             public T read(@Nullable JsonReader in) throws IOException {
73                 if (in == null) {
74                     return null;
75                 }
76                 /* read the object using the default adapter, but translate the names in the reader */
77                 T result = delegate.read(MappingJsonReader.getConfigMapper(in));
78                 /* do the '~' expansion afterwards */
79                 expandTidleInTopics(BaseChannelConfiguration.class.cast(result));
80                 return result;
81             }
82
83             @Override
84             public void write(@Nullable JsonWriter out, T value) throws IOException {
85                 delegate.write(out, value);
86             }
87         };
88     }
89
90     private <T> TypeAdapter<T> createHADevice(Gson gson, TypeToken<T> type) {
91         /* The delegate is the 'default' adapter */
92         final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
93
94         return new TypeAdapter<T>() {
95             @Override
96             public T read(@Nullable JsonReader in) throws IOException {
97                 if (in == null) {
98                     return null;
99                 }
100                 /* read the object using the default adapter, but translate the names in the reader */
101                 T result = delegate.read(MappingJsonReader.getDeviceMapper(in));
102                 return result;
103             }
104
105             @Override
106             public void write(@Nullable JsonWriter out, T value) throws IOException {
107                 delegate.write(out, value);
108             }
109         };
110     }
111
112     private void expandTidleInTopics(BaseChannelConfiguration config) {
113         Class<?> type = config.getClass();
114
115         String tilde = config.tilde;
116
117         while (type != Object.class) {
118             Field[] fields = type.getDeclaredFields();
119
120             for (Field field : fields) {
121                 if (String.class.isAssignableFrom(field.getType()) && field.getName().endsWith("_topic")) {
122                     field.setAccessible(true);
123
124                     try {
125                         final String oldValue = (String) field.get(config);
126
127                         String newValue = oldValue;
128                         if (StringUtils.isNotBlank(oldValue)) {
129                             if (oldValue.charAt(0) == '~') {
130                                 newValue = tilde + oldValue.substring(1);
131                             } else if (oldValue.charAt(oldValue.length() - 1) == '~') {
132                                 newValue = oldValue.substring(0, oldValue.length() - 1) + tilde;
133                             }
134                         }
135
136                         field.set(config, newValue);
137                     } catch (IllegalArgumentException e) {
138                         throw new RuntimeException(e);
139                     } catch (IllegalAccessException e) {
140                         throw new RuntimeException(e);
141                     }
142                 }
143             }
144
145             type = type.getSuperclass();
146         }
147     }
148 }