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.jpos.service;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Enumeration;
023:
024: import jpos.JposException;
025: import jpos.ScannerConst;
026: import jpos.services.EventCallbacks;
027: import jpos.events.DataEvent;
028:
029: import org.ofbiz.pos.adaptor.KeyboardAdaptor;
030: import org.ofbiz.pos.adaptor.KeyboardReceiver;
031:
032: /**
033: * Generic Keyboard Wedge Barcode Scanner
034: *
035: * Configure your scanner:
036: * 1) Send STX Preamble
037: * 2) Send barcode id as prefix
038: * 3) Termination char CR
039: * 4) Do NOT send ETX Postamble
040: */
041: public class ScannerKybService extends BaseService implements
042: jpos.services.ScannerService17, KeyboardReceiver {
043:
044: public static final String module = ScannerKybService.class
045: .getName();
046: private static final int TYPELOC_PREFIX = 50;
047: private static final int TYPELOC_SUFFIX = 60;
048: private static final int TYPELOC_NONE = 99;
049:
050: protected Map barcodeIdMap = new HashMap();
051:
052: protected byte[] scannedDataLabel = new byte[0];
053: protected byte[] scannedData = new byte[0];
054: protected String codeId = new String();
055:
056: protected boolean decodeData = true;
057: protected boolean eventEnabled = true;
058: protected boolean autoDisable = false;
059: protected int powerState = 1;
060: protected int codeLocation = TYPELOC_PREFIX;
061:
062: public ScannerKybService() {
063: KeyboardAdaptor.getInstance(this , KeyboardAdaptor.SCANNER_DATA);
064: }
065:
066: public void open(String deviceName, EventCallbacks ecb)
067: throws JposException {
068: super .open(deviceName, ecb);
069: this .readCodeMap();
070: if (entry.hasPropertyWithName("BarcodeTypePosition")) {
071: if (entry.getProp("BarcodeTypePosition").getValueAsString()
072: .equalsIgnoreCase("suffix")) {
073: this .codeLocation = TYPELOC_SUFFIX;
074: } else if (entry.getProp("BarcodeTypePosition")
075: .getValueAsString().equalsIgnoreCase("prefix")) {
076: this .codeLocation = TYPELOC_PREFIX;
077: } else {
078: this .codeLocation = TYPELOC_NONE;
079: }
080: }
081: }
082:
083: // ScannerService12
084: public boolean getAutoDisable() throws JposException {
085: return this .autoDisable;
086: }
087:
088: public void setAutoDisable(boolean b) throws JposException {
089: this .autoDisable = b;
090: }
091:
092: public boolean getDecodeData() throws JposException {
093: return this .decodeData;
094: }
095:
096: public void setDecodeData(boolean b) throws JposException {
097: this .decodeData = b;
098: }
099:
100: public byte[] getScanData() throws JposException {
101: return this .scannedData;
102: }
103:
104: public byte[] getScanDataLabel() throws JposException {
105: if (this .decodeData) {
106: return this .scannedDataLabel;
107: } else {
108: return new byte[0];
109: }
110: }
111:
112: public int getScanDataType() throws JposException {
113: if (codeId != null && barcodeIdMap.containsKey(codeId)) {
114: return ((Integer) barcodeIdMap.get(codeId)).intValue();
115: }
116: return ScannerConst.SCAN_SDT_UNKNOWN;
117: }
118:
119: public void clearInput() throws JposException {
120: this .scannedDataLabel = new byte[0];
121: this .scannedData = new byte[0];
122: this .codeId = new String();
123: }
124:
125: // ScannerService13
126: public int getCapPowerReporting() throws JposException {
127: return 0;
128: }
129:
130: public int getPowerNotify() throws JposException {
131: return 0;
132: }
133:
134: public void setPowerNotify(int i) throws JposException {
135: }
136:
137: public int getPowerState() throws JposException {
138: return 0;
139: }
140:
141: // KeyboardReceiver
142: public synchronized void receiveData(int[] codes, char[] chars) {
143: String dataStr = new String(chars);
144: this .parseScannedString(dataStr);
145:
146: // fire off the event notification
147: DataEvent event = new DataEvent(this , 0);
148: this .fireEvent(event);
149: }
150:
151: private void parseScannedString(String str) {
152: if (str == null) {
153: return;
154: }
155:
156: // parse the scanned data
157: if (str != null) {
158: str = str.trim();
159: this .scannedData = str.getBytes();
160: if (this .decodeData) {
161: if (this .codeLocation == TYPELOC_PREFIX) {
162: this .codeId = str.substring(0, 1).toUpperCase();
163: this .scannedDataLabel = str.substring(1).getBytes();
164: } else if (this .codeLocation == TYPELOC_SUFFIX) {
165: this .codeId = str.substring(str.length() - 1);
166: this .scannedDataLabel = str.substring(0,
167: str.length() - 1).getBytes();
168: } else {
169: this .codeId = "";
170: this .scannedDataLabel = str.getBytes();
171: }
172: }
173: }
174: }
175:
176: private void readCodeMap() {
177: if (barcodeIdMap == null) {
178: barcodeIdMap = new HashMap();
179: }
180: if (barcodeIdMap.size() > 0) {
181: return;
182: }
183:
184: Enumeration names = entry.getPropertyNames();
185: if (names != null) {
186: while (names.hasMoreElements()) {
187: String codeType = (String) names.nextElement();
188: if (codeType.startsWith("CodeType:")) {
189: String codeValue = entry.getProp(codeType)
190: .getValueAsString();
191: if ("CodeType:CODE11".equals(codeType)) {
192: barcodeIdMap
193: .put(
194: codeValue.toUpperCase(),
195: new Integer(
196: ScannerConst.SCAN_SDT_OTHER));
197: } else if ("CodeType:CODE39".equals(codeType)) {
198: barcodeIdMap.put(codeValue.toUpperCase(),
199: new Integer(
200: ScannerConst.SCAN_SDT_Code39));
201: } else if ("CodeType:CODE93".equals(codeType)) {
202: barcodeIdMap.put(codeValue.toUpperCase(),
203: new Integer(
204: ScannerConst.SCAN_SDT_Code93));
205: } else if ("CodeType:CODE128".equals(codeType)) {
206: barcodeIdMap.put(codeValue.toUpperCase(),
207: new Integer(
208: ScannerConst.SCAN_SDT_Code128));
209: } else if ("CodeType:CODABAR".equals(codeType)) {
210: barcodeIdMap.put(codeValue.toUpperCase(),
211: new Integer(
212: ScannerConst.SCAN_SDT_Codabar));
213: } else if ("CodeType:I2OF5".equals(codeType)) {
214: barcodeIdMap
215: .put(
216: codeValue.toUpperCase(),
217: new Integer(
218: ScannerConst.SCAN_SDT_OTHER));
219: } else if ("CodeType:ID2OF5".equals(codeType)) {
220: barcodeIdMap
221: .put(
222: codeValue.toUpperCase(),
223: new Integer(
224: ScannerConst.SCAN_SDT_OTHER));
225: } else if ("CodeType:MSI".equals(codeType)) {
226: barcodeIdMap
227: .put(
228: codeValue.toUpperCase(),
229: new Integer(
230: ScannerConst.SCAN_SDT_OTHER));
231: } else if ("CodeType:UPCA".equals(codeType)) {
232: barcodeIdMap
233: .put(
234: codeValue.toUpperCase(),
235: new Integer(
236: ScannerConst.SCAN_SDT_UPCA));
237: } else if ("CodeType:UPCE".equals(codeType)) {
238: barcodeIdMap
239: .put(
240: codeValue.toUpperCase(),
241: new Integer(
242: ScannerConst.SCAN_SDT_UPCE));
243: } else if ("CodeType:EAN13".equals(codeType)) {
244: barcodeIdMap
245: .put(
246: codeValue.toUpperCase(),
247: new Integer(
248: ScannerConst.SCAN_SDT_EAN13));
249: } else if ("CodeType:EAN8".equals(codeType)) {
250: barcodeIdMap
251: .put(
252: codeValue.toUpperCase(),
253: new Integer(
254: ScannerConst.SCAN_SDT_EAN8));
255: }
256: }
257: }
258: }
259: }
260: }
|