Browse Source

[FIX] socket manager

Gogs 7 years ago
parent
commit
eca6153397

+ 0 - 1
src/main/java/org/robert/printer/App.java

@@ -29,7 +29,6 @@ public class App {
 
 			tray.setupServer(new PrintersSocketServer());
 		} catch (Exception e) {
-			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
 	}

+ 6 - 0
src/main/java/org/robert/printer/common/PrinterDataType.java

@@ -0,0 +1,6 @@
+package org.robert.printer.common;
+
+public enum PrinterDataType {
+	RAW,
+	PDF
+}

+ 102 - 0
src/main/java/org/robert/printer/common/PrinterProcessor.java

@@ -0,0 +1,102 @@
+package org.robert.printer.common;
+
+import java.io.IOException;
+import java.util.Locale;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import javax.print.DocFlavor;
+import javax.print.DocPrintJob;
+import javax.print.PrintException;
+import javax.print.SimpleDoc;
+import javax.print.attribute.HashPrintRequestAttributeSet;
+import javax.print.attribute.PrintRequestAttributeSet;
+import javax.print.attribute.standard.JobName;
+import javax.print.event.PrintJobEvent;
+import javax.print.event.PrintJobListener;
+
+public class PrinterProcessor {
+
+	@SuppressWarnings("unused")
+	private PrinterDataType dataType;
+
+	public PrinterProcessor() {
+		this.dataType = PrinterDataType.RAW;
+	}
+
+	/**
+	 *
+	 * @param printerName
+	 * @throws PrintException
+	 */
+	public void print(String printerName, String commands) throws PrintException {
+		if (commands == null || commands.length() == 0) {
+			return;
+		}
+
+		SimpleDoc document = new SimpleDoc(commands.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
+
+		PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
+		attributes.add(new JobName("Printing raw", Locale.getDefault()));
+
+		DocPrintJob job = PrintersDiscovery.getPrintService(printerName).createPrintJob();
+
+		final AtomicBoolean finished = new AtomicBoolean(false);
+		job.addPrintJobListener(new PrintJobListener() {
+			@Override
+			public void printJobRequiresAttention(PrintJobEvent pje) {
+			}
+
+			@Override
+			public void printJobNoMoreEvents(PrintJobEvent pje) {
+				finished.set(true);
+			}
+
+			@Override
+			public void printJobFailed(PrintJobEvent pje) {
+				finished.set(true);
+			}
+
+			@Override
+			public void printJobCompleted(PrintJobEvent pje) {
+				finished.set(true);
+			}
+
+			@Override
+			public void printJobCanceled(PrintJobEvent pje) {
+				finished.set(true);
+			}
+
+			@Override
+			public void printDataTransferCompleted(PrintJobEvent pje) {
+				finished.set(true);
+			}
+		});
+
+		job.print(document, attributes);
+
+		while (!finished.get()) {
+			try {
+				Thread.sleep(200);
+			} catch (Exception e) {
+				// Ignorar excepcion
+			}
+		}
+	}
+
+	/**
+	 * @throws PrintException
+	 *
+	 */
+	public void print(String commands) throws PrintException {
+		this.print("", commands);
+	}
+
+	public static void main(String[] args) throws IOException {
+		PrinterProcessor test = new PrinterProcessor();
+		try {
+			test.print("EPSON_TM-P2.01", "Hola mundo\nOtra linea");
+		} catch (PrintException e) {
+			e.printStackTrace();
+		}
+	}
+}

+ 17 - 0
src/main/java/org/robert/printer/common/PrintersDiscovery.java

@@ -22,4 +22,21 @@ public abstract class PrintersDiscovery {
 
 		return json;
 	}
+
+	/**
+	 *
+	 * @param printerName
+	 * @return
+	 */
+	public static PrintService getPrintService(String printerName) {
+		PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
+
+		for (PrintService service : services) {
+			if (service.getName().equals(printerName)) {
+				return service;
+			}
+		}
+
+		return PrintServiceLookup.lookupDefaultPrintService();
+	}
 }

+ 10 - 0
src/main/java/org/robert/printer/exceptions/PrintException.java

@@ -0,0 +1,10 @@
+package org.robert.printer.exceptions;
+
+public class PrintException extends Exception {
+
+	/**
+	 *
+	 */
+	private static final long serialVersionUID = 1L;
+
+}

+ 25 - 4
src/main/java/org/robert/printer/ws/PrintersSocketHandler.java

@@ -1,10 +1,9 @@
 package org.robert.printer.ws;
 
 import java.io.IOException;
-import java.io.Reader;
 import java.util.HashMap;
 
-import org.apache.commons.io.IOUtils;
+import javax.print.PrintException;
 
 import org.eclipse.jetty.websocket.api.Session;
 import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
@@ -12,7 +11,9 @@ import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
 import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
 import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
 import org.eclipse.jetty.websocket.api.annotations.WebSocket;
+import org.json.JSONException;
 import org.json.JSONObject;
+import org.robert.printer.common.PrinterProcessor;
 import org.robert.printer.common.PrintersDiscovery;
 
 @WebSocket
@@ -60,8 +61,8 @@ public class PrintersSocketHandler {
 	}
 
 	@OnWebSocketMessage
-	public void onMessage(Session session, Reader reader) throws IOException {
-		String message = IOUtils.toString(reader);
+	public void onMessage(Session session, String message) throws IOException {
+		System.out.println("------>" + message);
 
 		if (message == null || message.isEmpty()) {
 			return;
@@ -86,6 +87,26 @@ public class PrintersSocketHandler {
 			JSONObject printerNames = PrintersDiscovery.getPrinterNames();
 			this.send(session, printerNames);
 		}
+
+		if (action.equals("print")) {
+			String printer = json.optString("printer");
+			String commands = json.optString("data");
+
+			PrinterProcessor processor = new PrinterProcessor();
+			try {
+				processor.print(printer, commands);
+
+				JSONObject ok = new JSONObject();
+				ok.append("status", "ok");
+
+				this.send(session, ok);
+			} catch (PrintException e) {
+				JSONObject error = new JSONObject();
+				error.append("error", e.getMessage());
+
+				this.send(session, error);
+			}
+		}
 	}
 
 	/**

+ 1 - 1
src/main/java/org/robert/printer/ws/PrintersSocketServer.java

@@ -18,7 +18,7 @@ public class PrintersSocketServer {
 	private final AtomicBoolean running;
 
 	public PrintersSocketServer() {
-		this.host = "127.0.0.1";
+		this.host = "0.0.0.0";
 		this.port = 8700;
 		this.running = new AtomicBoolean(false);
 	}

BIN
target/classes/org/robert/printer/App.class


BIN
target/classes/org/robert/printer/common/PrintersDiscovery.class


BIN
target/classes/org/robert/printer/ws/PrintersSocketHandler.class


BIN
target/classes/org/robert/printer/ws/PrintersSocketServer.class