001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.pos.device;
019:
020: import java.util.Map;
021:
022: import jpos.JposException;
023:
024: import org.ofbiz.base.container.ContainerConfig;
025: import org.ofbiz.base.util.Debug;
026: import org.ofbiz.base.util.GeneralException;
027: import org.ofbiz.base.util.UtilValidate;
028: import org.ofbiz.pos.device.impl.CashDrawer;
029: import org.ofbiz.pos.device.impl.CheckScanner;
030: import org.ofbiz.pos.device.impl.Journal;
031: import org.ofbiz.pos.device.impl.Keyboard;
032: import org.ofbiz.pos.device.impl.LineDisplay;
033: import org.ofbiz.pos.device.impl.Msr;
034: import org.ofbiz.pos.device.impl.PinPad;
035: import org.ofbiz.pos.device.impl.Receipt;
036: import org.ofbiz.pos.device.impl.Scanner;
037:
038: public class DeviceLoader {
039:
040: public static final String module = DeviceLoader.class.getName();
041:
042: public static CashDrawer[] drawer = null;
043: public static CheckScanner check = null;
044: public static Journal journal = null;
045: public static Keyboard keyboard = null;
046: public static LineDisplay ldisplay = null;
047: public static Msr msr = null;
048: public static PinPad pinpad = null;
049: public static Receipt receipt = null;
050: public static Scanner scanner = null;
051:
052: public static void load(Map devices) throws GeneralException {
053: // load the keyboard
054: if (devices.get("Keyboard") != null) {
055: String keyboardDevice = ((ContainerConfig.Container.Property) devices
056: .get("Keyboard")).value;
057: if (UtilValidate.isNotEmpty(keyboardDevice)
058: && !"[NOT IMPLEMENTED]".equals(keyboardDevice)) {
059: keyboard = new Keyboard(keyboardDevice, -1);
060: try {
061: keyboard.open();
062: } catch (jpos.JposException jpe) {
063: Debug.logError(jpe, "JPOS Exception", module);
064: throw new GeneralException(jpe.getOrigException());
065: }
066: }
067: }
068:
069: // load the scanner
070: if (devices.get("Scanner") != null) {
071: String scannerDevice = ((ContainerConfig.Container.Property) devices
072: .get("Scanner")).value;
073: if (UtilValidate.isNotEmpty(scannerDevice)
074: && !"[NOT IMPLEMENTED]".equals(scannerDevice)) {
075: scanner = new Scanner(scannerDevice, -1);
076: try {
077: scanner.open();
078: } catch (jpos.JposException jpe) {
079: Debug.logError(jpe, "JPOS Exception", module);
080: throw new GeneralException(jpe.getOrigException());
081: }
082: }
083: }
084:
085: // load the check reader
086: if (devices.get("CheckScanner") != null) {
087: String checkScannerDevice = ((ContainerConfig.Container.Property) devices
088: .get("CheckScanner")).value;
089: if (UtilValidate.isNotEmpty(checkScannerDevice)
090: && !"[NOT IMPLEMENTED]".equals(checkScannerDevice)) {
091: check = new CheckScanner(checkScannerDevice, -1);
092: try {
093: check.open();
094: } catch (jpos.JposException jpe) {
095: Debug.logError(jpe, "JPOS Exception", module);
096: throw new GeneralException(jpe.getOrigException());
097: }
098: }
099: }
100:
101: // load the msr
102: if (devices.get("Msr") != null) {
103: String msrDevice = ((ContainerConfig.Container.Property) devices
104: .get("Msr")).value;
105: if (UtilValidate.isNotEmpty(msrDevice)
106: && !"[NOT IMPLEMENTED]".equals(msrDevice)) {
107: msr = new Msr(msrDevice, -1);
108: try {
109: msr.open();
110: } catch (jpos.JposException jpe) {
111: Debug.logError(jpe, "JPOS Exception", module);
112: throw new GeneralException(jpe.getOrigException());
113: }
114: }
115: }
116:
117: // load the receipt printer
118: if (devices.get("Receipt") != null) {
119: String receiptDevice = ((ContainerConfig.Container.Property) devices
120: .get("Receipt")).value;
121: if (UtilValidate.isNotEmpty(receiptDevice)
122: && !"[NOT IMPLEMENTED]".equals(receiptDevice)) {
123: receipt = new Receipt(receiptDevice, -1);
124: try {
125: receipt.open();
126: } catch (jpos.JposException jpe) {
127: Debug.logError(jpe, "JPOS Exception", module);
128: throw new GeneralException(jpe.getOrigException());
129: }
130: }
131: }
132:
133: // load the journal printer
134: if (devices.get("Journal") != null) {
135: String journalDevice = ((ContainerConfig.Container.Property) devices
136: .get("Journal")).value;
137: if (UtilValidate.isNotEmpty(journalDevice)
138: && !"[NOT IMPLEMENTED]".equals(journalDevice)) {
139: journal = new Journal(journalDevice, -1);
140: try {
141: journal.open();
142: } catch (jpos.JposException jpe) {
143: Debug.logError(jpe, "JPOS Exception", module);
144: throw new GeneralException(jpe.getOrigException());
145: }
146: }
147: }
148:
149: // load the line display
150: if (devices.get("LineDisplay") != null) {
151: String lineDisplayDevice = ((ContainerConfig.Container.Property) devices
152: .get("LineDisplay")).value;
153: if (UtilValidate.isNotEmpty(lineDisplayDevice)
154: && !"[NOT IMPLEMENTED]".equals(lineDisplayDevice)) {
155: ldisplay = new LineDisplay(lineDisplayDevice, -1);
156: try {
157: ldisplay.open();
158: } catch (jpos.JposException jpe) {
159: Debug.logError(jpe, "JPOS Exception", module);
160: throw new GeneralException(jpe.getOrigException());
161: }
162: }
163: }
164:
165: // load the cash drawer(s) -- Currently only supports one drawer per terminal
166: for (int i = 1; i < 10; i++) { // more than 10 cash drawers on a terminal??
167: String idName = "CashDrawer." + i;
168: if (devices.get(idName) != null) {
169: String cashDrawerDevice = ((ContainerConfig.Container.Property) devices
170: .get(idName)).value;
171: if (UtilValidate.isNotEmpty(cashDrawerDevice)
172: && !"[NOT IMPLEMENTED]"
173: .equals(cashDrawerDevice)) {
174: if (drawer == null) {
175: drawer = new CashDrawer[10];
176: }
177:
178: // create the instance
179: drawer[i - 1] = new CashDrawer(cashDrawerDevice, -1);
180: try {
181: drawer[i - 1].open();
182: } catch (jpos.JposException jpe) {
183: Debug.logError(jpe, "JPOS Exception", module);
184: throw new GeneralException(jpe
185: .getOrigException());
186: }
187: }
188: }
189: }
190: }
191:
192: public static void enable(boolean enable) {
193: if (keyboard != null) {
194: keyboard.enable(enable);
195: }
196: if (scanner != null) {
197: scanner.enable(enable);
198: }
199: if (msr != null) {
200: msr.enable(enable);
201: }
202: if (check != null) {
203: check.enable(enable);
204: }
205: if (ldisplay != null) {
206: ldisplay.enable(enable);
207: }
208: if (pinpad != null) {
209: pinpad.enable(enable);
210: }
211: if (receipt != null) {
212: receipt.enable(enable);
213: }
214:
215: // cash drawers and journal printer are
216: // never able to be disabled so we can
217: // notify when the drawer is open and
218: // print any information needed to the
219: // journal
220: }
221:
222: public static void stop() throws GeneralException {
223: try {
224: if (keyboard != null) {
225: keyboard.close();
226: }
227: if (scanner != null) {
228: scanner.close();
229: }
230: if (msr != null) {
231: msr.close();
232: }
233: if (check != null) {
234: check.close();
235: }
236: if (ldisplay != null) {
237: ldisplay.close();
238: }
239: if (pinpad != null) {
240: pinpad.close();
241: }
242:
243: if (drawer != null) {
244: for (int i = 0; i < drawer.length; i++) {
245: if (drawer[i] != null) {
246: drawer[i].close();
247: }
248: }
249: }
250:
251: if (receipt != null) {
252: receipt.close();
253: }
254: if (journal != null) {
255: journal.close();
256: }
257: } catch (JposException jpe) {
258: Debug.logError(jpe, "JPOS Exception", module);
259: throw new GeneralException(jpe.getOrigException());
260: }
261: }
262: }
|