1 25 package org.ofbiz.pos.device.impl; 26 27 import jpos.JposException; 28 29 import org.ofbiz.base.util.Debug; 30 import org.ofbiz.pos.device.GenericDevice; 31 import org.ofbiz.pos.screen.PosScreen; 32 import org.ofbiz.pos.screen.DialogCallback; 33 import org.ofbiz.pos.screen.PosDialog; 34 35 41 public class CashDrawer extends GenericDevice implements Runnable , DialogCallback { 42 43 public static final String module = CashDrawer.class.getName(); 44 45 protected boolean openCalled = false; 46 protected boolean waiting = false; 47 protected Thread waiter = null; 48 protected long startTime = -1; 49 protected int comError = 0; 50 51 public CashDrawer(String deviceName, int timeout) { 52 super(deviceName, timeout); 53 this.control = new jpos.CashDrawer(); 54 } 55 56 protected void initialize() throws JposException { 57 Debug.logInfo("CashDrawer [" + control.getPhysicalDeviceName() + "] Claimed : " + control.getClaimed(), module); 58 } 59 60 public void receiveDialogCb(PosDialog dialog) { 61 if (this.openCalled) { 62 this.openDrawer(); 63 } 64 } 65 66 public void resetComError() { 67 this.comError = 0; 68 } 69 70 public void openDrawer() { 71 if (this.comError > 2) { 72 return; 74 } 75 try { 76 this.openCalled = true; 77 ((jpos.CashDrawer) control).openDrawer(); 78 this.openCalled = false; 79 } catch (JposException e) { 81 Debug.logError(e, module); 82 this.comError++; 83 PosScreen.currentScreen.showDialog("dialog/error/drawererror", this); 84 } 85 } 86 87 public boolean isDrawerOpen() { 88 try { 89 return ((jpos.CashDrawer) control).getDrawerOpened(); 90 } catch (JposException e) { 91 Debug.logError(e, module); 92 } 93 return false; 94 } 95 96 protected synchronized void startWaiter() { 97 if (!this.isDrawerOpen()) { 98 this.waiter = new Thread (this); 99 this.waiter.setDaemon(false); 100 this.waiter.setName(this.getClass().getName()); 101 this.waiting = true; 102 this.waiter.start(); 103 } else { 104 Debug.logWarning("Drawer already open!", module); 105 } 106 } 107 108 public void run() { 109 Debug.log("Starting Waiter Thread", module); 110 this.startTime = System.currentTimeMillis(); 111 while (waiting) { 112 boolean isOpen = true; 113 try { 114 isOpen = ((jpos.CashDrawer) control).getDrawerOpened(); 115 } catch (JposException e) { 116 Debug.logError(e, module); 117 this.waiting = false; 118 PosScreen.currentScreen.showDialog("dialog/error/drawererror"); 119 } 120 if (isOpen) { 121 long now = (System.currentTimeMillis() - startTime); 122 if ((now > 4499) && (now % 500 == 0)) { 123 java.awt.Toolkit.getDefaultToolkit().beep(); 124 } 125 if ((now > 4499) && (now % 5000 == 0)) { 126 PosScreen.currentScreen.showDialog("dialog/error/draweropen"); 127 } 128 } else { 129 this.waiting = false; 130 } 131 } 132 this.startTime = -1; 133 Debug.log("Waiter finished", module); 134 } 135 } 136 137 | Popular Tags |