提交 0b37f68d authored 作者: wangqiang's avatar wangqiang

Merge remote-tracking branch 'origin/master'

# Conflicts: # apq-pc-client/src/main/java/com/priusis/client/service/core/MqttServiceImpl.java
......@@ -27,17 +27,22 @@ public class InstalledProgramCache {
@Scheduled(fixedDelay = 3000)
protected void loadProgramListJob() throws IOException {
public void loadProgramListJob() throws IOException {
List<Map<String, String>> programs = new ArrayList<>();
Process process = Runtime.getRuntime()
.exec("cmd /c reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
String key;
while ((key = reader.readLine()) != null) {
Map<String, String> info = queryProgramInfo(key);
if (info.containsKey("name")) programs.add(info);
String[] regPaths = {
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\",
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
};
for (String regPath : regPaths) {
Process process = Runtime.getRuntime().exec("cmd /c reg query " + regPath);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"))) {
String key;
while ((key = reader.readLine()) != null) {
Map<String, String> info = queryProgramInfo(key);
if (info.containsKey("name")) programs.add(info);
}
process.destroy();
}
process.destroy();
}
programList = programs;
}
......
......@@ -356,7 +356,8 @@ public class MqttServiceImpl implements MqttService, MqttHandler, MqttClientCall
private void onRpcCommand(String message) {
JsonNode payload = fromString(message);
MqttRpcDataMessage mqttRpcDataMessage = MqttRpcDataMessage.builder()
.method(payload.get("method").asText()).params(payload.get("params").asText()).build();
.method(payload.get("method").asText())
.params(Optional.ofNullable(payload.get("params")).map(JsonNode::asText).orElse(null)).build();
// 存储rpc下发的数据
......
......@@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.Map;
......@@ -85,4 +86,15 @@ public class ConfigController {
}
return R.success();
}
@PostConstruct
public void init() {
try {
// 初始化自动保存一次数据上报设置
saveDataReporting(getDataReporting().getData());
} catch (Exception e) {
log.error("初始化异常", e);
}
}
}
package com.priusis.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.StreamProgress;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
......@@ -25,13 +26,13 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
/**
......@@ -52,9 +53,19 @@ public class ProgramController {
@Value(value = "${apq.url.upgrade}")
private String upgradeUrl;
@Value(value = "${apq.url.install-status-upissue}")
private String installStatusUpIssueUrl;
@Value(value = "${apq.url.upgrade-status-upissue}")
private String upgradeStatusUpIssueUrl;
@Value(value = "${apq.url.add-program}")
private String addProgramUrl;
@Resource
private InstalledProgramCache installedProgramCache;
......@@ -76,17 +87,21 @@ public class ProgramController {
if (CollUtil.isEmpty(rows)) return R.success(page.setRecords(Collections.emptyList()));
List<Map<String, String>> programs = installedProgramCache.getPrograms();
List<Map<String, String>> list = rows.stream().map(JSONObject.class::cast)
.map(o -> MapUtil.<String, String>builder()
.put("id", o.getStr("id"))
.put("name", StrUtil.subBefore(o.getStr("fileName"), '.', true))
.put("size", o.getStr("fileSize"))
.put("version", o.getStr("version"))
.put("localVersion", null)
.put("url", o.getStr("url"))
.build())
.map(o -> {
Map<String, String> map = new HashMap<>();
map.put("id", o.getStr(type.equals("install") ? "installId" : "upgradeId"));
map.put("name", StrUtil.subBefore(o.getStr("fileName"), '.', true));
map.put("size", o.getStr("fileSize"));
map.put("version", o.getStr("version"));
map.put("localVersion", null);
map.put("url", o.getStr("url"));
Long id = Convert.toLong(map.get("id"));
map.put("status", SET.contains(id) ? "1" : MAP.containsKey(id) ? "2" : "0");
return map;
})
.peek(m -> programs.stream()
.collect(Collectors.toMap(p -> StrUtil.similar(p.get("name").toLowerCase(), m.get("name").toLowerCase()),
p -> StrUtil.nullToEmpty(p.get("version")), (l, r) -> r))
p -> p.getOrDefault("version", ""), (l, r) -> r))
.entrySet()
.stream()
.max(Map.Entry.comparingByKey())
......@@ -97,19 +112,27 @@ public class ProgramController {
return R.success(page.setRecords(list));
}
private static final Set<String> SET = new ConcurrentHashSet<>();
private static final Map<String, Thread> MAP = new ConcurrentHashMap<>();
@GetMapping(value = "download_and_install")
public void downloadAndInstall(@RequestParam @NotBlank String url, HttpServletResponse response) throws IOException {
@GetMapping(value = "{type}/download_and_install")
public void downloadAndInstall(@PathVariable @Pattern(regexp = "install|upgrade") String type,
@RequestParam @NotNull Long id,
@RequestParam @NotBlank String url,
HttpServletResponse response) throws IOException {
File dir = new File(SysConfigUtil.getProperty("download.path",
System.getProperty("user.home") + File.separator + "Downloads"));
if (!dir.exists()) dir.mkdirs();
HttpResponse rsp = HttpUtil.createGet(url).execute(true);
double length = rsp.contentLength();
DecimalFormat decimalFormat = new DecimalFormat("0.#####");
String key = type + "-" + id;
File file = rsp.writeBodyForFile(dir, new StreamProgress() {
@SneakyThrows
@Override
public void start() {
SET.add(key);
response.getWriter().write("0\n");
}
......@@ -123,24 +146,43 @@ public class ProgramController {
@Override
public void finish() {
response.getWriter().close();
SET.remove(key);
}
});
try {
Runtime.getRuntime().exec("cmd /c " + file.getPath());
} catch (Exception e) {
log.error("执行安装文件 {} 失败", file.getPath(), e);
}
MAP.put(key, new Thread(() -> {
int exitVal = -1;
try {
exitVal = Runtime.getRuntime().exec("cmd /c \"" + file.getPath() +"\"").waitFor();
} catch (Exception e) {
log.error("执行安装文件 {} 失败", file.getPath(), e);
}
MAP.remove(key);
if (exitVal == 0) {
String reqUrl = gateway + StrUtil.format(type.equals("install") ? installStatusUpIssueUrl : upgradeStatusUpIssueUrl,
MacAddrUtil.get(), id);
HttpUtil.post(reqUrl, (String) null);
}
}));
MAP.get(key).start();
}
@GetMapping(value = "{type}/is_installing")
public R<Boolean> isInstalling(@PathVariable @Pattern(regexp = "install|upgrade") String type,
@RequestParam @NotNull Long id) {
return R.success(MAP.containsKey(type + "-" + id));
}
@GetMapping(value = "get_version")
public R<String> getVersion(@RequestParam @NotBlank String name) throws IOException {
installedProgramCache.loadProgramListJob();
return installedProgramCache.getPrograms().stream()
.collect(Collectors.toMap(p -> StrUtil.similar(p.get("name").toLowerCase(), name.toLowerCase()),
p -> p.get("version"), (l, r) -> r))
p -> p.getOrDefault("version", ""), (l, r) -> r))
.entrySet()
.stream()
.max(Map.Entry.comparingByKey())
.filter(e -> e.getKey() >= MIN_MATCH)
.filter(e -> StrUtil.isNotBlank(e.getValue()))
.map(Map.Entry::getValue)
.map(R::success)
.orElseGet(R::success);
......@@ -155,9 +197,8 @@ public class ProgramController {
return;
}
List<String> programNames = installedProgramCache.getPrograms()
.stream().map(p -> p.get("name")).collect(Collectors.toList());
.stream().map(p -> p.get("name")).sorted().collect(Collectors.toList());
HttpUtil.post(gateway + addProgramUrl,
JSONUtil.createObj().set("macAddress", mac).set("programList", programNames).toString());
}
}
......@@ -44,9 +44,12 @@ apq:
device_info: /facility/facility/getByMacAddress?macAddress={}
install: /ops/install/getClientList?macAddress={}&pageNum={}&pageSize={}
upgrade: /ops/upgrade/getClientList?macAddress={}&pageNum={}&pageSize={}
install-status-upissue: /ops/install/upIssueStatus/{}/{}
upgrade-status-upissue: /ops/upgrade/upIssueStatus/{}/{}
add-program: /ops/program/addFacProgram
send-control-req: /facility/facility/remoteRequest
# VNC中继服务器
vnc-repeater: 120.24.236.245:5500
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论