Commit a6f5c7a1192d129c47578d626b9e151a9d2b02d7

Authored by 陶汉栋
2 parents 44ae409b 440ab2e4
Exists in master and in 1 other branch cloud_copy

Merge branch 'master' of http://git.shunzhi.net/taohd/mycloud

# Conflicts:
#	cloud/weigeng/src/main/java/com/sincere/weigeng/WatchServer.java
#	cloud/weigeng/src/main/java/com/sincere/weigeng/controller/WgController.java
#	cloud/weigeng/src/main/java/com/sincere/weigeng/utils/WgUdpCommShort.java
cloud/weigeng/src/main/java/com/sincere/weigeng/WatchServer.java 0 → 100644
... ... @@ -0,0 +1,530 @@
  1 +package com.sincere.weigeng;
  2 +
  3 +
  4 +import com.sincere.common.dto.smartCampus.UserDto;
  5 +import com.sincere.common.dto.xiaoan.CheckInDto;
  6 +import com.sincere.common.dto.xiaoan.CheckOutDto;
  7 +import com.sincere.common.dto.xiaoan.PassFailDto;
  8 +import com.sincere.common.dto.xiaoan.SendMessageDto;
  9 +import com.sincere.common.util.DateUtils;
  10 +import com.sincere.weigeng.feign.SmFeign;
  11 +import com.sincere.weigeng.feign.XaFeign;
  12 +import com.sincere.weigeng.logs.LogName;
  13 +import com.sincere.weigeng.logs.LoggerUtils;
  14 +import com.sincere.weigeng.utils.WGUtils;
  15 +import com.sincere.weigeng.utils.WatchingShortHandler;
  16 +import com.sincere.weigeng.utils.WgUdpCommShort;
  17 +import org.apache.commons.lang3.StringUtils;
  18 +import org.apache.mina.core.session.IoSession;
  19 +import org.apache.mina.transport.socket.DatagramSessionConfig;
  20 +import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
  21 +import org.slf4j.Logger;
  22 +import org.springframework.beans.factory.annotation.Autowired;
  23 +import org.springframework.boot.ApplicationArguments;
  24 +import org.springframework.boot.ApplicationRunner;
  25 +import org.springframework.stereotype.Component;
  26 +
  27 +import java.io.IOException;
  28 +import java.net.InetSocketAddress;
  29 +import java.util.*;
  30 +import java.util.concurrent.ConcurrentHashMap;
  31 +
  32 +/**
  33 + * @author chen
  34 + * @version 1.0
  35 + * @date 2019/10/12 0012 16:57
  36 + */
  37 +
  38 +@Component
  39 +public class WatchServer implements ApplicationRunner {
  40 +
  41 + private static final Logger Log_orderSuccess = LoggerUtils.Logger(LogName.orderSuccess);
  42 + private static final Logger Log_orderFail = LoggerUtils.Logger(LogName.orderFail);
  43 + private static final Logger Log_kaoInfo = LoggerUtils.Logger(LogName.kaoInfo);
  44 + private static final Logger Log_heartBeat = LoggerUtils.Logger(LogName.heartBeat);
  45 + private static final Logger Log_error = LoggerUtils.Logger(LogName.error);
  46 +
  47 + private static List<Long> snoList = new ArrayList<>();
  48 + private static List<Long> indexList = new ArrayList<>();
  49 + private static WatchingShortHandler watchingShortHandler ;
  50 + private static Queue<byte[]> queue = new LinkedList<>();
  51 + private static Map<Long , IoSession> sessionMap = new ConcurrentHashMap<>();
  52 +
  53 + @Autowired
  54 + SmFeign smFeign;
  55 +
  56 + @Autowired
  57 + XaFeign xaFeign;
  58 +
  59 + private static String ip = "172.16.3.175";
  60 + private static int port = 1200;
  61 +
  62 + @Override
  63 + public void run(ApplicationArguments args) {
  64 + //启动服务监听
  65 + this.WatchingServerRunning(ip,port);
  66 + }
  67 +
  68 + // 进入服务器监控状态
  69 + public int WatchingServerRunning(String watchServerIP,int watchServerPort) {
  70 + watchingShortHandler = new WatchingShortHandler(queue,sessionMap);
  71 + // 创建UDP数据包NIO
  72 + NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
  73 + // NIO设置底层IOHandler
  74 + acceptor.setHandler(watchingShortHandler);
  75 +
  76 + // 设置是否重用地址? 也就是每个发过来的udp信息都是一个地址?
  77 + DatagramSessionConfig dcfg = acceptor.getSessionConfig();
  78 + dcfg.setReuseAddress(false);
  79 + // 绑定端口地址
  80 + try {
  81 + acceptor.bind(new InetSocketAddress(watchServerIP, watchServerPort));
  82 + } catch (IOException e) {
  83 + Log_orderSuccess.info("绑定接收服务器失败....");
  84 + e.printStackTrace();
  85 + return 0;
  86 + }
  87 + Log_orderSuccess.info("绑定接收服务器成功....");
  88 + long recordIndex = 0;
  89 + while(true) {
  90 + if (!queue.isEmpty()) {
  91 + byte[] recvBuff;
  92 + synchronized (queue) {
  93 + recvBuff= queue.poll();
  94 + }
  95 + if (recvBuff[1]== 0x20) {
  96 + long sn = WgUdpCommShort.getLongByByte(recvBuff, 4, 4);
  97 + smFeign.updateLinkTime(sn+"");
  98 + Log_heartBeat.info("设备"+sn);
  99 + boolean isExist = true ;
  100 + long recordIndexGet = WgUdpCommShort.getLongByByte(recvBuff, 8, 4);
  101 + if(snoList.indexOf(sn) >= 0){
  102 + int number = snoList.indexOf(sn);
  103 + recordIndex = indexList.get(number);
  104 + indexList.set(number,recordIndexGet);
  105 + }else {
  106 + snoList.add(sn);
  107 + recordIndex = 0 ;
  108 + indexList.add(recordIndexGet);
  109 + System.out.println("设备"+sn+"上线");
  110 + isExist = false ;
  111 + }
  112 + if(isExist){
  113 + if (recordIndex < recordIndexGet || (recordIndexGet - recordIndex) < -5) {
  114 + watching(recvBuff);
  115 + }
  116 + }
  117 + }else {
  118 + push(recvBuff);
  119 + }
  120 + } else {
  121 + long times = 100;
  122 + try {
  123 + Thread.sleep(times);
  124 + } catch (InterruptedException e) {
  125 + e.printStackTrace();
  126 + }
  127 + }
  128 + }
  129 + }
  130 +
  131 + private void watching(byte[] recv){
  132 + long res = 0;
  133 + //8-11 记录的索引号
  134 + //(=0表示没有记录) 4 0x00000000
  135 + int recordIndex = 0;
  136 + recordIndex = WGUtils.byteToInt(recv, 8, 4);
  137 + //12 记录类型**********************************************
  138 + //0=无记录
  139 + //1=刷卡记录
  140 + //2=门磁,按钮, 设备启动, 远程开门记录
  141 + //3=报警记录 1
  142 + //0xFF=表示指定索引位的记录已被覆盖掉了. 请使用索引0, 取回最早一条记录的索引值
  143 + int recordType = recv[12];
  144 +
  145 + //13 有效性(0 表示不通过, 1表示通过) 1
  146 + int recordValid = recv[13];
  147 +
  148 + //14 门号(1,2,3,4) 1 业务需要-->1出2进
  149 + int recordDoorNO = recv[14];
  150 +
  151 + //15 进门/出门(1表示进门, 2表示出门) 1 0x01
  152 + //int recordInOrOut = recv[15];
  153 +
  154 + //16-19 卡号(类型是刷卡记录时)
  155 + //或编号(其他类型记录) 4
  156 + String cardNo = WGUtils.getCardNo(recv, 16, 4);
  157 +
  158 + //20-26 刷卡时间:
  159 + //年月日时分秒 (采用BCD码)见设置时间部分的说明
  160 + String recordTime = "2000-01-01 00:00:00";
  161 + recordTime = String.format("%02X%02X-%02X-%02X %02X:%02X:%02X",
  162 + recv[20], recv[21], recv[22], recv[23], recv[24], recv[25], recv[26]);
  163 + //2012.12.11 10:49:59 7
  164 + //27 记录原因代码(可以查 “刷卡记录说明.xls”文件的ReasonNO)
  165 + //处理复杂信息才用 1
  166 + int reason = recv[27];
  167 + if (recordType == 0) {
  168 + Log_orderSuccess.info(String.format("索引位={0} 无记录", recordIndex));
  169 + }else if (recordType == 0xff) {
  170 + Log_orderSuccess.info("指定索引位的记录已被覆盖掉了,请使用索引0, 取回最早一条记录的索引值");
  171 + } else if (recordType == 1) {
  172 + long sno = 0;
  173 + sno = WgUdpCommShort.getLongByByte(recv, 4, 4);//解析设备号
  174 + String msg = "索引位=" + recordIndex
  175 + + ",卡号=" + cardNo
  176 + +"进出=" + (recordDoorNO == 1 ? "出门" : "进门")
  177 + + ",有效=" + (recordValid == 1 ? "通过" : "禁止")
  178 + + ",时间=" + recordTime
  179 + + ",描述=" + WGUtils.getReasonDetailChinese(reason) + "";
  180 + Log_orderSuccess.info("控制器:" + sno + msg);
  181 + if(recordValid == 1) {
  182 + //有效刷卡调考勤存储过程
  183 + CheckInDto checkIn = new CheckInDto();
  184 + checkIn.setDeviceId(sno+"");
  185 + checkIn.setCardNo(cardNo);
  186 + checkIn.setFunNo(8);
  187 + checkIn.setFlag(recordDoorNO == 1 ? 1 : 0);
  188 + checkIn.setCheckTime(recordTime);
  189 + CheckOutDto checkOutDto = xaFeign.checkIn(checkIn);
  190 + if(checkOutDto.getIsSuccess() == 1){
  191 + //考勤成功
  192 + String nowDate = DateUtils.date2String(new Date(),DateUtils.format2);
  193 + Log_kaoInfo.info("考勤成功!,设备:"+sno+"卡号:"+cardNo+"方向:"+(recordDoorNO == 1 ? "出门" : "进门")+"______刷卡时间"+ recordTime+";入库时间:"+nowDate);
  194 + }else {
  195 + //考勤失败
  196 + Log_orderFail.error("考勤失败!,设备:"+sno+"卡号:"+cardNo + "---"+ checkOutDto.getOut());
  197 + }
  198 + }else {
  199 + //判断是否请假
  200 + String studentNum = smFeign.checkLeave(cardNo);
  201 + if (StringUtils.isNotBlank(studentNum)) {
  202 + int outOf = recordDoorNO == 1 ? 1 : 0;
  203 + //远程开门
  204 + openDoor(sno,"64",recordDoorNO,cardNo);
  205 + Log_orderSuccess.info("请假开门成功"+cardNo);
  206 + } else {
  207 + //没有请假不做任何处理,则是刷卡异常,入库
  208 + UserDto user = smFeign.selectUserByCardNum(cardNo);
  209 + PassFailDto passFail = new PassFailDto();
  210 + passFail.setCardNum(cardNo);
  211 + passFail.setDeviceId(sno+"");
  212 + passFail.setDirection((recordDoorNO == 1 ? "出门" : "进门"));
  213 + passFail.setResultIntro(WGUtils.getReasonDetailChinese(reason));
  214 + passFail.setInTime(DateUtils.string2Date(recordTime, DateUtils.format2));
  215 + passFail.setCreateTime(new Date());
  216 + passFail.setSchoolId(user.getSchoolId());
  217 + xaFeign.insertPassFail(passFail);
  218 + }
  219 + }
  220 + }
  221 + }
  222 +
  223 + private void push(byte[] recv){
  224 + long index = WgUdpCommShort.getXidOfCommand(recv);
  225 + long sno = WgUdpCommShort.getLongByByte(recv, 4, 4);
  226 + String functionId = WGUtils.byte2Hex(recv[1]);
  227 + SendMessageDto sendMessage = xaFeign.selectMessage(sno+"",index,functionId);
  228 + if(sendMessage != null){
  229 + if(recv[8] == 1){
  230 + sendMessage.setCorrect(1);
  231 + }else {
  232 + sendMessage.setCorrect(0);
  233 + }
  234 + StringBuffer result = new StringBuffer();
  235 + for(byte b : recv){
  236 + result.append(WGUtils.byte2Hex(b)).append("-");
  237 + }
  238 + sendMessage.setResult(result.toString().substring(0,result.toString().length()-1));
  239 + xaFeign.updateMessage(sendMessage.getId(),sendMessage.getResult(),sendMessage.getCorrect());
  240 + }
  241 + }
  242 +
  243 + //远程开门
  244 + public long openDoor(long sno , String outsideOrderId,int doorNo , String cardNo){
  245 + WgUdpCommShort pkt = new WgUdpCommShort();
  246 + pkt.iDevSn = sno;
  247 + try{
  248 + int doorNO =doorNo;
  249 + pkt.Reset();
  250 + pkt.functionID = WGUtils.toFunctionHex(outsideOrderId);
  251 + pkt.data[0] =(byte) (doorNO & 0xff);
  252 + if(StringUtils.isNotBlank(cardNo)){
  253 + pkt.data[20] = WGUtils.toHex(cardNo.substring(0,2));
  254 + pkt.data[21] = WGUtils.toHex(cardNo.substring(2,4));
  255 + pkt.data[22] = WGUtils.toHex(cardNo.substring(4,6));
  256 + pkt.data[23] = WGUtils.toHex(cardNo.substring(6,8));
  257 + }
  258 + byte[] bytes = pkt.toByte();
  259 + long index = WgUdpCommShort.getXidOfCommand(bytes);
  260 + long result = insert(sno+"",outsideOrderId,cardNo,index,bytes);
  261 + pkt.run(watchingShortHandler.getSessionMap().get(sno),bytes);
  262 + return result ;
  263 + }catch (Exception e){
  264 + Log_error.error(e.toString());
  265 + }
  266 + return 0L;
  267 + }
  268 +
  269 + //重置控制板时间
  270 + public long setTime(long sno , String outsideOrderId){
  271 + WgUdpCommShort pkt = new WgUdpCommShort();
  272 + pkt.iDevSn = sno;
  273 + try{
  274 + pkt.Reset();
  275 + pkt.functionID = WGUtils.toFunctionHex(outsideOrderId);
  276 + Calendar cal = (Calendar.getInstance());
  277 + pkt.data[0] = WGUtils.toHex(String.valueOf(cal.get(Calendar.YEAR)).substring(0,2));
  278 + pkt.data[1] = WGUtils.toHex(String.valueOf(cal.get(Calendar.YEAR)).substring(2,4));
  279 + pkt.data[2] = WGUtils.toHex(String.valueOf(cal.get(Calendar.MONTH)+1));
  280 + pkt.data[3] = WGUtils.toHex(String.valueOf(cal.get(Calendar.DATE)));
  281 + pkt.data[4] =WGUtils.toHex(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)));
  282 + pkt.data[5] =WGUtils.toHex(String.valueOf(cal.get(Calendar.MINUTE)));
  283 + pkt.data[6] = WGUtils.toHex(String.valueOf(cal.get(Calendar.SECOND)));
  284 + byte[] bytes = pkt.toByte();
  285 + long index = WgUdpCommShort.getXidOfCommand(bytes);
  286 + long result = insert(sno+"",outsideOrderId,null,index,bytes);
  287 + pkt.run(watchingShortHandler.getSessionMap().get(sno),bytes);
  288 + return result ;
  289 + }catch (Exception e){
  290 + Log_error.error(e.toString());
  291 + }
  292 + return 0L;
  293 + }
  294 +
  295 + //设置考勤时段
  296 + public List<Long> SetAttendanceTime(long sno ,String outsideOrderId, int shiduan , Date begin ,Date end ,
  297 + int isMonDay ,int isTuesDay , int isWednesDay ,int isThursDay , int isFriday ,
  298 + int isSaturDay , int isWeekend , String shiqu){
  299 + List<Long> resultList = new ArrayList<>();
  300 + WgUdpCommShort pkt = new WgUdpCommShort();
  301 + pkt.iDevSn = sno;
  302 + try{
  303 + pkt.Reset();
  304 + pkt.functionID = WGUtils.toFunctionHex(outsideOrderId);
  305 + String[] shiQuArray = shiqu.split(",");
  306 + int shiDuanCount = shiQuArray.length / 6 ;
  307 + for (int i = 0; i < shiDuanCount; i++){
  308 + if (i == 0) {
  309 + pkt.data[0] = WGUtils.toHex(shiduan+"");
  310 + } else {
  311 + pkt.data[0] = WGUtils.toHex((shiduan+20*i)+"");
  312 + }
  313 + Calendar c = Calendar.getInstance();
  314 + c.setTime(begin);
  315 + //开始时间 20 19 01 01
  316 + pkt.data[1] = WGUtils.toHex(String.valueOf(c.get(Calendar.YEAR)).substring(0,2));
  317 + pkt.data[2] = WGUtils.toHex(String.valueOf(c.get(Calendar.YEAR)).substring(2,4));
  318 + pkt.data[3] = WGUtils.toHex(String.valueOf(c.get(Calendar.MONTH)+1));
  319 + pkt.data[4] = WGUtils.toHex(String.valueOf(c.get(Calendar.DATE)));
  320 + //结束时间
  321 + c.setTime(end);
  322 + pkt.data[5] = WGUtils.toHex(String.valueOf(c.get(Calendar.YEAR)).substring(0,2));
  323 + pkt.data[6] = WGUtils.toHex(String.valueOf(c.get(Calendar.YEAR)).substring(2,4));
  324 + pkt.data[7] = WGUtils.toHex(String.valueOf(c.get(Calendar.MONTH)+1));
  325 + pkt.data[8] = WGUtils.toHex(String.valueOf(c.get(Calendar.DATE)));
  326 + //星期几 有效
  327 + pkt.data[9] = WGUtils.toHex(String.valueOf(isMonDay));
  328 + pkt.data[10] = WGUtils.toHex(String.valueOf(isTuesDay));
  329 + pkt.data[11] = WGUtils.toHex(String.valueOf(isWednesDay));
  330 + pkt.data[12] = WGUtils.toHex(String.valueOf(isThursDay));
  331 + pkt.data[13] = WGUtils.toHex(String.valueOf(isFriday));
  332 + pkt.data[14] = WGUtils.toHex(String.valueOf(isSaturDay));
  333 + pkt.data[15] = WGUtils.toHex(String.valueOf(isWeekend));
  334 + //有效时区
  335 + pkt.data[16] = WGUtils.toHex(shiQuArray[i * 6 + 0].substring(0, 2));
  336 + pkt.data[17] = WGUtils.toHex(shiQuArray[i * 6 + 0].substring(3, 5));
  337 +
  338 + pkt.data[18] = WGUtils.toHex(shiQuArray[i * 6 + 1].substring(0, 2));
  339 + pkt.data[19] = WGUtils.toHex(shiQuArray[i * 6 + 1].substring(3, 5));
  340 +
  341 + pkt.data[20] = WGUtils.toHex(shiQuArray[i * 6 + 2].substring(0, 2));
  342 + pkt.data[21] = WGUtils.toHex(shiQuArray[i * 6 + 2].substring(3, 5));
  343 +
  344 + pkt.data[22] = WGUtils.toHex(shiQuArray[i * 6 + 3].substring(0, 2));
  345 + pkt.data[23] = WGUtils.toHex(shiQuArray[i * 6 + 3].substring(3, 5));
  346 +
  347 + pkt.data[24] = WGUtils.toHex(shiQuArray[i * 6 + 4].substring(0, 2));
  348 + pkt.data[25] = WGUtils.toHex(shiQuArray[i * 6 + 4].substring(3, 5));
  349 +
  350 + pkt.data[26] = WGUtils.toHex(shiQuArray[i * 6 + 5].substring(0, 2));
  351 + pkt.data[27] = WGUtils.toHex(shiQuArray[i * 6 + 5].substring(3, 5));
  352 + if (shiDuanCount != 1) {
  353 + //需要链接时段
  354 + if (i != shiDuanCount - 1) {
  355 + //只要不是最后一个时段
  356 + pkt.data[28] = WGUtils.toHex((shiduan+20*(i+1)+""));
  357 + } else {
  358 + pkt.data[28] = 0;
  359 + }
  360 + } else {
  361 + pkt.data[28] = 0;
  362 + }
  363 + byte[] bytes = pkt.toByte();
  364 + long index = WgUdpCommShort.getXidOfCommand(bytes);
  365 + long result = insert(sno+"",outsideOrderId,null,index,bytes);
  366 + pkt.run(watchingShortHandler.getSessionMap().get(sno),bytes);
  367 + resultList.add(result);
  368 + }
  369 + return resultList ;
  370 + }catch (Exception e){
  371 + Log_error.error(e.toString());
  372 + }
  373 + return resultList;
  374 + }
  375 +
  376 + //设置权限
  377 + public long SetSignalCardInfo(long sno , String outsideOrderId, String cardNo , int shiduan , Date begin , Date end){
  378 + WgUdpCommShort pkt = new WgUdpCommShort();
  379 + pkt.iDevSn = sno;
  380 + try{
  381 + pkt.Reset();
  382 + pkt.functionID = WGUtils.toFunctionHex(outsideOrderId);
  383 + pkt.iDevSn = sno;
  384 + //0D D7 37 00
  385 + pkt.data[0] = WGUtils.toHex(cardNo.substring(0,2));
  386 + pkt.data[1] = WGUtils.toHex(cardNo.substring(2,4));
  387 + pkt.data[2] = WGUtils.toHex(cardNo.substring(4,6));
  388 + pkt.data[3] = WGUtils.toHex(cardNo.substring(6,8));
  389 +
  390 + //20 10 01 01 起始日期: 2010年01月01日 (必须大于2001年)
  391 + Calendar c = Calendar.getInstance();
  392 + c.setTime(begin);
  393 + pkt.data[4] = WGUtils.toHex(String.valueOf(c.get(Calendar.YEAR)).substring(0,2));
  394 + pkt.data[5] = WGUtils.toHex(String.valueOf(c.get(Calendar.YEAR)).substring(2,4));
  395 + pkt.data[6] = WGUtils.toHex(String.valueOf(c.get(Calendar.MONTH)+1));
  396 + pkt.data[7] = WGUtils.toHex(String.valueOf(c.get(Calendar.DATE)));
  397 + //20 29 12 31 截止日期: 2029年12月31日
  398 + c.setTime(end);
  399 + pkt.data[8] = WGUtils.toHex(String.valueOf(c.get(Calendar.YEAR)).substring(0,2));
  400 + pkt.data[9] = WGUtils.toHex(String.valueOf(c.get(Calendar.YEAR)).substring(2,4));
  401 + pkt.data[10] = WGUtils.toHex(String.valueOf(c.get(Calendar.MONTH)+1));
  402 + pkt.data[11] = WGUtils.toHex(String.valueOf(c.get(Calendar.DATE)));
  403 + //01 允许通过 一号门 [对单门, 双门, 四门控制器有效]
  404 + pkt.data[12] = WGUtils.toHex(shiduan+"");
  405 + //01 允许通过 二号门 [对双门, 四门控制器有效]
  406 + pkt.data[13] = WGUtils.toHex(shiduan+"");
  407 + //01 允许通过 三号门 [对四门控制器有效]
  408 + pkt.data[14] = WGUtils.toHex(shiduan+"");
  409 + //01 允许通过 四号门 [对四门控制器有效]
  410 + pkt.data[15] = WGUtils.toHex(shiduan+"");
  411 + byte[] bytes = pkt.toByte();
  412 + long index = WgUdpCommShort.getXidOfCommand(bytes);
  413 + long result = insert(sno+"",outsideOrderId,cardNo,index,bytes);
  414 + pkt.run(watchingShortHandler.getSessionMap().get(sno),bytes);
  415 + return result ;
  416 + }catch (Exception e){
  417 + Log_error.error(e.toString());
  418 + }
  419 + return 0l;
  420 + }
  421 +
  422 + //删除单张卡权限
  423 + public long clearSinglePower(long sno ,String outsideOrderId, String cardNo){
  424 + WgUdpCommShort pkt = new WgUdpCommShort();
  425 + pkt.iDevSn = sno;
  426 + try{
  427 + pkt.Reset();
  428 + pkt.functionID = WGUtils.toFunctionHex(outsideOrderId);
  429 + pkt.iDevSn = sno;
  430 + pkt.data[0] = WGUtils.toHex(cardNo.substring(0,2));
  431 + pkt.data[1] = WGUtils.toHex(cardNo.substring(2,4));
  432 + pkt.data[2] = WGUtils.toHex(cardNo.substring(4,6));
  433 + pkt.data[3] = WGUtils.toHex(cardNo.substring(6,8));
  434 + byte[] bytes = pkt.toByte();
  435 + long index = WgUdpCommShort.getXidOfCommand(bytes);
  436 + long result = insert(sno+"",outsideOrderId,cardNo,index,bytes);
  437 + pkt.run(watchingShortHandler.getSessionMap().get(sno),bytes);
  438 + return result ;
  439 + }catch (Exception e){
  440 + Log_error.error(e.toString());
  441 + }
  442 + return 0l;
  443 + }
  444 +
  445 + //删除全部权限
  446 + public long clearAllPower(long sno ,String outsideOrderId){
  447 + WgUdpCommShort pkt = new WgUdpCommShort();
  448 + pkt.iDevSn = sno;
  449 + try{
  450 + pkt.Reset();
  451 + pkt.functionID = WGUtils.toFunctionHex(outsideOrderId);
  452 + pkt.iDevSn = sno;
  453 + pkt.data[0] = (byte) 0x55 ;
  454 + pkt.data[1] = (byte) 0xAA ;
  455 + pkt.data[2] = (byte) 0xAA ;
  456 + pkt.data[3] = (byte) 0x55 ;
  457 + byte[] bytes = pkt.toByte();
  458 + long index = WgUdpCommShort.getXidOfCommand(bytes);
  459 + long result = insert(sno+"",outsideOrderId,null,index,bytes);
  460 + pkt.run(watchingShortHandler.getSessionMap().get(sno),bytes);
  461 + return result ;
  462 + }catch (Exception e){
  463 + Log_error.error(e.toString());
  464 + }
  465 + return 0l;
  466 + }
  467 +
  468 + //查询卡权限
  469 + public long searchPower(long sno ,String outsideOrderId, String cardNo){
  470 + WgUdpCommShort pkt = new WgUdpCommShort();
  471 + pkt.iDevSn = sno;
  472 + try{
  473 + pkt.Reset();
  474 + pkt.functionID = WGUtils.toFunctionHex(outsideOrderId);
  475 + pkt.iDevSn = sno;
  476 + pkt.data[0] = WGUtils.toHex(cardNo.substring(0,2));
  477 + pkt.data[1] = WGUtils.toHex(cardNo.substring(2,4));
  478 + pkt.data[2] = WGUtils.toHex(cardNo.substring(4,6));
  479 + pkt.data[3] = WGUtils.toHex(cardNo.substring(6,8));
  480 + byte[] bytes = pkt.toByte();
  481 + long index = WgUdpCommShort.getXidOfCommand(bytes);
  482 + long result = insert(sno+"",outsideOrderId,cardNo,index,bytes);
  483 + pkt.run(watchingShortHandler.getSessionMap().get(sno),bytes);
  484 + return result ;
  485 + }catch (Exception e){
  486 + Log_error.error(e.toString());
  487 + }
  488 + return 0l;
  489 + }
  490 +
  491 + //删除时段
  492 + public long clearShiDuan(long sno , String outsideOrderId){
  493 + WgUdpCommShort pkt = new WgUdpCommShort();
  494 + pkt.iDevSn = sno;
  495 + try{
  496 + pkt.Reset();
  497 + pkt.functionID = WGUtils.toFunctionHex(outsideOrderId);
  498 + pkt.iDevSn = sno;
  499 + pkt.data[0] = (byte) 0x55 ;
  500 + pkt.data[1] = (byte) 0xAA ;
  501 + pkt.data[2] = (byte) 0xAA ;
  502 + pkt.data[3] = (byte) 0x55 ;
  503 + byte[] bytes = pkt.toByte();
  504 + long index = WgUdpCommShort.getXidOfCommand(bytes);
  505 + long result = insert(sno+"",outsideOrderId,null,index,bytes);
  506 + pkt.run(watchingShortHandler.getSessionMap().get(sno),bytes);
  507 + return result ;
  508 + }catch (Exception e){
  509 + Log_error.error(e.toString());
  510 + }
  511 + return 0l;
  512 + }
  513 +
  514 + private long insert(String sn ,String functionId , String cardNo , long index , byte[] recv){
  515 +
  516 + StringBuffer send = new StringBuffer();
  517 + for(byte b : recv){
  518 + send.append(WGUtils.byte2Hex(b)).append("-");
  519 + }
  520 + SendMessageDto sendMessage = new SendMessageDto();
  521 + sendMessage.setDeviceId(sn);
  522 + sendMessage.setFunctionId(Integer.toHexString(Integer.valueOf(functionId)));
  523 + sendMessage.setIndex(index);
  524 + sendMessage.setCardNo(cardNo);
  525 + sendMessage.setCreateTime(new Date());
  526 + sendMessage.setSend(send.toString().substring(0,send.toString().length()-1));
  527 + xaFeign.insertMessage(sendMessage);
  528 + return sendMessage.getId();
  529 + }
  530 +}
