Prechádzať zdrojové kódy

Merge remote-tracking branch 'origin/master'

wuhongshuang 2 rokov pred
rodič
commit
3586baac0e

+ 97 - 1
src/main/java/com/szwl/controller/TEquipmentController.java

@@ -13,6 +13,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.szwl.constant.ResponseCodesEnum;
 import com.szwl.model.bean.*;
+import com.szwl.model.bo.JsonMessage;
 import com.szwl.model.bo.R;
 import com.szwl.model.bo.ResponseModel;
 import com.szwl.model.entity.*;
@@ -31,12 +32,19 @@ import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.mvc.support.RedirectAttributes;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
 import java.net.URL;
 import java.net.URLConnection;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
+import static com.szwl.constant.ResponseCodesEnum.A0001;
 import static com.szwl.constant.ResponseCodesEnum.P0002;
 
 /**
@@ -69,6 +77,7 @@ public class TEquipmentController {
     TAdminEquipmentService tAdminEquipmentService;
     private int j;
 
+    private static final int TIMEOUT = 30000;  // 超时时间为30秒
     /**
      * @param adminName     商户的登录名
      * @param equipmentName 机器名称
@@ -875,7 +884,7 @@ public class TEquipmentController {
                 nowTime2 = getNetworkTime();
             }
             if (nowTime2 == null) {
-                nowTime2 = new Date().getTime();
+                nowTime2 = System.currentTimeMillis();
             }
             if (nowTime2 - old < 15000) {
                 return ResponseEntity
@@ -1454,5 +1463,92 @@ public class TEquipmentController {
                         .setMessage("SUCCESS"));
     }
 
+    /**
+     * 下载日志
+     * @param equipmentId
+     * @param day
+     * @param response
+     */
+    @ApiOperation(value = "下载日志")
+    @GetMapping("/downloadLog")
+    public ResponseModel<?> downloadLog(String equipmentId, String day, HttpServletResponse response) throws IOException, InterruptedException {
+        // 1、发送MQ到安卓端上传日志
+        if(day.length()!=8){
+            return R.fail(A0001,"日期格式有误");
+        }
+        if(StringUtils.isEmpty(equipmentId)) {
+            return R.fail(A0001);
+        }
+        TEquipment tEquipment = tEquipmentService.getById(equipmentId);
+        if (tEquipment == null) {
+            return R.fail(A0001,"该设备不存在");
+        }
+        String clientId = tEquipment.getClientId();
+        String kind = day+"-"+clientId;
+        String filepath = "/home/hboxs/log/"+kind+".txt";
+        // 1.1 如果文件已存在,直接下载
+        if(new File(filepath).exists()) {
+            downloadFile(filepath, response);
+            return R.ok();
+        }
+        String channel = tEquipment.getChannel();
+        String equimentType = tEquipment.getEquimentType();
+        if(StringUtils.isEmpty(channel)||channel.equals("1")||StringUtils.isEmpty(equimentType)){
+            //用个推
+            PushUtils.push(tEquipment.getGtClientId(), "", "", PushUtils.buildJson("log", kind).toString());
+        }
+        if(StringUtils.isNotEmpty(channel)&&channel.equals("2")&&StringUtils.isNotEmpty(equimentType)){
+            //用Mq
+            tEquipmentService.sentMessage(tEquipment.getClientId(), PushUtils.buildJson("log", kind).toString());
+        }
+        // 2、设置定时器查看日志是否已上传
+        ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
+        long startTime = System.currentTimeMillis();
+        ScheduledFuture<?> future = exec.schedule(() -> {
+            // 超时处理逻辑
+            if (new File(filepath).exists()) {
+                return null;
+            } else {
+                throw new RuntimeException("找不到日志");
+            }
+        }, TIMEOUT, TimeUnit.MILLISECONDS);
+
+        while (true) {
+            if (new File(filepath).exists()) {
+                // 文件存在,取消定时器
+                future.cancel(false);
+                break;
+            }else {
+                Thread.sleep(1000);
+                if (System.currentTimeMillis() - startTime >= 20000L) {
+                    // 超过最大等待时间,抛出异常
+                    future.cancel(false);
+                    throw new RuntimeException("等待文件生成超时");
+                }
+            }
+        }
+        // 3、从服务器下载文件
+        downloadFile(filepath, response);
+        return R.ok();
+    }
+
+    // 从服务器下载文件
+    public void downloadFile(String filepath, HttpServletResponse response) throws IOException {
+        File logFile = new File(filepath);
+        response.setHeader("Content-Disposition", "attachment; filename=\"" + logFile.getName() + "\"");
+
+        FileInputStream fis = new FileInputStream(logFile);
+        OutputStream os = response.getOutputStream();
+
+        byte[] buffer = new byte[4096];
+        int length;
+        while ((length = fis.read(buffer)) > 0) {
+            os.write(buffer, 0, length);
+            os.flush();
+        }
+        os.close();
+        fis.close();
+        response.flushBuffer();
+    }
 }