-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSendSMS.java
More file actions
514 lines (447 loc) · 17.5 KB
/
SendSMS.java
File metadata and controls
514 lines (447 loc) · 17.5 KB
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
package Jvakt;
/*
* 2025-06-09 V.58 Michael Ekdal Changed the order of the createStatement to accomodate changes in postgresql-42.7.6.jar
* 2024-07-09 V.57 Michael Ekdal Added more info when reporting in status to Jvakt server.
* 2023-08-21 V.56 Michael Ekdal Increased sleep times when waiting for the operator response.
* 2023-01-03 V.55 Michael Ekdal Added send of the status to Jvakt server
* 2022-06-23 V.54 Michael Ekdal Added getVersion() to get at consistent version throughout all classes.
*/
import java.io.*;
import java.net.InetAddress;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
//import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//import java.sql.Timestamp;
import java.util.*;
import java.time.*;
public class SendSMS {
static String DBUrl = "jdbc:postgresql://localhost:5433/Jvakt";
static Connection conn = null;
// static boolean swDelete;
static boolean swTiming;
static boolean swShDay; // set when the scheduled day is active
static boolean swDormant = false;
static boolean swShowVer = true;
static boolean swOK = false;
static boolean swSTS = false;
static java.sql.Date zDate;
static java.sql.Timestamp zD;
static java.sql.Timestamp zTs;
static java.sql.Time zT;
static Long lhou, lmin, Lsec, Lrptdat, Lchktim;
// static int serrors = 0;
static boolean error = false;
// static int errors = 0;
// static int warnings = 0;
static boolean warning = false;
// static int infos = 0;
// static int resolved = 0;
static boolean resolved = false;
static String expected = "";
static String reply = "";
// static String subject = "";
static String body = "";
static String toSMSW;
static boolean swMail = false;
static Properties props;
static Properties prop;
static String toSMS;
static String SMShost;
static String SMSport;
static int SMSporti;
static Socket sock = null;
static OutputStreamWriter osw;
static InputStreamReader isr;
static String agent = null;
static String jvhost = "localhost";
static String jvport = "1956";
static int port = 1956;
static InetAddress inet;
static File configF;
// static Authenticator auth;
static List<String> listTo;
public static void main(String[] args ) throws IOException, UnknownHostException {
String version = "SendSMS ";
version += getVersion()+".58";
String database = "jVakt";
String dbuser = "jVakt";
String dbpassword = "";
String dbhost = "localhost";
String dbport = "5433";
// String jvhost = "localhost";
// String jvport = "1956";
String config = null;
Calendar cal = Calendar.getInstance();
for (int i=0; i<args.length; i++) {
if (args[i].equalsIgnoreCase("-config")) config = args[++i];
if (args[i].equalsIgnoreCase("-nover")) swShowVer =false;
}
if (config == null ) configF = new File("Jvakt.properties");
else configF = new File(config,"Jvakt.properties");
if (swShowVer) System.out.println("----- Jvakt: "+new Date()+" Version: "+version+" - config file: "+configF);
prop = new Properties();
InputStream input = null;
input = new FileInputStream(configF);
prop.load(input);
// get the property value and print it out
database = prop.getProperty("database");
dbuser = prop.getProperty("dbuser");
dbpassword = prop.getProperty("dbpassword");
if (dbpassword.startsWith("==y")) {
byte[] decodedBytes = Base64.getDecoder().decode(dbpassword.substring(3));
String decodedString = new String(decodedBytes);
dbpassword=decodedString;
}
dbhost = prop.getProperty("dbhost");
dbport = prop.getProperty("dbport");
jvport = prop.getProperty("jvport");
int jvporti = Integer.parseInt(jvport);
jvhost = prop.getProperty("jvhost");
toSMSW = prop.getProperty("toSMS");
SMShost = prop.getProperty("SMShost");
SMSport = prop.getProperty("SMSport");
SMSporti = Integer.parseInt(SMSport);
String mode = prop.getProperty("mode");
if (!mode.equalsIgnoreCase("active")) swDormant = true;
input.close();
listTo = new ArrayList<String>(); // All SMS numbers.
String[] toAddr = toSMSW.split("\\,");
for(int i=0 ; i<toAddr.length;i++) {
// System.out.println(toAddr[i]);
listTo.add(toAddr[i]);
}
// Iterator iterator = listTo.iterator();
// while(iterator.hasNext()) {
//// String element = (String) iterator.next();
// System.out.println(iterator.next());
// }
// try {
SendMsg jm = new SendMsg(jvhost, jvporti); // kollar om JvaktServer är tillgänglig.
// System.out.println(jm.open());
if (jm.open().startsWith("DORMANT")) {
swDormant = true;
}
jm.close();
// }
// catch (IOException e1) {
// System.err.println(e1);
// System.err.println(e1.getMessage());
// }
if (swDormant) {
System.out.println(LocalDateTime.now()+" *** Jvakt in DORMANT mode, SendSMS exiting *** ");
System.exit(4);
}
LocalDateTime nu = LocalDateTime.now(); // The current date and time
// LocalDateTime midnatt = LocalDateTime.of(nu.getYear(), nu.getMonthValue(), nu.getDayOfMonth() , 0, 0, 0, 0);
// Timestamp mi = Timestamp.valueOf(midnatt);
DayOfWeek DOW = nu.getDayOfWeek();
Statement stmt = null;
String s;
// boolean swHits;
// String cause = "";
zDate = new java.sql.Date((new Date(System.currentTimeMillis())).getTime());
zTs = new java.sql.Timestamp((new Date(System.currentTimeMillis())).getTime());
// System.out.println("**********SendSMS ******** " + LocalDateTime.now());
try {
Class.forName("org.postgresql.Driver").newInstance();
DBUrl = "jdbc:postgresql://"+dbhost+":"+dbport+"/"+database;
// System.out.println(DBUrl);
// System.out.println("dbuser= " + dbuser +" dbpassword "+ dbpassword);
conn = DriverManager.getConnection(DBUrl,dbuser,dbpassword);
// conn.setAutoCommit(true);
conn.setAutoCommit(false);
s = new String("select * from status " +
"WHERE state='A' " +
" and (sms='M' or sms='T' or sms='R')" +
" and prio < 30" +
";");
// System.out.println(s);
// stmt = conn.createStatement(ResultSet.CONCUR_UPDATABLE,ResultSet.TYPE_SCROLL_INSENSITIVE);
stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE,ResultSet.CLOSE_CURSORS_AT_COMMIT );
stmt.setFetchSize(1000);
ResultSet rs = stmt.executeQuery(s);
// swHits = false; // is there already a record?
swSTS = true;
while (rs.next()) {
System.out.println("\n"+LocalDateTime.now()+" **** main RS - State:"+rs.getString("state")+" Id:" + rs.getString("id")+" Type:"+rs.getString("type")+" Prio:"+rs.getString("prio")+" Console:"+rs.getString("console")+" Status:"+rs.getString("status")+ " Sms:"+rs.getString("sms"));
// swHits = true;
swTiming = false;
error = false;
resolved = false;
warning = false;
zD = rs.getTimestamp("rptdat");
Lsec = (zTs.getTime() / 1000 - zD.getTime() / 1000);
Lchktim = rs.getTime("chktim").getTime();
Lrptdat = rs.getTime("rptdat").getTime();
swShDay = false;
if (rs.getString("chkday").startsWith("*ALL") || rs.getString("chkday").startsWith(DOW.name().substring(0, 2) )) {
cal.setTime(rs.getTime("chktim"));
// if (nu.getHour() > rs.getTime("chktim").getHours() ) {
if (nu.getHour() > cal.get(Calendar.HOUR_OF_DAY) ) {
swShDay = true; System.out.println(LocalDateTime.now()+" Timmen swShDay: "+swShDay);
}
// else if (nu.getHour() == rs.getTime("chktim").getHours() && nu.getMinute() > rs.getTime("chktim").getMinutes() ) {
else if (nu.getHour() == cal.get(Calendar.HOUR_OF_DAY) && nu.getMinute() > cal.get(Calendar.MINUTE) ) {
swShDay = true; System.out.println(LocalDateTime.now()+" Minuten swShDay: "+swShDay);
}
// else if (nu.getHour() == rs.getTime("chktim").getHours() && nu.getMinute() == rs.getTime("chktim").getMinutes() && nu.getSecond() > rs.getTime("chktim").getSeconds() ) {
else if (nu.getHour() == cal.get(Calendar.HOUR_OF_DAY) && nu.getMinute() == cal.get(Calendar.MINUTE) && nu.getSecond() > cal.get(Calendar.SECOND) ) {
swShDay = true; System.out.println(LocalDateTime.now()+" Sekunden swShDay: "+swShDay);
}
// check chktimto
cal.setTime(rs.getTime("chktimto"));
if (nu.getHour() > cal.get(Calendar.HOUR_OF_DAY) ) {
swShDay = false;
}
else if (nu.getHour() == cal.get(Calendar.HOUR_OF_DAY) && nu.getMinute() > cal.get(Calendar.MINUTE) ) {
swShDay = false;
}
else if (nu.getHour() == cal.get(Calendar.HOUR_OF_DAY) && nu.getMinute() == cal.get(Calendar.MINUTE) && nu.getSecond() > cal.get(Calendar.SECOND) ) {
swShDay = false;
}
}
if (rs.getInt("prio") <= 10) swShDay = true; // always handle prio 10 and below.
System.out.println(LocalDateTime.now()+" swShDay: "+swShDay);
if (swShDay) {
if (rs.getString("sms").equalsIgnoreCase("S")) {
System.out.println(LocalDateTime.now()+" -- Already got an S in sms, continues...");
continue;
}
body = rs.getString("id")+" "+rs.getString("body");
if (rs.getString("sms").equalsIgnoreCase("M") ) {
System.out.println(LocalDateTime.now()+" Error " + body);
error = true;
}
else if (rs.getString("sms").equalsIgnoreCase("R")) {
System.out.println(LocalDateTime.now()+" Resolved " + body);
resolved = true;
}
else {
body = rs.getString("id")+" The Jvakt agent did not report in due time.";
System.out.println(LocalDateTime.now()+" Timeout " + body);
warning = true;
}
if (rs.getString("sms").equalsIgnoreCase("R"))
rs.updateString("sms", " ");
else rs.updateString("sms", "S");
rs.updateTimestamp("smsdat", new java.sql.Timestamp((new Date(System.currentTimeMillis())).getTime()));
try { rs.updateRow(); } catch(NullPointerException npe2) {}
System.out.println(LocalDateTime.now()+" After update");
if (sendSMS()) conn.commit();
else conn.rollback();
}
}
rs.close();
stmt.close();
conn.commit();
conn.close();
}
catch (SQLException e) {
System.err.println("SQLExeption " + e);
// System.err.println(e.getMessage());
}
catch (Exception e) {
System.err.println("Exeption " + e);
// System.err.println(e.getMessage());
}
finally {
if (swSTS) try {sendSTS(true);} catch (IOException e) { e.printStackTrace();}
if (!swSTS) try {sendSTS(false);} catch (IOException e) { e.printStackTrace();}
}
}
// Connects to the SMS terminal and sends the text
static boolean sendSMS() {
swOK = false;
System.out.println(LocalDateTime.now()+" --- Sending Jvakt SMS ---\n");
if (error) {
body = "ERROR: " + body;
}
if (warning) {
body = "TIME-OUT: "+body ;
}
if (resolved) {
body = "RESOLVED: "+body ;
}
toSMSW = "";
swOK = false;
// int n = 0;
for(Object object : listTo) {
// if (n>0) toSMSW = toEmailW + ",";
// n++;
// String element = (String) object;
toSMSW = (String) object;
toSMS = toSMSW;
// System.out.println("To:"+toEmailW+" Subject: " + subject );
System.out.println(LocalDateTime.now()+" --- Sending SMS to: "+toSMS +" Body: " + body );
// Connect to Com-Server
try {
System.out.println(LocalDateTime.now()+" Connecting to: "+SMShost +":" + SMSporti );
// sock = new Socket( SMShost, SMSporti );
sock = new Socket();
sock.connect(new InetSocketAddress(SMShost, SMSporti), 2000);
sock.setSoTimeout( 2000 ); // receive timeout
osw = new OutputStreamWriter( sock.getOutputStream() );
isr = new InputStreamReader( sock.getInputStream() );
} catch( IOException e ) {
swOK = false;
System.out.println(LocalDateTime.now()+" IOExeption while connecting to SMShost" + e);
break;
}
// Sending ( http://chadselph.github.io/smssplit/ )
try {
System.out.println(LocalDateTime.now()+" Sending \"AT+CMGF=1\\r\\n\"" );
osw.write( "AT+CMGF=1\r\n" ); // SMS text mode
osw.flush();
ReceiveText();
// if (reply.indexOf("OK") > 0 ) System.out.println(LocalDateTime.now()+" Received OK "+reply);
if (reply.indexOf("O") > 0 ) System.out.println(LocalDateTime.now()+" Received OK "+reply);
// if (reply.startsWith("O") ) System.out.println(LocalDateTime.now()+" Received OK "+reply);
// System.out.println("Sending AT+CMGS="+toSMS +"\\r\\n" );
System.out.println(LocalDateTime.now()+" Sending AT+CMGS="+toSMS +"\\r" );
// osw.write( "AT+CMGS="+ toSMS + "\r\n" );
osw.write( "AT+CMGS="+ toSMS + "\r" );
osw.flush();
ReceiveText();
if (reply.indexOf(">") > 0 ) System.out.println(LocalDateTime.now()+" Received > "+reply);
if (body.length() > 140 ) body = body.substring(0, 139);
body = body.replaceAll("_", "-"); // replace _ with - because SMS creates a �
body = body.replace('Å', 'A');
body = body.replace('Ä', 'A');
body = body.replace('Ö', 'O');
body = body.replace('å', 'a');
body = body.replace('ä', 'a');
body = body.replace('ö', 'o');
body = body.replaceAll("[^a-zA-Z0-9.:-]" , " ");
// System.out.println("Sending body: "+body +"\\r\\n" );
System.out.println(LocalDateTime.now()+" Sending body: "+body );
// osw.write( body + "\r\n" + "\u001A" );
osw.write( body +"\u001A" );
osw.flush();
ReceiveText();
// if (reply.indexOf("+CMGS") > 0 ) {
// if (reply.startsWith("+")) {
if (reply.indexOf("+") > 0 ) {
System.out.println(LocalDateTime.now()+" Received +CMGS "+reply);
swOK = true;
} else {
// swOK = false;
System.out.println(LocalDateTime.now()+" Did not receive +CMGS ! "+reply);
break;
}
} catch( IOException e ) {
// swOK = false;
System.out.println(LocalDateTime.now()+" IOExeption while sending SMS " + e);
break;
}
// closing and disconnecting
try {
osw.close();
isr.close();
sock.close();
} catch( IOException e ) {}
}
if (swOK) {
System.out.println(LocalDateTime.now()+" RETURN true");
return true;
}
else {
swSTS=false;
System.out.println(LocalDateTime.now()+" Failure sending SMS message");
try {sendSTS(false);} catch (IOException e) { e.printStackTrace();}
return false;
}
}
static public void ReceiveText() {
try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace();}
String s;
int i, len, timeouts;
char c[] = new char[ 100 ];
timeouts = 0;
for( ;; ) {
s = "";
if (timeouts>10) {
// timeout in input stream
System.out.println(LocalDateTime.now()+" Aborting because of timeout in isr!");
try {sendSTS(false);} catch (IOException e) { e.printStackTrace();}
System.exit(12);
// break;
}
try {
if (isr.ready()) {
try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace();}
len = isr.read( c, 0, 100 );
}
else {
timeouts++;
System.out.println(LocalDateTime.now()+" isr is not ready, waiting...");
try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace();}
continue;
}
if( len < 0 ) {
timeouts++;
System.out.println(LocalDateTime.now()+" no reply, waiting...");
try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace();}
continue;
// return;
}
for( i = 0; i < len; i++ ) {
if( c[ i ] != 0 ) s += c[ i ];
}
reply = s;
} catch (InterruptedIOException e) {
timeouts++;
System.out.println(LocalDateTime.now()+" ReceiverText InterruptedIOException in input stream: " + e);
} catch (IOException e) {
timeouts++;
System.out.println(LocalDateTime.now()+" ReceiverText IOException: " + e);
// break;
}
if( s.length() > 0 ) {
System.out.println(LocalDateTime.now()+" "+s);
System.out.flush();
try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace();}
return;
}
}
}
// sends status to the Jvakt server
static protected void sendSTS( boolean STS) throws IOException {
// System.out.println("--- Connecting to "+jvhost+":"+jvport);
Message jmsg = new Message();
SendMsg jm = new SendMsg(jvhost, port);
// System.out.println(jm.open());
jm.open();
jmsg.setId("Jvakt-SendSMS");
if (STS) {
jmsg.setBody("The SendSMS program is active. "+configF.getCanonicalPath());
jmsg.setRptsts("OK");
}
else {
jmsg.setBody("The SendSMS program aborted the SMS sending! "+configF.getCanonicalPath());
jmsg.setRptsts("ERR");
}
jmsg.setType("T");
inet = InetAddress.getLocalHost();
// System.out.println("-- Inet: "+inet);
agent = inet.toString();
jmsg.setAgent(agent);
if (!jm.sendMsg(jmsg)) System.out.println(LocalDateTime.now()+" --- Rpt to Jvakt Failed for Jvakt-SendSMS ---");
jm.close();
}
static private String getVersion() {
String version = "0";
try {
Class<?> c1 = Class.forName("Jvakt.Version",false,ClassLoader.getSystemClassLoader());
Version ver = new Version();
version = ver.getVersion();
}
catch (java.lang.ClassNotFoundException ex) {
version = "?";
}
return version;
}
}