2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.hpprinter.internal;
15 import static org.openhab.binding.hpprinter.internal.HPPrinterBindingConstants.CGROUP_STATUS;
17 import java.util.ArrayList;
18 import java.util.List;
20 import java.util.function.Predicate;
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;
37 * The {@link HPPrinterHandler} is responsible for handling commands, which are
38 * sent to one of the channels.
40 * @author Stewart Cossey - Initial contribution
43 public class HPPrinterHandler extends BaseThingHandler {
44 private final HttpClient httpClient;
45 private @Nullable HPPrinterBinder binder;
47 public HPPrinterHandler(final Thing thing, final HttpClient httpClient) {
49 this.httpClient = httpClient;
53 public void thingUpdated(final Thing thing) {
54 super.thingUpdated(thing);
56 final HPPrinterBinder printerBinder = this.binder;
57 if (printerBinder != null) {
58 printerBinder.dynamicallyAddChannels(thing.getUID());
63 public void handleCommand(final ChannelUID channelUID, final Command command) {
67 public void initialize() {
68 scheduler.submit(() -> initializeScheduled());
72 * Scheduled initialization task which will be executed on a separate thread
74 private void initializeScheduled() {
75 final HPPrinterConfiguration config = getConfigAs(HPPrinterConfiguration.class);
77 if (!"".equals(config.ipAddress)) {
78 final HPPrinterBinder localBinder = binder = new HPPrinterBinder(this, httpClient, scheduler, config);
80 localBinder.dynamicallyAddChannels(thing.getUID());
81 localBinder.retrieveProperties();
84 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "You must set an IP Address");
89 public void dispose() {
90 final HPPrinterBinder localBinder = binder;
91 if (localBinder != null) {
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]))) {
107 protected void updateStatus(final ThingStatus status) {
108 super.updateStatus(status);
112 public void channelLinked(ChannelUID channelUID) {
113 final HPPrinterBinder localBinder = binder;
114 if (localBinder != null) {
115 localBinder.channelsChanged();
120 public void channelUnlinked(ChannelUID channelUID) {
121 final HPPrinterBinder localBinder = binder;
122 if (localBinder != null) {
123 localBinder.channelsChanged();
128 protected void updateStatus(final ThingStatus status, final ThingStatusDetail thingStatusDetail,
129 @Nullable final String message) {
130 super.updateStatus(status, thingStatusDetail, message);
133 public void updateState(final String group, final String channel, final State state) {
134 super.updateState(new ChannelUID(thing.getUID(), group, channel), state);
137 public void binderAddChannels(final List<Channel> channels) {
138 final List<Channel> thingChannels = new ArrayList<>(getThing().getChannels());
140 for (final Channel channel : channels) {
141 addOrUpdateChannel(channel, thingChannels);
144 updateThing(editThing().withChannels(thingChannels).build());
148 protected ThingBuilder editThing() {
149 return super.editThing();
153 protected @Nullable ThingHandlerCallback getCallback() {
154 return super.getCallback();
158 protected void updateProperties(final @Nullable Map<String, String> properties) {
159 super.updateProperties(properties);
162 private static void addOrUpdateChannel(final Channel newChannel, final List<Channel> thingChannels) {
163 removeChannelByUID(thingChannels, newChannel.getUID());
164 thingChannels.add(newChannel);
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);