... ...
cloud/weigeng/src/main/java/com/sincere/weigeng/controller/WgController.java 0 → 100644
... ... @@ -0,0 +1,216 @@
  1 +package com.sincere.weigeng.controller;
  2 +
  3 +import com.sincere.common.dto.smartCampus.SchoolDto;
  4 +import com.sincere.common.dto.smartCampus.StudentCardDto;
  5 +import com.sincere.common.dto.smartCampus.UpdateCardDto;
  6 +import com.sincere.common.dto.smartCampus.UserDto;
  7 +import com.sincere.common.dto.xiaoan.SendFailDto;
  8 +import com.sincere.common.dto.xiaoan.SendMessageDto;
  9 +import com.sincere.common.dto.xiaoan.SendSuccessDto;
  10 +import com.sincere.weigeng.feign.SmFeign;
  11 +import com.sincere.weigeng.feign.XaFeign;
  12 +import com.sincere.weigeng.logs.LogName;
  13 +import com.sincere.weigeng.logs.LoggerUtils;
  14 +import com.sincere.weigeng.WatchServer;
  15 +import com.sincere.weigeng.vo.*;
  16 +import io.swagger.annotations.Api;
  17 +import io.swagger.annotations.ApiOperation;
  18 +import org.slf4j.Logger;
  19 +import org.springframework.beans.factory.annotation.Autowired;
  20 +import org.springframework.web.bind.annotation.RequestBody;
  21 +import org.springframework.web.bind.annotation.RequestMapping;
  22 +import org.springframework.web.bind.annotation.RequestMethod;
  23 +import org.springframework.web.bind.annotation.RestController;
  24 +
  25 +import java.util.List;
  26 +
  27 +/**
  28 + * @author chen
  29 + * @version 1.0
  30 + * @date 2019/10/14 0014 9:12
  31 + */
  32 +@RestController
  33 +@Api(value = "微耕")
  34 +public class WgController {
  35 +
  36 + private static final Logger Log_orderSuccess = LoggerUtils.Logger(LogName.orderSuccess);
  37 + private static final Logger Log_orderFail = LoggerUtils.Logger(LogName.orderFail);
  38 +
  39 + @Autowired
  40 + SmFeign smFeign;
  41 +
  42 + @Autowired
  43 + XaFeign xaFeign;
  44 +
  45 + @Autowired
  46 + WatchServer watchServer;
  47 +
  48 +
  49 + @RequestMapping(value = "setTime" , method = RequestMethod.GET)
  50 + public void setTime(long sn){
  51 + watchServer.setTime(sn,"48");
  52 + }
  53 +
  54 + @ApiOperation("远程开门")
  55 + @RequestMapping(value = "openDoor" , method = RequestMethod.POST)
  56 + public boolean openDoor(@RequestBody OpenDoorVo openDoorVo) {
  57 + String functionId = smFeign.selectOutOrderId(openDoorVo.getType(),openDoorVo.getId());
  58 + long messageId = watchServer.openDoor(openDoorVo.getSn(),functionId,openDoorVo.getDoorNo(),null);
  59 + SendMessageDto message = getResult(messageId);
  60 + if(message.getCorrect() == 1){
  61 + Log_orderSuccess.info("web端远程开门成功!");
  62 + }else {
  63 + Log_orderFail.info("远程开门失败");
  64 + }
  65 + return message.getCorrect() == 1 ;
  66 + }
  67 +
  68 +
  69 + @ApiOperation("设置考勤时段")
  70 + @RequestMapping(value = "setAttendanceTime" , method = RequestMethod.POST)
  71 + public boolean setAttendanceTime(@RequestBody AttendanceTimeVo attendanceTimeVo){
  72 + String functionId = smFeign.selectOutOrderId(attendanceTimeVo.getType(),attendanceTimeVo.getId());
  73 + List<Long> result = watchServer.SetAttendanceTime(attendanceTimeVo.getSn(),functionId,attendanceTimeVo.getShiduan(),
  74 + attendanceTimeVo.getStart(),attendanceTimeVo.getEnd(),attendanceTimeVo.getIsMonday(),attendanceTimeVo.getIsTuesday(),
  75 + attendanceTimeVo.getIsWednesday(),attendanceTimeVo.getIsThursday(),attendanceTimeVo.getIsFriday(),
  76 + attendanceTimeVo.getIsSaturday(),attendanceTimeVo.getIsWeekend(),attendanceTimeVo.getShiqu());
  77 + boolean isSuccess = true ;
  78 + for(Long messageId : result){
  79 + SendMessageDto message = getResult(messageId);
  80 + if(message.getCorrect() == 1){
  81 + Log_orderSuccess.info("设备"+attendanceTimeVo.getSn()+"时段"+attendanceTimeVo.getShiduan()+"!!设置成功");
  82 + }else {
  83 + Log_orderFail.info("设备"+attendanceTimeVo.getSn()+"时段"+attendanceTimeVo.getShiduan()+"!!设置失败");
  84 + isSuccess = false ;
  85 + }
  86 + }
  87 + return isSuccess;
  88 + }
  89 +
  90 +
  91 + @ApiOperation("单个卡号的权限添加或修改")
  92 + @RequestMapping(value = "setSignalCardInfo" , method = RequestMethod.POST)
  93 + public boolean setSignalCardInfo(@RequestBody SignalCardInfoVo signalCardInfoVo){
  94 + String functionId = smFeign.selectOutOrderId(signalCardInfoVo.getType(),signalCardInfoVo.getId());
  95 + String cardNo = initCardNo(signalCardInfoVo.getCardNo());
  96 + long messageId = watchServer.SetSignalCardInfo(signalCardInfoVo.getSn(),functionId,cardNo,
  97 + signalCardInfoVo.getShiduan(),signalCardInfoVo.getStartTime(),signalCardInfoVo.getEndTime());
  98 + SendMessageDto message = getResult(messageId);
  99 + UserDto user = smFeign.selectUserByCardNum(cardNo);
  100 + StudentCardDto studentCard = smFeign.selectStudentCard(cardNo);
  101 + SchoolDto school = smFeign.selectSchoolBySchoolId(user.getSchoolId());
  102 + UpdateCardDto updateCard = smFeign.selectUpdateCardByUpdateId(signalCardInfoVo.getUpdateId());
  103 + if(message.getCorrect() == 1){
  104 + Log_orderSuccess.info("设备"+signalCardInfoVo.getSn()+"时段"+signalCardInfoVo.getShiduan()+"卡号"+cardNo+"!!设置成功");
  105 + SendSuccessDto sendSuccess = new SendSuccessDto(user,school,studentCard,updateCard);
  106 + sendSuccess.setDeviceId(signalCardInfoVo.getSn()+"");
  107 + sendSuccess.setShiduan(signalCardInfoVo.getShiduan());
  108 + sendSuccess.setCardNum(cardNo);
  109 + sendSuccess.setUpdateId(signalCardInfoVo.getUpdateId());
  110 + sendSuccess.setOpenFlag(1);
  111 + sendSuccess.setStatus(1);
  112 + xaFeign.insertSendSuccess(sendSuccess);
  113 + }else {
  114 + Log_orderFail.info("设备"+signalCardInfoVo.getSn()+"时段"+signalCardInfoVo.getShiduan()+"卡号"+cardNo+"!!设置失败");
  115 + SendFailDto sendFail = new SendFailDto(user,school,studentCard,updateCard);
  116 + sendFail.setDeviceId(signalCardInfoVo.getSn()+"");
  117 + sendFail.setShiduan(signalCardInfoVo.getShiduan());
  118 + sendFail.setCardNum(cardNo);
  119 + sendFail.setUpdateId(signalCardInfoVo.getUpdateId());
  120 + sendFail.setOpenFlag(1);
  121 + sendFail.setStatus(1);
  122 + sendFail.setFailType(2);
  123 + sendFail.setFailContent("其他");
  124 + xaFeign.insertSendFail(sendFail);
  125 + }
  126 + return message.getCorrect() == 1;
  127 + }
  128 +
  129 +
  130 + @ApiOperation("清除单个卡号权限")
  131 + @RequestMapping(value = "clearSinglePower" , method = RequestMethod.POST)
  132 + public boolean clearSinglePower(@RequestBody CardInfo cardInfo){
  133 + String functionId = smFeign.selectOutOrderId(cardInfo.getType(),cardInfo.getId());
  134 + String cardNo = initCardNo(cardInfo.getCardNo());
  135 + long messageId = watchServer.clearSinglePower(cardInfo.getSn(),functionId,cardNo);
  136 + SendMessageDto message = getResult(messageId);
  137 + if(message.getCorrect() == 1){
  138 + Log_orderSuccess.info("卡号"+cardNo+"清除权限成功");
  139 + SendSuccessDto sendSuccess = new SendSuccessDto();
  140 + sendSuccess.setCardNum(cardNo);
  141 + sendSuccess.setDeviceId(cardInfo.getSn()+"");
  142 + xaFeign.updateSendSuccess(sendSuccess);
  143 + }else {
  144 + Log_orderFail.info("卡号"+cardNo+"清除权限失败");
  145 + }
  146 + return message.getCorrect() == 1 ;
  147 + }
  148 +
  149 + @ApiOperation("清除全部权限")
  150 + @RequestMapping(value = "clearAllPower" , method = RequestMethod.POST)
  151 + public boolean clearAllPower(@RequestBody CleanShiDuanVo cleanShiDuanVo){
  152 + String functionId = smFeign.selectOutOrderId(cleanShiDuanVo.getType(),cleanShiDuanVo.getId());
  153 + long messageId = watchServer.clearAllPower(cleanShiDuanVo.getSn(),functionId);
  154 + SendMessageDto message = getResult(messageId);
  155 + if(message.getCorrect() == 1){
  156 + Log_orderSuccess.info("设备"+cleanShiDuanVo.getSn()+"清除权限成功");
  157 + SendSuccessDto sendSuccess = new SendSuccessDto();
  158 + sendSuccess.setDeviceId(cleanShiDuanVo.getSn()+"");
  159 + xaFeign.updateSendSuccess(sendSuccess);
  160 + }else {
  161 + Log_orderFail.info("设备"+cleanShiDuanVo.getSn()+"清除权限失败");
  162 + }
  163 + return message.getCorrect() == 1 ;
  164 + }
  165 +
  166 +
  167 + @ApiOperation("清除时段")
  168 + @RequestMapping(value = "clearShiDuan" , method = RequestMethod.POST)
  169 + public boolean clearShiDuan(@RequestBody CleanShiDuanVo cleanShiDuanVo){
  170 + String functionId = smFeign.selectOutOrderId(cleanShiDuanVo.getType(),cleanShiDuanVo.getId());
  171 + long messageId = watchServer.clearShiDuan(cleanShiDuanVo.getSn(),functionId);
  172 + SendMessageDto sendMessage = getResult(messageId);
  173 + if(sendMessage.getCorrect() == 1){
  174 + Log_orderSuccess.info("设备"+cleanShiDuanVo.getSn()+"时段清除成功");
  175 + }else {
  176 + Log_orderFail.info("设备"+cleanShiDuanVo.getSn()+"时段清除失败");
  177 + }
  178 + return sendMessage.getCorrect() == 1;
  179 + }
  180 +
  181 + @ApiOperation("查询单个卡号 权限")
  182 + @RequestMapping(value = "searchPower" , method = RequestMethod.POST)
  183 + public boolean searchPower(@RequestBody CardInfo cardInfo){
  184 + String functionId = smFeign.selectOutOrderId(cardInfo.getType(),cardInfo.getId());
  185 + String cardNo = initCardNo(cardInfo.getCardNo());
  186 + long messageId = watchServer.searchPower(cardInfo.getSn(),functionId,cardNo);
  187 + SendMessageDto sendMessage = getResult(messageId);
  188 + if(sendMessage.getCorrect() == 1){
  189 + Log_orderSuccess.info("设备"+cardInfo.getSn()+"卡号"+cardNo+"查询权限成功");
  190 + }else {
  191 + Log_orderFail.info("设备"+cardInfo.getSn()+"卡号"+cardNo+"查询权限失败");
  192 + }
  193 + return sendMessage.getCorrect() == 1;
  194 + }
  195 +
  196 + @RequestMapping(value = "watch" , method = RequestMethod.GET)
  197 + public int watch(){
  198 + return 1 ;
  199 + }
  200 +
  201 + private SendMessageDto getResult(long messageId){
  202 + try{
  203 + Thread.sleep(300);
  204 + }catch (Exception e){
  205 +
  206 + }
  207 + return xaFeign.selectById(messageId);
  208 + }
  209 +
  210 + private String initCardNo(String cardNo){
  211 + while (cardNo.length() < 8){
  212 + cardNo = "0" + cardNo ;
  213 + }
  214 + return cardNo ;
  215 + }
  216 +}
