📄 smssender.java
字号:
break;
case 2: // '\002'
s6 = JMTools.msgDecodeE8(s3);
s7 = "en";
break;
case 8: // '\b'
if(jm.getDEFAULT_ENCODE().toUpperCase().equals("GB2312"))
{
s6 = JMTools.msgDecodeGB(s3);
s7 = "cn";
break;
}
if(jm.getDEFAULT_ENCODE().toUpperCase().equals("UTF-8"))
{
s6 = JMTools.msgDecodeUTF8(s3);
s7 = "un";
} else
{
s6 = JMTools.msgDecodeUTF8(s3);
s7 = "un";
}
break;
default:
s6 = "error in decoding";
s7 = "en";
break;
}
System.out.print(s5 + "\tREC:\t" + k + "\t" + s4 + " ... ");
String s8 = "";
if(insertMessage(s8, s4, s5, s6) == 0)
System.out.println("OK");
else
System.out.println("Fail");
delMsg(k);
} else
if(l1 == SMS_STATUS_REPORT)
{
//int j2 = Integer.decode("0x" + s3.substring(0, 2)).intValue();
// s3 = s3.substring(2);
delMsg(k);
// 如果需要接收短信到达终端的确认信息,需要加下面一段话,将根据GSM网关模块的短信ID更新系统的短信ID状态为已收到
//updateFeedback(j2);
} else
{
delMsg(k);
}
} else
{
int l = Integer.decode(s1.substring(i1 - 1, i1)).intValue();
delMsg(l);
}
} while(true);
}
private boolean getWaitMessage()
{
if(intSendCount >= jm.getMAX_SEND_COUNT())
return false;
intSendCount++;
boolean flag = false;
try
{
CallableStatement cs = dataTools.conn.prepareCall( "begin DX_GET_SendingMSGID(?,?,?,?); end;" ) ;
cs.registerOutParameter(1, Types.INTEGER);
cs.registerOutParameter(2, Types.VARCHAR);
cs.registerOutParameter(3, Types.VARCHAR);
cs.registerOutParameter(4, Types.INTEGER);
cs.executeUpdate();
intMsgID = cs.getInt(1);
if (intMsgID!=-1)
{
strMsgMP = cs.getString(2);
strMsgText = cs.getString(3);
intMsgRetry = cs.getInt(4);
strMsgLanguage = "cn";
intMsgFeedback = 0;
flag = true;
}
cs.close();
}
catch(SQLException sqlexception)
{
System.out.println(JMTools.getDatetimeString() + "\t[database error]");
}
return flag;
}
private int sendNow(int i, String s, String s1, String s2, boolean flag)
{
byte abyte0[] = new byte[1024];
if(!jm.getCOUNTRY_PREFIX().equals("") && !jm.getCOUNTRY_PREFIX().equals("+") && s.startsWith("+") && !s.startsWith("+" + jm.getCOUNTRY_CODE()))
s = jm.getCOUNTRY_PREFIX() + s.substring(1);
String s9;
if(s2.equals("un"))
try
{
String s7 = new String(s1.getBytes(), "UTF-8");
if(s7.length() > 70)
s9 = new String(s7.substring(0, 70).getBytes("UTF-8"));
else
s9 = s1;
}
catch(Exception exception)
{
s9 = "error in encoding";
}
else
if(s2.equals("cn"))
try
{
String s8 = new String(s1.getBytes(), "GB2312");
if(s8.length() > 70)
s9 = new String(s8.substring(0, 70).getBytes("GB2312"));
else
s9 = s1;
}
catch(Exception exception1)
{
s9 = "error in encoding";
}
else
if(s2.equals("en"))
{
if(s1.length() > 160)
s9 = s1.substring(0, 160);
else
s9 = s1;
} else
if(s1.length() > 160)
s9 = s1.substring(0, 160);
else
s9 = s1;
String s10;
String s11;
int k;
if(s.startsWith("+"))
{
s10 = "91";
s11 = JMTools.encodeMP(s.substring(1));
k = s.length() - 1;
} else
{
s10 = "A1";
s11 = JMTools.encodeMP(s);
k = s.length();
}
if(k > 255)
return 1;
String s14 = JMTools.toHexString(k);
String s12;
String s13;
int l;
if(s2.equals("un"))
{
s12 = "08";
s13 = JMTools.msgEncodeUTF8(s9);
l = s13.length() / 2;
} else
if(s2.equals("cn"))
{
s12 = "08";
s13 = JMTools.msgEncodeGB(s9);
l = s13.length() / 2;
} else
if(s2.equals("en"))
{
s12 = "00";
s13 = JMTools.msgEncodeE7(s9);
l = s9.getBytes().length;
} else
{
s12 = "00";
s13 = JMTools.msgEncodeE7(s9);
l = s9.getBytes().length;
}
if(l > 255)
return 2;
String s15 = JMTools.toHexString(l);
String s16 = (flag ? "003100" : "001100") + s14 + s10 + s11 + "00" + s12 + "A7" + s15 + s13;
int i1 = (s16.length() - 2) / 2;
String s6 = "";
try
{
String s3 = "AT+CMGS=" + Integer.toString(i1) + "\r";
output.write(s3.getBytes());
output.flush();
s6 = "";
int j;
while((j = input.read(abyte0, 0, 1024)) != -1)
{
String s4 = new String(abyte0, 0, j);
s6 = s6 + s4;
if(s6.endsWith("> ") || s6.endsWith("ERROR\r\n"))
break;
}
if(!s6.endsWith("> "))
return 3;
s3 = s16 + "\032";
output.write(s3.getBytes());
output.flush();
s6 = "";
while((j = input.read(abyte0, 0, 1024)) != -1)
{
String s5 = new String(abyte0, 0, j);
s6 = s6 + s5;
if(s6.endsWith("OK\r\n") || s6.endsWith("ERROR\r\n") || s6.indexOf("\r\n+CMS ERROR") >= 0)
break;
}
}
catch(Exception exception2)
{
System.out.println(JMTools.getDatetimeString() + "\t[comm error : send msg]");
bCommError = true;
}
if(!s6.endsWith("OK\r\n"))
return 4;
return 0;
}
public void systemLoop()
{
bRun = true;
for(bSleep = true; bRun; bSleep = true)
{
if(bCommError)
{
try
{
input.close();
output.close();
conn.close();
conn = new Socket(InetAddress.getByName(jm.getGATEWAY_IP()), jm.getGATEWAY_PORT());
conn.setSoTimeout(jm.getTIMEOUT_SEC() * 1000);
input = new DataInputStream(conn.getInputStream());
output = new DataOutputStream(conn.getOutputStream());
}
catch(Exception exception)
{
System.out.println(JMTools.getDatetimeString() + "\t[comm error : refresh connection]");
}
bCommError = false;
initComm();
}
if(getMsgCount() > 0 && getMsgList() == 0)
processInbox();
intMsgID = 0;
intSendCount = 0;
while(getWaitMessage())
{
System.out.print(JMTools.getDatetimeString() + "\t");
if(strMsgMP.startsWith(":"))
{
if(strMsgMP.equals(":" + jm.getLINK_NAME()))
{
if(strMsgText.equals("STOP"))
{
System.out.println("CMD:\tClose current connection to IP-GSM gateway.");
bRun = false;
} else
if(strMsgText.equals("INIT"))
{
System.out.print("CMD:\tInitialize GSM terminal ... ");
if(initComm() == 0)
System.out.println("OK");
else
System.out.println("Fail");
} else
{
System.out.println("");
}
updateStatus(intMsgID, STATUS_NOT_SEND);
} else
{
System.out.println("");
updateStatus(intMsgID, STATUS_WAIT);
}
} else
{
System.out.print("SND:\t" + Integer.toString(intMsgID) + "\t" + strMsgMP + " ... ");
int errcode = 0;
errcode = sendNow(intMsgID, strMsgMP, strMsgText, strMsgLanguage, intMsgFeedback != 0);
if(errcode == 0)
{
updateStatus(intMsgID, STATUS_SENT_OK);
System.out.println("OK");
} else
{
updateStatus(intMsgID, STATUS_SENT_FAIL);
System.out.println("Fail"+errcode);
}
}
}
int i = 0;
while(bSleep)
{
try
{
Thread.sleep(1000L);
}
catch(Exception exception1)
{
System.out.println("System Error.");
}
if(++i >= jm.getSLEEP_SEC())
bSleep = false;
}
}
}
public void systemQuit()
{
try
{
output.close();
input.close();
conn.close();
}
catch(Exception exception)
{
System.out.println("Communication Error.");
}
dataTools.DataDisconnect();
System.out.println(JMTools.getDatetimeString() + "\tSYS:\tNormal Shutdown");
}
public static void main(String[] args) {
SMSSender jetmessage = new SMSSender();
jetmessage.systemLoop();
jetmessage.systemQuit();
System.exit(0);
}
protected static String JM_VERSION = "1.0";
public JMDefine jm;
public int STATUS_WAIT;
public int STATUS_NOT_SEND;
public int STATUS_SENT_OK;
public int STATUS_SENT_FAIL;
public int STATUS_RECEIVED;
public int STATUS_SENDING;
public int STATUS_CANCEL;
public int SMS_DELIVER;
public int SMS_SUBMIT;
public int SMS_STATUS_REPORT;
private boolean bCommError;
private int intSendCount;
private int intMsgID;
private int intMsgRetry;
private int intMsgFeedback;
private String strMsgMP;
private String strMsgText;
private String strMsgLanguage;
private String strMsgList;
public DataTools dataTools;
public Socket conn;
public DataInputStream input;
public DataOutputStream output;
public boolean bRun;
public boolean bSleep;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -