2 * Copyright (c) 2010-2021 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.*;
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 final HPPrinterConfiguration config = getConfigAs(HPPrinterConfiguration.class);
70 if (!"".equals(config.ipAddress)) {
71 final HPPrinterBinder localBinder = binder = new HPPrinterBinder(this, httpClient, scheduler, config);
73 localBinder.dynamicallyAddChannels(thing.getUID());
74 localBinder.retrieveProperties();
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "You must set an IP Address");
82 public void dispose() {
83 final HPPrinterBinder localBinder = binder;
84 if (localBinder != null) {
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]))) {
99 protected void updateStatus(final ThingStatus status) {
100 super.updateStatus(status);
104 public void channelLinked(ChannelUID channelUID) {
105 final HPPrinterBinder localBinder = binder;
106 if (localBinder != null) {
107 localBinder.channelsChanged();
112 public void channelUnlinked(ChannelUID channelUID) {
113 final HPPrinterBinder localBinder = binder;
114 if (localBinder != null) {
115 localBinder.channelsChanged();
119 protected void updateStatus(final ThingStatus status, final ThingStatusDetail thingStatusDetail,
120 @Nullable final String message) {
121 super.updateStatus(status, thingStatusDetail, message);
124 public void updateState(final String group, final String channel, final State state) {
125 super.updateState(new ChannelUID(thing.getUID(), group, channel), state);
128 public void binderAddChannels(final List<Channel> channels) {
129 final List<Channel> thingChannels = new ArrayList<>(getThing().getChannels());
131 for (final Channel channel : channels) {
132 addOrUpdateChannel(channel, thingChannels);
135 updateThing(editThing().withChannels(thingChannels).build());
138 protected ThingBuilder editThing() {
139 return super.editThing();
142 protected @Nullable ThingHandlerCallback getCallback() {
143 return super.getCallback();
146 protected void updateProperties(final Map<String, String> properties) {
147 super.updateProperties(properties);
150 private static void addOrUpdateChannel(final Channel newChannel, final List<Channel> thingChannels) {
151 removeChannelByUID(thingChannels, newChannel.getUID());
152 thingChannels.add(newChannel);
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);