📄 readrecievequeue.java
字号:
package com.gps.center.dataservice;
import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import com.gps.center.baseclass.ParseData;
import com.gps.center.baseclass.MsgObj;
import com.gps.center.baseclass.ParseInterface;
public class ReadRecieveQueue extends ParseData implements Runnable,
ParseInterface {
private String[][] expression;
public ReadRecieveQueue() {
readxml();
readDeviceType();
loadDeviceClass();
}
public void run() {
Pattern regex;
Matcher matcher;
ParseInterface parseobj;
boolean findStartword = false;
boolean findCommandword = false;
boolean findEndword = false;
for (int n = 0; n < expression.length; n++) {
while (super.recieveQueue.isEmpty()) {//队列为空时休眠
try {
Thread.sleep(700);
} catch (InterruptedException e) {
System.out.println(e);
}
}
if (!super.recieveQueue.isEmpty()) {
MsgObj msg = getRecieveMsg();//获得队列中的对象
for (int i = 0; i < expression.length; i++) {
findStartword = false;
findCommandword = false;
findEndword = false;
for (int j = 1; j <= 3; j++) {//循环解析
regex = Pattern.compile(expression[i][j]);//按照指定的进行正则解析
matcher = regex.matcher(msg.cMsg);
//cMsgtype:1--TCPMSG,2--DEVICEUDPMSG,3--SMSMSG
//4--TCPSENDTODEVICE,5--UDPSENDTODEVICE,6--SMSSENDTODEVICE,7--TCPSENDTOTERMINAL,8--TCPRETURNTERMINAL
switch (j) {
case 1:
findStartword = matcher.find();
break;
case 2:
findCommandword = matcher.find();
break;
case 3:
findEndword = matcher.find();
break;
}
}
if (findStartword & findCommandword & findEndword) {
if (!super.classMap.containsKey(expression[i][0])) {
parseobj = loadClass(expression[i][0]);
super.classMap.put((expression[i][0]), parseobj);
parseobj.ParseMsg(msg);//创建类的实例并调用ParseMsg处理
} else {
parseobj = (ParseInterface)super.classMap.get(
expression[i][0]);
parseobj.ParseMsg(msg);
}
break;
}
}
}
}
}
protected static ParseInterface loadClass(String classname) {
ParseInterface s = null;
try {
Class parseClass = Class.forName("com.gps.center.parsedata." +
classname.trim());
s = (ParseInterface) parseClass.newInstance();//生成类的实例
} catch (Exception e) {
System.out.println("erro for Class.forName(servicename)" +
classname);
}
return s;
}
private void readxml() {
try {
File f = new File("ExpressionInfo.xml");
if (f.exists()) {
DocumentBuilderFactory factory = DocumentBuilderFactory.
newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("Center");
expression = new String[nl.getLength()][4];
for (int i = 0; i < nl.getLength(); i++) {
expression[i][0] = new String((doc.getElementsByTagName(
"ClassName").
item(i).
getFirstChild().getNodeValue()).trim());
expression[i][1] = new String((doc.getElementsByTagName(
"Startword").
item(i).
getFirstChild().
getNodeValue()).trim());
expression[i][2] = new String((doc.getElementsByTagName(
"Commandword").item(i).getFirstChild().getNodeValue()).
trim());
expression[i][3] = new String((doc.getElementsByTagName(
"Endword").
item(i).
getFirstChild().
getNodeValue()).trim());
}
} else {
System.out.println("no find file:ExpressionInfo.xml");
System.exit( -1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void readDeviceType() {
try {
File f = new File("VehicleTable.xml");
if (f.exists()) {
DocumentBuilderFactory factory = DocumentBuilderFactory.
newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("Center");
super.DeviceIdType = new String[nl.getLength()][2];
for (int i = 0; i < nl.getLength(); i++) {
DeviceIdType[i][0] = new String((doc.getElementsByTagName(
"DeviceID").
item(i).
getFirstChild().getNodeValue()).
trim());
DeviceIdType[i][1] = new String((doc.getElementsByTagName(
"DeviceType").item(i).getFirstChild().getNodeValue()).
trim());
}
} else {
System.out.println("no find file:ExpressionInfo.xml");
System.exit( -1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadDeviceClass() {//添加其它类型的车台
ParseInterface parseobj;
String[] DeviceType = {"HC2000"};//添加车台类型字符串
if (!super.classMap.containsKey(DeviceIdType[0])) {
parseobj = loadClass(DeviceType[0]);
super.classMap.put((DeviceType[0]), parseobj);
}
}
public void ParseMsg(MsgObj msg) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -