Commit 2d76ca93 authored by zengtianlai3's avatar zengtianlai3

新增终端状态,服务自诊状态

parent a65e3c83
......@@ -7,9 +7,13 @@ public class Consts {
public static final int EXECUTOR_THREAD_NUM = 30;
public static final int FORWARD_THREAD_NUM = 30;
public static final int OPERATE_THREAD_NUM = 5;
public static final Integer DEVICE_STATE_ONLINE = 1;// 设备在线
public static final Integer DEVICE_STATE_OFFLINE = 0;// 设备离线
public static final Integer DEVICE_STATE_FAILURE = -1;// 设备故障
public static final int DEVICE_STATE_ONLINE = 1;// 设备在线
public static final int DEVICE_STATE_OFFLINE = 0;// 设备离线
public static final int SERVICE_DX_THRESHOLD_NORMAL = 100;
public static final int SERVICE_DX_THRESHOLD_BUSY = 500;
public static final int SERVICE_DX_STATUS_FLUENT = 0;//流畅
public static final int SERVICE_DX_STATUS_NORMAL = 1;//正常
public static final int SERVICE_DX_STATUS_BUSY = 2;//繁忙
}
......@@ -5,15 +5,14 @@ import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.*;
import iot.sixiang.license.device.DeviceManager;
import iot.sixiang.license.log.BusinessType;
import iot.sixiang.license.log.MyLog;
import iot.sixiang.license.model.BaseResult;
import iot.sixiang.license.model.PageInfoModel;
import iot.sixiang.license.model.PageResult;
import iot.sixiang.license.model.vo.DeviceDetailVo;
import iot.sixiang.license.model.vo.DeviceVo;
import iot.sixiang.license.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -36,6 +35,8 @@ public class DeviceController {
@Autowired
private DeviceService deviceService;
@Autowired
private DeviceManager deviceManager;
/**
* 添加device
......@@ -61,7 +62,7 @@ public class DeviceController {
}
/**
* 可按条件分页查询所有的user
* 可按条件分页查询所有的设备
* @param pageNo
* @param pageSize
* @param appName
......@@ -91,5 +92,30 @@ public class DeviceController {
List<DeviceVo> pageResult = records.getResult();
return new PageResult(200,"查找成功",pageNo,pages,total,pageResult);
}
@ApiOperation(value = "获取设备详细信息接口", notes = "用于获取设备详细信息列表")
@GetMapping("detail/list")
@MyLog(title = "获取设备详细信息接口", optParam = "#{pageNo},#{pageSize},#{appName},#{userName}", businessType = BusinessType.SELECT)
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNo", value = "当前在第几页", required = true, dataType = "int"),
@ApiImplicitParam(name = "pageSize", value = "每页显示多少页", required = true, dataType = "int"),
@ApiImplicitParam(name = "appName", value = "应用名"),
@ApiImplicitParam(name = "userName", value = "用户名")
})
public PageResult getDeviceDetailList(@RequestParam(value = "pageNo", defaultValue = "0") int pageNo,
@RequestParam(value = "pageSize", defaultValue = "0") int pageSize,
@RequestParam(value = "appName", required = false) String appName,
@RequestParam(value = "userName", required = false) String userName) {
PageInfoModel<DeviceDetailVo> records = deviceManager.getDeviceDetailList(pageNo, pageSize, appName, userName);
int total = records.getTotal();
int pages = total / pageSize;//pages为总页数
int mod = total % pageSize;
if (mod != 0) {
pages = pages + 1;
}
List<DeviceDetailVo> pageResult = records.getResult();
return new PageResult(200, "查找成功", pageNo, pages, total, pageResult);
}
}
......@@ -3,10 +3,12 @@ package iot.sixiang.license.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import iot.sixiang.license.forward.ForwardManager;
import iot.sixiang.license.log.BusinessType;
import iot.sixiang.license.log.MyLog;
import iot.sixiang.license.model.ResResult;
import iot.sixiang.license.model.SamMonitor;
import iot.sixiang.license.model.vo.ServerStatusVo;
import iot.sixiang.license.operate.OperateManager;
import iot.sixiang.license.service.MonitorService;
import lombok.extern.slf4j.Slf4j;
......@@ -26,6 +28,8 @@ public class OperateController {
private OperateManager operateManager;
@Autowired
private MonitorService monitorService;
@Autowired
private ForwardManager forwardManager;
@ApiOperation(value = "服务接口", notes = "用于获取服务列表")
@GetMapping("monitor/server")
......@@ -51,4 +55,13 @@ public class OperateController {
HashMap<String, List<Integer>> monitorList = monitorService.getMonitorList(type);
return ResResult.success().record(monitorList);
}
@ApiOperation(value = "获取服务自诊断信息接口", notes = "用于获取服务诊断信息")
@GetMapping("diagnosis/server")
@MyLog(title = "获取服务诊断信息", businessType = BusinessType.SELECT)
@ApiImplicitParam()
public ResResult getDiagnosisInfo() {
ServerStatusVo serverStatus = forwardManager.getServerStatus();
return ResResult.success().record(serverStatus);
}
}
......@@ -3,9 +3,11 @@ package iot.sixiang.license.device;
import iot.sixiang.license.consts.Consts;
import iot.sixiang.license.model.PageInfoModel;
import iot.sixiang.license.model.SessionContext;
import iot.sixiang.license.model.vo.DeviceDetailVo;
import iot.sixiang.license.model.vo.DeviceVo;
import iot.sixiang.license.net.TcpServer;
import iot.sixiang.license.service.DeviceService;
import iot.sixiang.license.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
......@@ -87,6 +89,31 @@ public class DeviceManager {
}
return session;
}
public boolean getAuthStatusByChannelId(String channelId) {
Iterator<Map.Entry<String, SessionContext>> it = sessionContexts.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, SessionContext> entry = it.next();
SessionContext targetSession = entry.getValue();
if (targetSession.getChannelId().equals(channelId)) {
return targetSession.isAuthStatus();
}
}
return false;
}
public SessionContext getSessionBySn(String sn) {
Iterator<Map.Entry<String, SessionContext>> it = sessionContexts.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, SessionContext> entry = it.next();
SessionContext targetSession = entry.getValue();
if (targetSession.getSn().equals(sn)) {
return targetSession;
}
}
return null;
}
public synchronized boolean removeSessionByChannelId(String channelId) {
Iterator<Map.Entry<String, SessionContext>> it = sessionContexts.entrySet().iterator();
while (it.hasNext()) {
......@@ -109,12 +136,46 @@ public class DeviceManager {
SessionContext targetSession = entry.getValue();
if (targetSession.getChannelId().equals(channelId)) {
targetSession.setAuthStatus(false);
targetSession.setStatus(Consts.DEVICE_STATE_OFFLINE);
targetSession.setOnline(new Date());
targetSession.setOffline(CommonUtil.getSystemTime());
return true;
}
}
return false;
}
public PageInfoModel<DeviceDetailVo> getDeviceDetailList(int pageNo, int pageSize, String appName, String userName) {
PageInfoModel<DeviceVo> records = deviceService.getDeviceList(pageNo, pageSize, appName, userName);
List<DeviceVo> deviceVos = records.getResult();
PageInfoModel<DeviceDetailVo> detailVoPageInfoModel = new PageInfoModel<>();
List<DeviceDetailVo> detailVos = new ArrayList<>();
for (DeviceVo vo : deviceVos) {
DeviceDetailVo detailVo = new DeviceDetailVo();
detailVo.setDeviceId(vo.getDeviceId());
detailVo.setAppName(vo.getAppName());
detailVo.setUserName(vo.getUserName());
detailVo.setSn(vo.getSn());
detailVo.setOnline("");
detailVo.setOffline("");
String sn = vo.getSn();
SessionContext session = this.getSessionBySn(sn);
if (session != null) {
int status = session.getStatus();
String online = session.getOnline();
String offline = session.getOffline();
detailVo.setStatus(status);
detailVo.setOnline(online);
detailVo.setOffline(offline);
}
detailVos.add(detailVo);
}
detailVoPageInfoModel.setTotal(detailVos.size());
detailVoPageInfoModel.setResult(detailVos);
return detailVoPageInfoModel;
}
}
......@@ -14,6 +14,7 @@ import iot.sixiang.license.event.EventPublisher;
import iot.sixiang.license.event.ForwardClientRequestEvent;
import iot.sixiang.license.idreader.Safety;
import iot.sixiang.license.model.SessionContext;
import iot.sixiang.license.util.CommonUtil;
import iot.sixiang.license.util.HexUtil;
import iot.sixiang.license.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
......@@ -58,19 +59,19 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
int cmdInt = cmd & 0xFF;
log.debug("recev msg " + HexUtil.bytes2hex(protocol.getContent()));
boolean license = false;
boolean auth = false;
if (cmdInt == Consts.CMD_LICENSE) {
license = handlerLicense(channel, remoteIp, remotePort, protocol);
auth = handlerCheckAuth(channel, remoteIp, remotePort, protocol);
} else {
license = handlerCheckLicense(channel, remoteIp, remotePort, protocol);
if (license) {
auth = handlerCheckAuthStatus(channel, remoteIp, remotePort, protocol);
if (auth) {
handlerForward(channel, remoteIp, remotePort, protocol);
}
}
//TODO 以下为正式代码
if (license == false) {
if (auth == false) {
channel.close();
}
}
......@@ -154,7 +155,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
log.error("client has error,ip:" + clientIp + ",port:" + port);
}
private boolean handlerLicense(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) {
private boolean handlerCheckAuth(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) {
//TODO 正式代码要放开
......@@ -179,10 +180,12 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
session.setRemoteIp(remoteIp);
session.setRemotePort(remotePort);
session.setAppId(appId);
session.setSn(sn);
session.setChannelId(channelId);
session.setClientChannel(channel);
session.setAuthStatus(true);
session.setStatus(Consts.DEVICE_STATE_ONLINE);
session.setOnline(new Date());
session.setOnline(CommonUtil.getSystemTime());
DeviceManager deviceManager = SpringUtil.getBean(DeviceManager.class);
deviceManager.putSession(appId, session);
......@@ -196,16 +199,10 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
return license;
}
private boolean handlerCheckLicense(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) {
private boolean handlerCheckAuthStatus(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) {
String channelId = channel.id().asLongText();
DeviceManager deviceManager = SpringUtil.getBean(DeviceManager.class);
SessionContext session = deviceManager.getSessionByChannelId(channelId);
if (session != null) {
return true;
} else {
return false;
}
return deviceManager.getAuthStatusByChannelId(channelId);
}
private void handlerForward(SocketChannel channel, String remoteIp, int remotePort, DeviceProtocol protocol) {
......
package iot.sixiang.license.forward;
import iot.sixiang.license.consts.Consts;
import iot.sixiang.license.model.SessionContext;
import lombok.Getter;
import lombok.Setter;
import iot.sixiang.license.model.vo.ServerStatusVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
......@@ -67,9 +65,18 @@ public class ForwardManager {
return false;
}
public void getSession(){
public ServerStatusVo getServerStatus() {
ServerStatusVo vo = new ServerStatusVo();
int size = sessionContexts.size();
int statue = Consts.SERVICE_DX_STATUS_FLUENT;
if (size >= Consts.SERVICE_DX_THRESHOLD_NORMAL) {
statue = Consts.SERVICE_DX_STATUS_NORMAL;
} else if (size > Consts.SERVICE_DX_THRESHOLD_BUSY) {
statue = Consts.SERVICE_DX_STATUS_BUSY;
}
vo.setCount(size);
vo.setStatus(statue);
return vo;
}
......
......@@ -10,12 +10,13 @@ import java.util.Date;
public class SessionContext {
private String remoteIp;
private int remotePort;
private String appId;//设备序列号
private String appKey;//设备key/授权ID
private String token;//连接标识
private int status;//0 offline,1 online
private Date online;//上线时间
private Date offline;//下线时间
private String appId;
private String appKey;
private String sn;
private boolean authStatus;//授权验证状态
private int status;//当前状态,0 offline,1 online
private String online;//上线时间
private String offline;//下线时间
private String channelId;
private SocketChannel clientChannel;
......
package iot.sixiang.license.model.vo;
import lombok.Data;
import java.util.Date;
@Data
public class DeviceDetailVo extends DeviceVo {
private int status;//当前状态,0 offline,1 online
private String online;//上线时间
private String offline;//下线时间
}
package iot.sixiang.license.model.vo;
import lombok.Data;
@Data
public class ServerStatusVo {
private int count;//服务实时工作通道数量
private int status;//诊断状态,通道数量小于100流畅,100-500正常,500以上拥挤
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment