]> git.basschouten.com Git - openhab-addons.git/blob
19773d6eba057efe43237b3a68e8d597387f4c78
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.api;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
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;
23
24 /**
25  * The {@link HPStatus} is responsible for handling reading of status data.
26  *
27  * @author Stewart Cossey - Initial contribution
28  */
29 @NonNullByDefault
30 public class HPStatus {
31     public static final String ENDPOINT = "/DevMgmt/ProductStatusDyn.xml";
32
33     private static final Map<String, String> STATUS_MESSAGES = initializeStatus();
34
35     private final String printerStatus;
36     private final boolean trayEmptyOrOpen;
37
38     public HPStatus(Document document) {
39         NodeList nodes = document.getDocumentElement().getElementsByTagName("psdyn:Status");
40
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);
48             }
49             if ("trayEmpty".equals(statusCategory)) {
50                 localTrayEmptyOrOpen = true;
51             }
52         }
53         trayEmptyOrOpen = localTrayEmptyOrOpen;
54         printerStatus = localPrinterStatus;
55     }
56
57     private static Map<String, String> initializeStatus() {
58         Map<String, String> statusMap = new HashMap<>();
59
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...");
68         return statusMap;
69     }
70
71     public boolean getTrayEmptyOrOpen() {
72         return trayEmptyOrOpen;
73     }
74
75     public @Nullable String getPrinterStatus() {
76         return printerStatus;
77     }
78 }