| | |
| | | package org.jeecg.modules.iot.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.modules.iot.entity.*; |
| | | import org.jeecg.modules.iot.service.*; |
| | | import org.jeecg.modules.iot.util.FtpUtil; |
| | | import org.jeecg.modules.iot.mapper.ServerDeployMapper; |
| | | import org.jeecg.modules.iot.mqtt.config.MqttCustomerClient; |
| | | import org.jeecg.modules.iot.service.*; |
| | | import org.jeecg.modules.iot.util.FileUtil; |
| | | import org.jeecg.modules.iot.util.FtpUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.context.annotation.Lazy; |
| | |
| | | import javax.xml.transform.TransformerFactory; |
| | | import javax.xml.transform.dom.DOMSource; |
| | | import javax.xml.transform.stream.StreamResult; |
| | | import java.io.*; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.FileNotFoundException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | |
| | | */ |
| | | @Service |
| | | public class ServerDeployServiceImpl extends ServiceImpl<ServerDeployMapper, ServerDeploy> implements IServerDeployService { |
| | | |
| | | @Autowired |
| | | private FtpUtil ftpUtil; |
| | | @Value("${ftp.address}") |
| | | private String ftpAddress; |
| | | @Value("${operatingSystem}") |
| | | private String operatingSystem; |
| | | @Autowired |
| | | private IInfluxdbDeployService influxdbDeployService; |
| | | @Autowired |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result<?> addDeployDocument(String id) { |
| | | // 验证FTP连接 |
| | | boolean isConnected = FtpUtil.testFtpConnection(); |
| | | boolean isConnected = ftpUtil.testFtpConnection(); |
| | | if (!isConnected) { |
| | | return Result.error("FTP连接失败,请检查配置!"); |
| | | } |
| | |
| | | serverDeploy.setDeployIssueTime(new Date()); |
| | | baseMapper.updateById(serverDeploy); |
| | | // 生成本地文件夹 |
| | | // FileUtil.createDir("D:/iot/" + serverDeploy.getServerCode() + "/deploy/" + formattedDate); |
| | | FileUtil.createDir(ftpAddress + serverDeploy.getServerCode() + "/deploy/" + formattedDate + "/script"); |
| | | // 生成本地文件夹 |
| | | FileUtil.createDir(ftpAddress + serverDeploy.getServerCode() + "/deploy/" + formattedDate); |
| | | MqttParameter mqttParameter = new MqttParameter(); |
| | | // 上传到ftp |
| | | if (operatingSystem.equals("windows")) { |
| | | String basePath = "/deploy"; // FTP服务器基础目录 |
| | | String filePath = serverDeploy.getServerCode() + "/deploy/" + formattedDate; // FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath |
| | | String filePath = "/" + serverDeploy.getServerCode() + "/deploy/" + formattedDate; // FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath |
| | | String filename = "CollectionConfiguration.lmx";// 上传到FTP服务器上的文件名 |
| | | //创建一个输入流 |
| | | try { |
| | | FileInputStream fileInputStream = new FileInputStream(ftpAddress + serverDeploy.getServerCode() + "/deploy/" + formattedDate + "/CollectionConfiguration.lmx"); |
| | | FtpUtil.uploadFile(basePath, filePath, filename, fileInputStream); |
| | | ftpUtil.uploadFile(basePath, filePath, filename, fileInputStream); |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | mqttParameter.setParameter4(basePath + "/" + filePath); |
| | | } else { |
| | | String basePath = "/iot"; // FTP服务器基础目录 |
| | | String filePath = "/deploy/" + serverDeploy.getServerCode() + "/deploy/" + formattedDate; // FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath |
| | | String filename = "CollectionConfiguration.lmx";// 上传到FTP服务器上的文件名 |
| | | //创建一个输入流 |
| | | try { |
| | | FileInputStream fileInputStream = new FileInputStream(ftpAddress + serverDeploy.getServerCode() + "/deploy/" + formattedDate + "/CollectionConfiguration.lmx"); |
| | | ftpUtil.uploadFileKylin(basePath, filePath, filename, fileInputStream); |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | mqttParameter.setParameter4(filePath); |
| | | } |
| | | // 存储脚本,复制整个文件夹 |
| | | FtpUtil.uploadFolder(new File(ftpAddress+serverDeploy.getServerCode()+"/script/"), "/deploy/"+serverDeploy.getServerCode()+"/deploy/"+formattedDate+"/script"); |
| | | // 生成脚本 |
| | | ftpUtil.uploadFolderKylin(new File(ftpAddress + serverDeploy.getServerCode() + "/script/"), "/deploy/" + serverDeploy.getServerCode() + "/deploy/" + formattedDate + "/script"); |
| | | // 发送配置文件版信号 |
| | | MqttParameter mqttParameter = new MqttParameter(); |
| | | mqttParameter.setId(serverDeploy.getServerCode()); |
| | | mqttParameter.setType("version"); |
| | | mqttParameter.setParameter2(formattedDate); |
| | | mqttParameter.setParameter4(basePath +"/" +filePath); |
| | | mqttCustomerClient.pushlish(2, false, serverDeploy.getServerCode(), mqttParameter); |
| | | return Result.ok("配置文件生成成功"); |
| | | } |