Selaa lähdekoodia

修改打印机获取名称时,将状态返回

zhoupeng 1 vuosi sitten
vanhempi
commit
5705a8aeb2

+ 18 - 3
PrintServer/src/main/java/com/jd/printserver/controller/PrintServerController.java

@@ -23,6 +23,8 @@ import javax.print.PrintServiceLookup;
 import javax.print.attribute.Attribute;
 import javax.print.attribute.PrintServiceAttributeSet;
 import javax.print.attribute.standard.PrinterIsAcceptingJobs;
+import javax.print.attribute.standard.PrinterState;
+import javax.print.attribute.standard.PrinterStateReason;
 import javax.servlet.http.HttpServletRequest;
 import java.awt.print.PrinterJob;
 import java.io.File;
@@ -107,10 +109,23 @@ public class PrintServerController extends BaseController {
     @RequestMapping(value = "/getPrintName")
     @CrossOrigin
     public AjaxResult getPrintName(){
-        List<String> list = new ArrayList<>();
+        List<Map<String,String>> list=new ArrayList<>();
         for (PrintService ps : PrinterJob.lookupPrintServices()) {
-            String printName = ps.toString();
-            list.add(printName);
+            Map<String,String> map=new HashMap<>();
+            PrintServiceAttributeSet attributes = ps.getAttributes();
+            Attribute stateAttr = attributes.get(PrinterState.class);
+            Attribute reasonAttr = attributes.get(PrinterStateReason.class);
+
+            PrinterState printerState = (PrinterState) stateAttr;
+            PrinterStateReason printerStateReason = (PrinterStateReason) reasonAttr;
+            map.put("name",ps.toString());
+
+            if (printerState == PrinterState.IDLE && printerStateReason == null) {
+                map.put("state","在线");
+            } else {
+                map.put("state","离线");
+            }
+            list.add(map);
         }
         return success(list);
     }