KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > device > impl > CashDrawer


1 /*
2  * $Id: CashDrawer.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

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 /**
36  *
37  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
38  * @version $Rev: 5462 $
39  * @since 3.2
40  */

41 public class CashDrawer extends GenericDevice implements Runnable JavaDoc, DialogCallback {
42
43     public static final String JavaDoc module = CashDrawer.class.getName();
44
45     protected boolean openCalled = false;
46     protected boolean waiting = false;
47     protected Thread JavaDoc waiter = null;
48     protected long startTime = -1;
49     protected int comError = 0;
50
51     public CashDrawer(String JavaDoc 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             // only attempt this 3 times
73
return;
74         }
75         try {
76             this.openCalled = true;
77             ((jpos.CashDrawer) control).openDrawer();
78             this.openCalled = false;
79             //this.startWaiter();
80
} 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 JavaDoc(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