1 25 package org.ofbiz.pos.jpos.service; 26 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.Enumeration ; 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 52 public class ScannerKybService extends BaseService implements jpos.services.ScannerService17, KeyboardReceiver { 53 54 public static final String 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 barcodeIdMap = new HashMap (); 60 61 protected byte[] scannedDataLabel = new byte[0]; 62 protected byte[] scannedData = new byte[0]; 63 protected String codeId = new String (); 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 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 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 ) 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 (); 129 } 130 131 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 public synchronized void receiveData(int[] codes, char[] chars) { 149 String dataStr = new String (chars); 150 this.parseScannedString(dataStr); 151 152 DataEvent event = new DataEvent(this, 0); 154 this.fireEvent(event); 155 } 156 157 private void parseScannedString(String str) { 158 if (str == null) { 159 return; 160 } 161 162 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 (); 184 } 185 if (barcodeIdMap.size() > 0) { 186 return; 187 } 188 189 Enumeration names = entry.getPropertyNames(); 190 if (names != null) { 191 while (names.hasMoreElements()) { 192 String codeType = (String ) names.nextElement(); 193 if (codeType.startsWith("CodeType:")) { 194 String codeValue = entry.getProp(codeType).getValueAsString(); 195 if ("CodeType:CODE11".equals(codeType)) { 196 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_OTHER)); 197 } else if ("CodeType:CODE39".equals(codeType)) { 198 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_Code39)); 199 } else if ("CodeType:CODE93".equals(codeType)) { 200 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_Code93)); 201 } else if ("CodeType:CODE128".equals(codeType)) { 202 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_Code128)); 203 } else if ("CodeType:CODABAR".equals(codeType)) { 204 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_Codabar)); 205 } else if ("CodeType:I2OF5".equals(codeType)) { 206 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_OTHER)); 207 } else if ("CodeType:ID2OF5".equals(codeType)) { 208 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_OTHER)); 209 } else if ("CodeType:MSI".equals(codeType)) { 210 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_OTHER)); 211 } else if ("CodeType:UPCA".equals(codeType)) { 212 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_UPCA)); 213 } else if ("CodeType:UPCE".equals(codeType)) { 214 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_UPCE)); 215 } else if ("CodeType:EAN13".equals(codeType)) { 216 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_EAN13)); 217 } else if ("CodeType:EAN8".equals(codeType)) { 218 barcodeIdMap.put(codeValue.toUpperCase(), new Integer (ScannerConst.SCAN_SDT_EAN8)); 219 } 220 } 221 } 222 } 223 } 224 } 225 | Popular Tags |