Quellcode durchsuchen

大屏参数设置增加几个参数
增加定时任务,从webaccess获取大屏参数的信息,包括历史信息
获取webaccess数据,容易超时报错,增加连接时间

dongjh vor 1 Jahr
Ursprung
Commit
4eae882cbf

+ 44 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/message/websocket/WebSocket.java

@@ -99,6 +99,50 @@ public class WebSocket {
         }
     }
 
+    /**
+     * ws推送消息(object)
+     *
+     * @param userId
+     * @param message
+     */
+    public void pushMessageByObject(String userId, Object message) {
+        for (Map.Entry<String, Session> item : sessionPool.entrySet()) {
+            //userId key值= {用户id + "_"+ 登录token的md5串}
+            //TODO vue2未改key新规则,暂时不影响逻辑
+            if (item.getKey().contains(userId)) {
+                Session session = item.getValue();
+                try {
+                    //update-begin-author:taoyan date:20211012 for: websocket报错 https://gitee.com/jeecg/jeecg-boot/issues/I4C0MU
+                    synchronized (session){
+                        log.info("【系统 WebSocket】推送单人消息:" + message.toString());
+                        session.getBasicRemote().sendObject(message);
+                    }
+                    //update-end-author:taoyan date:20211012 for: websocket报错 https://gitee.com/jeecg/jeecg-boot/issues/I4C0MU
+                } catch (Exception e) {
+                    log.error(e.getMessage(),e);
+                }
+            }
+        }
+    }
+
+    /**
+     * ws遍历群发消息(object)
+     */
+    public void pushMessageByObject(Object message) {
+        try {
+            for (Map.Entry<String, Session> item : sessionPool.entrySet()) {
+                try {
+                    item.getValue().getAsyncRemote().sendObject(message);
+                } catch (Exception e) {
+                    log.error(e.getMessage(), e);
+                }
+            }
+            log.info("【系统 WebSocket】群发消息:" + message.toString());
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+        }
+    }
+
 
     /**
      * ws接受客户端消息

+ 2 - 1
module-guan/src/main/java/org/jeecg/modules/webaccess/controller/WebAccessController.java

@@ -68,7 +68,8 @@ public class WebAccessController {
     @ApiOperation(value = "获取历史数据(webaccess)", notes = "获取历史数据(webaccess)")
     @RequestMapping(value = "/getWADataLog", method = RequestMethod.GET)
     public Result getWADataLog() {
-        JSONObject jsonObject = webAccessService.generateDataLogObject("2023-12-19 12:00:00", "M", 1, 1000, "0");
+        JSONObject jsonObject = webAccessService.generateDataLogObject("2023-12-19 12:00:00", "M",
+                1, 1000, "0", "设备状态");
         JSONObject result = webAccessService.GetDataLog(jsonObject);
         JSONArray jsonArray = result.getJSONArray("DataLog");
 

+ 17 - 2
module-guan/src/main/java/org/jeecg/modules/webaccess/entity/GuanDashboardParam.java

@@ -41,12 +41,27 @@ public class GuanDashboardParam {
     /**获取历史记录条数*/
     @Excel(name = "获取历史记录条数")
     @ApiModelProperty(value = "获取历史记录条数")
-    private Integer lognum;
+    private Integer records;
 
     /**获取历史记录时长(小时)*/
     @Excel(name = "获取历史记录时长(小时)")
     @ApiModelProperty(value = "获取历史记录时长")
-    private Integer logduration;
+    private Integer duration;
+
+    /**数据之间隔时间*/
+    @Excel(name = "数据之间隔时间")
+    @ApiModelProperty(value = "数据之间隔时间")
+    private Integer intervals;
+
+    /**查询之时间单位,S:秒,M:分,H:时,D:日*/
+    @Excel(name = "查询之时间单位")
+    @ApiModelProperty(value = "数据之间隔时间")
+    private String intervaltype;
+
+    /**数据型态,0:Last,1:Min,2:Max,3:Avg*/
+    @Excel(name = "数据型态")
+    @ApiModelProperty(value = "数据型态")
+    private String datatype;
 
     /**创建时间*/
     @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")

