Browse Source

告警接口分离

wukai 1 năm trước cách đây
mục cha
commit
aa60cc1fd3

+ 100 - 62
ruoyi-admin/src/main/java/com/ruoyi/biz/controller/ApiController.java

@@ -3,10 +3,9 @@ package com.ruoyi.biz.controller;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import com.ruoyi.biz.domain.*;
-import com.ruoyi.biz.service.IIotService;
-import com.ruoyi.biz.service.ITwinCalcDayService;
-import com.ruoyi.biz.service.ITwinDataService;
-import com.ruoyi.biz.service.ITwinDeviceService;
+import com.ruoyi.biz.service.*;
+import com.ruoyi.biz.service.impl.AsyncServiceImpl;
+import com.ruoyi.biz.service.impl.IotTokenServiceImpl;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.R;
 import io.swagger.annotations.Api;
@@ -17,6 +16,8 @@ import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.*;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
 
 /**
  * swagger 用户测试方法
@@ -30,73 +31,39 @@ public class ApiController extends BaseController {
     @Resource
     private ITwinCalcDayService twinCalcDayService;
     @Resource
+    private ITwinCalc2hrService calc2hrService;
+    @Resource
     private ITwinDataService twinDataService;
     @Resource
     private ITwinDeviceService deviceService;
     @Resource
     private IIotService iotService;
+    @Resource
+    private IotTokenServiceImpl iotTokenService;
+    @Resource
+    private AsyncServiceImpl asyncService;
 
-    @ApiOperation("首页数据")
+    @ApiOperation("首页统计数据")
     @GetMapping("/index")
     @CrossOrigin(origins = "*")
     public R<IndexData> index() {
+        TwinCalc2hr calc2hr = calc2hrService.calcToday();
         IndexData indexData = new IndexData();
         IndexEfficiency efficiency = new IndexEfficiency();
-        Float aTime = new Random().nextInt(1200) / 100f;
-        Float bTime = new Random().nextInt(1200) / 100f;
-        Float aLength = new Random().nextInt(90000) / 100f;
-        Float bLength = new Random().nextInt(90000) / 100f;
-        Float tLength = aLength + bLength;
-        Float aWeight = BigDecimal.valueOf(aLength * 3000 / 1000 / 1000).setScale(2, RoundingMode.HALF_UP).floatValue();
-        Float bWeight = BigDecimal.valueOf(bLength * 3000 / 1000 / 1000).setScale(2, RoundingMode.HALF_UP).floatValue();
-        Float tWeight = BigDecimal.valueOf(aWeight + bWeight).setScale(2, RoundingMode.HALF_UP).floatValue();
         Float ae = new Random().nextInt(2000) / 100f + 80f;
         Float be = new Random().nextInt(2000) / 100f + 80f;
-        Float te = ae + be;
-        efficiency.setTotalLength(tLength);
-        efficiency.setTotalWeight(tWeight);
-        efficiency.setATime(aTime);
-        efficiency.setALength(aLength);
-        efficiency.setAWeight(aWeight);
+        efficiency.setTotalLength(calc2hr.getLengthA().add(calc2hr.getLengthB()).floatValue());
+        efficiency.setTotalWeight(calc2hr.getWeightA().add(calc2hr.getWeightB()).floatValue());
+        efficiency.setATime(calc2hr.getOpenTimeA().floatValue());
+        efficiency.setALength(calc2hr.getLengthA().floatValue());
+        efficiency.setAWeight(calc2hr.getWeightA().floatValue());
         efficiency.setAEfficiency(ae);
-
-        efficiency.setBTime(bTime);
-        efficiency.setBLength(bLength);
-        efficiency.setBWeight(bWeight);
+        efficiency.setBTime(calc2hr.getOpenTimeB().floatValue());
+        efficiency.setBLength(calc2hr.getLengthB().floatValue());
+        efficiency.setBWeight(calc2hr.getWeightB().floatValue());
         efficiency.setBEfficiency(be);
         indexData.setEfficiency(efficiency);
-        List<IndexAlarm> alarmList = new ArrayList<>();
-        IndexAlarm indexAlarm = new IndexAlarm();
-        indexAlarm.setCode("C_2");
-        indexAlarm.setName("小经编机2号");
-        indexAlarm.setType(1);
-        alarmList.add(indexAlarm);
-        indexAlarm = new IndexAlarm();
-        indexAlarm.setCode("C_5");
-        indexAlarm.setName("小经编机5号");
-        indexAlarm.setType(2);
-        alarmList.add(indexAlarm);
-        indexAlarm = new IndexAlarm();
-        indexAlarm.setCode("C_7");
-        indexAlarm.setName("小经编机7号");
-        indexAlarm.setType(25);
-        alarmList.add(indexAlarm);
-        indexAlarm = new IndexAlarm();
-        indexAlarm.setCode("C_8");
-        indexAlarm.setName("小经编机8号");
-        indexAlarm.setType(8);
-        alarmList.add(indexAlarm);
-        indexAlarm = new IndexAlarm();
-        indexAlarm.setCode("C_97");
-        indexAlarm.setName("小经编机97号");
-        indexAlarm.setType(22);
-        alarmList.add(indexAlarm);
-        indexAlarm = new IndexAlarm();
-        indexAlarm.setCode("C_98");
-        indexAlarm.setName("小经编机98号");
-        indexAlarm.setType(15);
-        alarmList.add(indexAlarm);
-        indexData.setAlarm(alarmList);
+
         List<WeekData> weekDataList = new ArrayList<>();
         for (int i = 0; i < 7; i++) {
             WeekData weekData = new WeekData();
@@ -136,14 +103,86 @@ public class ApiController extends BaseController {
 //            weekDataList.add(weekData);
 //        });
         indexData.setWeekData(weekDataList);
+        return R.ok(indexData);
+    }
+
+    @ApiOperation("首页告警数据")
+    @GetMapping("/alarm")
+    @CrossOrigin(origins = "*")
+    public R<IndexData> alarm() {
+        IndexData indexData = new IndexData();
+        List<IndexAlarm> alarmList = new ArrayList<>();
+        int stop1 = 0, stop2 = 0, stop6 = 0;
+        iotTokenService.getToken();
+        List<TwinDevice> list = deviceService.selectTwinDeviceList(new TwinDevice());
+        List<Future<Map<String, Object>>> futureList = new ArrayList<>();
+        for (int i = 0; i < list.size(); i++) {
+            TwinDevice twinDevice = list.get(i);
+            futureList.add(asyncService.currData(twinDevice));
+        }
+        try {
+            for (Future<Map<String, Object>> future : futureList) {
+                Map<String, Object> map = future.get();
+                TwinDevice device = (TwinDevice) map.get("device");
+                int total = (int) map.get("total");
+                if (total == 0) {
+                    //可能会出现接口返回无数据的情况
+                    continue;
+                }
+
+                if (map.get("Alarm_unit_27") == null) {
+                    System.err.println("=======");
+                }
+                for (int i = 1; i <= 26; i++) {
+
+                    boolean flag = (boolean) map.get("Alarm_unit_" + i);
+                    if (flag) {
+                        IndexAlarm indexAlarm = new IndexAlarm();
+                        indexAlarm.setCode(device.getDeviceCode());
+                        indexAlarm.setName(device.getDeviceName());
+                        indexAlarm.setType(i);
+                        alarmList.add(indexAlarm);
+                    }
+                }
+
+                int alarm27 = (int) map.get("Alarm_unit_27");
+                if (alarm27 != 0) {
+                    IndexAlarm indexAlarm = new IndexAlarm();
+                    indexAlarm.setCode(device.getDeviceCode());
+                    indexAlarm.setName(device.getDeviceName());
+                    indexAlarm.setType(27);
+                    alarmList.add(indexAlarm);
+                }
+
+                int stopStatus = (int) map.get("Capacity_data_48");
+                switch (stopStatus) {
+                    case 1:
+                        stop1++;
+                        break;
+                    case 2:
+                        stop2++;
+                        break;
+                    case 6:
+                        stop6++;
+                        break;
+                    default:
+                }
+            }
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e);
+        }
+        indexData.setAlarm(alarmList);
+
         IndexDevice device = new IndexDevice();
         device.setTotal(140);
-        device.setOnline(48);
-        device.setStop1(new Random().nextInt(50));
-        device.setStop2(new Random().nextInt(50));
-        device.setStop6(new Random().nextInt(50));
-        device.setStop8(new Random().nextInt(50));
-        device.setAlarm(new Random().nextInt(50));
+        device.setOnline(list.size());
+        device.setStop1(stop1);
+        device.setStop2(stop2);
+        device.setStop6(stop6);
+        device.setStop8(new Random().nextInt(5));
+        device.setAlarm(alarmList.size());
         indexData.setDevice(device);
         return R.ok(indexData);
     }
@@ -155,7 +194,6 @@ public class ApiController extends BaseController {
         TwinDevice info = deviceService.selectTwinDeviceByCode(device);
         String table = info.getDevicePath();
         String sql = "select last * from " + table;
-        System.err.println(sql);
         JSONObject jsonObject = iotService.query(sql);
         JSONObject data = jsonObject.getJSONObject("data");
         JSONArray values = data.getJSONArray("values");

+ 8 - 0
ruoyi-admin/src/main/java/com/ruoyi/biz/service/ITaskService.java

@@ -37,4 +37,12 @@ public interface ITaskService {
      * @param date 指定日期 yyyy-mm-dd
      */
     void calc(String date);
