Package com.gmv.its.deepsy.hal.printer

Classes

interface  Job
 Interface that represents a job in a printer queue. More...
 
interface  JobInformation
 Job information. More...
 
interface  JobObserver
 Job observer. More...
 
enum  JobStatus
 
interface  JobStatusEvent
 Job status event. More...
 
enum  PaperLevel
 Paper level values. More...
 
interface  PaperLevelEvent
 Paper level event. More...
 
interface  PrintConfig
 Print configuration. More...
 
interface  Printer
 Printer. More...
 
enum  PrinterAlarm
 Printer alarm types. More...
 
interface  PrinterAlarmEvent
 Printer alarm event. More...
 
interface  PrinterEvent
 Printer event. More...
 
interface  PrinterInformation
 Printer information. More...
 
interface  PrinterListEvent
 PrinterList Event. More...
 
interface  PrinterListObserver
 Printer list observer. More...
 
class  PrinterLowLevelException
 Printer low level exception. More...
 
interface  PrinterManager
 Printer manager. More...
 
class  PrinterNotAvailableException
 Printer not available exception. More...
 
class  PrinterObserver
 Printer observer. More...
 
enum  PrinterStatus
 
class  PrinterStatusEvent
 Printer status change event. More...
 

Detailed Description

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.

PrinterManager printerManager = DeepsyPrinterManager.getInstance();


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;

printerManager.close();

Service availability

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();
}
}
}


Examples

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;
import com.gmv.its.deepsy.printer.DeepsyPrinterManager;
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;
import com.gmv.its.deepsy.printer.DeepsyPrinterManager;
public class SetPrinterConfigurationApp {
public static void main(String[] args) {
try (PrinterManager printerManager = DeepsyPrinterManager.getInstance()) {
List<Printer> printerList = printerManager.getPrinters();
for (Printer p : printerList) {
p.setDefaultConfiguration(new PrintConfig(PrintConfig.PrinterParameter.PAPER_WIDTH, 50));
// Setting speed to normal
p.setDefaultConfiguration(new PrintConfig(PrintConfig.PrinterParameter.SPEED, 2));
}
} 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;
import com.gmv.its.deepsy.printer.DeepsyPrinterManager;
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 com.gmv.its.deepsy.printer.DeepsyPrinterManager;
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();
}
}
}
class MyPrinterObserver extends PrinterObserver {
@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();
}
}
}
class MyJobObserver implements JobObserver {
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());
// Print a job
ArrayList<PrintConfig> configurations = new ArrayList<PrintConfig>();
configurations.add(new PrintConfig(PrinterParameter.PAPER_ADJ, 1));
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 com.gmv.its.deepsy.printer.DeepsyPrinterManager;
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();
}
}
}
class MyPrinterObservers extends PrinterObserver {
@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();
}
}
}