+ 42 - 0
module-guan/src/main/java/org/jeecg/modules/webaccess/job/WAMsgJob.java

@@ -0,0 +1,42 @@
+package org.jeecg.modules.webaccess.job;
+
+import lombok.extern.slf4j.Slf4j;
+import net.sf.json.JSONArray;
+import net.sf.json.JSONObject;
+import org.jeecg.common.util.DateUtils;
+import org.jeecg.modules.message.websocket.WebSocket;
+import org.jeecg.modules.webaccess.service.WebAccessService;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.springframework.beans.factory.annotation.Autowired;
+
+@Slf4j
+public class WAMsgJob implements Job {
+
+    @Autowired
+    private WebAccessService webAccessService;
+
+    @Autowired
+    private WebSocket webSocket;
+
+    @Override
+    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+
+        log.info(String.format(" 获取设备实时值! 时间:" + DateUtils.getTimestamp()));
+
+        JSONArray list = new JSONArray();
+
+        // 设备实时值
+        JSONObject tagObject = webAccessService.getDashboardTagNameValues();
+        tagObject.put("Name", "设备实时值");
+        list.add(tagObject);
+
+        // 设备历史值
+        JSONObject logObject = webAccessService.getDashboardDataLog();
+        logObject.put("Name", "设备历史值");
+        list.add(logObject);
+
+        webSocket.pushMessageByObject(list);
+    }
+}

+ 5 - 2
module-guan/src/main/java/org/jeecg/modules/webaccess/mapper/xml/GuanDashboardParamMapper.xml

@@ -8,8 +8,11 @@
         <result property="tagName"    column="tag_name"    />
         <result property="status"    column="status"    />
         <result property="iflog"    column="iflog"    />
-        <result property="lognum"    column="lognum"    />
-        <result property="logduration"    column="logduration"    />
+        <result property="records"    column="records"    />
+        <result property="duration"    column="duration"    />
+        <result property="intervals"    column="intervals"    />
+        <result property="intervaltype"    column="intervaltype"    />
+        <result property="datatype"    column="datatype"    />
         <result property="createTime"    column="create_time"    />
         <result property="createBy"    column="create_by"    />
         <result property="updateTime"    column="update_time"    />

+ 62 - 21
module-guan/src/main/java/org/jeecg/modules/webaccess/service/WebAccessService.java

