-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGemini.java
623 lines (525 loc) · 19.2 KB
/
Gemini.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import java.io.*;
import java.util.*;
import javax.comm.*;
public class Gemini implements Runnable, SerialPortEventListener {
//class Gemini {
Thread listenThread;
// private static CommPortIdentifier portId;
private static OutputStream outputStream;
private static InputStream inputStream;
private static SerialPort serialPort;
private static byte [] RcvBuf;// = new byte[200];
private static int SndCkSum;
private static int SndKey;
private static int SndKeyOff;
private static int CkSum;
private static int Key;
private static int keyoff;
private static int i;
public static String keyPadMsg1="", keyPadMsg2="";
public static boolean debug=true;
public static boolean connected=false;
public static String passCode;
public static void main(String[] args) {
passCode="123456";
if (Connect()) {
// Rcv();
// GetLog();
// GetStatus(2); //open zones
// GetStatus(3); //protected zones
// System.out.println("And the version is:" + GetPanelType());
// if (!Arm())
// System.out.println("Arm failed!");
// if (!DisArm())
// System.out.println("Disarm failed!");
// GetZoneDescrs();
// Rcv();
Disconnect();
}
else
System.out.println("Failed to connect to gemini!!");
}
public static boolean Connect() {
if ((passCode.length() != 6) || (serialPort==null))
return (false);
else {
if (debug)
System.out.println ("\nAbout to send security cmd.\n");
if (serialPort.isCTS() == false) {
i=0;
if (debug)
System.out.println("CTS false, waiting in connect");
while (serialPort.isCTS() == false)
i++;
if (debug)
System.out.println("CTS took "+ i);
}
SendByte(0x86);
if (serialPort.isCTS() == true) {
i=0;
if (debug)
System.out.println("CTS true, waiting in connect");
while (serialPort.isCTS() == true)
i++;
if (debug)
System.out.println("CTS took "+ i);
}
serialPort.setRTS(true);
byte[] num= new byte[6];
if (passCode.length() != 6)
return (false);
SndKeyOff=0x05;
SendByte(SndKeyOff);
SndKey = (SndKeyOff + 0x35) & 0xFF;
SndCkSum = (SndKeyOff + 1) & 0xFF;
SndChr(0x1A);
SndChr(0x06);
SndChr(0x00);
SndChr(0x00);
SndChr(0x00);
SndChr(0x00);
num=passCode.getBytes();
for (int i=0;i<6;i++) {
if (num[i]==48)
SndChr(10);
else
SndChr((int) num[i] - 48); //-48 to convert "0" ascii (48) to numeric 0
}
SndChr(SndCkSum);
if (debug)
System.out.println ("\nDone sending security cmd.\n");
boolean result = Rcv() && (RcvBuf[4] == 0);
connected=result;
return (result);
}
}
public static void Disconnect() {
serialPort.setRTS(false);
if (debug)
System.out.print("Waiting for panel to finish\n");
i=0;
while (serialPort.isCTS() == false)
i++;
serialPort.close();
if (debug)
System.out.println("Panel finished in " + i);
}
public static boolean Disarm() {
if (debug)
System.out.println ("\nAbout to DisArm.\n");
SndKeyOff=0x86;
SendByte(SndKeyOff);
SndKey = (SndKeyOff + 0x35) & 0xFF;
SndCkSum = (SndKeyOff + 1) & 0xFF;
SndChr(0x19);
SndChr(0x00);
SndChr(0x03);
SndChr(0x00);
SndChr(0x01);
SndChr(0x80);
SndChr(SndCkSum);
boolean Result=Rcv();
if (debug)
System.out.println ("\nDone DisArming.\n");
return (Result);
}
public static boolean Arm() {
if (debug)
System.out.println ("\nAbout to Arm.\n");
SndKeyOff=0x86;
SendByte(SndKeyOff);
SndKey = (SndKeyOff + 0x35) & 0xFF;
SndCkSum = (SndKeyOff + 1) & 0xFF;
SndChr(0x19);
SndChr(0x00);
SndChr(0x01);
SndChr(0x00);
SndChr(0x01);
SndChr(0x80);
SndChr(SndCkSum);
boolean Result = Rcv();
if (debug)
System.out.println ("\nDone Arming.\n");
return (Result);
}
public static void GetLog() {
if (debug)
System.out.println ("\nAbout to get log.\n");
SndKeyOff=0x86;
SendByte(SndKeyOff);
SndKey = (SndKeyOff + 0x35) & 0xFF;
SndCkSum = (SndKeyOff + 1) & 0xFF;
SndChr(0x32);
SndChr(0x04);
SndChr(0x1f);
SndChr(0xfc);
SndChr(0x00);
SndChr(0x00);
SndChr(SndCkSum);
Rcv(); // Returns # of log entries, we'll ignore for now
SndChr(0x32);
SndChr(0x80);
SndChr(0x03);
SndChr(0x02);
SndChr(0x00);
SndChr(0x00);
SndChr(SndCkSum);
if (debug)
System.out.println ("\nDone getting log.\n");
}
public static byte GetPanelType() {
if (debug)
System.out.println ("\nAbout to send version check.\n");
SndKeyOff=0x86;
SendByte(SndKeyOff);
SndKey = (SndKeyOff + 0x35) & 0xFF;
SndCkSum = (SndKeyOff + 1) & 0xFF;
SndChr(0x1C);
SndChr(0x00);
SndChr(0x00);
SndChr(0x00);
SndChr(0x00);
SndChr(0x00);
SndChr(SndCkSum);
if (debug)
System.out.println ("\nDone sending version check.\n");
if (!Rcv())
return(0);
else
return(RcvBuf[5]);
}
public static void GetZoneDescrs() {
if (debug)
System.out.println ("\nAbout to zone descriptions.\n");
SndKeyOff=0x86;
SendByte(SndKeyOff);
SndKey = (SndKeyOff + 0x35) & 0xFF;
SndCkSum = (SndKeyOff + 1) & 0xFF;
SndChr(0x05);
SndChr(0x00);
SndChr(0x00);
SndChr(0x00);
SndChr(0x00);
SndChr(0x00);
SndChr(SndCkSum);
if (debug)
System.out.println ("\nDone sending zone cmd.\n");
}
public static void GetStatus(int status) {
/* Status values as follows:
0 = time in seconds since 1/1/70
1 = shorted zones
2 = open zones
3 = protected zones
4 = manually bypassed zones
5 = remotely bypassed zones
6 = RF open zones
7 = RF shorted zones
*/
if (debug)
System.out.println ("\nAbout to get status.\n");
SndKeyOff=0x86;
SendByte(SndKeyOff);
SndKey = (SndKeyOff + 0x35) & 0xFF;
SndCkSum = (SndKeyOff + 1) & 0xFF;
SndChr(0x39);
SndChr(0x00);
SndChr(0x00);
SndChr((byte) status);
SndChr(0x00);
SndChr(0x00);
SndChr(SndCkSum);
if (debug)
System.out.println ("\nDone getting status.\n");
}
/* Send and receive a byte at a time */
private static void SendByte(int info) {
try {
outputStream.write((byte) (info) & 0xFF);
// System.out.print ("Raw sent: " + Integer.toHexString((byte) (info) & 0xFF));
} catch (IOException e) {}
}
private static void SndChr(int TmpByte) {
int Send;
SndCkSum = (SndCkSum + TmpByte) & 0xFF;
Send= TmpByte ^ SndKey;
SendByte(Send);
if (debug)
System.out.println (" Byte to be sent=" + Integer.toHexString(TmpByte) + " ( " + TmpByte + "), SndKey="+Integer.toHexString(SndKey) + " Actual byte sent = " + Integer.toHexString(Send));
SndKey = (SndKey + SndKeyOff) & 0xFF;
}
/* Receive response */
private static boolean Rcv() {
byte cmdByte, countByte;
int rcvcksum;
if (serialPort.isCTS() == true) {
i=0;
System.out.println("CTS true, waiting in rcv??");
while (serialPort.isCTS() == true)
i++;
System.out.println("Done waiting in rcv "+i);
}
CkSum=1;
keyoff= ReadByte();
CkSum=(CkSum+keyoff) & 0xFF;
Key=(keyoff + 0x35) & 0xFF;
cmdByte = ReadAndChecksum();
countByte= ReadAndChecksum();
RcvBuf = new byte[countByte + 6];
RcvBuf[0] = cmdByte;
RcvBuf[1] = countByte;
if (debug)
System.out.println ("\nEntering for loop...");
for (i=2; i<=RcvBuf[1]+5; i++)
RcvBuf[i] = ReadAndChecksum();
if (debug)
System.out.println ("Leaving for loop...\n");
i = ReadByte();
rcvcksum = (int) (i ^ Key);
if (debug)
System.out.println ("Checksum check is " + (boolean) (rcvcksum==CkSum) + "\n");
return ((cmdByte!=0x3F) && (rcvcksum==CkSum));
}
private static int ReadByte() {
byte [] readBuffer = new byte[1];
try {
int numBytes = inputStream.read(readBuffer);
// System.out.print ("Numbytes="+numBytes+" buffer=" + readBuffer[0]);
// System.out.println ("Raw rcv: " + readBuffer[0]);
}catch (IOException e) {}
return((int) readBuffer[0] & 0xFF); //Remove sign bit
}
private static byte ReadAndChecksum() {
byte Info = (byte) (ReadByte() ^ Key);
if (debug)
System.out.println ("Byte & checksum read=" + Integer.toHexString(Info & 0xFF) + " char=" + (char) Info);
CkSum = (CkSum + Info) & 0xFF;
Key = (Key + keyoff) & 0xFF;
return (Info);
}
public void Wait(int delay) {
try {
Thread.currentThread().sleep(delay * 1000);
Thread.currentThread().yield();
} catch(InterruptedException e) {}
}
public Gemini() {}
/************ Thread ***********/
public Gemini(String portName) {
boolean portFound=true;
CommPortIdentifier portId=null;
try {
portId=CommPortIdentifier.getPortIdentifier(portName);
} catch (NoSuchPortException e) {
System.out.println("Error! No such port '" + portName + "' found.");
keyPadMsg1="Error! No such port '" + portName + "' found.";
portFound=false;
}
if (portFound) {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_EVEN);
} catch (UnsupportedCommOperationException e) {}
/* try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
listenThread = new Thread(this);
listenThread.start(); */
}
}
public void run() {
try {
// while (true)
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readByte = new byte[1];
byte[] readBuffer = new byte[34];
byte[] keyPad = new byte[32];
byte checkSum;
byte counter=0;
byte i=0;
boolean notDone=true;
boolean panel;
boolean status;
if (false) {
panel=false;
status=false;
try {
int numBytes = inputStream.read(readByte);
while (notDone && (inputStream.available() > 0)) {
if (readByte[0] == 3) {
inputStream.read(readByte);
if (readByte[0] == 0x25) {
notDone=false;
panel=true;
}
}
if (readByte[0] == 1) {
inputStream.read(readByte);
if (readByte[0] == 6) {
notDone=false;
status=true;
}
}
if (notDone) {
// Dump unrecognized data.
System.out.print(" Dumping " + Integer.toHexString((int) readByte[0] & 0xFF) );
inputStream.read(readByte);
}
}
if (notDone)
System.out.println("\nRan out of data??");
if (status) {
// Real time status message
i=0;
while (inputStream.available() < 4) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
counter++;
if (counter>100)
break;
}
if (counter<100) {
while (i < 4) {
inputStream.read(readByte);
readBuffer[i++]=readByte[0];
}
if (debug) {
System.out.println("Yeah!! Got a status msg!");
// System.out.println(Integer.toHexString((int) readBuffer[0] & 0xFF) + " " + Integer.toHexString((int) readBuffer[1] & 0xFF) + " " + Integer.toHexString((int) readBuffer[2] & 0xFF) + " " + Integer.toHexString((int) readBuffer[3] & 0xFF));
// System.out.println("Yeah!! Finished status msg!\n");
}
}
else
System.out.println("Timeout in status!");
}
if (panel) {
// Change in panel display
inputStream.read(readByte);
byte icon1=readByte[0];
inputStream.read(readByte);
byte icon2=readByte[0];
i=0;
while (inputStream.available() < keyPad.length) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
counter++;
if (counter>100)
break;
}
if (inputStream.available() >= keyPad.length + 1) {
numBytes = inputStream.read(keyPad);
inputStream.read(readByte);
checkSum=readByte[0];
}
if (counter<100) {
String keyPadMsgs = new String(keyPad);
keyPadMsg1 = new String(keyPadMsgs.substring(0,16));
keyPadMsg2 = new String(keyPadMsgs.substring(16));
String iconStr = new String();
iconStr=processIcons(icon1, icon2);
if (debug)
System.out.println("Got a new panel msg!");
/* outWml = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream("C:\\Program Files\\Apache Group\\Apache\\htdocs\\a.wml")));
outHtml = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream("C:\\Program Files\\Apache Group\\Apache\\htdocs\\a.html")));
outHtml.write(firstLine + "<br>" + secondLine + "<br>");
if (iconStr != "")
outHtml.write(iconStr+"\n");
outWml.write("<?xml version=\"1.0\"?>");
outWml.write("<!DOCTYPE wml PUBLIC \"-//PHONE.COM//DTD WML 1.1//EN\"\n");
outWml.write("\"http://www.phone.com/dtd/wml11.dtd\"><wml>\n");
outWml.write("<head>\n");
outWml.write("<meta http-equiv=\"Cache_Control\" content=\"max-age=0\"/>\n");
outWml.write("</head>\n");
outWml.write("<card ontimer='a.wml'><timer value='30'/><p>\n");
outWml.write(firstLine + "<br/>" + secondLine+"\n");
if (iconStr != "")
outWml.write(iconStr+"\n");
outWml.write("</p></card></wml>\n");
outWml.close();
outHtml.close();
System.out.println(firstLine + "\n" + secondLine);
if (iconStr != "")
System.out.println(iconStr);
System.out.println("****************");*/
}
}
} catch (IOException e) {}
}
break;
}
}
private static String processIcons(byte icon1, byte icon2) {
String iconStr = new String("");
if (icon1!=0) {
if ((icon1 & 1) ==1)
iconStr = iconStr + "Red ";
if ((icon1 & 2) ==2)
iconStr = iconStr + "Green ";
if ((icon1 & 4) ==4)
iconStr = iconStr + "Bypass ";
if ((icon1 & 8) ==8)
iconStr = iconStr + "SysTbl ";
if ((icon1 & 16) ==16)
iconStr = iconStr + "Fire ";
if ((icon1 & 32) ==32)
iconStr = iconStr + "FireTbl ";
if ((icon1 & 64) ==64)
iconStr = iconStr + "AudOnCont ";
// if ((icon1 & 128) ==128)
// iconStr = iconStr + "E/E ";
if (icon2!=0 && iconStr != "")
iconStr+="\n";
}
if (icon2!=0) {
iconStr+="Pulse: ";
if ((icon2 & 1) ==1)
iconStr = iconStr + "Alarm ";
if ((icon2 & 2) ==2)
iconStr = iconStr + "Green ";
if ((icon2 & 4) ==4)
iconStr = iconStr + "Bypass ";
if ((icon2 & 8) ==8)
iconStr = iconStr + "SysTbl ";
if ((icon2 & 16) ==16)
iconStr = iconStr + "Instant ";
if ((icon2 & 64) ==64)
iconStr = iconStr + "AudCont ";
if ((icon2 & 128) ==128)
iconStr = iconStr + "Armed ";
}
return(iconStr);
}
}