To work with Deepsy printer service it is necessary to use DeepsyPrinterManager located in com.gmv.its.deepsy.printer for getting the initial PrinterManager object.
DeepsyPrinterManager.getInstance has an optional parameter indicating the ip address of remote service. For instance:
PrinterManager printerManager = DeepsyPrinterManager.getInstance(
"192.168.1.10");
PrinterManager is an AutoCloseable interface. An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur. To obtain a list of Printers objects:
try (
PrinterManager printerManager = DeepsyPrinterManager.getInstance()){
List<Printer> printerList = printerManager.getPrinters();
} catch (PrinterNotAvailableException e) {
e.printStackTrace();
} catch (PrinterLowLevelException e) {
e.printStackTrace();
}
If this object is not inside a try with resources block, it is recommended to call to printerManager.close() in order to finalize properly;
To detect service availability the DeepsyPrinterManager implements Manager interface which provides methods for getting availability and subscribing to availability events.
class MyManagerObserver implements ManagerObserver{
public void notify(ManagerConnectionEvent event) {
System.out.println(event.getManager().getName() +
" in "+
event.getManager().getIp() +
" "+
event.getAction().toString());
}
}
public class App {
public static void main(String[] args) {
ManagerObserver observer = new MyManagerObserver();
try (
PrinterManager printerManager = DeepsyPrinterManager.getInstance()){
printerManager.subscribe(observer);
} catch (CannotSubscribeException e1) {
e1.printStackTrace();
}
}
}
Add print job
For adding a print job it is necessary to call to printImage method of Printer.
package com.gmv.its.dpyjavaexamples.printer;
import java.util.List;
public class AddAprinterJobApp {
public static void main(String[] args) {
try (
PrinterManager printerManager = DeepsyPrinterManager.getInstance()) {
List<Printer> printerList = printerManager.getPrinters();
for (Printer p : printerList) {
Job job = p.printImage(
"/tmp/ticket.png");
}
} catch (PrinterNotAvailableException e) {
e.printStackTrace();
} catch (PrinterLowLevelException e) {
e.printStackTrace();
}
}
}
Set printer configuration
Setting printer configuration is provided by Printer.setDefaultConfiguration. In this example, there is a change in paper width configuration.
package com.gmv.its.dpyjavaexamples.printer;
import java.util.List;
public class SetPrinterConfigurationApp {
public static void main(String[] args) {
try (
PrinterManager printerManager = DeepsyPrinterManager.getInstance()) {
List<Printer> printerList = printerManager.getPrinters();
for (Printer p : printerList) {
}
} catch (PrinterNotAvailableException e) {
e.printStackTrace();
} catch (PrinterLowLevelException e) {
e.printStackTrace();
}
}
}
Print a self-test ticket
An example on how to print a self-test ticket is provided:
package com.gmv.its.dpyjavaexamples.printer;
import java.util.List;
public class PrintAtestTicketApp {
public static void main(String[] args) {
try (
PrinterManager printerManager = DeepsyPrinterManager.getInstance()) {
List<Printer> printerList = printerManager.getPrinters();
for (Printer p : printerList) {
p.printTestTicket();
}
} catch (PrinterNotAvailableException e) {
e.printStackTrace();
} catch (PrinterLowLevelException e) {
e.printStackTrace();
}
}
}
Subscribe to events
There are 4 types of events in the printer system:
This example subscribes to every event.
package com.gmv.its.dpyjavaexamples.printer;
import java.util.ArrayList;
import java.util.List;
class MyPrinterListObserver implements PrinterListObserver {
public void notify(PrinterListEvent event) {
try {
System.out.println(
"Printer list, printer " + event.getPrinter().getId() +
" " +
event.getAction());
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void notifyNewAlarm(PrinterAlarmEvent event) {
try {
System.out.println(
"Printer" + event.getPrinter().getId() +
" event:" + event.getAlarm().name());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void notifyNewStatus(PrinterStatusEvent event) {
try {
System.out.println(
"Printer " + event.getPrinter().getId() +
" status:" + event.getStatus().name());
}catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void notifyNewPaperLevel(PaperLevelEvent event) {
try {
System.out.println(
"Printer " + event.getPrinter().getId() +
" paper level:" + event.getPaperLevel());
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void notify(JobStatusEvent event) {
try {
System.out.println(
"Job " + event.getJob().getJobInformation().getUri() +
" of printer " + event.getJob().getPrinter().getPrinterInformation().getSerialNumber() + " status:"
+ event.getStatus().name());
} catch (PrinterLowLevelException e) {
e.printStackTrace();
} catch (PrinterNotAvailableException e) {
e.printStackTrace();
}
}
}
public class PrinterEventsApp {
public static void main(String[] args) {
try (
PrinterManager printerManager = DeepsyPrinterManager.getInstance(
"192.168.0.125")) {
printerManager.subscribe(new MyPrinterListObserver());
List<Printer> printerList = printerManager.getPrinters();
System.out.println(
"There are " + printerList.size() +
" printers");
for (Printer p : printerList) {
p.subscribe(new MyPrinterObserver());
ArrayList<PrintConfig> configurations = new ArrayList<PrintConfig>();
Job job = p.printImage(
"/mntDAT/tickets/renfe_ticket.png", configurations);
job.subscribe(new MyJobObserver());
}
Thread.sleep(10000);
} catch (PrinterNotAvailableException e) {
e.printStackTrace();
} catch (PrinterLowLevelException e) {
e.printStackTrace();
} catch (CannotSubscribeException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Paper Level
package com.gmv.its.dpyjavaexamples.printer;
import java.util.ArrayList;
import java.util.List;
class MyPrinterListObservers implements PrinterListObserver {
public void notify(PrinterListEvent event) {
try {
System.out.println(
"Printer list, printer " + event.getPrinter().getPrinterInformation().getSerialNumber() +
" " +
event.getAction());
} catch (PrinterLowLevelException e) {
e.printStackTrace();
} catch (PrinterNotAvailableException e) {
e.printStackTrace();
}
}
}
@Override
public void notifyNewAlarm(PrinterAlarmEvent event) {
try {
System.out.println(
"Printer" + event.getPrinter().getId() +
" event:" + event.getAlarm().name());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void notifyNewStatus(PrinterStatusEvent event) {
try {
System.out.println(
"Printer " + event.getPrinter().getId() +
" status:" + event.getStatus().name());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void notifyNewPaperLevel(PaperLevelEvent event) {
try {
System.out.println(
"Printer " + event.getPrinter().getId() +
" paper level:" + event.getPaperLevel());
}catch (Exception e) {
e.printStackTrace();
}
}
}
public class GetPaperLevel {
public static void main(String[] args) {
try (
PrinterManager printerManager = DeepsyPrinterManager.getInstance(
"192.168.0.125")) {
printerManager.subscribe(new MyPrinterListObservers());
List<Printer> printerList = printerManager.getPrinters();
System.out.println(
"There are " + printerList.size() +
" printers");
for (Printer p : printerList) {
p.subscribe(new MyPrinterObservers());
PrinterInformation info = p.getPrinterInformation();
System.out.println(
"SN: "+ info.getSerialNumber()+
" Paper level: " + info.getPaperLevel());
}
Thread.sleep(10000);
for (Printer p : printerList) {
p.subscribe(new MyPrinterObservers());
PrinterInformation info = p.getPrinterInformation();
System.out.println(
"SN: "+ info.getSerialNumber()+
" Paper level: " + info.getPaperLevel());
}
Thread.sleep(10000);
} catch (PrinterNotAvailableException e) {
e.printStackTrace();
} catch (PrinterLowLevelException e) {
e.printStackTrace();
} catch (CannotSubscribeException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}