CommandClient.java
8.2 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
package com.sy.socket;
import com.sy.model.CommandInfoX22;
import com.sy.model.GatherInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
@Component
@Slf4j
public class CommandClient {
private static final String GATHER_TEMPLATE = "<GATHER_INFO AREA_ID=\"${AREA_ID}\" CHNL_NO=\"${CHNL_NO}\" I_E_TYPE=\"${I_E_TYPE}\" SEQ_NO=\"${SEQ_NO}\">\n" +
"\t<IC>\n" +
"\t\t<DR_IC_NO/>\n" +
"\t\t<IC_DR_CUSTOMS_NO/>\n" +
"\t\t<IC_CO_CUSTOMS_NO/>\n" +
"\t\t<IC_BILL_NO/>\n" +
"\t\t<IC_FORM_TYPE/>\n" +
"\t\t<IC_GROSS_WT/>\n" +
"\t\t<IC_VE_CUSTOMS_NO/>\n" +
"\t\t<IC_VE_NAME/>\n" +
"\t\t<IC_CONTA_ID/>\n" +
"\t\t<IC_ESEAL_ID/>\n" +
"\t\t<IC_REG_DATETIME/>\n" +
"\t\t<IC_PER_DAY_DUE/>\n" +
"\t</IC>\n" +
"\t<WEIGHT>\n" +
"\t\t<GROSS_WT>${GROSS_WT}</GROSS_WT>\n" +
"\t</WEIGHT>\n" +
"\t<CAR>\n" +
"\t\t<VE_NAME>${VE_NAME}</VE_NAME>\n" +
"\t\t<CAR_EC_NO/>\n" +
"\t\t<CAR_EC_NO2/>\n" +
"\t\t<VE_CUSTOMS_NO/>\n" +
"\t\t<VE_WT/>\n" +
"\t</CAR>\n" +
"\t<CONTA>\n" +
"\t\t<CONTA_NUM/>\n" +
"\t\t<CONTA_RECO>${CONTA_RECO}</CONTA_RECO>\n" +
"\t\t<CONTA_ID_F/>\n" +
"\t\t<CONTA_ID_B/>\n" +
"\t\t<CONTA_MODEL_F/>\n" +
"\t\t<CONTA_MODEL_B/>\n" +
"\t</CONTA>\n" +
"\t<ORDER_NUM/>\n" +
"\t<BAR_CODE>${BAR_CODE}</BAR_CODE>\n" +
"\t<SEAL>\n" +
"\t\t<ESEAL_ID/>\n" +
"\t\t<SEAL_KEY/>\n" +
"\t</SEAL>\n" +
"</GATHER_INFO>";
private static final String COMMAND_INFO_TEMPLATE = "<COMMAND_INFO AREA_ID=\"${AREA_ID}\" CHNL_NO=\"${CHNL_NO}\" I_E_TYPE=\"${I_E_TYPE}\" SEQ_NO=\"${SEQ_NO}\">\n" +
"\t<CHECK_RESULT>${CHECK_RESULT}</CHECK_RESULT>\n" +
"\t<OP_HINT>${OP_HINT}</OP_HINT>\n" +
"\t<SEAL>\n" +
"\t\t<ESEAL_ID/>\n" +
"\t\t<SEAL_KEY>${SEAL_KEY}</SEAL_KEY>\n" +
"\t\t<OPEN_TIMES/>\n" +
"\t\t<ESEAL_IC_NO/>\n" +
"\t</SEAL>\n" +
"\t<SZ_MSG/>\n" +
"</COMMAND_INFO>";
/**
* @Param GatherInfo 卡口采集信息
* @Param flag 是否放行信息
* */
public static void Client(GatherInfo info,String flag) {
String xmlBody = getXmlInfo(info,flag);
sendWithSocket(info,xmlBody);
}
private static void sendWithSocket(GatherInfo info,String xmlBody){
Socket socket =null;
OutputStream op = null;
try {
//ip+端口
socket = new Socket("10.50.7.10", 9003);
logger.info("socket通讯创建连接成功");
op = socket.getOutputStream();
//xml字节流
byte[]xBody =xmlBody.getBytes("GB2312");
//包头
byte[] head = new byte[4];
head[0]=(byte)0xE2;
head[1]=(byte)0x5C;
head[2]=(byte)0x4B;
head[3]=(byte)0x89;
//消息类型
byte[] mType = new byte[1];
mType[0] = (byte)0x22;
//场站号
byte[]station =info.getAreaid().getBytes("ASCII");
//通道号
byte[]aisle =info.getChnlno().getBytes("ASCII");
//进出标识
byte[]eType =info.getIetype().getBytes("ASCII");
//标识符
byte[] bwFlag = new byte[4];
bwFlag[0]=(byte)0x00;
bwFlag[1]=(byte)0x00;
bwFlag[2]=(byte)0x00;
bwFlag[3]=(byte)0x00;
//xml字节流长度
byte[]xmlLength = intToByte4(xBody.length);
//包尾
byte[]end = new byte[2];
end[0]=(byte)0xFF;
end[1]=(byte)0xFF;
System.out.println();
//总长 4为总长的length
byte [] packge = intToByte4((head.length+xBody.length+mType.length+station.length+aisle.length+eType
.length+bwFlag.length+xmlLength.length+end.length+4));
byte[]allByte = byteMergerAll(head,packge,mType,station,aisle,eType,bwFlag,xmlLength,xBody,end);
op.write(allByte);
op.flush();
op.close();
log.info("发送完毕");
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
log.info("创建连接失败"+e.getMessage());
} catch (IOException e) {
e.printStackTrace();
log.info("文件发送失败"+e.getMessage());
}
}
/**
* @Param info 卡口采集信息
* @Param flag 放行标识
* @Result 生成放行报文
* */
private static String getXmlInfo(GatherInfo info,String message) {
String flag = null;
if ("直接放行".equals(message) || "海关放行".equals(message)){
flag = "00";
} else{
flag = "11";
}
StringBuffer buffer = new StringBuffer();
buffer.append("<COMMAND_INFO AREA_ID=\""+info.getAreaid()+"\" CHNL_NO=\""+info.getChnlno()+"\" I_E_TYPE=\""+info
.getIetype()+"\" SEQ_NO=\""+info.getSeqno()+"\">");
String type = null;
if("I".equals(info.getIetype())){
type="000000200000000000";
}else{
type="000000100000000000";
}
buffer.append("<CHECK_RESULT>"+flag+type+"</CHECK_RESULT><OP_HINT>"+message+"</OP_HINT>");
buffer.append("<SEAL>");
buffer.append("<ESEAL_ID>"+info.getEsealid()+"</ESEAL_ID>");
if(flag == "00"){
buffer.append("<SEAL_KEY>1234567890</SEAL_KEY>");
}else{
buffer.append("<SEAL_KEY>"+info.getSealkey()+"</SEAL_KEY>");
}
buffer.append("<OPEN_TIMES></OPEN_TIMES>");
buffer.append("<ESEAL_IC_NO></ESEAL_IC_NO>");
buffer.append("</SEAL>");
buffer.append("<SZ_MSG></SZ_MSG>");
buffer.append("</COMMAND_INFO>");
return buffer.toString();
}
/**
* 生成GATHERINFO 报文内容
* @param info 过卡信息
* @return X21报文
*/
public static void gatherInfoBuildAndSend(GatherInfo info){
String gatherXML = "";
gatherXML = GATHER_TEMPLATE.replace("${AREA_ID}",info.getAreaid())
.replace("${CHNL_NO}",info.getChnlno())
.replace("${I_E_TYPE}",info.getIetype())
.replace("${SEQ_NO}",info.getSeqno())
.replace("${GROSS_WT}", info.getGrosswt().toString())
.replace("${VE_NAME}",info.getVename())
.replace("${CONTA_RECO}",info.getContareco())
//给三宝的去掉BARCODE,todo:不去掉做个测试
.replace("${BAR_CODE}",info.getBarcode());
sendWithSocket(info,gatherXML);
}
//int转byte
private static byte[] intToByte4(int i) {
byte[] targets = new byte[4];
//低位到高位
targets[0] = (byte) (i & 0xFF);
targets[1] = (byte) (i >> 8 & 0xFF);
targets[2] = (byte) (i >> 16 & 0xFF);
targets[3] = (byte) (i >> 24 & 0xFF);
return targets;
}
//byte转int
private static int byteArrayToInt(byte[] bytes) {
int value = 0;
// 由高位到低位
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (bytes[i] & 0x000000FF) << shift;// 往高位游
}
return value;
}
//合并byte数据
private static byte[] byteMergerAll(byte[]... values) {
int length_byte = 0;
for (int i = 0; i < values.length; i++) {
length_byte += values[i].length;
}
byte[] all_byte = new byte[length_byte];
int countLength = 0;
for (int i = 0; i < values.length; i++) {
byte[] b = values[i];
System.arraycopy(b, 0, all_byte, countLength, b.length);
countLength += b.length;
}
return all_byte;
}
}