// @PostConstruct public void receiveSms() throws IOException, ClassNotFoundException { String hostIp = InetAddress.getLocalHost().getHostAddress(); System.out.println(hostIp); SGIPServerInitInfo info = new SGIPServerInitInfo(); info.IP = hostIp; info.port = 8801; // 本机监听端口,默认8801 info.userName = this.userName; info.passWord = this.passWord; SGIPServer server = SGIPServer.getInstance(); server.init(info); server.start(); // 启动短信接收线程 Thread recThread = new Thread(this); recThread.start(); }
public void run() { SGIPServer server = SGIPServer.getInstance(); System.out.println("开始监听是否有接收到用户回复的短信。"); while (true) { if (server.MessageLength() > 0) { System.out.println("接收到用户回复的短信。"); // msg 在多线程并发情况下可能为空 SGIPRecieveMsg msg = server.ReceiveMessage(); if (msg != null) { if (msg.messageType == 1) { // deliver SGIPDeliver deliver = (SGIPDeliver) msg.obj; System.out.println(deliver.getBody().getMessageCoding()); try { // 将用户回复的短信,转发到指定的用户号码 String _spNumber = deliver.getBody().getSPNumber(); System.out.println(_spNumber); // 该短信发起者的标识号,默认为空,表示是系统短信,如考勤短信 String srcNo = ""; // 回复短信的用户手机号码 String destNo = deliver.getBody().getUserNumber().substring(2); // 用户短信回复的内容 String msgContent = null; if (8 == deliver.getBody().getMessageCoding()) { msgContent = new String(deliver.getBody().getMessageContent(), "UnicodeBigUnmarked"); } else if (0 == deliver.getBody().getMessageCoding()) { msgContent = new String(deliver.getBody().getMessageContent()); } System.out.println("用户短信回复=" + msgContent); // 这里配合正元做设备对接短信校验码 // 假设正元测试的手机号码是: 18668030215 // 赵彦华-博奥-正元 2013.10.21 日购买的杭州联通卡 if ("18668030215".equalsIgnoreCase(destNo)) { String msg_key = this.handler.processSmsAuthentication(destNo, msgContent); if (null != msg_key) { // 回复设备短信认证码 TSmsend tSmsend = new TSmsend(); tSmsend.setId(new BigDecimal(0)); // 在保存到数据库的时候,将由触发器重新赋值 tSmsend.setFlag(0); tSmsend.setDestnbr(destNo); tSmsend.setMsgcontent(msg_key); tSmsend.setSrcnbr(""); this.tSmsendDao.save(tSmsend); } } // 这里开始处理二代卡回复的短信 // 这些短信内容,均需写入二代卡短信日志表(t_smsend_2card_log) // 平台使用该短信号码(10655922101111111111)向二代卡发送指令 if ("10655922101111111111".equalsIgnoreCase(_spNumber)) { TSmsend2cardLog tSmsend2cardLog = new TSmsend2cardLog(); tSmsend2cardLog.setId(new BigDecimal(0)); // 在保存到数据库的时候,将由触发器重新赋值 tSmsend2cardLog.setSrcnbr(destNo); tSmsend2cardLog.setDestnbr(_spNumber); tSmsend2cardLog.setFlag(new Integer(2)); // 2表示二代卡回复短信给平台 tSmsend2cardLog.setMsgcontent(msgContent); tSmsend2cardLog.setSendtime(new Timestamp(System.currentTimeMillis())); this.tSmsend2cardLogDao.save(tSmsend2cardLog); // 不需要将二代卡的短信回复,保存到数据库短信表中,只需要记录日志即可 } else if (!_spNumber.equalsIgnoreCase(this.spNumber)) { // 目前直接用用户手机号码后十位做标识 // 补前缀=1 _spNumber = "1" + _spNumber.substring(_spNumber.length() - 10); System.out.println("_spNumber==" + _spNumber); TSmsend tSmsend = new TSmsend(); tSmsend.setId(new BigDecimal(0)); // 在保存到数据库的时候,将由触发器重新赋值 tSmsend.setFlag(0); tSmsend.setDestnbr(_spNumber); tSmsend.setMsgcontent(msgContent); tSmsend.setSrcnbr(destNo); this.tSmsendDao.save(tSmsend); } } catch (Exception e) { e.printStackTrace(); } System.out.println(deliver.getBody().getUserNumber()); } else if (msg.messageType == 2) { // report SGIPReport report = (SGIPReport) msg.obj; System.out.println("SGIP State=" + report.getBody().getState()); } } } } }