]> git.basschouten.com Git - openhab-addons.git/blob
91ba3df4657e3faa15f2fdf63dbb9447cbc56f9c
[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.hpprinter.internal;
14
15 import static org.openhab.binding.hpprinter.internal.HPPrinterBindingConstants.*;
16
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.function.Predicate;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.openhab.core.thing.Channel;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.binding.BaseThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerCallback;
32 import org.openhab.core.thing.binding.builder.ThingBuilder;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.State;
35
36 /**
37  * The {@link HPPrinterHandler} is responsible for handling commands, which are
38  * sent to one of the channels.
39  *
40  * @author Stewart Cossey - Initial contribution
41  */
42 @NonNullByDefault
43 public class HPPrinterHandler extends BaseThingHandler {
44     private final HttpClient httpClient;
45     private @Nullable HPPrinterBinder binder;
46
47     public HPPrinterHandler(final Thing thing, final HttpClient httpClient) {
48         super(thing);
49         this.httpClient = httpClient;
50     }
51
52     @Override
53     public void thingUpdated(final Thing thing) {
54         super.thingUpdated(thing);
55
56         final HPPrinterBinder printerBinder = this.binder;
57         if (printerBinder != null) {
58             printerBinder.dynamicallyAddChannels(thing.getUID());
59         }
60     }
61
62     @Override
63     public void handleCommand(final ChannelUID channelUID, final Command command) {
64     }
65
66     @Override
67     public void initialize() {
68         final HPPrinterConfiguration config = getConfigAs(HPPrinterConfiguration.class);
69
70         if (!"".equals(config.ipAddress)) {
71             final HPPrinterBinder localBinder = binder = new HPPrinterBinder(this, httpClient, scheduler, config);
72
73             localBinder.dynamicallyAddChannels(thing.getUID());
74             localBinder.retrieveProperties();
75             localBinder.open();
76         } else {
77             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "You must set an IP Address");
78         }
79     }
80
81     @Override
82     public void dispose() {
83         final HPPrinterBinder localBinder = binder;
84         if (localBinder != null) {
85             localBinder.close();
86             binder = null;
87         }
88     }
89
90     protected Boolean areStatusChannelsLinked(final String[] channels) {
91         for (int i = 0; i < channels.length; i++) {
92             if (isLinked(new ChannelUID(thing.getUID(), CGROUP_STATUS, channels[i]))) {
93                 return true;
94             }
95         }
96         return false;
97     }
98
99     protected void updateStatus(final ThingStatus status) {
100         super.updateStatus(status);
101     }
102
103     @Override
104     public void channelLinked(ChannelUID channelUID) {
105         final HPPrinterBinder localBinder = binder;
106         if (localBinder != null) {
107             localBinder.channelsChanged();
108         }
109     }
110
111     @Override
112     public void channelUnlinked(ChannelUID channelUID) {
113         final HPPrinterBinder localBinder = binder;
114         if (localBinder != null) {
115             localBinder.channelsChanged();
116         }
117     }
118
119     protected void updateStatus(final ThingStatus status, final ThingStatusDetail thingStatusDetail,
120             @Nullable final String message) {
121         super.updateStatus(status, thingStatusDetail, message);
122     }
123
124     public void updateState(final String group, final String channel, final State state) {
125         super.updateState(new ChannelUID(thing.getUID(), group, channel), state);
126     }
127
128     public void binderAddChannels(final List<Channel> channels) {
129         final List<Channel> thingChannels = new ArrayList<>(getThing().getChannels());
130
131         for (final Channel channel : channels) {
132             addOrUpdateChannel(channel, thingChannels);
133         }
134
135         updateThing(editThing().withChannels(thingChannels).build());
136     }
137
138     protected ThingBuilder editThing() {
139         return super.editThing();
140     }
141
142     protected @Nullable ThingHandlerCallback getCallback() {
143         return super.getCallback();
144     }
145
146     protected void updateProperties(final Map<String, String> properties) {
147         super.updateProperties(properties);
148     }
149
150     private static void addOrUpdateChannel(final Channel newChannel, final List<Channel> thingChannels) {
151         removeChannelByUID(thingChannels, newChannel.getUID());
152         thingChannels.add(newChannel);
153     }
154
155     private static void removeChannelByUID(final List<Channel> thingChannels, final ChannelUID channelUIDtoRemove) {
156         final Predicate<Channel> channelPredicate = c -> c.getUID().getId().equals(channelUIDtoRemove.getId());
157         thingChannels.removeIf(channelPredicate);
158     }
159 }