提交 802a7f91 authored 作者: yangli's avatar yangli

.

上级 b4be7ed5
...@@ -358,7 +358,7 @@ public class MqttServiceImpl implements MqttService, MqttHandler, MqttClientCall ...@@ -358,7 +358,7 @@ public class MqttServiceImpl implements MqttService, MqttHandler, MqttClientCall
try { try {
if ("remote_control".equals(mqttRpcDataMessage.getMethod())) {// 远程控制请求 if ("remote_control".equals(mqttRpcDataMessage.getMethod())) {// 远程控制请求
SysConfigUtil.saveProperty("control.request", mqttRpcDataMessage.getParams());// 存储参数 SysConfigUtil.saveProperty("control.request", "1");
return; return;
} }
......
package com.priusis.controller; package com.priusis.controller;
import cn.hutool.core.date.DateField; import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.priusis.cache.InstalledProgramCache; import com.priusis.cache.InstalledProgramCache;
import com.priusis.lib.R; import com.priusis.lib.R;
import com.priusis.util.SysConfigUtil; import com.priusis.util.SysConfigUtil;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.ini4j.ConfigParser; import org.ini4j.ConfigParser;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import oshi.hardware.NetworkIF;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
...@@ -26,6 +35,7 @@ import java.util.TimerTask; ...@@ -26,6 +35,7 @@ import java.util.TimerTask;
* @author yangli * @author yangli
* @since 2021/09/16 * @since 2021/09/16
*/ */
@Slf4j
@Validated @Validated
@RestController @RestController
@RequestMapping(value = "remote_control") @RequestMapping(value = "remote_control")
...@@ -35,6 +45,12 @@ public class RemoteControlController { ...@@ -35,6 +45,12 @@ public class RemoteControlController {
@Value(value = "${vnc-repeater}") @Value(value = "${vnc-repeater}")
private String vncRepeater; private String vncRepeater;
@Value(value = "${apq.iot-gateway}")
private String gateway;
@Value(value = "${apq.url.device_info}")
private String deviceInfoUrl;
@Resource @Resource
private InstalledProgramCache installedProgramCache; private InstalledProgramCache installedProgramCache;
...@@ -42,20 +58,22 @@ public class RemoteControlController { ...@@ -42,20 +58,22 @@ public class RemoteControlController {
* 前端定时任务查询远程控制请求,不为空则表示有请求,展示确认弹窗 * 前端定时任务查询远程控制请求,不为空则表示有请求,展示确认弹窗
*/ */
@GetMapping(value = "get_control_request") @GetMapping(value = "get_control_request")
public R<JSONObject> getControlRequest() throws IOException { public R<Boolean> getControlRequest() throws IOException {
return R.success(JSONUtil.parseObj(SysConfigUtil.getProperty("control.request"))); return R.success(Convert.toBool(SysConfigUtil.getProperty("control.request"), false));
} }
/** /**
* 接受远程控制请求 * 接受远程控制请求
*/ */
@GetMapping(value = "accept_request") @GetMapping(value = "accept_request")
public R<Void> acceptRequest() throws IOException, ConfigParser.DuplicateSectionException, ConfigParser.NoSectionException { public R<Void> acceptRequest(@RequestParam @NotNull Long minutes)
String requestParams = SysConfigUtil.saveProperty("control.request", null); throws IOException, ConfigParser.DuplicateSectionException, ConfigParser.NoSectionException {
if (StrUtil.isBlank(requestParams)) return R.error("无远程请求"); Boolean hasRequest = Convert.toBool(SysConfigUtil.saveProperty("control.request", "0"), false);
JSONObject obj = JSONUtil.parseObj(requestParams); if (!hasRequest) return R.error("无远程请求");
String id = obj.getStr("id"); // 查询设备的远程ID
// 设置ID String remoteId = getRemoteId();
if (StrUtil.isBlank(remoteId)) return R.error("未查询到设备的远程ID");
// 设置远程ID
String path = installedProgramCache.getPrograms().stream() String path = installedProgramCache.getPrograms().stream()
.filter(m -> "UltraVNC".equalsIgnoreCase(m.get("name"))) .filter(m -> "UltraVNC".equalsIgnoreCase(m.get("name")))
.findFirst() .findFirst()
...@@ -67,15 +85,13 @@ public class RemoteControlController { ...@@ -67,15 +85,13 @@ public class RemoteControlController {
configParser.read(file); configParser.read(file);
if (!configParser.hasSection("admin")) configParser.addSection("admin"); if (!configParser.hasSection("admin")) configParser.addSection("admin");
configParser.set("admin", "service_commandline", configParser.set("admin", "service_commandline",
StrUtil.format("-autoreconnect ID:{} -connect {} -run", id, vncRepeater)); StrUtil.format("-autoreconnect ID:{} -connect {} -run", remoteId, vncRepeater));
configParser.write(file); configParser.write(file);
// 重启VNC Server服务 // 重启VNC Server服务
Runtime.getRuntime().exec("cmd /c net stop uvnc_service"); Runtime.getRuntime().exec("cmd /c net stop uvnc_service");
Runtime.getRuntime().exec("cmd /c net start uvnc_service"); Runtime.getRuntime().exec("cmd /c net start uvnc_service");
// 请求控制的时间,单位分钟
int minute = obj.getInt("time");
// 定时任务,指定分钟后执行一次 // 定时任务,指定分钟后执行一次
Timer timer = new Timer(); Timer timer = new Timer();
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
...@@ -86,16 +102,37 @@ public class RemoteControlController { ...@@ -86,16 +102,37 @@ public class RemoteControlController {
Runtime.getRuntime().exec("cmd /c net stop uvnc_service"); Runtime.getRuntime().exec("cmd /c net stop uvnc_service");
timer.cancel(); timer.cancel();
} }
}, DateTime.now().offset(DateField.MINUTE, minute)); }, minutes * 60 * 1000);
return R.success(); return R.success();
} }
/** /**
* 拒绝远程控制请求 * 拒绝远程控制请求
*/ */
@GetMapping(value = "refuse_request") @GetMapping(value = "refuse_request")
public R<Void> refuseRequest() throws IOException { public R<Void> refuseRequest() throws IOException {
SysConfigUtil.saveProperty("control.request", null); SysConfigUtil.saveProperty("control.request", "0");
return R.success(); return R.success();
} }
private String getMacAddress() throws UnknownHostException, SocketException {
NetworkIF networkIF = new NetworkIF();
networkIF.setNetworkInterface(NetworkInterface.getByInetAddress(InetAddress.getLocalHost()));
return networkIF.getMacaddr();
}
private String remoteId;
private String getRemoteId() throws SocketException, UnknownHostException {
if (StrUtil.isNotBlank(remoteId)) return remoteId;
String mac = getMacAddress();
JSONObject rsp = JSONUtil.parseObj(HttpUtil.get(gateway + StrUtil.format(deviceInfoUrl, mac)));
if (ObjectUtil.notEqual(rsp.getInt("code"), 0)) {
log.warn("查询设备信息失败, msg: {}", rsp.getStr("msg"));
return null;
}
return remoteId = rsp.getJSONObject("data").getStr("remoteId");
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论