+
+    /**
+     * 统计指定日期指定时段数据
+     *
+     * @param date   指定日期 yyyy-mm-dd
+     * @param period 时段
+     */
+    void calc(String date, int period);
 }

+ 14 - 2
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/AsyncServiceImpl.java

@@ -39,11 +39,23 @@ public class AsyncServiceImpl {
         JSONArray values = data.getJSONArray("values");
 
         Map<String, Object> dataMap = new HashMap<>(16);
-
+        dataMap.put("device", twinDevice);
+        dataMap.put("total", values.size());
         for (int i = 0; i < values.size(); i++) {
             JSONArray d = values.getJSONArray(i);
             String key = d.getStr(0).replace(table + ".", "");
-            dataMap.put(key, d.getFloat(1));
+            switch (d.getStr(2)) {
+                case "FLOAT":
+                    dataMap.put(key, d.getFloat(1));
+                    break;
+                case "BOOLEAN":
+                    dataMap.put(key, d.getBool(1));
+                    break;
+                case "INT32":
+                case "INT64":
+                    dataMap.put(key, d.getInt(1));
+                    break;
+            }
         }
         return new AsyncResult<>(dataMap);
     }

+ 16 - 0
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/TaskServiceImpl.java

@@ -91,6 +91,22 @@ public class TaskServiceImpl implements ITaskService {
     }
 
     /**
+     * 统计指定日期指定时段数据
+     *
+     * @param date   指定日期 yyyy-mm-dd
+     * @param period 时段
+     */
+    @Override
+    public void calc(String date, int period) {
+        LocalDate localDate = LocalDate.parse(date);
+        LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
+        LocalDateTime start = ldt.plusHours(2 * period).minusHours(2);
+        LocalDateTime end = start.plusHours(2);
+        start = start.minusSeconds(1);
+        calc4device(start, end);
+    }
+
+    /**
      * 统计指定日期数据
      *
      * @param date 指定日期

+ 11 - 11
ruoyi-admin/src/test/java/com/jjt/DataPF.java

@@ -59,17 +59,17 @@ public class DataPF {
 
     @Test
     void data() {
-//        calc2hrService.calc("2024-05-11");
-//        calc2hrService.calc("2024-05-12");
-//        calc2hrService.calc("2024-05-13");
-//        calc2hrService.calc("2024-05-14");
-//        calc2hrService.calc("2024-05-15");
-//        calc2hrService.calc("2024-05-16");
-//        calc2hrService.calc("2024-05-17");
-//        calc2hrService.calc("2024-05-18");
-//        calc2hrService.calc("2024-05-19");
-//        calc2hrService.calc("2024-05-20");
-        taskService.calcLastPeriod();
+//        taskService.calc("2024-05-15");
+//        taskService.calc("2024-05-16");
+//        taskService.calc("2024-05-17");
+//        taskService.calc("2024-05-18");
+//        taskService.calc("2024-05-19");
+//        taskService.calc("2024-05-20");
+//        taskService.calc("2024-05-21",12);
+        for (int i = 1; i <= 5; i++) {
+            taskService.calc("2024-05-22", i);
+        }
+//        taskService.calcLastPeriod();
 //        String table = "root.tl.suxi.knittings97_plc1";
 //        long startTime = 1715709599000l;
 //        long endTime = 1715716800000l;