yejian 3 роки тому
батько
коміт
2e6079cfc0

+ 34 - 12
imcs-admin-boot/imcs-business-biz/src/main/java/com/github/zuihou/business/productionResourceCenter/service/impl/ProgramServiceImpl.java

@@ -268,33 +268,38 @@ public class ProgramServiceImpl extends SuperServiceImpl<ProgramMapper, Program>
                 moduleMap = modules.stream().collect(Collectors.groupingBy(Module::getId));
             }
             for(Productionresource productionresource : productionresources){
+                String localFolder = uploadFolder + "0000/cncFile/" + productionresource.getId();
+                // 删除该机床下的所有的程序数据
+                baseMapper.delete(Wraps.<Program>lbQ().in(Program::getResourceId,productionresource.getId()));
                 // 根据不同系统查找数控程序
                 // TODO 真实机床调试 Scpclient
                 List<String> fileNames = new ArrayList<>();
+                String cncFilePath = "";
                 String ip = productionresource.getIp();
-                String port = productionresource.getPort();
-                String userName = "";
-                String password = "";
+                String port = "22";
+                String userName = "manufact";
+                String password = "SUNRISE";
                 Scpclient scpclient = new Scpclient(ip,Integer.valueOf(port),userName,password);
                 List<Module> resourceModules = moduleMap.get(productionresource.getModuleId());
                 if(CollectionUtil.isNotEmpty(resourceModules)){
                     Module resourceModule = resourceModules.get(0);
                     String cncSystem = resourceModule.getCncSystem().getKey();
-                    String cncFilePath = resourceModule.getCncFilePath();
+                    cncFilePath  = resourceModule.getCncFilePath();
                     // 模拟现在直接nginx下查找文件同步
-                    fileNames = scpclient.getSubfolderName(cncFilePath);
+                    fileNames = scpclient.getSubfolderName(cncFilePath,localFolder);
                 }
                 if(CollectionUtil.isNotEmpty(fileNames)){
-                    uploadFolder = uploadFolder + "0000/cncFile/";
                     for(String fileName : fileNames){
-                        scpclient.getFile(fileName,uploadFolder);
+//                        scpclient.getFile(cncFilePath + fileName,uploadFolder);
                         // 入库设备程序表
                         Program program = new Program();
                         program.setResourceId(productionresource.getId());
                         program.setName(productionresource.getName());
                         program.setNo(productionresource.getCode());
-                        String cncFilePath = "http://" + nginxIp + ":" + nginxPort + "/file/0000/cncFile/" + fileName;
-                        program.setFilePath(cncFilePath);
+                        String programFilePath = "http://" + nginxIp + ":" + nginxPort + "/file/0000/cncFile/"+ productionresource.getId() + "/" + fileName;
+                        program.setFilePath(programFilePath);
+                        File programFile = new File(localFolder + "/" + fileName);
+                        program.setSize(Float.valueOf(programFile.length()));
                         program.setSource("2");
                         program.setSynTime(LocalDateTime.now());
                         //根据编码规则
@@ -302,7 +307,7 @@ public class ProgramServiceImpl extends SuperServiceImpl<ProgramMapper, Program>
                     }
 
                     //设置程序数量
-                    List<Program>programList = baseMapper.selectList(Wraps.<Program>lbQ().eq(Program::getResourceId,productionresource.getId()));
+                    List<Program> programList = baseMapper.selectList(Wraps.<Program>lbQ().eq(Program::getResourceId,productionresource.getId()));
 
                     if(productionresource!=null){
                         productionresource.setProgramNum(programList.size());
@@ -320,8 +325,25 @@ public class ProgramServiceImpl extends SuperServiceImpl<ProgramMapper, Program>
 
     @Override
     public Boolean equipmentDelete(Program data){
-        if(1==1){//调用删除接口
-            baseMapper.deleteById(data.getId());
+        Productionresource productionresource = productionresourceMapper.selectById(data.getResourceId());
+        Module resourceModule = moduleService.getById(productionresource.getModuleId());
+        // 删除机床上的程序
+        String ip = productionresource.getIp();
+        String port = "22";
+        String userName = "manufact";
+        String password = "SUNRISE";
+        Scpclient scpclient = new Scpclient(ip,Integer.valueOf(port),userName,password);
+        Program program = baseMapper.selectById(data.getId());
+        String fileName = program.getFilePath().substring(program.getFilePath().lastIndexOf("/") + 1);
+        scpclient.remove(resourceModule.getCncFilePath(),fileName);
+
+        baseMapper.deleteById(data.getId());
+        //设置程序数量
+        List<Program>programList = baseMapper.selectList(Wraps.<Program>lbQ().eq(Program::getResourceId,data.getResourceId()));
+
+        if(productionresource!=null){
+            productionresource.setProgramNum(programList.size());
+            productionresourceMapper.updateById(productionresource);
         }
         return true;
 

+ 79 - 112
imcs-admin-boot/imcs-common/src/main/java/com/github/zuihou/common/util/Scpclient.java

@@ -50,138 +50,105 @@ public class Scpclient {
         this.password = passward;
     }
 
-    public void getFile(String remoteFile, String localTargetDirectory) {
-        Connection conn = new Connection(ip, port);
+    /**
+     * 得到服务器上指定文件夹下所有子文件夹(第一级子文件夹)
+     * @return
+     */
+    public List<String> getSubfolderName(String targetFolder,String localFolder) {
+        List<String> folderNameList = new ArrayList<String>();
+        Connection connection = null;
+        Session sess = null;
+        BufferedReader stdoutReader = null;
         try {
-            conn.connect();
-            boolean isAuthenticated = conn.authenticateWithPassword(username,
-                    password);
-            if (isAuthenticated == false) {
-                System.err.println("authentication failed");
-            }
-            SCPClient client = new SCPClient(conn);
-            client.get(remoteFile, localTargetDirectory);
-            conn.close();
-        } catch (IOException ex) {
-            Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null, ex);
-        }
-    }
 
-    public void putFile (String localFile, String remoteTargetDirectory){
-        Connection conn = new Connection(ip, port);
-        try {
-            conn.connect();
-            boolean isAuthenticated = conn.authenticateWithPassword(username,
-                    password);
-            if (isAuthenticated == false) {
-                System.err.println("authentication failed");
-            }
-            SCPClient client = new SCPClient(conn);
-            client.put(localFile, remoteTargetDirectory);
-            conn.close();
-        } catch (IOException ex) {
-            Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null, ex);
-        }
-    }
+            // 创建一个连接实例
+            connection = new Connection(ip, port);
+            // 连接
+            connection.connect();
+            // 认证
+            boolean isAuthenticated = connection.authenticateWithPassword(username, password);
+            // 创建一个会话
+            if(isAuthenticated){
+                SCPClient scpClient = connection.createSCPClient();
+                sess = connection.openSession();
+                String command = "ls " + targetFolder;
+                sess.execCommand(command);
+                InputStream stdout = new StreamGobbler(sess.getStdout());
+                stdoutReader = new BufferedReader(new InputStreamReader(stdout));
+
+                while (true) {
+                    String line = stdoutReader.readLine();
+                    if (line == null){
+                        break;
+                    }
+                    //获取文件名称
+                    folderNameList.add(line);
+                    SCPInputStream scpInputStream = scpClient.get(targetFolder + line);
+                    // 下载文件
+                    File file = new File(localFolder);
+                    if (!file.exists()) {
+                        file.mkdirs();
+                    }
+                    downLoadFileIO(scpInputStream,localFolder + "/" + line);
+                }
 
-    public void putFile (String localFile, String remoteFileName, String remoteTargetDirectory, String mode){
-        Connection conn = new Connection(ip, port);
-        try {
-            conn.connect();
-            boolean isAuthenticated = conn.authenticateWithPassword(username,
-                    password);
-            if (isAuthenticated == false) {
-                System.err.println("authentication failed");
             }
-            SCPClient client = new SCPClient(conn);
-            if ((mode == null) || (mode.length() == 0)) {
-                mode = "0600";
+        } catch (IOException e) {
+            e.printStackTrace(System.err);
+        }finally {
+            if(sess!=null){
+                sess.close();
+            }
+            connection.close();
+            if(stdoutReader!=null){
+                try {
+                    stdoutReader.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
             }
-            client.put(localFile, remoteFileName, remoteTargetDirectory, mode);
-            //重命名
-            ch.ethz.ssh2.Session sess = conn.openSession();
-            String tmpPathName = remoteTargetDirectory + File.separator + remoteFileName;
-            String newPathName = tmpPathName.substring(0, tmpPathName.lastIndexOf("."));
-            sess.execCommand("mv " + remoteFileName + " " + newPathName);//重命名回来
-            conn.close();
-        } catch (IOException ex) {
-            Logger.getLogger(SCPClient.class.getName()).log(Level.SEVERE, null, ex);
         }
+        return folderNameList;
     }
-    public static byte[] getBytes (String filePath){
-        byte[] buffer = null;
-        try {
-            File file = new File(filePath);
-            FileInputStream fis = new FileInputStream(file);
-            ByteArrayOutputStream byteArray = new ByteArrayOutputStream(1024 * 1024);
-            byte[] b = new byte[1024 * 1024];
-            int i;
-            while ((i = fis.read(b)) != -1) {
-                byteArray.write(b, 0, i);
-            }
-            fis.close();
-            byteArray.close();
-            buffer = byteArray.toByteArray();
-        } catch (FileNotFoundException e) {
-            e.printStackTrace();
-        } catch (IOException e) {
-            e.printStackTrace();
+
+    private void downLoadFileIO(SCPInputStream scpInputStream,String localFolderFile) throws IOException {
+        // IO流下载文件
+        BufferedInputStream bis = new BufferedInputStream(scpInputStream);
+        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(localFolderFile));
+
+        byte[] bytes = new byte[1024];
+        int len;
+        while ((len = bis.read(bytes)) > 0) {
+            bos.write(bytes, 0, len);
         }
-        return buffer;
+        bis.close();
+        bos.close();
     }
 
-    /**
-     * 得到服务器上指定文件夹下所有子文件夹(第一级子文件夹)
-     * @return
-     */
-    public List<String> getSubfolderName(String targetFolder) {
-        List<String> folderNameList = new ArrayList<String>();
+    public void remove(String remoteFilePath, String fileName) {
+        Connection connection = null;
+        Session sess = null;
         try {
             // 创建一个连接实例
-            Connection connection = new Connection(ip, port);
+            connection = new Connection(ip, port);
             // 连接
             connection.connect();
             // 认证
             boolean isAuthenticated = connection.authenticateWithPassword(username, password);
             // 创建一个会话
-            Session sess = connection.openSession();
-            sess.requestPTY("bash");
-            sess.startShell();
-            InputStream stdout = new StreamGobbler(sess.getStdout());
-            InputStream stderr = new StreamGobbler(sess.getStderr());
-            BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(stdout));
-            BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
-            //向服务器上输入命令
-            PrintWriter out = new PrintWriter(sess.getStdin());
-            //进入文件存放的目录
-            out.println("cd " + targetFolder);
-            out.println("ll");
-            out.println("exit");
-            out.close();
-            sess.waitForCondition(ChannelCondition.CLOSED|ChannelCondition.EOF | ChannelCondition.EXIT_STATUS,30000);
-
-            //本机查看远程操作的指令及状态
-            System.out.println("输入指令后对应的显示信息:");
-            while (true) {
-                String line = stdoutReader.readLine();
-                if (line == null){
-                    break;
-                }
-                //获取文件名称
-                //取出正确的文件名称
-                StringBuffer sb = new StringBuffer(line.substring(line.lastIndexOf(":")+3, line.length()));
-                folderNameList.add(line);
+            if(isAuthenticated){
+                sess = connection.openSession();
+                String command = "cd " + remoteFilePath + "&&rm -rf "+ fileName;
+                sess.execCommand(command);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if(sess!=null){
+                sess.close();
             }
-            //关闭连接
-            sess.close();
             connection.close();
-            stderrReader.close();
-            stdoutReader.close();
-        } catch (IOException e) {
-            e.printStackTrace(System.err);
-            System.exit(2);
         }
-        return folderNameList;
     }
 }