| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import java.util.*; |
| 20 |
import java.io.*; |
| 21 |
import gnu.io.*; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class SNComHandler extends java.lang.Thread implements |
| 32 |
SerialPortEventListener { |
| 33 |
|
| 34 |
private static SNComThread comThread; |
| 35 |
private static SNLogger comLog; |
| 36 |
private static SNConfig serverConfig; |
| 37 |
private String comName = "Nil"; |
| 38 |
private boolean process = false; |
| 39 |
private String requestId = "Nil"; |
| 40 |
private String num = "Nil"; |
| 41 |
private boolean running = true; |
| 42 |
private CommPortIdentifier portId; |
| 43 |
private SerialPort serialPort = null; |
| 44 |
private OutputStream outputStream = null; |
| 45 |
private InputStream inputStream = null; |
| 46 |
private String errMessage = ""; |
| 47 |
private boolean fatalErr = false; |
| 48 |
private boolean waitForInput = true; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public SNComHandler() { |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public SNComHandler(SNComThread cThread, SNLogger sLog, SNConfig |
| 67 |
sConfig, String cName) throws NoSuchPortException { |
| 68 |
this.comThread = cThread; |
| 69 |
this.comLog = sLog; |
| 70 |
this.serverConfig = sConfig; |
| 71 |
this.comName = cName; |
| 72 |
portId = |
| 73 |
(CommPortIdentifier)CommPortIdentifier.getPortIdentifier(this.comName); |
| 74 |
|
| 75 |
this.comLog.log("Com Handler Initialised For " + comName); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public void run() { |
| 84 |
|
| 85 |
int resetCount = 0; |
| 86 |
int resetCount2 = 0; |
| 87 |
int resetCount3 = 0; |
| 88 |
while (running) { |
| 89 |
if (fatalErr) { |
| 90 |
comThread.queRequest(requestId, errMessage, "Error"); |
| 91 |
comLog.log(errMessage + " " + comName + " " + num); |
| 92 |
num = "Nil"; |
| 93 |
resetCount = 0; |
| 94 |
resetCount2 = 0; |
| 95 |
resetCount3 = 0; |
| 96 |
fatalErr = false; |
| 97 |
} |
| 98 |
if (!process) { |
| 99 |
try { |
| 100 |
Thread.sleep(500); |
| 101 |
} catch (InterruptedException e) {} |
| 102 |
continue; |
| 103 |
} |
| 104 |
comLog.debugInfo("**********We Are Processing***********"); |
| 105 |
if (num.equals("Nil")) { |
| 106 |
try { |
| 107 |
Thread.sleep(500); |
| 108 |
} catch (InterruptedException e) {} |
| 109 |
continue; |
| 110 |
} |
| 111 |
comLog.debugInfo("**********Trying To Open Port***********"); |
| 112 |
if (!openPort()) { |
| 113 |
closePort(); |
| 114 |
try { |
| 115 |
Thread.sleep(500); |
| 116 |
} catch (InterruptedException e) {} |
| 117 |
resetCount ++; |
| 118 |
|
| 119 |
if (resetCount > 3) { |
| 120 |
process = false; |
| 121 |
errMessage = "Error! Could Not Open Port"; |
| 122 |
fatalErr = true; |
| 123 |
} |
| 124 |
continue; |
| 125 |
} |
| 126 |
comLog.debugInfo("**********Trying To Reset Modem***********"); |
| 127 |
if (!reset()) { |
| 128 |
try { |
| 129 |
Thread.sleep(500); |
| 130 |
} catch (InterruptedException e) {} |
| 131 |
resetCount2 ++; |
| 132 |
|
| 133 |
if (resetCount2 > 3) { |
| 134 |
process = false; |
| 135 |
errMessage = "Error! Could Not Reset Modem"; |
| 136 |
fatalErr = true; |
| 137 |
} |
| 138 |
continue; |
| 139 |
} |
| 140 |
comLog.debugInfo("**********Trying To Dial***********"); |
| 141 |
if (!dial()) { |
| 142 |
comLog.debugInfo("**********" + errMessage + "***********"); |
| 143 |
try { |
| 144 |
Thread.sleep(500); |
| 145 |
} catch (InterruptedException e) {} |
| 146 |
resetCount3 ++; |
| 147 |
|
| 148 |
if (resetCount3 > 2) { |
| 149 |
process = false; |
| 150 |
errMessage = "Error! Could Not Dial"; |
| 151 |
fatalErr = true; |
| 152 |
} |
| 153 |
continue; |
| 154 |
} |
| 155 |
int numBytes = 0; |
| 156 |
byte[] readBuffer = new byte[20]; |
| 157 |
String sReadBuff = ""; |
| 158 |
boolean dLoop = true; |
| 159 |
while (dLoop) { |
| 160 |
try { |
| 161 |
while (inputStream.available() > 0) { |
| 162 |
numBytes = inputStream.read(readBuffer); |
| 163 |
String tmpR = new String(readBuffer); |
| 164 |
sReadBuff += tmpR.substring(0, numBytes); |
| 165 |
} |
| 166 |
} catch (IOException e) { |
| 167 |
dLoop = false; |
| 168 |
process = false; |
| 169 |
} |
| 170 |
if (sReadBuff.indexOf("NO CARRIER") != -1) { |
| 171 |
errMessage = ""; |
| 172 |
dLoop = false; |
| 173 |
} else if (sReadBuff.indexOf("ERROR") != -1) { |
| 174 |
errMessage = "Error! Recieved Data Not Clean " + num + " " + comName; |
| 175 |
dLoop = false; |
| 176 |
} else if (sReadBuff.length() > 5000) { |
| 177 |
errMessage = ""; |
| 178 |
dLoop = false; |
| 179 |
} |
| 180 |
} |
| 181 |
if (errMessage.equals("")) { |
| 182 |
comThread.queRequest(requestId, sReadBuff, "Ready"); |
| 183 |
comLog.log("Data Recieved " + " " + requestId + " " + num); |
| 184 |
System.out.println("*********" + sReadBuff + "*********"); |
| 185 |
} else { |
| 186 |
if (!fatalErr) { |
| 187 |
comThread.queRequest(requestId, errMessage, "Error"); |
| 188 |
comLog.log(errMessage + " " + comName + " " + num); |
| 189 |
System.out.println("*********" + errMessage + "*********"); |
| 190 |
} |
| 191 |
} |
| 192 |
closePort(); |
| 193 |
resetCount = 0; |
| 194 |
resetCount2 = 0; |
| 195 |
resetCount3 = 0; |
| 196 |
num = "Nil"; |
| 197 |
process = false; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private boolean openPort() { |
| 205 |
if (serialPort == null) { |
| 206 |
comLog.debugInfo("**********Open Port Routine***********"); |
| 207 |
try { |
| 208 |
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); |
| 209 |
comLog.debugInfo("**********Port Open***********"); |
| 210 |
} catch (PortInUseException e) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
if (inputStream == null) { |
| 214 |
try { |
| 215 |
inputStream = serialPort.getInputStream(); |
| 216 |
} catch (IOException e) { |
| 217 |
return false; |
| 218 |
} |
| 219 |
} |
| 220 |
if (outputStream == null) { |
| 221 |
try { |
| 222 |
outputStream = serialPort.getOutputStream(); |
| 223 |
} catch (IOException e) { |
| 224 |
return false; |
| 225 |
} |
| 226 |
} |
| 227 |
try { |
| 228 |
serialPort.addEventListener(this); |
| 229 |
} catch (TooManyListenersException e) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
serialPort.notifyOnDataAvailable(true); |
| 233 |
|
| 234 |
|
| 235 |
try { |
| 236 |
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, |
| 237 |
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); |
| 238 |
} catch (UnsupportedCommOperationException e) { |
| 239 |
return false; |
| 240 |
} |
| 241 |
} |
| 242 |
return true; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
private void closePort() { |
| 248 |
if (serialPort != null) { |
| 249 |
serialPort.notifyOnDataAvailable(false); |
| 250 |
serialPort.removeEventListener(); |
| 251 |
if (inputStream != null) { |
| 252 |
try { |
| 253 |
inputStream.close(); |
| 254 |
inputStream = null; |
| 255 |
} |
| 256 |
catch (IOException e) {} |
| 257 |
} |
| 258 |
if (outputStream != null) { |
| 259 |
try { |
| 260 |
outputStream.close(); |
| 261 |
outputStream = null; |
| 262 |
} |
| 263 |
catch (IOException e) {} |
| 264 |
} |
| 265 |
serialPort.close(); |
| 266 |
serialPort = null; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
private boolean reset() { |
| 274 |
try { |
| 275 |
outputStream.write(new String("atz").getBytes()); |
| 276 |
outputStream.write((byte)0x0D); |
| 277 |
outputStream.write((byte)0x0A); |
| 278 |
} catch (IOException e) { |
| 279 |
return false; |
| 280 |
} |
| 281 |
int waitingCount = 0; |
| 282 |
waitForInput = true; |
| 283 |
while (waitForInput) { |
| 284 |
try { |
| 285 |
Thread.sleep(100); |
| 286 |
} catch (InterruptedException e) {} |
| 287 |
waitingCount ++; |
| 288 |
|
| 289 |
if (waitingCount > 20) { |
| 290 |
return false; |
| 291 |
} |
| 292 |
} |
| 293 |
int numBytes = 0; |
| 294 |
byte[] readBuffer = new byte[20]; |
| 295 |
String sReadBuff = ""; |
| 296 |
try { |
| 297 |
while (inputStream.available() > 0) { |
| 298 |
numBytes = inputStream.read(readBuffer); |
| 299 |
String tmpR = new String(readBuffer); |
| 300 |
sReadBuff += tmpR.substring(0, numBytes); |
| 301 |
} |
| 302 |
} catch (IOException e) { |
| 303 |
return false; |
| 304 |
} |
| 305 |
|
| 306 |
if (sReadBuff.indexOf("OK") != -1) { |
| 307 |
try { |
| 308 |
Thread.sleep(1000); |
| 309 |
} catch (InterruptedException e) {} |
| 310 |
return true; |
| 311 |
} |
| 312 |
return false; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
private boolean dial() { |
| 319 |
try { |
| 320 |
comLog.debugInfo("**********" + num + "***********"); |
| 321 |
outputStream.write(new String("atd").getBytes()); |
| 322 |
outputStream.write(num.getBytes()); |
| 323 |
outputStream.write((byte)0x0D); |
| 324 |
outputStream.write((byte)0x0A); |
| 325 |
} catch (IOException e) { |
| 326 |
errMessage = "Error! Could Not Write To Port "; |
| 327 |
comLog.debugInfo("**********Error Writing***********"); |
| 328 |
return false; |
| 329 |
} |
| 330 |
int waitingCount = 0; |
| 331 |
waitForInput = true; |
| 332 |
while (waitForInput) { |
| 333 |
try { |
| 334 |
Thread.sleep(100); |
| 335 |
} catch (InterruptedException e) {} |
| 336 |
waitingCount ++; |
| 337 |
|
| 338 |
if (waitingCount > 200) { |
| 339 |
errMessage = "Error! Timed Out Waiting For Response"; |
| 340 |
return false; |
| 341 |
} |
| 342 |
} |
| 343 |
int numBytes = 0; |
| 344 |
byte[] readBuffer = new byte[20]; |
| 345 |
String sReadBuff = ""; |
| 346 |
boolean dLoop = true; |
| 347 |
while (dLoop) { |
| 348 |
try { |
| 349 |
while (inputStream.available() > 0) { |
| 350 |
numBytes = inputStream.read(readBuffer); |
| 351 |
String tmpR = new String(readBuffer); |
| 352 |
sReadBuff += tmpR.substring(0, numBytes); |
| 353 |
} |
| 354 |
if (sReadBuff.indexOf("NO CARRIER") != -1) { |
| 355 |
errMessage = "Error! No Carrier"; |
| 356 |
return false; |
| 357 |
} else if (sReadBuff.indexOf("BUSY") != -1) { |
| 358 |
errMessage = "Error! Busy"; |
| 359 |
return false; |
| 360 |
} else if (sReadBuff.indexOf("NO DIAL TONE") != -1) { |
| 361 |
errMessage = "Error! No Dial Tone"; |
| 362 |
return false; |
| 363 |
} else if (sReadBuff.indexOf("OK") != -1) { |
| 364 |
errMessage = "Error! Voice Call"; |
| 365 |
return false; |
| 366 |
} else if (sReadBuff.indexOf("CONNECT") != -1) { |
| 367 |
return true; |
| 368 |
} |
| 369 |
} catch (IOException e) { |
| 370 |
errMessage = "Error! Could Not Read From Com Port"; |
| 371 |
return false; |
| 372 |
} |
| 373 |
} |
| 374 |
errMessage = "Error! Invalid Data " + sReadBuff; |
| 375 |
return false; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
public void serialEvent(SerialPortEvent event) { |
| 382 |
switch(event.getEventType()) { |
| 383 |
case SerialPortEvent.BI: |
| 384 |
case SerialPortEvent.OE: |
| 385 |
case SerialPortEvent.FE: |
| 386 |
case SerialPortEvent.PE: |
| 387 |
case SerialPortEvent.CD: |
| 388 |
case SerialPortEvent.CTS: |
| 389 |
case SerialPortEvent.DSR: |
| 390 |
case SerialPortEvent.RI: |
| 391 |
case SerialPortEvent.OUTPUT_BUFFER_EMPTY: |
| 392 |
break; |
| 393 |
case SerialPortEvent.DATA_AVAILABLE: |
| 394 |
waitForInput = false; |
| 395 |
break; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public void processRequest(String req, String num) { |
| 405 |
requestId = req; |
| 406 |
this.num = num; |
| 407 |
process = true; |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public boolean isProcessing() { |
| 415 |
return process; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
public boolean isProcessing(String qID) { |
| 424 |
return requestId.equals(qID); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
class SNLogger |
| 429 |
{ |
| 430 |
public void debugInfo(java.lang.String it) |
| 431 |
{ |
| 432 |
} |
| 433 |
public void log(java.lang.String it) |
| 434 |
{ |
| 435 |
} |
| 436 |
} |
| 437 |
class SNComThread |
| 438 |
{ |
| 439 |
public void queRequest(java.lang.String a, java.lang.String b, java.lang.String c) |
| 440 |
{ |
| 441 |
} |
| 442 |
} |
| 443 |
class SNConfig |
| 444 |
{ |
| 445 |
} |