]> git.basschouten.com Git - openhab-addons.git/blob
b3debd40c86e87c401dd8c56ed50e3d66c22e071
[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.binding.hpprinter.internal;
14
15 import static org.openhab.binding.hpprinter.internal.HPPrinterBindingConstants.CGROUP_STATUS;
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         scheduler.submit(() -> initializeScheduled());
69     }
70
71     /**
72      * Scheduled initialization task which will be executed on a separate thread
73      */
74     private void initializeScheduled() {
75         final HPPrinterConfiguration config = getConfigAs(HPPrinterConfiguration.class);
76
77         if (!"".equals(config.ipAddress)) {
78             final HPPrinterBinder localBinder = binder = new HPPrinterBinder(this, httpClient, scheduler, config);
79
80             localBinder.dynamicallyAddChannels(thing.getUID());
81             localBinder.retrieveProperties();
82             localBinder.open();
83         } else {
84             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "You must set an IP Address");
85         }
86     }
87
88     @Override
89     public void dispose() {
90         final HPPrinterBinder localBinder = binder;
91         if (localBinder != null) {
92             localBinder.close();
93             binder = null;
94         }
95     }
96
97     protected Boolean areStatusChannelsLinked(final String[] channels) {
98         for (int i = 0; i < channels.length; i++) {
99             if (isLinked(new ChannelUID(thing.getUID(), CGROUP_STATUS, channels[i]))) {
100                 return true;
101             }
102         }
103         return false;
104     }
105
106     @Override
107     protected void updateStatus(final ThingStatus status) {
108         super.updateStatus(status);
109     }
110
111     @Override
112     public void channelLinked(ChannelUID channelUID) {
113         final HPPrinterBinder localBinder = binder;
114         if (localBinder != null) {
115             localBinder.channelsChanged();
116         }
117     }
118
119     @Override
120     public void channelUnlinked(ChannelUID channelUID) {
121         final HPPrinterBinder localBinder = binder;
122         if (localBinder != null) {
123             localBinder.channelsChanged();
124         }
125     }
126
127     @Override
128     protected void updateStatus(final ThingStatus status, final ThingStatusDetail thingStatusDetail,
129             @Nullable final String message) {
130         super.updateStatus(status, thingStatusDetail, message);
131     }
132
133     public void updateState(final String group, final String channel, final State state) {
134         super.updateState(new ChannelUID(thing.getUID(), group, channel), state);
135     }
136
137     public void binderAddChannels(final List<Channel> channels) {
138         final List<Channel> thingChannels = new ArrayList<>(getThing().getChannels());
139
140         for (final Channel channel : channels) {
141             addOrUpdateChannel(channel, thingChannels);
142         }
143
144         updateThing(editThing().withChannels(thingChannels).build());
145     }
146
147     @Override
148     protected ThingBuilder editThing() {
149         return super.editThing();
150     }
151
152     @Override
153     protected @Nullable ThingHandlerCallback getCallback() {
154         return super.getCallback();
155     }
156
157     @Override
158     protected void updateProperties(final @Nullable Map<String, String> properties) {
159         super.updateProperties(properties);
160     }
161
162     private static void addOrUpdateChannel(final Channel newChannel, final List<Channel> thingChannels) {
163         removeChannelByUID(thingChannels, newChannel.getUID());
164         thingChannels.add(newChannel);
165     }
166
167     private static void removeChannelByUID(final List<Channel> thingChannels, final ChannelUID channelUIDtoRemove) {
168         final Predicate<Channel> channelPredicate = c -> c.getUID().getId().equals(channelUIDtoRemove.getId());
169         thingChannels.removeIf(channelPredicate);
170     }
171 }