diff --git a/pom.xml b/pom.xml index ff82031..befd0d6 100644 --- a/pom.xml +++ b/pom.xml @@ -9,11 +9,11 @@ com.example - demo + CPI 0.0.1-SNAPSHOT ChinaPostWebService ChinaPostWebService - + jar 1.8 @@ -25,7 +25,11 @@ cxf-spring-boot-starter-jaxws 3.4.2 - + + + + + @@ -33,7 +37,11 @@ - + + com.alibaba.csb.sdk + http-client + 1.1.5.9 + org.springframework.boot spring-boot-starter-web-services @@ -101,6 +109,15 @@ org.springframework.boot spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + true + diff --git a/src/main/java/com/example/api/R.java b/src/main/java/com/example/api/R.java index b3fb2ad..d7ae194 100644 --- a/src/main/java/com/example/api/R.java +++ b/src/main/java/com/example/api/R.java @@ -43,32 +43,21 @@ public class R implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "状态码", required = true) - private int code; -// @ApiModelProperty(value = "是否成功", required = true) -// private boolean success; -// @ApiModelProperty(value = "承载数据") -// private T data; + private int status; + @ApiModelProperty(value = "返回消息", required = true) private String msg; + @ApiModelProperty(value = "RIFD", required = true) + private String rqtms; -// private R(IResultCode resultCode) { -// this(resultCode, null, resultCode.getMessage()); -// } -// -// private R(IResultCode resultCode, String msg) { -// this(resultCode, null, msg); -// } -// -// private R(IResultCode resultCode, T data) { -// this(resultCode, data, resultCode.getMessage()); -// } - -// private R(IResultCode resultCode, T data, String msg) { -// this(resultCode.getCode(), data, msg); -// } + public R(int status, String msg, String rqtms) { + this.status = status; + this.msg = msg; + this.rqtms = rqtms; + } private R(int code, String msg) { - this.code = code; + this.status = code; // this.data = data; this.msg = msg; // this.success = ResultCode.SUCCESS.code == code; @@ -82,7 +71,7 @@ public class R implements Serializable { */ public static boolean isSuccess(@Nullable R result) { return Optional.ofNullable(result) - .map(x -> ObjectUtil.nullSafeEquals(ResultCode.SUCCESS.code, x.code)) + .map(x -> ObjectUtil.nullSafeEquals(ResultCode.SUCCESS.code, x.status)) .orElse(Boolean.FALSE); } @@ -133,6 +122,9 @@ public class R implements Serializable { public static R data(int code, String msg) { return new R<>(code, msg); } + public static R data(int code, String msg,String rqtms) { + return new R<>(code, msg,rqtms); + } /** * 返回R diff --git a/src/main/java/com/example/config/CxfConfig.java b/src/main/java/com/example/config/CxfConfig.java index 6298a96..55eb625 100644 --- a/src/main/java/com/example/config/CxfConfig.java +++ b/src/main/java/com/example/config/CxfConfig.java @@ -37,7 +37,7 @@ public class CxfConfig { @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), mPersonService()); - endpoint.publish("/mPerson"); + endpoint.publish("/cpfinterface"); return endpoint; } } diff --git a/src/main/java/com/example/csb/RequestMessageHeader.java b/src/main/java/com/example/csb/RequestMessageHeader.java new file mode 100644 index 0000000..d135088 --- /dev/null +++ b/src/main/java/com/example/csb/RequestMessageHeader.java @@ -0,0 +1,46 @@ +package com.example.csb; + +public class RequestMessageHeader { + + private String sysCode; // 系统代码 + + private String sign; // 签名 + + private String serialNo;// 消息唯一流水号 + + private String sendDate;// 消息发送日期时间 + + public String getSysCode() { + return sysCode; + } + + public void setSysCode(String sysCode) { + this.sysCode = sysCode; + } + + public String getSign() { + return sign; + } + + public void setSign(String sign) { + this.sign = sign; + } + + public String getSerialNo() { + return serialNo; + } + + public void setSerialNo(String serialNo) { + this.serialNo = serialNo; + } + + public String getSendDate() { + return sendDate; + } + + public void setSendDate(String sendDate) { + this.sendDate = sendDate; + } + + +} diff --git a/src/main/java/com/example/csb/UploadCSBController.java b/src/main/java/com/example/csb/UploadCSBController.java new file mode 100644 index 0000000..3cb9119 --- /dev/null +++ b/src/main/java/com/example/csb/UploadCSBController.java @@ -0,0 +1,94 @@ +package com.example.csb; + +import com.alibaba.csb.sdk.HttpCaller; +import com.alibaba.csb.sdk.HttpCallerException; +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.digest.DigestUtils; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +/** + * Created by wangh on 2021/3/16-15:46。 + */ +@Controller +public class UploadCSBController { + private String inStore = "scanAnyContainerCodeByCSB"; + private String outStore = "getScanInfoAppointRfidByCSB"; + private String pramIN = "batchOpenQuery"; + private String pramOut = "containerRegisterQuery"; + private String pram; + // csb处理 + String requestURL = "http://211.156.197.239:8086/ceshi_csb_broker";//ceshiyun + String API_NAME; // CSB发布服务定义的服务名 + String version = "1.0.0"; + String ak = "dd979a77b2a44e54b65f01dcbc0bae04"; + String sk = "kCZfusS4Zn300MTnYtIlWzhdoE8="; + + @PostMapping("/api/uploadCsb") + private String uploadCSBByAndroidInfo(int code, String containerRegisterQuery) { + System.out.println("\n调用上传接口"+containerRegisterQuery); +// containerRegisterQuery="{\"opOrgCode\":\"11600071\",\"opOrgName\":\"大连中心\",\"userCode\":\"1116\",\"userName\":\"admin\",\"provice\":\"210000\",\"destinationOrgCode\":\"11600019\",\"destinationOrgName\":\"大连航站\",\"rqzl\":\"02\",\"rqzlmc\":\"国内普通邮袋\",\"rqxh\":\"1\",\"rqxhmc\":\"F01\",\"zbid\":\"1400001\",\"rqtm\":\"1972102100000009\"}"; +// containerRegisterQuery="{\n" + +// " \"opOrgCode\": \"11600071\",\n" + +// " \"opOrgName\": \"大连中心\",\n" + +// " \"userCode\": \"RFID\",\n" + +// " \"userName\": \"RFID\",\n" + +// " \"province\": \"210000\",\n" + +// " \"deviceId\": \"RFID-001\",\n" + +// " \"functionID\": \"101\",\n" + +// " \"rqtm\": \"1002001300000023\"\n" + +// "}"; + RequestMessageHeader msg = new RequestMessageHeader(); + msg.setSysCode("aa");// aa + msg.setSerialNo(UUID.randomUUID().toString()); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + msg.setSendDate(sdf.format(new Date())); + msg.setSign(getSign("CSHIPING12", containerRegisterQuery)); + String msgs = JSONObject.toJSONString(msg); // 转换为JSON String + Map params = new HashMap(); + params.put("messageHeader", msgs); + // 用户安全校验的签名密钥对 + if (code == 101) { + API_NAME = inStore; + pram = pramIN; + } else if (code == 103) { + API_NAME = outStore; + pram = pramOut; + } + + params.put(pram, containerRegisterQuery); + + // + try { + String result = HttpCaller.doPost(requestURL, API_NAME, version, params, ak, sk); + if (result != null) { + // 返回结果处理, 如转换为JSON对象 + System.out.println(result); + } + } catch (HttpCallerException ie) { + ie.printStackTrace(); + } + return "error"; + } + + public static String getSign(String password, String... params) { + StringBuffer sb = new StringBuffer(password); + if (params != null) { + for (String o : params) { + sb.append(o); + } + } + String mysign = Base64 + .encodeBase64String(DigestUtils.md5Hex(DigestUtils.md5Hex(sb.toString())).getBytes()) + .replaceAll("\r|\n", ""); +// System.out.println("1231231231\n"+mysign+"\n"); + return mysign; + } +} diff --git a/src/main/java/com/example/csb/test.java b/src/main/java/com/example/csb/test.java new file mode 100644 index 0000000..7f916bf --- /dev/null +++ b/src/main/java/com/example/csb/test.java @@ -0,0 +1,78 @@ +package com.example.csb; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.codec.binary.Base64; +import com.alibaba.csb.sdk.HttpCaller; +import com.alibaba.csb.sdk.HttpCallerException; + + + +public class test { + public static String getSign(String password, String... params) { + StringBuffer sb = new StringBuffer(password); + if (params != null) { + for (String o : params) { + sb.append(o); + } + } + String mysign = Base64 + .encodeBase64String(DigestUtils.md5Hex(DigestUtils.md5Hex(sb.toString())).getBytes()) + .replaceAll("\r|\n", ""); +// System.out.println("1231231231\n"+mysign+"\n"); + return mysign; + } + + public static void aaa() throws ParseException { + + String containerRegisterQuery="{\"opOrgCode\":\"11600071\",\"opOrgName\":\"大连中心\",\"userCode\":\"1116\",\"userName\":\"admin\",\"provice\":\"210000\",\"destinationOrgCode\":\"11600019\",\"destinationOrgName\":\"大连航站\",\"rqzl\":\"02\",\"rqzlmc\":\"国内普通邮袋\",\"rqxh\":\"1\",\"rqxhmc\":\"F01\",\"zbid\":\"1400001\",\"rqtm\":\"1972102100000009\"}"; + RequestMessageHeader msg = new RequestMessageHeader(); + msg.setSysCode("aa");// aa + msg.setSerialNo(UUID.randomUUID().toString()); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + msg.setSendDate(sdf.format(new Date())); + msg.setSign(getSign("CSHIPING12", containerRegisterQuery)); + String msgs = JSONObject.toJSONString(msg); // 转换为JSON String + Map params = new HashMap(); + params.put("messageHeader", msgs); + params.put("containerRegisterQuery", containerRegisterQuery); + + + + // csb处理 + String requestURL = "http://211.156.197.239:8086/ceshi_csb_broker";//ceshiyun + String API_NAME = "getScanInfoAppointRfidByCSB"; // CSB发布服务定义的服务名 + String version = "1.0.0"; + String ak = "dd979a77b2a44e54b65f01dcbc0bae04"; + String sk = "kCZfusS4Zn300MTnYtIlWzhdoE8="; // 用户安全校验的签名密钥对 + + // + try { + String result = HttpCaller.doPost(requestURL, API_NAME, version, params, ak, sk); + if (result != null) { + // 返回结果处理, 如转换为JSON对象 + System.out.println("params:" + params); + System.out.println(result); + // System.out.println( + // "转成Map:" + GsonUtil.toObject(HttpCaller.changeCharset(result), Map.class)); + } + else { + System.out.println("没有任何数据。"); + } + } + catch (HttpCallerException ie) { + // 调用异常处理 + System.out.println("2"); + ie.printStackTrace(); + } + + } + +} diff --git a/src/main/java/com/example/entity/ContainerRegisterQuery.java b/src/main/java/com/example/entity/ContainerRegisterQuery.java index 2e19979..db3eae9 100644 --- a/src/main/java/com/example/entity/ContainerRegisterQuery.java +++ b/src/main/java/com/example/entity/ContainerRegisterQuery.java @@ -4,7 +4,8 @@ import java.io.Serializable; public class ContainerRegisterQuery implements Serializable { -// private String messageHeader; + private String messageHeader; + private String containerRegisterQuery; // private String opOrgCode; // private String userCode; // private String userName; @@ -19,14 +20,22 @@ public class ContainerRegisterQuery implements Serializable { public void setCommandCode(int commandCode) { this.commandCode = commandCode; } -// public String getMessageHeader() { -// return messageHeader; -// } -// -// public void setMessageHeader(String messageHeader) { -// this.messageHeader = messageHeader; -// } -// + public String getMessageHeader() { + return messageHeader; + } + + public void setMessageHeader(String messageHeader) { + this.messageHeader = messageHeader; + } + + public String getContainerRegisterQuery() { + return containerRegisterQuery; + } + + public void setContainerRegisterQuery(String containerRegisterQuery) { + this.containerRegisterQuery = containerRegisterQuery; + } + // // // public String getOpOrgCode() { // return opOrgCode; diff --git a/src/main/java/com/example/entity/OutCenter.java b/src/main/java/com/example/entity/OutCenter.java index c54751f..cd23b87 100644 --- a/src/main/java/com/example/entity/OutCenter.java +++ b/src/main/java/com/example/entity/OutCenter.java @@ -10,7 +10,6 @@ public class OutCenter implements Serializable { private String userCode; private String userName; private String provice; - private String rqzlmc; private String zbid; private String destinationOrgName; diff --git a/src/main/java/com/example/services/IService.java b/src/main/java/com/example/services/IService.java index edbee53..12fecdb 100644 --- a/src/main/java/com/example/services/IService.java +++ b/src/main/java/com/example/services/IService.java @@ -1,6 +1,5 @@ package com.example.services; -import com.example.MyResult; import com.example.api.R; import javax.jws.WebMethod; @@ -10,125 +9,31 @@ import javax.jws.WebService; /** * Created by wangh on 2021/1/14-10:10。 */ -@WebService(targetNamespace = "http://service.example.com/" ,name = "authorPortType") +@WebService(targetNamespace = "http://service.example.com/", name = "authorPortType") public interface IService { /** * 入库开始读 * - * @param messageHeader * @param containerRegisterQuery - * @param opOrgCode 机构代码 - * @param userCode 登录人代码 - * @param userName 登录人名称 - * @param province 省份代码 - * @param pdaip - * @return */ @WebMethod R getScanInfoInCenterStart( - @WebParam(name = "messageHeader") String messageHeader, -// @WebParam(name = "ContainerRegisterQuery") String ContainerRegisterQuery, - @WebParam(name = "opOrgCode") String opOrgCode, - @WebParam(name = "userCode") String userCode, - @WebParam(name = "userName") String userName, - @WebParam(name = "province") String province, - @WebParam(name = "pdaip") String pdaip); + @WebParam(name = "containerRegisterQuery") String containerRegisterQuery, + @WebParam(name = "pdaID") String pdaip, + @WebParam(name="functionID") int functionID); /** * 入库 结束读 * - * @param messageHeader 入参1,参数名: -// * @param containerRegisterQuery 入参2,将下列参数封装到containerRegisterQuery - * @param opOrgCode 机构代码 - * @param userCode 登录人代码 - * @param userName 登录人名称 - * @param province 省份代码 - * @param pdaip 指定pda停止读的ip - * @return + */ @WebMethod R getScanInfoInCenterStop( - @WebParam(name = "messageHeader") String messageHeader, -// @WebParam(name = "ContainerRegisterQuery") String containerRegisterQuery, - @WebParam(name = "opOrgCode") String opOrgCode, - @WebParam(name = "userCode") String userCode, - @WebParam(name = "userName") String userName, - @WebParam(name = "province") String province, - @WebParam(name = "pdaip") String pdaip + @WebParam(name = "containerRegisterQuery") String containerRegisterQuery + , @WebParam(name = "pdaID") String pdaip + ,@WebParam(name="functionID") int functionID ); - /** - * 出库开始 - * - * @param messageHeader 入参1,参数名: - * @param messageHeader - * @param containerRegisterQuery 入参2,将下列参数封装到containerRegisterQuery - * @param opOrgCode 机构代码 - * @param opOrgName 省份代码 - * @param userCode 登录人代码 - * @param userName 登录人名称 - * @param provice 省份代码 - * @param rqzlmc 容器种类名称 - * @param zbid 总包id - * @param destinationOrgName 寄达局名称 - * @param destinationOrgCode 寄达局编号 - * @param rqzl 容器种类 - * @param rqxh 容器型号 - * @param rqxhmc 容器型号名称 - * @param pdaip 指定pda开始读的ip - */ - @WebMethod - R getScanInfoOutCenterStart(@WebParam(name = "messageHeader") String messageHeader, - @WebParam(name = "ContainerRegisterQuery") String containerRegisterQuery, - @WebParam(name = "opOrgCode") String opOrgCode, - @WebParam(name = "opOrgName") String opOrgName, - @WebParam(name = "userCode") String userCode, - @WebParam(name = "userName") String userName, - @WebParam(name = "provice") String provice, - @WebParam(name = "rqzlmc") String rqzlmc, - @WebParam(name = "zbid") String zbid, - @WebParam(name = "destinationOrgName") String destinationOrgName, - @WebParam(name = "destinationOrgCode") String destinationOrgCode, - @WebParam(name = "rqzl") String rqzl, - @WebParam(name = "rqxh") String rqxh, - @WebParam(name = "rqxhmc") String rqxhmc, - @WebParam(name = "pdaip") String pdaip - ); - /** - * 出库停止 - * @param messageHeader 入参1,参数名: - * @param messageHeader - * @param containerRegisterQuery 入参2,将下列参数封装到containerRegisterQuery - * @param opOrgCode 机构代码 - * @param opOrgName 省份代码 - * @param userCode 登录人代码 - * @param userName 登录人名称 - * @param provice 省份代码 - * @param rqzlmc 容器种类名称 - * @param zbid 总包id - * @param destinationOrgName 寄达局名称 - * @param destinationOrgCode 寄达局编号 - * @param rqzl 容器种类 - * @param rqxh 容器型号 - * @param rqxhmc 容器型号名称 - * @param pdaip 指定pda停止读的ip - * @return - */ - @WebMethod - R getScanInfoOutCenterStop(@WebParam(name = "messageHeader") String messageHeader, - @WebParam(name = "ContainerRegisterQuery") String containerRegisterQuery, - @WebParam(name = "opOrgCode") String opOrgCode, - @WebParam(name = "opOrgName") String opOrgName, - @WebParam(name = "userCode") String userCode, - @WebParam(name = "userName") String userName, - @WebParam(name = "provice") String provice, - @WebParam(name = "rqzlmc") String rqzlmc, - @WebParam(name = "zbid") String zbid, - @WebParam(name = "destinationOrgName") String destinationOrgName, - @WebParam(name = "destinationOrgCode") String destinationOrgCode, - @WebParam(name = "rqzl") String rqzl, - @WebParam(name = "rqxh") String rqxh, - @WebParam(name = "rqxhmc") String rqxhmc, - @WebParam(name = "pdaip") String pdaip); + } diff --git a/src/main/java/com/example/services/impl/ServiceImpl.java b/src/main/java/com/example/services/impl/ServiceImpl.java index 20ef823..c5db735 100644 --- a/src/main/java/com/example/services/impl/ServiceImpl.java +++ b/src/main/java/com/example/services/impl/ServiceImpl.java @@ -1,5 +1,6 @@ package com.example.services.impl; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.example.MyResult; import com.example.api.R; @@ -7,242 +8,146 @@ import com.example.entity.ContainerRegisterQuery; import com.example.entity.OutCenter; import com.example.services.IService; +import javax.jws.WebParam; import javax.jws.WebService; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; +import static com.example.services.impl.WebSocketServerContent.getRecMessage; + /** * Created by wangh on 2021/1/14-10:29。 */ @WebService( - targetNamespace ="http://service.example.com/" , + targetNamespace = "http://service.example.com/", serviceName = "authorService", //服务name名称 portName = "authorPortName", endpointInterface = "com.example.services.IService")//指定发布webservcie的接口类,此类也需要接入@WebService注解 public class ServiceImpl implements IService { - + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + public static String rfid; private static boolean m_GetInCenterStartSuccessful; private static boolean m_GetInCenterStopSuccessful; private static boolean m_GetOutCenterStartSuccessful; private static boolean m_GetOutCenterStopSuccessful; + private static boolean StartFlag = false; private static final Semaphore m_RecData = new Semaphore(1); + private static final Semaphore m_RecstopData = new Semaphore(1); - public boolean DealValidPack(String ValidData){ + public boolean DealValidPack(String ValidData) { try { - if(!ValidData.isEmpty()||!ValidData.equals("")){ + rfid = ValidData; + System.out.println("RFID=" + rfid); + if (!ValidData.isEmpty() || !ValidData.equals("")) { // m_RecData.release(); - switch (ValidData){ + switch (ValidData) { case "101": m_GetInCenterStartSuccessful = true; break; - case "102": - m_GetInCenterStopSuccessful = true; - break; case "103": - m_GetOutCenterStartSuccessful = true; + case "105": + m_GetInCenterStartSuccessful = true; break; - case"104": - m_GetOutCenterStopSuccessful = true; + case "102": + case "104": + case "106": + m_RecstopData.release(); + m_GetInCenterStopSuccessful = true; break; default: - m_GetInCenterStartSuccessful = true; + m_GetInCenterStopSuccessful = true; break; + } } - } - catch (Exception ex){ + } catch (Exception ex) { return false; } return true; } - //请求地址 http://localhost:8090/services/mPerson?wsdl + + //请求地址 http://localhost:9090/services/cpfinterface?wsdl @Override -// public MyResult getScanInfoInCenterStart(String messageHeader, -// String ContainerRegisterQuery, -// String opOrgCode, -// String userCode, -// String userName, -// String province, -// String pdaip) { - public R getScanInfoInCenterStart( - String messageHeader, - String opOrgCode, - String userCode, - String userName, - String province, - String pdaip) { - boolean isSendFlag = false; - //示例 + public R getScanInfoInCenterStart( + String containerRegisterQuery, + String pdaip, + int functionID) { + System.out.println("\n调用开始读参数为:" + "\npdaID=" + pdaip + "\nfunctionID=" + functionID + "\ncontainerRegisterQuery=" + containerRegisterQuery + + "\t开始请求时间" + sdf.format(new Date())); ContainerRegisterQuery containerRegisterQuery1 = new ContainerRegisterQuery(); -// containerRegisterQuery1.setMessageHeader(messageHeader); -// containerRegisterQuery1.setOpOrgCode(opOrgCode); -// containerRegisterQuery1.setUserCode(userCode); -// containerRegisterQuery1.setUserName(userName); -// containerRegisterQuery1.setProvince(province); + containerRegisterQuery1.setContainerRegisterQuery(containerRegisterQuery); containerRegisterQuery1.setPdaip(pdaip); - containerRegisterQuery1.setCommandCode(101); + containerRegisterQuery1.setCommandCode(functionID); String json = JSONObject.toJSONString(containerRegisterQuery1); - if(WebSocketServerContent.getSocket(pdaip, json)){ + if(StartFlag){ + return R.data(100, "向客户端发送消息失败,请先发送结束入库读指令在进行其他操作!"); + } + if (WebSocketServerContent.getSocket(pdaip, json)) { + StartFlag = true; try { - Thread.sleep(2000); - if(m_GetInCenterStartSuccessful){ - isSendFlag = true; + Thread.sleep(500); + if (m_GetInCenterStartSuccessful) { m_GetInCenterStartSuccessful = false; - return R.data(200,"向客户端发送消息成功!"); - } - else { - System.out.println("《《《等待客户端返回消息超时!"); - isSendFlag = false; - return R.data(201,"向客户端发送消息成功,接收客户端消息超时!"); + System.out.println("\n开始读返回:成功" + "\t开始请求时间" + sdf.format(new Date())); + return R.data(200, "向客户端发送消息成功!"); + } else { + System.out.println("\n开始读返回:消息超时!"); + return R.data(201, "向客户端发送消息成功,接收客户端消息超时!"); } -// if(m_RecData.tryAcquire(2000, TimeUnit.MILLISECONDS)) { -// if(m_GetDataSuccessful){ -// isSendFlag = true; -// m_GetDataSuccessful = false; -// return R.data(200,"success"); -// } -// else { -// System.out.println("《《《等待客户端返回消息超时!"); -// isSendFlag = false; -// m_GetDataSuccessful = false; -// return R.data(401,"timeout"); -// } -// } -// else{ -// System.out.println("《《《等待信号量释放超时!"); -// isSendFlag = false; -// return R.data(402,"semtimeout"); -// } + } catch (Exception ex) { - }catch (Exception ex){ - return R.data(100,"向客户端发送消息失败!"); + return R.data(100, "向客户端发送消息失败!"); } + } else { + System.out.println("\n开始读返回:失败!" + "\t返回时间" + sdf.format(new Date())); + return R.data(101, "向客户端发送消息失败!"); } - else { - return R.data(101,"向客户端发送消息失败!"); - } + + } + //停止读 @Override - public R getScanInfoInCenterStop( - String messageHeader, String opOrgCode, String userCode, String userName, String province, - String pdaip) { - boolean isSendFlag = false; - //示例 + public R getScanInfoInCenterStop(String containerRegisterQuery, + String pdaip, + int functionID) { + System.out.println("\n调用结束读参数为:" + "\npdaID=" + pdaip + "\nfunctionID=" + functionID + "\ncontainerRegisterQuery=" + containerRegisterQuery); ContainerRegisterQuery containerRegisterQuery2 = new ContainerRegisterQuery(); -// containerRegisterQuery2.setMessageHeader(messageHeader); -// containerRegisterQuery2.setOpOrgCode(opOrgCode); -// containerRegisterQuery2.setUserCode(userCode); -// containerRegisterQuery2.setUserName(userName); -// containerRegisterQuery2.setProvince(province); + containerRegisterQuery2.setContainerRegisterQuery(containerRegisterQuery); containerRegisterQuery2.setPdaip(pdaip); - containerRegisterQuery2.setCommandCode(102); + containerRegisterQuery2.setCommandCode(functionID); String json = JSONObject.toJSONString(containerRegisterQuery2); - if(WebSocketServerContent.getSocket(pdaip, json)){ + if (WebSocketServerContent.getSocket(pdaip, json)) { try { - Thread.sleep(2000); - if(m_GetInCenterStopSuccessful){ - isSendFlag = true; + m_RecstopData.acquire(); + //Thread.sleep(1000); + if (m_GetInCenterStopSuccessful) { m_GetInCenterStopSuccessful = false; - return R.data(200,"向客户端发送消息成功!"); - } - else { - System.out.println("《《《等待客户端返回消息超时!"); - isSendFlag = false; - return R.data(201,"向客户端发送消息成功,接收客户端消息超时!"); - } - }catch (Exception ex){ - return R.data(100,"向客户端发送消息失败!"); - } - } - else { - return R.data(101,"向客户端发送消息失败!"); - } - } + System.out.println("\n结束读返回:成功,返回数据=" + rfid); + if (functionID==106){ + return R.data(200, "向客户端发送消息成功!", "[\"1972102100000002\"]"); - @Override - public R getScanInfoOutCenterStart(String messageHeader, String containerRegisterQuery, String opOrgCode, String opOrgName, String userCode, String userName, String provice, String rqzlmc, String zbid, String destinationOrgName, String destinationOrgCode, String rqzl, String rqxh, String rqxhmc, String pdaip) { - boolean isSendFlag = false; - //示例 - OutCenter outCenter1 = new OutCenter(); - outCenter1.setMessageHeader(messageHeader); - outCenter1.setOpOrgCode(opOrgCode); - outCenter1.setOpOrgName(opOrgName); - outCenter1.setUserCode(userCode); - outCenter1.setUserName(userName); - outCenter1.setProvice(provice); - outCenter1.setRqzlmc(rqzlmc); - outCenter1.setZbid(zbid); - outCenter1.setDestinationOrgName(destinationOrgName); - outCenter1.setDestinationOrgCode(destinationOrgCode); - outCenter1.setRqzl(rqzl); - outCenter1.setRqxh(rqxh); - outCenter1.setRqxhmc(rqxhmc); - outCenter1.setPdaip(pdaip); - outCenter1.setCommandCode(103); - String json = JSONObject.toJSONString(outCenter1); - if(WebSocketServerContent.getSocket(pdaip, json)){ - try { - Thread.sleep(2000); - if(m_GetOutCenterStartSuccessful){ - isSendFlag = true; - m_GetOutCenterStartSuccessful = false; - return R.data(200,"向客户端发送消息成功!"); - } - else { - System.out.println("《《《等待客户端返回消息超时!"); - isSendFlag = false; - return R.data(201,"向客户端发送消息成功,接收客户端消息超时!"); - } - }catch (Exception ex){ - return R.data(100,"向客户端发送消息失败!"); - } - } - else { - return R.data(101,"向客户端发送消息失败!"); - } - } + }else { + StartFlag = false; + return R.data(200, "向客户端发送消息成功!"); - @Override - public R getScanInfoOutCenterStop(String messageHeader, String containerRegisterQuery, String opOrgCode, String opOrgName, String userCode, String userName, String provice, String rqzlmc, String zbid, String destinationOrgName, String destinationOrgCode, String rqzl, String rqxh, String rqxhmc, String pdaip) { - boolean isSendFlag = false; - //示例 - OutCenter outCenter2 = new OutCenter(); - outCenter2.setMessageHeader(messageHeader); - outCenter2.setOpOrgCode(opOrgCode); - outCenter2.setOpOrgName(opOrgName); - outCenter2.setUserCode(userCode); - outCenter2.setUserName(userName); - outCenter2.setProvice(provice); - outCenter2.setRqzlmc(rqzlmc); - outCenter2.setZbid(zbid); - outCenter2.setDestinationOrgName(destinationOrgName); - outCenter2.setDestinationOrgCode(destinationOrgCode); - outCenter2.setRqzl(rqzl); - outCenter2.setRqxh(rqxh); - outCenter2.setRqxhmc(rqxhmc); - outCenter2.setPdaip(pdaip); - outCenter2.setCommandCode(104); - String json = JSONObject.toJSONString(outCenter2); - if(WebSocketServerContent.getSocket(pdaip, json)){ - try { - Thread.sleep(2000); - if(m_GetOutCenterStopSuccessful){ - isSendFlag = true; - m_GetOutCenterStopSuccessful = false; - return R.data(200,"向客户端发送消息成功!"); + } + } else { + System.out.println("\n结束读返回:消息超时!"); + return R.data(201, "向客户端发送消息成功,接收客户端消息超时!"); } - else { - System.out.println("《《《等待客户端返回消息超时!"); - isSendFlag = false; - return R.data(201,"向客户端发送消息成功,接收客户端消息超时!"); - } - }catch (Exception ex){ - return R.data(100,"向客户端发送消息失败!"); + } catch (Exception ex) { + return R.data(100, "向客户端发送消息失败!"); } - } - else { - return R.data(101,"向客户端发送消息失败!"); + } else { + System.out.println("\n结束读返回:失败!"); + return R.data(101, "向客户端发送消息失败!"); } } + + } diff --git a/src/main/java/com/example/services/impl/WebSocketServerContent.java b/src/main/java/com/example/services/impl/WebSocketServerContent.java index d5b7701..07d3d33 100644 --- a/src/main/java/com/example/services/impl/WebSocketServerContent.java +++ b/src/main/java/com/example/services/impl/WebSocketServerContent.java @@ -19,7 +19,7 @@ import java.util.concurrent.Semaphore; public class WebSocketServerContent { private String sid = ""; ServiceImpl ServiceImpl = new ServiceImpl(); -// RFIDController RFIDController = new RFIDController(); + // RFIDController RFIDController = new RFIDController(); //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。 private static int onlineCount = 0; //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识 @@ -28,24 +28,26 @@ public class WebSocketServerContent { private Session session; private static String RecMessage = ""; + /** * 连接建立成功调用的方法 - * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据 + * + * @param session 可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据 */ @OnOpen - public void onOpen(Session session,@PathParam("sid") String sid){ + public void onOpen(Session session, @PathParam("sid") String sid) { this.session = session; this.sid = sid; webSocketSet.add(this); //加入set中 addOnlineCount(); //在线数加1 - System.out.println("有一连接加入!"+session.getId()+"当前在线人数为" + getOnlineCount()+",IP:" + sid); + System.out.println("有一连接加入!" + session.getId() + "当前在线人数为" + getOnlineCount() + ",IP:" + sid); } /** * 连接关闭调用的方法 */ @OnClose - public void onClose(){ + public void onClose() { webSocketSet.remove(this); //从set中删除 subOnlineCount(); //在线数减1 System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount()); @@ -54,26 +56,27 @@ public class WebSocketServerContent { /** * 收到客户端消息后调用的方法 * - * @param message - * 客户端发送过来的消息 + * @param message 客户端发送过来的消息 */ @OnMessage public void onMessage(String message, Session session) throws InterruptedException { - System.out.println("手持机发来的信息是"+message); - RecMessage = message; -// Thread.sleep(3000); - //this.sendMessage("Hello, " + message, session); + System.out.println("手持机发来的信息是:" + message); + ServiceImpl.DealValidPack(message); + //RecMessage = message; } + public static String getRecMessage() { + return RecMessage; + } @OnError public void onError(Session session, Throwable error) { - System.out.println("发生错误"); - error.printStackTrace(); + System.out.println("未断开连接强退发生错误"); +// error.printStackTrace(); } @Bean - public void Runable(){ + public void Runable() { Thread t1 = new Thread(new Runnable() { @SneakyThrows public void run() { @@ -82,14 +85,15 @@ public class WebSocketServerContent { }); t1.start(); } + private void DealMessageDoWork() throws InterruptedException { - while (true){ - if(ServiceImpl != null){ - if(!RecMessage.isEmpty() || !RecMessage.equals("")){ + while (true) { + if (ServiceImpl != null) { + if (!RecMessage.isEmpty() || !RecMessage.equals("")) { ServiceImpl.DealValidPack(RecMessage); RecMessage = ""; } - System.out.println(DateUtils.getTime() +"一开始就运行"); +// System.out.println(DateUtils.getTime() +"一开始就运行"); Thread.sleep(1000); } @@ -101,7 +105,7 @@ public class WebSocketServerContent { */ public void sendMessage(String message, Session toSession) { try { - System.out.println("服务端给客户端发送消息"+message); + System.out.println("服务端给客户端发送消息" + message); toSession.getBasicRemote().sendText(message); } catch (Exception e) { System.out.println("服务端发送消息给客户端失败"); @@ -109,31 +113,29 @@ public class WebSocketServerContent { } } - public static synchronized boolean getSocket(String id,String message) { + public static synchronized boolean getSocket(String id, String message) { boolean isFlag = false; try { - if(id.equals("") || id == null){ - for (WebSocketServerContent i: webSocketSet - ) { + if (id.equals("") || id == null) { + for (WebSocketServerContent i : webSocketSet) { i.sendMessage(message, i.session); isFlag = true; } - } - else{ - for (WebSocketServerContent i: webSocketSet - ) { - if(i.sid.equals(id)){ - i.sendMessage(message,i.session); + } else { + for (WebSocketServerContent i : webSocketSet) { + if (i.sid.equals(id)) { + i.sendMessage(message, i.session); isFlag = true; } } } - }catch (Exception ex){ + } catch (Exception ex) { return false; } return isFlag; } + public static synchronized int getOnlineCount() { return onlineCount; }