api-设备唯一Id 修改为 设备号 + 场馆号 + 进场OR出场
This commit is contained in:
@@ -17,7 +17,8 @@ public class ClientChannelCache {
|
||||
/**
|
||||
* 缓存通道
|
||||
*/
|
||||
public static void putChannelType(String clientId, Channel channel) {
|
||||
public static void putChannelType(String deviceName, Integer venueId, DeviceType deviceType, Channel channel) {
|
||||
String clientId = deviceName + Constant.SPIT_WORD + venueId + Constant.SPIT_WORD + deviceType;
|
||||
ClientChannelCache.channel.put(clientId, channel);
|
||||
}
|
||||
|
||||
@@ -25,28 +26,24 @@ public class ClientChannelCache {
|
||||
* 获取当前通道
|
||||
*/
|
||||
public static Channel getCurrentChannel(String deviceName, Integer venueId, DeviceType deviceType){
|
||||
String clientId = deviceName + Constant.SPIT_WORD + venueId + Constant.SPIT_WORD + deviceType.getCode();
|
||||
return getCurrentChannel(clientId);
|
||||
String clientId = deviceName + Constant.SPIT_WORD + venueId + Constant.SPIT_WORD + deviceType;
|
||||
return channel.get(clientId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取通道
|
||||
*/
|
||||
public static Channel getCurrentChannel(String clientId) {
|
||||
return channel.get(clientId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断通道是否存在
|
||||
*/
|
||||
public static boolean contains(String clientId) {
|
||||
public static boolean contains(String deviceName, Integer venueId, DeviceType deviceType) {
|
||||
String clientId = deviceName + Constant.SPIT_WORD + venueId + Constant.SPIT_WORD + deviceType;
|
||||
return channel.containsKey(clientId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除通道
|
||||
*/
|
||||
public static void removeChannelType(String clientId) {
|
||||
public static void removeChannelType(String deviceName, Integer venueId, DeviceType deviceType) {
|
||||
String clientId = deviceName + Constant.SPIT_WORD + venueId + Constant.SPIT_WORD + deviceType;
|
||||
channel.remove(clientId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
package com.sv.netty.controller;
|
||||
|
||||
import com.sv.netty.config.ClientChannelCache;
|
||||
import com.sv.netty.config.ErrorCode;
|
||||
import com.sv.netty.netty.message.MessageDTO;
|
||||
import com.sv.netty.netty.message.MessageType;
|
||||
import com.ydd.framework.core.common.dto.ResponseDTO;
|
||||
import io.netty.channel.Channel;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class QRCodeControler {
|
||||
|
||||
/**
|
||||
* 废弃
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/scanQRCode")
|
||||
public ResponseDTO scanQRCode(@RequestParam String venueId,
|
||||
@RequestParam String cardType,@RequestParam String memberId) {
|
||||
// 1、获取到请求,说明扫码成功,预校验余额成功,通知客户端(Android)进行加载状态。
|
||||
|
||||
// 2、进行预校验,判断是否可以进行扫码通行 // TODO check余额方法
|
||||
boolean checkResult = true;
|
||||
|
||||
if(checkResult){
|
||||
// 如果是成功的 通知客户端(Android)进行开门
|
||||
return ResponseDTO.ok();
|
||||
}else {
|
||||
return new ResponseDTO(ErrorCode.MEMBER_NOT_EXIST.getCode(),
|
||||
ErrorCode.MEMBER_NOT_EXIST.getMsg(),System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检验客户端读取能力
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/checkAlive")
|
||||
public ResponseDTO checkAlive(@RequestParam String id) {
|
||||
System.out.println(ClientChannelCache.channel.size());
|
||||
Channel channel = ClientChannelCache.getCurrentChannel(id);
|
||||
MessageDTO messageDTO = new MessageDTO();
|
||||
messageDTO.setMessageType(MessageType.LOAD);
|
||||
messageDTO.setMessage("欢迎光临张三" );
|
||||
// sendMessage(channel,messageDTO);
|
||||
// channel.writeAndFlush("check client alive ! ");
|
||||
channel.writeAndFlush(messageDTO);
|
||||
return ResponseDTO.ok();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import com.sv.entity.Venue;
|
||||
import com.sv.entity.VenueType;
|
||||
import com.sv.netty.config.ClientChannelCache;
|
||||
import com.sv.netty.config.Constant;
|
||||
import com.sv.netty.netty.message.MessageDTO;
|
||||
import com.sv.netty.netty.message.MessageType;
|
||||
import com.sv.netty.netty.service.MessageService;
|
||||
import com.sv.service.api.QRCodeService;
|
||||
import com.ydd.framework.core.common.dto.ResponseDTO;
|
||||
@@ -38,7 +40,7 @@ public class QREnterController extends BaseApiController {
|
||||
Integer venueId = getVenueId(deviceId);
|
||||
String deviceName = getDeviceName(deviceId);
|
||||
DeviceType enterOrOut = getEnterOrOut(deviceId);
|
||||
Venue venue = qrCodeService.initEnter(venueId, deviceName);
|
||||
Venue venue = qrCodeService.initEnter(venueId, deviceName,enterOrOut);
|
||||
if (venue!=null){
|
||||
Channel channel = ClientChannelCache.getCurrentChannel(deviceName,venueId ,enterOrOut);
|
||||
if (DeviceType.OUT == enterOrOut){
|
||||
@@ -62,7 +64,7 @@ public class QREnterController extends BaseApiController {
|
||||
Integer venueId = getVenueId(deviceId);
|
||||
String deviceName = getDeviceName(deviceId);
|
||||
DeviceType enterOrOut = getEnterOrOut(deviceId);
|
||||
Venue venue = qrCodeService.initEnter(venueId, deviceName);
|
||||
Venue venue = qrCodeService.initEnter(venueId, deviceName,enterOrOut);
|
||||
Channel channel = ClientChannelCache.getCurrentChannel(deviceName,venueId ,enterOrOut);
|
||||
messageService.sendLoading(channel,memberId);
|
||||
return ResponseDTO.ok().addAttribute("venueInit", venue);
|
||||
@@ -114,4 +116,21 @@ public class QREnterController extends BaseApiController {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检验客户端读取能力
|
||||
* @return
|
||||
*/
|
||||
@RequestMapping("/checkAlive")
|
||||
public ResponseDTO checkAlive(@RequestParam String id) {
|
||||
System.out.println(ClientChannelCache.channel.size());
|
||||
Integer venueId = getVenueId(id);
|
||||
String deviceName = getDeviceName(id);
|
||||
DeviceType enterOrOut = getEnterOrOut(id);
|
||||
Channel channel = ClientChannelCache.getCurrentChannel(deviceName,venueId,enterOrOut);
|
||||
logger.info("验证设备"+ id + "通讯情况");
|
||||
MessageDTO messageDTO = new MessageDTO(MessageType.LOAD,"测试信息123");
|
||||
channel.writeAndFlush(messageDTO);
|
||||
return ResponseDTO.ok();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.sv.netty.netty;
|
||||
|
||||
import com.enums.DeviceType;
|
||||
import com.sv.netty.config.Constant;
|
||||
import com.sv.netty.config.SpringContextHolder;
|
||||
import com.sv.netty.netty.message.ChannelParam;
|
||||
@@ -53,6 +54,7 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
logger.info("客户端" + hb.getDeviceName() + "&" + hb.getVenueId() + "【" + clientIp + "】上报心跳...");
|
||||
ctx.channel().attr(Constant.CHANNEL_PARAM).get().setVenueId(hb.getVenueId());
|
||||
ctx.channel().attr(Constant.CHANNEL_PARAM).get().setDeviceName(hb.getDeviceName());
|
||||
ctx.channel().attr(Constant.CHANNEL_PARAM).get().setDeviceType(hb.getEnterOrOut());
|
||||
messageService.online(clientIp,ctx.channel(), hb);
|
||||
} catch (Exception e) {
|
||||
logger.error("[" + clientIp + "] host unknown error",e);
|
||||
@@ -83,7 +85,8 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
logger.error("Client ip [" + clientIP + "] has inactive");
|
||||
Integer venueId = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getVenueId();
|
||||
String deviceName = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getDeviceName();
|
||||
messageService.Offline(deviceName,venueId);
|
||||
DeviceType deviceType = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getDeviceType();
|
||||
messageService.Offline(deviceName,venueId,deviceType);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,7 +101,8 @@ public class ServerHandler extends SimpleChannelInboundHandler<String> {
|
||||
Channel channel = ctx.channel();
|
||||
Integer venueId = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getVenueId();
|
||||
String deviceName = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getDeviceName();
|
||||
messageService.Offline(deviceName,venueId);
|
||||
DeviceType deviceType = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getDeviceType();
|
||||
messageService.Offline(deviceName,venueId,deviceType);
|
||||
if(channel.isActive()) {
|
||||
// 错误产生,关闭连接
|
||||
ctx.close();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.sv.netty.netty.message;
|
||||
|
||||
import com.enums.DeviceType;
|
||||
|
||||
/**
|
||||
* 会话中存储的客户端对象
|
||||
*
|
||||
@@ -23,6 +25,12 @@ public class ChannelParam {
|
||||
*/
|
||||
private Integer venueId;
|
||||
|
||||
/**
|
||||
* 设备进场出场类型
|
||||
* @param clientIP
|
||||
*/
|
||||
private DeviceType deviceType;
|
||||
|
||||
public ChannelParam(String clientIP) {
|
||||
this.clientIp = clientIP;
|
||||
}
|
||||
@@ -50,4 +58,12 @@ public class ChannelParam {
|
||||
public void setVenueId(Integer venueId) {
|
||||
this.venueId = venueId;
|
||||
}
|
||||
|
||||
public DeviceType getDeviceType() {
|
||||
return deviceType;
|
||||
}
|
||||
|
||||
public void setDeviceType(DeviceType deviceType) {
|
||||
this.deviceType = deviceType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.sv.netty.netty.message;
|
||||
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
|
||||
import com.enums.DeviceType;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
@@ -17,10 +16,8 @@ public class HeartBeat implements Serializable {
|
||||
private Integer venueId; //场馆号
|
||||
|
||||
private String deviceName; //设备号
|
||||
/**
|
||||
* 进场 ENTER ; 出厂 OUT
|
||||
*/
|
||||
private String EnterOrOut; //出入标志
|
||||
|
||||
private DeviceType EnterOrOut; //出入标志
|
||||
|
||||
public Integer getVenueId() {
|
||||
return venueId;
|
||||
@@ -38,11 +35,11 @@ public class HeartBeat implements Serializable {
|
||||
this.deviceName = deviceName;
|
||||
}
|
||||
|
||||
public String getEnterOrOut() {
|
||||
public DeviceType getEnterOrOut() {
|
||||
return EnterOrOut;
|
||||
}
|
||||
|
||||
public void setEnterOrOut(String enterOrOut) {
|
||||
public void setEnterOrOut(DeviceType enterOrOut) {
|
||||
EnterOrOut = enterOrOut;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ public interface MessageService {
|
||||
* @param deviceName
|
||||
* @param venueId
|
||||
*/
|
||||
void Offline(String deviceName, Integer venueId);
|
||||
void Offline(String deviceName, Integer venueId,DeviceType deviceType);
|
||||
|
||||
/**
|
||||
* 统计目前的链接数
|
||||
|
||||
@@ -70,14 +70,14 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
@Override
|
||||
public void online(String clientId, Channel channel, HeartBeat heartBeat) {
|
||||
// 处理心跳信息
|
||||
if (!ClientChannelCache.contains(heartBeat.getDeviceName()+heartBeat.getVenueId())){
|
||||
if (!ClientChannelCache.contains(heartBeat.getDeviceName(),heartBeat.getVenueId(),heartBeat.getEnterOrOut())){
|
||||
// 此处存储客户端的channel 信息key 为 deviceId + venueId
|
||||
Venue thisVenue = venueService.findById(heartBeat.getVenueId());
|
||||
if (thisVenue == null ){
|
||||
logger.error("this client choose venue Error! venueId == " + heartBeat.getVenueId());
|
||||
} else {
|
||||
ClientChannelCache.putChannelType(heartBeat.getDeviceName()+heartBeat.getVenueId(),channel);
|
||||
deviceService.online(heartBeat.getDeviceName(),heartBeat.getVenueId(),thisVenue.getType(),clientId);
|
||||
ClientChannelCache.putChannelType(heartBeat.getDeviceName(),heartBeat.getVenueId(),heartBeat.getEnterOrOut(),channel);
|
||||
deviceService.online(heartBeat.getDeviceName(),heartBeat.getVenueId(),heartBeat.getEnterOrOut(),thisVenue.getType(),clientId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,10 +88,10 @@ public class AppMessageHandlerAdapter implements MessageService {
|
||||
* @param venueId
|
||||
*/
|
||||
@Override
|
||||
public void Offline(String deviceName, Integer venueId) {
|
||||
public void Offline(String deviceName, Integer venueId,DeviceType deviceType) {
|
||||
if (deviceName != null && venueId != null){
|
||||
ClientChannelCache.removeChannelType(deviceName+ venueId);
|
||||
deviceService.offline(deviceName,venueId);
|
||||
ClientChannelCache.removeChannelType(deviceName,venueId,deviceType);
|
||||
deviceService.offline(deviceName,venueId,deviceType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user