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.api;
15 import java.util.HashMap;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.NodeList;
25 * The {@link HPStatus} is responsible for handling reading of status data.
27 * @author Stewart Cossey - Initial contribution
30 public class HPStatus {
31 public static final String ENDPOINT = "/DevMgmt/ProductStatusDyn.xml";
33 private static final Map<String, String> STATUS_MESSAGES = initializeStatus();
35 private final String printerStatus;
36 private final boolean trayEmptyOrOpen;
38 public HPStatus(Document document) {
39 NodeList nodes = document.getDocumentElement().getElementsByTagName("psdyn:Status");
41 String localPrinterStatus = "Unknown";
42 boolean localTrayEmptyOrOpen = false;
43 for (int i = 0; i < nodes.getLength(); i++) {
44 Element element = (Element) nodes.item(i);
45 String statusCategory = element.getElementsByTagName("pscat:StatusCategory").item(0).getTextContent();
46 if (!"genuineHP".equals(statusCategory) && !"trayEmpty".equals(statusCategory)) {
47 localPrinterStatus = STATUS_MESSAGES.getOrDefault(statusCategory, statusCategory);
49 if ("trayEmpty".equals(statusCategory)) {
50 localTrayEmptyOrOpen = true;
53 trayEmptyOrOpen = localTrayEmptyOrOpen;
54 printerStatus = localPrinterStatus;
57 private static Map<String, String> initializeStatus() {
58 Map<String, String> statusMap = new HashMap<>();
60 statusMap.put("processing", "Printing...");
61 statusMap.put("scanProcessing", "Scanning...");
62 statusMap.put("inPowerSave", "Power Save");
63 statusMap.put("ready", "Idle");
64 statusMap.put("initializing", "Initializing...");
65 statusMap.put("closeDoorOrCover", "Door/Cover Open");
66 statusMap.put("inkSystemInitializing", "Loading Ink...");
67 statusMap.put("shuttingDown", "Shutting Down...");
71 public boolean getTrayEmptyOrOpen() {
72 return trayEmptyOrOpen;
75 public @Nullable String getPrinterStatus() {