@@ -15,6 +15,7 @@ import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpMethod;
 import org.springframework.http.MediaType;
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 
@@ -28,8 +29,8 @@ import java.util.Map;
  */
 @Service
 public class WebAccessService {
-    @Autowired
-    private RestTemplate restTemplate;
+//    @Autowired
+//    private RestTemplate restTemplate;
 
     @Autowired
     private ISysDictItemService sysDictItemService;
@@ -263,6 +264,14 @@ public class WebAccessService {
         headers.setContentType(type);
         headers.add("Authorization", "Basic " + base64Creds);
         HttpEntity<String> formEntity;
+
+        // 配置http请求的连接超时时间和读取超时时间,原来的注入的方法,容易超时
+        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory ();
+        factory.setConnectTimeout(60 * 1000);
+        factory.setReadTimeout(5 * 60 * 1000);
+        factory.setConnectionRequestTimeout(5 * 60 * 1000);
+        RestTemplate restTemplate = new RestTemplate(factory);
+
         JSONObject result;
         if (jsonObject == null) {
             formEntity = new HttpEntity<String>(null, headers);
@@ -318,9 +327,7 @@ public class WebAccessService {
 
         return rtn;
     }
-    //endregion
 
-    //region <<获取历史记录>>
     /**
      *
      * 生成获取历史记录的参数
@@ -340,37 +347,30 @@ public class WebAccessService {
      * @param Interval 每笔数据之间隔时间,以 IntervalType 为单位。
      * @param Records 欲查询之每个点的数据笔数。
      * @param DataType 数据型态,0 - Last,1-Min,2 - Max,3 - Avg。
+     * @param TagName 点位名。
      * @return
      */
-    public JSONObject generateDataLogObject(String StartTime, String IntervalType, Integer Interval, Integer Records, String DataType) {
+    public JSONObject generateDataLogObject(String StartTime, String IntervalType, Integer Interval,
+                                            Integer Records, String DataType, String TagName) {
         JSONArray list = new JSONArray();
         JSONObject rtn = new JSONObject();
 
-        // 获取大屏参数设置
-        GuanDashboardParam paramat = new GuanDashboardParam();
-        paramat.setStatus("1"); // 状态为启用
-        paramat.setIflog("Y"); // 需要获取历史记录
-        List<GuanDashboardParam> dashboardParamList = dashboardParamMapper.selectAllDashboardParam(paramat);
-
         rtn.put("StartTime", StartTime);
         rtn.put("IntervalType", IntervalType);
         rtn.put("Interval", Interval);
         rtn.put("Records", Records);
-        if (dashboardParamList != null && dashboardParamList.size() > 0) {
-            for (GuanDashboardParam param : dashboardParamList) {
-                JSONObject sub = new JSONObject();
-                sub.put("Name", param.getTagName());
-                sub.put("DataType", DataType);
-                list.add(sub);
-            }
-        }
-        rtn.put("Tags", list);
 
+        JSONObject sub = new JSONObject();
+        sub.put("Name", TagName);
+        sub.put("DataType", DataType);
+
+        list.add(sub);
+        rtn.put("Tags", list);
         return rtn;
     }
     //endregion
 
-    //region <<根据大屏参数设置,获取点位实时值>>
+    //region <<根据大屏参数设置,获取点位值>>
     /**
      * 取得测点(Tag)的量测值
      *
@@ -400,5 +400,46 @@ public class WebAccessService {
         JSONObject jsonObject = getTagNameValuesNoAnalysis(waparams);
         return jsonObject;
     }
+
+    /**
+     * 取得历史记录
+     *
+     * @return
+     */
+    public JSONObject getDashboardDataLog() {
+        // 获取大屏参数设置
+        GuanDashboardParam paramat = new GuanDashboardParam();
+        paramat.setStatus("1"); // 状态为启用
+        paramat.setIflog("Y"); // 需要获取历史记录
+        List<GuanDashboardParam> dashboardParamList = dashboardParamMapper.selectAllDashboardParam(paramat);
+
+        // 组合参数信息
+        JSONArray list = new JSONArray();
+        if (dashboardParamList != null && dashboardParamList.size() > 0) {
+            for (GuanDashboardParam param : dashboardParamList) {
+                if (param.getTagName() != null && !"".equals(param.getTagName())) {
+                    JSONObject jo = generateDataLogObject("2023-12-26 21:00:00",
+                            param.getIntervaltype() == null || "".equals(param.getIntervaltype()) ? "M" : param.getIntervaltype(),
+                            param.getIntervals() == null ? 1 : param.getIntervals(),
+                            param.getRecords() == null ? 1000 : param.getRecords(),
+                            param.getDatatype() == null || "".equals(param.getDatatype()) ? "0" : param.getDatatype(),
+                            param.getTagName());
+
+                    // 获取信息
+                    JSONObject dataLog = GetDataLog(jo);
+                    JSONObject logObj = new JSONObject();
+                    logObj.put("Name", param.getParamName());
+                    logObj.put("Values", dataLog.getJSONArray("DataLog").getJSONObject(0).getJSONArray("Values"));
+                    list.add(logObj);
+                }
+            }
+        }
+        else {
+            return null;
+        }
+        JSONObject rtn = new JSONObject();
+        rtn.put("历史记录", list);
+        return rtn;
+    }
     //endregion
 }