]> git.basschouten.com Git - openhab-addons.git/blob
a1ca4c901830015f373649ea5ff985f216737f7c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.iammeter.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.thing.Channel;
17 import org.openhab.core.thing.Thing;
18 import org.openhab.core.types.State;
19
20 import com.google.gson.JsonElement;
21 import com.google.gson.JsonObject;
22 import com.google.gson.JsonParser;
23
24 /**
25  * The {@link IammeterHandler} is responsible for handling commands, which are
26  * sent to one of the channels.
27  *
28  * @author Yang Bo - Initial contribution
29  */
30
31 @NonNullByDefault
32 public class IammeterHandler extends IammeterBaseHandler {
33
34     public IammeterHandler(Thing thing) {
35         super(thing);
36     }
37
38     @Override
39     protected void resolveData(String response) {
40         JsonElement iammeterDataElement = JsonParser.parseString(response);
41         JsonObject iammeterData = iammeterDataElement.getAsJsonObject();
42         String keyWord = "Data";
43         if (iammeterData.has("data") || (iammeterData.has("Data") && iammeterData.has("SN"))) {
44             if (iammeterData.has("data")) {
45                 keyWord = "data";
46             }
47             for (IammeterWEM3080Channel channelConfig : IammeterWEM3080Channel.values()) {
48                 Channel channel = getThing().getChannel(channelConfig.getId());
49                 if (channel != null) {
50                     State state = getQuantityState(
51                             iammeterData.get(keyWord).getAsJsonArray().get(channelConfig.ordinal()).toString(),
52                             channelConfig.getUnit());
53                     updateState(channel.getUID(), state);
54                 }
55             }
56         }
57     }
58 }