Browse Source

添加故障诊断接口

wangchangsheng 2 years ago
parent
commit
7d8be54a68

+ 126 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/controller/diagnose/TrainFaultDiagnoseController.java

@@ -0,0 +1,126 @@
+package com.gyee.impala.controller.diagnose;
+
+
+import com.alibaba.fastjson.JSONObject;
+import com.gyee.impala.common.result.JsonResult;
+import com.gyee.impala.common.result.ResultCode;
+import com.gyee.impala.common.util.DateUtil;
+import com.gyee.impala.model.custom.diagnose.DataInfo;
+import com.gyee.impala.model.custom.diagnose.ExecuteInfo;
+import com.gyee.impala.model.master.Casefault;
+import com.gyee.impala.model.master.diagnose.Diagnosepoint;
+import com.gyee.impala.service.custom.diagnose.DataPointService;
+import com.gyee.impala.service.custom.diagnose.TrainFaultDiagnoseService;;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import java.util.Calendar;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * 故障诊断
+ */
+@Slf4j
+@CrossOrigin
+@RestController
+@RequestMapping("/api/diagnose")
+public class TrainFaultDiagnoseController {
+
+
+    @Autowired
+    private TrainFaultDiagnoseService trainFaultDiagnoseService;
+
+
+    private ExecuteInfo executeInfo;
+
+
+    @Autowired
+    private DataPointService dataService;
+
+    /**
+     * 线程池
+     */
+    @Resource
+    private ThreadPoolTaskExecutor taskExecutor;
+
+
+    private static final Object locker = new Object();
+
+
+
+
+
+    /** 开始诊断 查询 golden 所有原始数据
+     *  flag  ture: 所有数据 ;false: 前10条数据
+     * **/
+    @PostMapping("/pointdata")
+    public JSONObject getPointData(@RequestBody JSONObject json){
+        if (json == null)
+            return JsonResult.error(ResultCode.PARAM_IS_BLANK);
+
+
+        log.info("进入诊断程序");
+        boolean flag = json.getBooleanValue("flag");
+        List<Diagnosepoint> points = JSONObject.parseArray(json.getJSONArray("points").toString(), Diagnosepoint.class);
+        List<Casefault> faults = JSONObject.parseArray(json.getJSONArray("faults").toString(), Casefault.class);
+
+        /** 组装数据  删除添加的故障类型**/
+        dataService.formatUniformcode(points.stream().filter(a -> !a.getUniformcode().equals("faulttype")).collect(Collectors.toList()));
+        executeInfo = new ExecuteInfo();
+        Calendar cal = Calendar.getInstance();
+        DataInfo[] dataInfos = new DataInfo[faults.size()];
+        for (int i = 0; i < faults.size(); i++){
+            DataInfo data = new DataInfo();
+            data.setId(Long.valueOf(faults.get(i).getId()));
+            data.setStationId(faults.get(i).getStationen());
+            data.setThingId(faults.get(i).getWindturbineid());
+            data.setModelId(faults.get(i).getModel());
+            data.setTag(faults.get(i).getFaultcode());
+            data.setFaultTime(faults.get(i).getStarttime());
+
+            cal.setTime(DateUtil.parseStrtoDate(faults.get(i).getStarttime(), DateUtil.YYYY_MM_DD_HH_MM_SS));
+            cal.add(Calendar.MINUTE, -30);
+            data.setStartTs(cal.getTimeInMillis() + "");
+            cal.add(Calendar.MINUTE, 30);
+            data.setEndTs(cal.getTimeInMillis() + "");
+            dataInfos[i] = data;
+        }
+        executeInfo.setDataInfos(dataInfos);
+        if (flag){
+            if (!trainFaultDiagnoseService.isComplete()) {
+                return JsonResult.error(4000, "已有正在诊断的故障模型...");
+            }
+            synchronized (locker) {
+                taskExecutor.submit(this::execute);
+            }
+            return JsonResult.success(ResultCode.SUCCESS);
+        }else {
+            Map<String, Object> mp = dataService.getFormData(executeInfo);
+            return JsonResult.successData(ResultCode.SUCCESS, mp);
+        }
+    }
+
+
+    /**
+     * 调用执行脚本
+     */
+    private void execute() {
+
+    }
+
+
+
+
+
+
+
+
+
+
+
+}

+ 86 - 0
gyee-sample-impala/src/main/java/com/gyee/impala/service/custom/diagnose/TrainFaultDiagnoseService.java

@@ -0,0 +1,86 @@
+package com.gyee.impala.service.custom.diagnose;
+
+
+import com.gyee.impala.common.config.GyeeConfig;
+import com.gyee.impala.common.util.DateUtil;
+import com.gyee.impala.common.util.JudeSystem;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+@Slf4j
+@Service
+public class TrainFaultDiagnoseService {
+
+    /**
+     * 保存脚本的位置
+     */
+    @Autowired
+    private GyeeConfig gyeeConfig;
+
+
+
+    /**
+     *
+     */
+    private boolean isComplete = true;
+
+    public boolean isComplete() {
+        return isComplete;
+    }
+
+
+
+    /**
+     * 在线训诊断用脚本
+     */
+    public void exec() {
+        //判断当前是否有模型在训练
+        if (!isComplete) {
+            log.error("已有进程在诊断");
+            return;
+        }
+
+        try {
+            isComplete = false;
+            log.info("开始执行诊断脚本" + (isComplete) + "...");
+
+            //组装调用脚本命令
+            Process p;
+            String cmdPath = gyeeConfig.getDiagnosePath();
+            String inst = JudeSystem.isWindows() ? "cmd" : "/bin/sh";
+            String c = JudeSystem.isWindows() ? "/c" : "-c";
+            String[] cmd = {inst, c, "python " + cmdPath +".py "};
+            log.info(cmd[0] + " " + cmd[1] + " " + cmd[2]);
+
+            //执行脚本
+            p = Runtime.getRuntime().exec(cmd);
+
+            //脚本输出信息
+            BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
+            BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
+            String si = null, se = null;
+            while ((si = bri.readLine()) != null || (se = bre.readLine()) != null) {
+                if (si != null) {
+                    System.out.println("诊断返回信息:" + si);
+                }
+                if (se != null) {
+                    System.err.println("诊断返回错误信息:" + se);
+                }
+            }
+            p.waitFor();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }finally {
+            isComplete = true;
+            log.info("诊断脚本执行结束" + (isComplete) + "...");
+        }
+    }
+
+
+
+}