KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > jpos > service > ScannerKybService


1 /*
2  * $Id: ScannerKybService.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.jpos.service;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Enumeration JavaDoc;
30
31 import jpos.JposException;
32 import jpos.ScannerConst;
33 import jpos.services.EventCallbacks;
34 import jpos.events.DataEvent;
35
36 import org.ofbiz.pos.adaptor.KeyboardAdaptor;
37 import org.ofbiz.pos.adaptor.KeyboardReceiver;
38
39 /**
40  * Generic Keyboard Wedge Barcode Scanner
41  *
42  * Configure your scanner:
43  * 1) Send STX Preamble
44  * 2) Send barcode id as prefix
45  * 3) Termination char CR
46  * 4) Do NOT send ETX Postamble
47  *
48  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
49  * @version $Rev: 5462 $
50  * @since 3.2
51  */

52 public class ScannerKybService extends BaseService implements jpos.services.ScannerService17, KeyboardReceiver {
53
54     public static final String JavaDoc module = ScannerKybService.class.getName();
55     private static final int TYPELOC_PREFIX = 50;
56     private static final int TYPELOC_SUFFIX = 60;
57     private static final int TYPELOC_NONE = 99;
58
59     protected Map JavaDoc barcodeIdMap = new HashMap JavaDoc();
60
61     protected byte[] scannedDataLabel = new byte[0];
62     protected byte[] scannedData = new byte[0];
63     protected String JavaDoc codeId = new String JavaDoc();
64
65     protected boolean decodeData = true;
66     protected boolean eventEnabled = true;
67     protected boolean autoDisable = false;
68     protected int powerState = 1;
69     protected int codeLocation = TYPELOC_PREFIX;
70
71     public ScannerKybService() {
72         KeyboardAdaptor.getInstance(this, KeyboardAdaptor.SCANNER_DATA);
73     }
74
75     public void open(String JavaDoc deviceName, EventCallbacks ecb) throws JposException {
76         super.open(deviceName, ecb);
77         this.readCodeMap();
78         if (entry.hasPropertyWithName("BarcodeTypePosition")) {
79             if (entry.getProp("BarcodeTypePosition").getValueAsString().equalsIgnoreCase("suffix")) {
80                 this.codeLocation = TYPELOC_SUFFIX;
81             } else if (entry.getProp("BarcodeTypePosition").getValueAsString().equalsIgnoreCase("prefix")) {
82                 this.codeLocation = TYPELOC_PREFIX;
83             } else {
84                 this.codeLocation = TYPELOC_NONE;
85             }
86         }
87     }
88
89     // ScannerService12
90
public boolean getAutoDisable() throws JposException {
91         return this.autoDisable;
92     }
93
94     public void setAutoDisable(boolean b) throws JposException {
95         this.autoDisable = b;
96     }
97
98     public boolean getDecodeData() throws JposException {
99         return this.decodeData;
100     }
101
102     public void setDecodeData(boolean b) throws JposException {
103         this.decodeData = b;
104     }
105
106     public byte[] getScanData() throws JposException {
107         return this.scannedData;
108     }
109
110     public byte[] getScanDataLabel() throws JposException {
111         if (this.decodeData) {
112             return this.scannedDataLabel;
113         } else {
114             return new byte[0];
115         }
116     }
117
118     public int getScanDataType() throws JposException {
119         if (codeId != null && barcodeIdMap.containsKey(codeId)) {
120             return ((Integer JavaDoc) barcodeIdMap.get(codeId)).intValue();
121         }
122         return ScannerConst.SCAN_SDT_UNKNOWN;
123     }
124
125     public void clearInput() throws JposException {
126         this.scannedDataLabel = new byte[0];
127         this.scannedData = new byte[0];
128         this.codeId = new String JavaDoc();
129     }
130
131     // ScannerService13
132
public int getCapPowerReporting() throws JposException {
133         return 0;
134     }
135
136     public int getPowerNotify() throws JposException {
137         return 0;
138     }
139
140     public void setPowerNotify(int i) throws JposException {
141     }
142
143     public int getPowerState() throws JposException {
144         return 0;
145     }
146
147     // KeyboardReceiver
148
public synchronized void receiveData(int[] codes, char[] chars) {
149         String JavaDoc dataStr = new String JavaDoc(chars);
150         this.parseScannedString(dataStr);
151
152         // fire off the event notification
153
DataEvent event = new DataEvent(this, 0);
154         this.fireEvent(event);
155     }
156
157     private void parseScannedString(String JavaDoc str) {
158         if (str == null) {
159             return;
160         }
161
162         // parse the scanned data
163
if (str != null) {
164             str = str.trim();
165             this.scannedData = str.getBytes();
166             if (this.decodeData) {
167                 if (this.codeLocation == TYPELOC_PREFIX) {
168                     this.codeId = str.substring(0, 1).toUpperCase();
169                     this.scannedDataLabel = str.substring(1).getBytes();
170                 } else if (this.codeLocation == TYPELOC_SUFFIX) {
171                     this.codeId = str.substring(str.length() - 1);
172                     this.scannedDataLabel = str.substring(0, str.length() - 1).getBytes();
173                 } else {
174                     this.codeId = "";
175                     this.scannedDataLabel = str.getBytes();
176                 }
177             }
178         }
179     }
180
181     private void readCodeMap() {
182         if (barcodeIdMap == null) {
183             barcodeIdMap = new HashMap JavaDoc();
184         }
185         if (barcodeIdMap.size() > 0) {
186             return;
187         }
188
189         Enumeration JavaDoc names = entry.getPropertyNames();
190         if (names != null) {
191             while (names.hasMoreElements()) {
192                 String JavaDoc codeType = (String JavaDoc) names.nextElement();
193                 if (codeType.startsWith("CodeType:")) {
194                     String JavaDoc codeValue = entry.getProp(codeType).getValueAsString();
195                     if ("CodeType:CODE11".equals(codeType)) {
196                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_OTHER));
197                     } else if ("CodeType:CODE39".equals(codeType)) {
198                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_Code39));
199                     } else if ("CodeType:CODE93".equals(codeType)) {
200                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_Code93));
201                     } else if ("CodeType:CODE128".equals(codeType)) {
202                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_Code128));
203                     } else if ("CodeType:CODABAR".equals(codeType)) {
204                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_Codabar));
205                     } else if ("CodeType:I2OF5".equals(codeType)) {
206                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_OTHER));
207                     } else if ("CodeType:ID2OF5".equals(codeType)) {
208                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_OTHER));
209                     } else if ("CodeType:MSI".equals(codeType)) {
210                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_OTHER));
211                     } else if ("CodeType:UPCA".equals(codeType)) {
212                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_UPCA));
213                     } else if ("CodeType:UPCE".equals(codeType)) {
214                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_UPCE));
215                     } else if ("CodeType:EAN13".equals(codeType)) {
216                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_EAN13));
217                     } else if ("CodeType:EAN8".equals(codeType)) {
218                         barcodeIdMap.put(codeValue.toUpperCase(), new Integer JavaDoc(ScannerConst.SCAN_SDT_EAN8));
219                     }
220                 }
221             }
222         }
223     }
224 }
225
Popular Tags