cuikaidong
2025-06-12 066063ed92fdd40da4dfe21770557f3adba3e1af
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package org.jeecg.modules.iot.controller;
 
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.iot.entity.*;
import org.jeecg.modules.iot.service.IEquipmentService;
import org.jeecg.modules.iot.util.FtpUtil;
import org.jeecg.modules.iot.mqtt.config.MqttCustomerClient;
import org.jeecg.modules.iot.service.IInfluxdbDeployService;
import org.jeecg.modules.iot.service.IMqttDeployService;
import org.jeecg.modules.iot.service.IServerDeployService;
import org.jeecg.modules.iot.util.FileUtil;
import org.jeecg.modules.iot.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
 
 
/**
 * @Description: 项目分类
 * @Author: cuikaidong
 * @Date: 2024-12-22
 * @Version: V1.0
 */
@Api(tags = "服务器配置")
@RestController
@RequestMapping("/serve/deploy")
@Slf4j
public class ServeDeployController extends JeecgController<ServerDeploy, IServerDeployService> {
    @Autowired
    private IServerDeployService serverDeployService;
    @Autowired
    private IInfluxdbDeployService influxdbDeployService;
    @Autowired
    private IMqttDeployService mqttDeployService;
    @Autowired
    private MqttCustomerClient mqttCustomerClient;
    @Resource
    private RestTemplate restTemplate;
    @Autowired
    private IEquipmentService equipmentService;
 
    /**
     * 通过projectCode查询
     *
     * @param projectCode
     * @return
     */
    @ApiOperation(value = "项目分类-通过projectCode查询", notes = "项目分类-通过projectCode查询")
    @GetMapping(value = "/queryByProjectCode")
    public Result<?> queryByProjectCode(@RequestParam(name = "projectCode", required = true) String projectCode) {
        ServerDeploy serverDeploy = serverDeployService.findByServerCode(projectCode);
        return Result.ok(serverDeploy);
    }
 
    /**
     * 分页列表查询
     *
     * @param serverDeploy
     * @param pageNo
     * @param pageSize
     * @param projectClassifyId
     * @return
     */
    @ApiOperation(value = "服务器配置-分页列表查询", notes = "服务器配置-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ServerDeploy serverDeploy,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   @RequestParam(name = "projectClassifyId", required = false) String projectClassifyId,
                                   HttpServletRequest req) {
        QueryWrapper<ServerDeploy> queryWrapper = QueryGenerator.initQueryWrapper(serverDeploy, req.getParameterMap());
        Page<ServerDeploy> page = new Page<>(pageNo, pageSize);
        IPage<ServerDeploy> pageList = serverDeployService.page(page, queryWrapper);
        List<ServerDeploy> records = pageList.getRecords();
        if (!records.isEmpty()) {
            Set<String> collect = records.stream().map(ServerDeploy::getId).collect(Collectors.toSet());
            // 查询mqtt数据
            List<MqttDeploy> mqttDeployList = mqttDeployService.findByServerIdsMqttDeploy(collect);
            // 查询Influxdb数据
            List<InfluxdbDeploy> influxdbList = influxdbDeployService.findByServerIdsInfluxdbDeploy(collect);
            records.forEach(r -> {
                // 终端地址
                r.setAddress("http://"+r.getServerAddress()+":"+ r.getServerPort());
                mqttDeployList.forEach(m -> {
                    if (m.getServerDeployId().equals(r.getId())) {
                        r.setMqttDeploy(m);
                    }
                });
                influxdbList.forEach(i -> {
                    if (i.getServerDeployId().equals(r.getId())) {
                        r.setInfluxdbDeploy(i);
                    }
                });
            });
        }
        return Result.ok(pageList);
    }
 
    private JSONObject exchange(String url) {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        headers.setBasicAuth("21d84e5c9863abed", "jmMwryEpKiH7FFScP9BEnsQUbYvFuZalzToEYn9ArNaFD", StandardCharsets.UTF_8);
        HttpEntity entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        log.info("response=>{}", response);
        if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
            String body = response.getBody();
            return JSONObject.parseObject(body);
        }
        return null;
    }
 
    /**
     * 添加
     *
     * @param serverDeploy
     * @return
     */
    @ApiOperation(value = "服务器配置-添加", notes = "服务器配置-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody ServerDeploy serverDeploy) {
        serverDeploy.setServerCode(this.randomDigit());
        serverDeploy.setGuardState(0);
        serverDeploy.setCollectState(0);
        serverDeploy.setDeployIssueTime(new Date());
        serverDeployService.save(serverDeploy);
        // 订阅Mqtt主题
        String[] topic = new String[1];
        topic[0] = "IOT\\" + serverDeploy.getServerCode() + "\\Mutually2";
        mqttCustomerClient.subscribe(topic);
        // 创建ftp文件夹目录
        FtpUtil.createFolder("/log", serverDeploy.getServerCode());
        FtpUtil.createFolder("/deploy", serverDeploy.getServerCode());
        FtpUtil.createFolder("/deploy/" + serverDeploy.getServerCode(), "software");
        FtpUtil.createFolder("/deploy/" + serverDeploy.getServerCode(), "deploy");
        // 生成本地文件夹
        boolean b = FileUtil.queryCatalogue("D:/iot/" + serverDeploy.getServerCode());
        if (b) {
            FileUtil.createDir("D:/iot/" + serverDeploy.getServerCode() + "/deploy");
            FileUtil.createDir("D:/iot/" + serverDeploy.getServerCode() + "/software");
        }
        FileUtil.createDir("D:/iot/" + serverDeploy.getServerCode() +"/script");
        return Result.ok("添加成功!");
    }
 
    /**
     * 添加Mqtt
     *
     * @param mqttDeploy
     * @return
     */
    @ApiOperation(value = "mqtt-添加", notes = "mqtt-添加")
    @PostMapping(value = "/add/mqtt")
    public Result<?> addMqtt(@RequestBody MqttDeploy mqttDeploy) {
        mqttDeployService.save(mqttDeploy);
        return Result.ok("添加成功!");
    }
 
    /**
     * 编辑
     *
     * @param mqttDeploy
     * @return
     */
    @ApiOperation(value = "mqtt-编辑", notes = "mqtt-编辑")
    @PutMapping(value = "/edit/mqtt")
    public Result<?> editMqtt(@RequestBody MqttDeploy mqttDeploy) {
        mqttDeployService.updateById(mqttDeploy);
        return Result.ok("编辑成功!");
    }
 
    /**
     * 编辑
     *
     * @param influxdbDeploy
     * @return
     */
    @ApiOperation(value = "influxdb-编辑", notes = "influxdb-编辑")
    @PutMapping(value = "/edit/influxdb")
    public Result<?> editInfluxdb(@RequestBody InfluxdbDeploy influxdbDeploy) {
        influxdbDeployService.updateById(influxdbDeploy);
        return Result.ok("编辑成功!");
    }
 
    /**
     * 添加influxdb
     *
     * @param influxdbDeploy
     * @return
     */
    @ApiOperation(value = "influxdb-添加", notes = "influxdb-添加")
    @PostMapping(value = "/add/influxdb")
    public Result<?> addMqtt(@RequestBody InfluxdbDeploy influxdbDeploy) {
        influxdbDeployService.save(influxdbDeploy);
        return Result.ok("添加成功!");
    }
 
    /**
     * 采集软件启停
     *
     * @param serverDeploy
     * @return
     */
    @ApiOperation(value = "服务器配置-采集软件启停", notes = "服务器配置-采集软件启停")
    @PutMapping(value = "/collect/put")
    public Result<?> collectPut(@RequestBody ServerDeploy serverDeploy) {
        Integer collectState = serverDeploy.getCollectState();
        MqttParameter mqttParameter = new MqttParameter();
        mqttParameter.setId(serverDeploy.getServerCode());
        // 启动采集软件
        mqttParameter.setType("start");
        if (collectState == 0) {
            mqttParameter.setParameter1("1");
        } else {
            mqttParameter.setParameter1("0");
        }
        mqttCustomerClient.pushlish(2, false, serverDeploy.getServerCode(), mqttParameter);
        return Result.ok();
    }
 
    /**
     * 获取日志
     *
     * @param serverCode
     * @return
     */
    @ApiOperation(value = "服务器配置-获取日志", notes = "服务器配置-获取日志")
    @GetMapping(value = "/obtain/log")
    public Result<?> obtainLog(@RequestParam(name = "serverCode") String serverCode,
                               @RequestParam(name = "dayStart") String dayStart,
                               @RequestParam(name = "dayEnd") String dayEnd) {
        MqttParameter mqttParameter = new MqttParameter();
        mqttParameter.setId(serverCode);
        // 启动采集软件
        mqttParameter.setType("log");
        mqttParameter.setParameter1(dayStart);
        mqttParameter.setParameter2(dayEnd);
        mqttParameter.setParameter3("log/" + serverCode + "/");
        mqttCustomerClient.pushlish(2, false, serverCode, mqttParameter);
        return Result.ok("上传成功!");
    }
 
    /**
     * 编辑
     *
     * @param serverDeploy
     * @return
     */
    @ApiOperation(value = "服务器配置-编辑", notes = "服务器配置-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody ServerDeploy serverDeploy) {
        serverDeployService.updateById(serverDeploy);
        return Result.ok("编辑成功!");
    }
 
    /**
     * 更新采集软件
     *
     * @param serverDeploy
     * @return
     */
    @ApiOperation(value = "服务器配置-更新采集软件", notes = "服务器配置-更新采集软件")
    @PutMapping(value = "/edit/collect")
    public Result<?> editCollectVersion(@RequestBody ServerDeploy serverDeploy) {
        serverDeployService.updateById(serverDeploy);
        String path = "D:\\IOT\\" + serverDeploy.getNewCollectAddress();
        if (!path.endsWith(".zip")) {
            return Result.error("请上传.zip文件!");
        }
        // 新建ftp文件夹
        FtpUtil.createFolder("/deploy/" + serverDeploy.getServerCode() + "/software/", serverDeploy.getLatestCollectVersion());
        // 复制采集软件到ftp
        // 上传到ftp
        String newCollectAddress = serverDeploy.getNewCollectAddress();
        String basePath = "/deploy/"; // FTP服务器基础目录
        String filePath = serverDeploy.getServerCode() + "/software/" + serverDeploy.getLatestCollectVersion(); // FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
        String filename = newCollectAddress.substring(14, newCollectAddress.length());// 上传到FTP服务器上的文件名
        try {
            FileInputStream fileInputStream = new FileInputStream("D:\\IOT\\" + newCollectAddress);
            FtpUtil.uploadFile(basePath, filePath, filename, fileInputStream);
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 发送消息
        // 启动采集软件
        MqttParameter mqttParameter = new MqttParameter();
        mqttParameter.setId(serverDeploy.getServerCode());
        mqttParameter.setType("version");
        mqttParameter.setParameter1(serverDeploy.getLatestCollectVersion());
        // 采集软件
        String newCollectVersion = "/deploy/" + serverDeploy.getServerCode() + "/software/" + serverDeploy.getLatestCollectVersion();
        mqttParameter.setParameter3(newCollectVersion);
        mqttCustomerClient.pushlish(2, false, serverDeploy.getServerCode(), mqttParameter);
        return Result.ok("更新成功!");
    }
 
 
    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "服务器配置-通过id删除")
    @ApiOperation(value = "服务器配置-通过id删除", notes = "服务器配置-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        List<Equipment> equipmentList = equipmentService.findEquipmentByServerId(id);
        if (equipmentList.size() > 0){
            return Result.error("删除失败,请先删除设备!");
        }
        serverDeployService.removeById(id);
        return Result.ok("删除成功!");
    }
 
 
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "服务器配置-通过id查询", notes = "服务器配置-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        ServerDeploy serverDeploy = serverDeployService.getById(id);
        return Result.ok(serverDeploy);
    }
 
    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "mqtt配置-通过服务器id查询", notes = "mqtt配置-通过服务器id查询")
    @GetMapping(value = "/mqtt/queryById")
    public Result<?> mqttQueryById(@RequestParam(name = "id", required = true) String id) {
        return Result.ok(mqttDeployService.findByServerIdMqttDeploy(id));
    }
 
    /**
     * 通过serverCode查询
     *
     * @param serverCode
     * @return
     */
    @ApiOperation(value = "服务器配置-通过serverCode查询", notes = "服务器配置-通过serverCode查询")
    @GetMapping(value = "/queryByServerCode")
    public Result<?> queryByServerCode(@RequestParam(name = "serverCode", required = true) String serverCode) {
        ServerDeploy serverDeploy = serverDeployService.findByServerCode(serverCode);
        return Result.ok(serverDeploy);
    }
 
    /**
     * 通过id生成配置文件
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "服务器配置-通过id生成配置文件", notes = "服务器配置-通过id生成配置文件")
    @GetMapping(value = "/deploy/document")
    public Result<?> addDeploy(@RequestParam(name = "id", required = true) String id) {
        return serverDeployService.addDeployDocument(id);
    }
 
    private String randomDigit() {
        Random random = new Random();
        String randomNumber = random.nextInt(9000) + 1000 + "";
        ServerDeploy byServerCode = serverDeployService.findByServerCode(randomNumber);
        if (byServerCode != null) {
            randomDigit();
        }
        return randomNumber;
    }
}