📄 smsserverhelper.java
字号:
package com.hualong.communication;
import com.hualong.Infos.ContinueCallLocationInfos;
import com.hualong.Infos.PersonInfos;
import com.hualong.Infos.SingleCallLocationInfos;
import com.hualong.Infos.VehicleInfos;
import com.hualong.Interfaces.CommunicationProtocol;
import com.hualong.Interfaces.SocketProtocol;
import com.hualong.application.ApplicationContext;
import com.hualong.communication.serial.SerialBean;
import com.hualong.core.device.Device;
import com.hualong.core.terminal.Terminal;
import com.hualong.database.DatabaseHelper;
import com.hualong.log.CommonLogger;
import com.hualong.tools.*;
/**
* Created by IntelliJ IDEA.
* User:
* Date: 2004-2-4
* Time: 10:39:11
* 模块功能描述:启动短消息服务,通过短消息通讯来接收数据,
* 并转发到接收数据方;
*/
public final class SMSServerHelper
implements SocketProtocol, CommunicationProtocol {
/**
* 串口短信通讯实例
*/
private SerialBean sb;
/**
* 短信收发服务器成功启动标志
*/
private volatile boolean bLoop;
/**
* 短信的发送缓冲区
*/
private String strWriteBuffer;
/**
* 短信的接收缓冲区
*/
private String strReadBuffer;
/**
* 网络数据包解析器
*/
private Parser parser;
/**
* 设置读写线程的睡眠时间(毫秒)
*/
private int nPause;
/**
* 读串口队列线程类
*/
private Reader reader;
/**
* 构造函数
*
* @param sb 串口实例
*/
public SMSServerHelper(SerialBean sb) {
if (sb == null) {
this.bLoop = false;
} else {
this.bLoop = true;
this.sb = sb;
}
this.nPause = 200;
}
/**
* 启动短信收发服务器
*/
public void startServer() {
this.parser = new Parser();
reader = new Reader();
reader.start();
}
/**
* 停止短信收发服务器
*/
public void stopServer() {
if (this.bLoop) {
this.bLoop = false;
this.sb.closePort();
}
}
/**
* 读串口队列线程操作类
*/
private class Reader extends Thread {
/**
* 构造函数
*/
public Reader() {
}
/**
* 线程函数
*/
public void run() {
int nCommandID;
System.out.println("开始SMSServerHelper Reader线程,reading ...");
while (bLoop) {
//数据包的命令码
strReadBuffer = sb.readSMS();
if (strReadBuffer == null || strReadBuffer.length() == 0) {
try {
sleep(nPause);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
System.out.println("SMSServerHelper Reader:" + strReadBuffer);
parser.clearParser();
parser.setData(strReadBuffer);
parser.parse();
nCommandID = parser.getCommandID();
//Modify by zfs 2004.01.01
if (nCommandID == 0)
continue;
switch (nCommandID) {
//导航设备向服务器注册,命令码值为:1
case DEVICE_REGISTER_SERVER:
{
String strData = parser.getDataBody();
RegisterParserEx registerParser = new RegisterParserEx(strData);
registerParser.parse();
//Modify by zfs 2004.02.04
int nDeviceID = registerParser.getDeviceID();
if (nDeviceID == 0)
break;
//验证设备的合法性
Device device = ApplicationContext.devicesQueue.findDeviceByID(nDeviceID);
if (device == null) {
String strMessage = "设备 " + Integer.toString(nDeviceID)
+ "是不合法的设备!";
CommonLogger.Info("设备注册", strMessage);
break;
}
//获取设备的SIM卡号
String strMobile = device.getDeviceInfos().getMobileCard();
if (strMobile == null || strMobile.length() == 0) {
String strMessage = "设备 " + Integer.toString(nDeviceID)
+ " 的SMS卡号为非法值,注册失败";
CommonLogger.Info("设备注册", strMessage);
break;
}
//设置监控设备的状态(缺省状态,即数据状态)
device.setDeviceCommunicationState(REGISTERED_SMS_STATE);
ApplicationContext.devicesQueue.setDeviceCommunicationState(nDeviceID, REGISTERED_SMS_STATE);
String strLogicID = registerParser.getDeviceLogicID();
ReceiveServer.rdQueue.register(nDeviceID, strLogicID);
strWriteBuffer = Integer.toString(SERVER_RESPONSE_REGISTER_DEVICE) + '|'
+ Integer.toString(nDeviceID) + ",NULL,NULL,NULL|1";
sb.sendSMS(strMobile, strWriteBuffer);
MyTime mtCurrent = new MyTime();
String strMessage = "监控设备 " + Integer.toString(nDeviceID)
+ " 已注册,注册时间为:" + mtCurrent.toString();
CommonLogger.Info("监控设备注册(GSM)", strMessage);
break;
}
//导航设备向服务器注销,命令码值为:2
case DEVICE_UNREGISTER_SERVER:
{
String strData = parser.getDataBody();
RegisterParserEx registerParser = new RegisterParserEx(strData);
registerParser.parse();
int nDeviceID = registerParser.getDeviceID();
if (nDeviceID == 0)
break;
//监控设备注销
ReceiveServer.rdQueue.unregister(nDeviceID);
ApplicationContext.devicesQueue.setDeviceCommunicationState(nDeviceID, UNREGISTER_STATE);
String strMessage = "监控设备 " + Integer.toString(nDeviceID) + " 已注消";
CommonLogger.Info("监控设备注消(GSM)", strMessage);
break;
}
//智能导航设备向指定的监控终端发送单呼位置信息
case DEVICE_RESPONSE_SINGLE_CALL_LOCATION_INFOS_TERMINAL:
{
//Modify by zfs 2004.01.10,忽略非法GPS数据包
if (strReadBuffer.length() < 50)
break;
String strData = parser.getDataBody();
if (strData == null)
break;
LocationInfosParser locationParser = new LocationInfosParser(strData);
locationParser.parse();
int nTerminalID = locationParser.getTerminalID();
if (nTerminalID == 0)
break;
Terminal terminal = ApplicationContext.terminalsQueue.findTerminalByID(nTerminalID);
if (terminal == null)
break;
int nDeviceID = locationParser.getDeviceID();
if (nDeviceID == 0)
break;
String strMessage;
SendServerHelper ssHelper = ApplicationContext.sendServer.ssHelperQueue.getHelperByDeviceID(nDeviceID);
if (ssHelper == null) {
strMessage = "监控终端 " + nDeviceID + " 未注册!";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -