Browse Source

主动注册优化

platform-api
4670101279 2 years ago
parent
commit
66d73d186e
  1. 57
      ruoyi-admin/src/main/resources/application-test.yml
  2. 4
      ruoyi-admin/src/main/resources/application.yml
  3. 15
      ruoyi-code/src/main/java/com/ruoyi/code/camera/controller/CameraController.java
  4. 30
      ruoyi-code/src/main/java/com/ruoyi/code/camera/domain/ScConfig.java
  5. 17
      ruoyi-code/src/main/java/com/ruoyi/code/camera/mapper/ScConfigMapper.java
  6. 5
      ruoyi-code/src/main/java/com/ruoyi/code/camera/scheduled/RegisterServer.java
  7. 2
      ruoyi-code/src/main/java/com/ruoyi/code/camera/service/ICameraService.java
  8. 14
      ruoyi-code/src/main/java/com/ruoyi/code/camera/service/IScConfigService.java
  9. 35
      ruoyi-code/src/main/java/com/ruoyi/code/camera/service/impl/CameraServiceImpl.java
  10. 18
      ruoyi-code/src/main/java/com/ruoyi/code/camera/service/impl/ScConfigServiceImpl.java
  11. 61
      ruoyi-code/src/main/java/utils/LoginListenThreadController.java
  12. 40
      ruoyi-code/src/main/java/utils/RegisterUtil.java

57
ruoyi-admin/src/main/resources/application-test.yml

@ -0,0 +1,57 @@
# 数据源配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
# 主库数据源
master:
url: jdbc:mysql://121.40.203.197:3306/znsc_test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true&allowPublicKeyRetrieval=true
username: znsc_test
password: znSC_1q2w#E$R
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: false
url:
username:
password:
# 初始连接数
initialSize: 5
# 最小连接池数量
minIdle: 10
# 最大连接池数量
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
# 配置检测连接是否有效
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username: ruoyi
login-password: 123456
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true

4
ruoyi-admin/src/main/resources/application.yml

@ -47,6 +47,7 @@ spring:
basename: i18n/messages basename: i18n/messages
profiles: profiles:
active: druid active: druid
# active: test
# 文件上传 # 文件上传
servlet: servlet:
multipart: multipart:
@ -135,4 +136,5 @@ xss:
# port: 8020 # port: 8020
# username: admin # username: admin
# password: admin123 # password: admin123
serverip: 172.16.46.58 localip: 172.16.46.58
serverip: 121.40.203.197

15
ruoyi-code/src/main/java/com/ruoyi/code/camera/controller/CameraController.java

@ -17,6 +17,7 @@ import utils.CameraUtil;
import utils.RegisterUtil; import utils.RegisterUtil;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -111,6 +112,12 @@ public class CameraController extends BaseController
return cameraService.register(); return cameraService.register();
} }
@RequestMapping("outLine")
public R outLine(){
return cameraService.outLine();
}
@RequestMapping("reboot/{id}") @RequestMapping("reboot/{id}")
public R reboot(@PathVariable Long id){ public R reboot(@PathVariable Long id){
Camera c = cameraService.getById(id); Camera c = cameraService.getById(id);
@ -130,10 +137,12 @@ public class CameraController extends BaseController
@RequestMapping("showVideo/{id}") @RequestMapping("showVideo/{id}")
public R showVideo(@PathVariable Long id){ public R showVideo(HttpServletRequest request,@PathVariable Long id){
//解析ip地址,非服务器ip禁止打开视频 //解析ip地址,非服务器ip禁止打开视频
String ip = IPUtils.getLocalIpAddress(); String ip = IPUtils.getIpAddr(request);
if(serverIp.equals(ip)) { List<String> ipArr = Arrays.asList(serverIp,"127.0.0.1");
System.out.println("show video ip : " + ip);
if(ipArr.contains(ip)) {
Camera c = cameraService.getById(id); Camera c = cameraService.getById(id);
System.out.println("show loginID:" + c.getLoginId()); System.out.println("show loginID:" + c.getLoginId());
RegisterUtil window = new RegisterUtil(); RegisterUtil window = new RegisterUtil();

30
ruoyi-code/src/main/java/com/ruoyi/code/camera/domain/ScConfig.java

@ -0,0 +1,30 @@
package com.ruoyi.code.camera.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
/**
* 系统配置表
*
* @author ruoyi
* @date 2022-06-21
*/
@Data
@TableName("sc_config")
public class ScConfig implements Serializable
{
private static final long serialVersionUID = 1L;
@TableId(type = IdType.INPUT)
private Long id;
private String name;
private String value;
}

17
ruoyi-code/src/main/java/com/ruoyi/code/camera/mapper/ScConfigMapper.java

@ -0,0 +1,17 @@
package com.ruoyi.code.camera.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.code.camera.domain.ScConfig;
import org.apache.ibatis.annotations.Mapper;
/**
* 系统配置mapper
*
* @author ruoyi
* @date 2022-06-21
*/
@Mapper
public interface ScConfigMapper extends BaseMapper<ScConfig>
{
}

5
ruoyi-code/src/main/java/com/ruoyi/code/camera/scheduled/RegisterServer.java

@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -34,12 +35,12 @@ public class RegisterServer {
@PostConstruct @PostConstruct
public void config() { public void config() {
sercer_ip = env.getProperty("serverip"); sercer_ip = env.getProperty("localip");
} }
@Autowired @Autowired
private ICameraService cameraService; private ICameraService cameraService;
@PostConstruct @Scheduled(initialDelay = 5000, fixedRate = Long.MAX_VALUE)
public void synStart(){ public void synStart(){
start(); start();
} }

2
ruoyi-code/src/main/java/com/ruoyi/code/camera/service/ICameraService.java

@ -22,6 +22,8 @@ public interface ICameraService extends IService<Camera>
R register(); R register();
R outLine();
void online(String ip,int port,String username,String password,String devsn,String loginID); void online(String ip,int port,String username,String password,String devsn,String loginID);
void outline(String loginID); void outline(String loginID);

14
ruoyi-code/src/main/java/com/ruoyi/code/camera/service/IScConfigService.java

@ -0,0 +1,14 @@
package com.ruoyi.code.camera.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.code.camera.domain.ScConfig;
/**
* Service接口
*
* @author ruoyi
* @date 2022-06-21
*/
public interface IScConfigService extends IService<ScConfig>
{
}

35
ruoyi-code/src/main/java/com/ruoyi/code/camera/service/impl/CameraServiceImpl.java

@ -8,6 +8,8 @@ import com.ruoyi.common.utils.Query;
import com.ruoyi.code.camera.domain.Camera; import com.ruoyi.code.camera.domain.Camera;
import com.ruoyi.code.camera.mapper.CameraMapper; import com.ruoyi.code.camera.mapper.CameraMapper;
import com.ruoyi.code.camera.service.ICameraService; import com.ruoyi.code.camera.service.ICameraService;
import com.ruoyi.common.utils.StringUtils;
import com.sun.jna.NativeLong;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import utils.RegisterUtil; import utils.RegisterUtil;
@ -38,14 +40,18 @@ public class CameraServiceImpl extends ServiceImpl<CameraMapper, Camera> impleme
public R register(){ public R register(){
//清空所有摄像机状态 //清空所有摄像机状态
List<Camera> list = list(); List<Camera> list = list();
list.forEach(d -> {
d.setLoginId("");
d.setPort(0);
d.setStatus(0);
});
updateBatchById(list);
RegisterUtil window = new RegisterUtil(); RegisterUtil window = new RegisterUtil();
for (Camera c : list) {
if(StringUtils.isNotBlank(c.getLoginId())){
window.logout(new NativeLong(Long.parseLong(c.getLoginId())));
c.setLoginId("");
c.setPort(0);
c.setStatus(0);
}
}
updateBatchById(list);
//先停 //先停
window.onBtnStopRegServer(); window.onBtnStopRegServer();
//再开 //再开
@ -56,6 +62,23 @@ public class CameraServiceImpl extends ServiceImpl<CameraMapper, Camera> impleme
return R.error("服务启动失败"); return R.error("服务启动失败");
} }
@Override
public R outLine(){
//清空所有摄像机状态
List<Camera> list = list();
RegisterUtil window = new RegisterUtil();
for (Camera c : list) {
if(StringUtils.isNotBlank(c.getLoginId())){
window.logout(new NativeLong(Long.parseLong(c.getLoginId())));
c.setLoginId("");
c.setPort(0);
c.setStatus(0);
}
}
updateBatchById(list);
return R.ok();
}
@Override @Override
public void online(String ip,int port,String username,String password,String devsn,String loginID){ public void online(String ip,int port,String username,String password,String devsn,String loginID){
Date now = new Date(); Date now = new Date();

18
ruoyi-code/src/main/java/com/ruoyi/code/camera/service/impl/ScConfigServiceImpl.java

@ -0,0 +1,18 @@
package com.ruoyi.code.camera.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.camera.domain.ScConfig;
import com.ruoyi.code.camera.mapper.ScConfigMapper;
import com.ruoyi.code.camera.service.IScConfigService;
import org.springframework.stereotype.Service;
/**
* Service业务层处理
*
* @author ruoyi
* @date 2022-06-21
*/
@Service("scConfigService")
public class ScConfigServiceImpl extends ServiceImpl<ScConfigMapper, ScConfig> implements IScConfigService
{
}

61
ruoyi-code/src/main/java/utils/LoginListenThreadController.java

@ -0,0 +1,61 @@
package utils;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
public class LoginListenThreadController {
private RegisterUtil m_runDevAutoReg;
public LoginListenThreadController(RegisterUtil m_runDevAutoReg){
this.m_runDevAutoReg = m_runDevAutoReg;
}
ScheduledThreadPoolExecutor threadPoolExecutor = new ScheduledThreadPoolExecutor(1);
boolean bExit = true;
public void startLoginListenThread() {
System.err.println("startLoginListenThread");
if (threadPoolExecutor == null) {
threadPoolExecutor = new ScheduledThreadPoolExecutor(1);
}
bExit = true;
threadPoolExecutor.schedule(() -> {
while(bExit) {
doSomeThing();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, 0, TimeUnit.SECONDS);
}
public void stopLoginListenThread() {
System.err.println("stopLoginListenThread");
bExit = false;
threadPoolExecutor.shutdownNow();
threadPoolExecutor.shutdown();
threadPoolExecutor = null;
}
public void doSomeThing(){
if(m_runDevAutoReg.m_listLoginInfos.size() > 0)
{
RegisterUtil.STU_LOGIN_INFO loginInfo = m_runDevAutoReg.m_listLoginInfos.get(0);
System.out.println(loginInfo.strIP);
System.out.println(loginInfo.nPort);
System.out.println(loginInfo.strDevSN);
// 登陆
m_runDevAutoReg.login(loginInfo.strIP, loginInfo.nPort, loginInfo.strDevSN);
m_runDevAutoReg.m_listLoginInfos.removeElementAt(0);
}
}
}

40
ruoyi-code/src/main/java/utils/RegisterUtil.java

@ -1,7 +1,10 @@
package utils; package utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.code.camera.domain.Camera; import com.ruoyi.code.camera.domain.Camera;
import com.ruoyi.code.camera.domain.ScConfig;
import com.ruoyi.code.camera.service.ICameraService; import com.ruoyi.code.camera.service.ICameraService;
import com.ruoyi.code.camera.service.IScConfigService;
import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.BeanUtil; import com.ruoyi.common.utils.BeanUtil;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
@ -118,6 +121,7 @@ public class RegisterUtil implements ActionListener {
// 将类对象存在map中,方便在回调中使用用户自定义变量作为key来获取类对象 // 将类对象存在map中,方便在回调中使用用户自定义变量作为key来获取类对象
private static Map<Integer, RegisterUtil> s_mapFlagToClass = new HashMap<Integer, RegisterUtil>(); private static Map<Integer, RegisterUtil> s_mapFlagToClass = new HashMap<Integer, RegisterUtil>();
public LoginListenThreadController loginListenThreadController;
///////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////
/** /**
* Create the application. * Create the application.
@ -126,6 +130,7 @@ public class RegisterUtil implements ActionListener {
{ {
initialize(); initialize();
initilizeRyNetLib(); initilizeRyNetLib();
loginListenThreadController = new LoginListenThreadController(this);
} }
protected void finalize() protected void finalize()
@ -761,16 +766,25 @@ public class RegisterUtil implements ActionListener {
// return; // return;
// } // }
String strServerIP = this.m_jtfServerIP; String strServerIP = this.m_jtfServerIP;
int nServerPort = Integer.parseInt(this.m_jtfServerPort); int nServerPort = Integer.parseInt(this.m_jtfServerPort);
this.m_lRegServerHandle = NetLib.instance.Net_RegStartServer( this.m_lRegServerHandle = NetLib.instance.Net_RegStartServer(
strServerIP, nServerPort, _cbRegServerCallBack, new NativeLong(1)); strServerIP, nServerPort, _cbRegServerCallBack, new NativeLong(1));
System.out.println("开启注册服务句柄 " + this.m_lRegServerHandle.longValue());
if(this.m_lRegServerHandle.longValue() != 0) if(this.m_lRegServerHandle.longValue() != 0)
{ {
IScConfigService configService = BeanUtil.getBean(IScConfigService.class);
ScConfig config = configService.getOne(new QueryWrapper<ScConfig>().eq("name","RegServerHandle"));
if("0".equals(config.getValue())){
config.setValue(this.m_lRegServerHandle.toString());
configService.updateById(config);
}
// 启动登陆监听 // 启动登陆监听
m_loginListenThread = new LoginListenThread(this); // m_loginListenThread = new LoginListenThread(this);
m_loginListenThread.startLoginListenThread(); // m_loginListenThread.startLoginListenThread();
loginListenThreadController.startLoginListenThread();
System.out.println("开启注册服务成功."); System.out.println("开启注册服务成功.");
refreshEnables(); refreshEnables();
@ -801,19 +815,25 @@ public class RegisterUtil implements ActionListener {
// 停止主动注册服务 // 停止主动注册服务
public void onBtnStopRegServer() public void onBtnStopRegServer()
{ {
if(0 == this.m_lRegServerHandle.longValue()) boolean bRet = false;
return;
logoutAll(); IScConfigService configService = BeanUtil.getBean(IScConfigService.class);
ScConfig config = configService.getOne(new QueryWrapper<ScConfig>().eq("name","RegServerHandle"));
if(!"0".equals(config.getValue())){
System.out.println("停止注册服务句柄 " + config.getValue());
bRet = NetLib.instance.Net_RegStopServer(new NativeLong(Long.parseLong(config.getValue())));
boolean bRet = NetLib.instance.Net_RegStopServer(this.m_lRegServerHandle); config.setValue("0");
configService.updateById(config);
}
if(bRet) if(bRet)
{ {
// 停止登陆监听 // 停止登陆监听
m_loginListenThread.stopLoginListenThread(); loginListenThreadController.stopLoginListenThread();
m_loginListenThread = null; // m_loginListenThread.stopLoginListenThread();
// m_loginListenThread = null;
this.m_lRegServerHandle.setValue(0); //
// this.m_lRegServerHandle.setValue(0);
System.out.println("停止注册服务成功."); System.out.println("停止注册服务成功.");
refreshEnables(); refreshEnables();
} }

Loading…
Cancel
Save