... ...
cloud/weigeng/src/main/java/com/sincere/weigeng/utils/WgUdpCommShort.java 0 → 100644
... ... @@ -0,0 +1,120 @@
  1 +package com.sincere.weigeng.utils;
  2 +
  3 +import org.apache.mina.core.buffer.IoBuffer;
  4 +import org.apache.mina.core.session.IoSession;
  5 +
  6 +public class WgUdpCommShort { //短报文协议
  7 +
  8 + public static final int WGPacketSize = 64; //报文长度
  9 + public static final byte Type = 0x17; //2015-04-30 08:50:29 0x19; //类型
  10 + public static final int ControllerPort = 60000; //控制器端口
  11 + public static final long SpecialFlag = 0x55AAAA55; //特殊标识 防止误操作
  12 +
  13 + public static byte[] longToByte(long number) {
  14 + byte[] b = new byte[8];
  15 + for (int i = 0; i < 8; i++) {
  16 + b[i] = (byte) (number % 256);
  17 + number >>= 8;
  18 + }
  19 + return b;
  20 + }
  21 +
  22 + //从字节转换为 long型数据, 最大长度为8字节 低位在前, 高位在后...
  23 + //bytlen (1--8), 不在此范围则返回 -1
  24 + public static long getLongByByte(byte[] data,int startIndex,int bytlen)
  25 + {
  26 + long ret =-1;
  27 + if ((bytlen >=1) && (bytlen <=8))
  28 + {
  29 + ret = getIntByByte(data[startIndex + bytlen-1]);
  30 + for (int i=1; i<bytlen; i++)
  31 + {
  32 + ret <<=8;
  33 + ret += getIntByByte(data[startIndex + bytlen-1-i]);
  34 + }
  35 + }
  36 + return ret;
  37 + }
  38 +
  39 + //将带符号的bt转换为不带符号的int类型数据
  40 + public static int getIntByByte(byte bt) //bt 转换为无符号的int
  41 + {
  42 + if (bt <0)
  43 + {
  44 + return (bt+256);
  45 + }
  46 + else
  47 + {
  48 + return bt;
  49 + }
  50 + }
  51 +
  52 +
  53 + public byte functionID; //功能号
  54 + public long iDevSn; //设备序列号 4字节
  55 + public byte[] data= new byte[56]; //56字节的数据 [含流水号]
  56 +
  57 + private static long _Global_xid = 0;
  58 + protected long _xid = 0; //2011-5-12 15:28:37
  59 + void GetNewXid() //2011-1-10 14:22:16 获取新的Xid
  60 + {
  61 + _Global_xid++;
  62 + _xid = _Global_xid; //新的值
  63 + }
  64 + public static long getXidOfCommand(byte[] cmd) //获取指令中的xid
  65 + {
  66 + long ret = -1;
  67 + if (cmd.length >= WGPacketSize)
  68 + {
  69 + ret = getLongByByte(cmd, 40, 4);
  70 + }
  71 + return ret;
  72 + }
  73 +
  74 + public WgUdpCommShort()
  75 + {
  76 + Reset();
  77 + }
  78 + public void Reset() //数据复位
  79 + {
  80 + for(int i=0; i<data.length; i++)
  81 + {
  82 + data[i] =0;
  83 + }
  84 + }
  85 + public byte[] toByte() //生成64字节指令包
  86 + {
  87 + byte[] buff =new byte[WGPacketSize];
  88 + for(int i=0; i<data.length; i++)
  89 + {
  90 + buff[i] =0;
  91 + }
  92 + buff[0] = Type;
  93 + buff[1] = functionID;
  94 + System.arraycopy(longToByte(iDevSn), 0, buff, 4, 4);
  95 + System.arraycopy(data, 0, buff, 8, data.length);
  96 +
  97 + GetNewXid();
  98 + System.arraycopy(longToByte(_xid), 0, buff, 40, 4);
  99 + return buff;
  100 + }
  101 +
  102 + public void run(IoSession ioSession , byte[] command){
  103 + byte[] bytCommand = command;
  104 + IoBuffer b;
  105 + Boolean bSent =false;
  106 + //ioSession = connFuture.getSession();
  107 + if (ioSession !=null)
  108 + {
  109 + if (ioSession.isConnected())
  110 + {
  111 + b = IoBuffer.allocate(bytCommand.length);
  112 + b.put(bytCommand);
  113 + b.flip();
  114 + ioSession.write(b);
  115 + bSent = true;
  116 + }
  117 + }
  118 + }
  119 +
  120 +}
... ...