From 07df3edd01d77d77d2653a6a9ee325e53e607955 Mon Sep 17 00:00:00 2001
From: bsw215583320 <baoshiwei121@163.com>
Date: 星期一, 04 十二月 2023 08:31:09 +0800
Subject: [PATCH] 增加opc设备维护和控制

---
 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java |  126 +++++++++++++++++++++++++++++++++--------
 1 files changed, 101 insertions(+), 25 deletions(-)

diff --git a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java
index 1c5b65d..cb98cfc 100644
--- a/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java
+++ b/jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java
@@ -5,24 +5,39 @@
 import com.alibaba.fastjson.TypeReference;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.mina.core.service.IoAcceptor;
+import org.apache.mina.core.service.IoHandler;
+import org.apache.mina.core.session.IoSession;
+import org.jeecg.common.api.CommonAPI;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.config.TenantContext;
+import org.jeecg.common.config.mqtoken.UserTokenContext;
+import org.jeecg.common.constant.CommonConstant;
+import org.jeecg.common.system.util.JwtUtil;
 import org.jeecg.common.util.RedisUtil;
+import org.jeecg.common.util.SpringContextUtils;
+import org.jeecg.modules.dry.api.JeecgSystemApi;
 import org.jeecg.modules.dry.common.CacheConstants;
 import org.jeecg.modules.dry.entity.*;
 import org.jeecg.modules.dry.service.*;
-import org.jeecg.modules.dry.vo.DryOrderTrendVo;
-import org.jeecg.modules.dry.vo.DryOrderVo;
-import org.jeecg.modules.dry.vo.RealTimeDataVo;
+import org.jeecg.modules.dry.socket.ServerHandler;
+import org.jeecg.modules.dry.socket.SocketServerConfig;
+import org.jeecg.modules.dry.util.DryUtil;
+import org.jeecg.modules.dry.vo.*;
 import org.jetbrains.annotations.NotNull;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+import java.net.Socket;
 import java.text.DecimalFormat;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+
 @Slf4j
 @Service
 public class DryRealTimeDataServiceImpl implements IDryRealTimeDataService {
@@ -43,11 +58,32 @@
     @Autowired
     private IDryProdRecordService prodRecordService;
 
+    @Autowired
+    private CommonAPI commonAPI;
+
+    private String token;
+
+
+
+    public String getTemporaryToken() {
+        if (token == null) {
+            RedisUtil redisUtil = SpringContextUtils.getBean(RedisUtil.class);
+            // 妯℃嫙鐧诲綍鐢熸垚Token
+            token = JwtUtil.sign("admin", "b668043e3ea4bc2d");
+            // 璁剧疆Token缂撳瓨鏈夋晥鏃堕棿涓� 5 鍒嗛挓
+            redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
+            redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, 5 * 60 * 1000);
+        }
+
+        return token;
+    }
+
     @Override
     @Transactional
     public Result<?> realTimeDataHandle(RealTimeDataVo realTimeDataVo) {
         TenantContext.setTenant(realTimeDataVo.getTenantid()+"");
         log.info("瀹炴椂鏁版嵁锛�"+realTimeDataVo.toString());
+
 
         // 1 鏌ヨ鎴栧垱寤哄伐鍗�
         // 1.1 浠巖edis鍙栧嚭宸ュ崟缂撳瓨
@@ -88,6 +124,13 @@
         orderVo.setState_windbox(realTimeDataVo.getState_windbox());
         orderVo.setState_valve(realTimeDataVo.getState_valve());
         orderVo.setOrderStatus(realTimeDataVo.getWorkorder_status());
+        orderVo.setEqp_status(realTimeDataVo.getEqp_status());
+        orderVo.setEqp_state(realTimeDataVo.getEqp_state());
+        orderVo.setWarning(realTimeDataVo.getEqp_warning());
+        orderVo.setFault(realTimeDataVo.getEqp_fault());
+        orderVo.setLevel(realTimeDataVo.getLevel());
+
+
         DryOrderTrendVo trendVo = new DryOrderTrendVo(realTimeDataVo);
         // 2.2 淇濆瓨宸ュ崟鍚按鐜囧彉鍖� 鎴� 閲嶉噺鍙樺寲
 
@@ -275,6 +318,7 @@
 
 
         LambdaQueryWrapper<DryEquipment> queryWrapper = new LambdaQueryWrapper<>();
+
         queryWrapper.eq(DryEquipment::getTenantId, realTimeDataVo.getTenantid());
         queryWrapper.eq(DryEquipment::getEnable, "Y");
 
@@ -283,32 +327,64 @@
 
         List<Double> dList = new ArrayList<>();
         DryOrderVo orderVo = (DryOrderVo) redisUtil.hget(CacheConstants.RedisKeyEnum.WORK_ORDER.getCode(), realTimeDataVo.getTenantid() + "_" + realTimeDataVo.getMachineid());
-try {
+            try {
 
 
-    if (dryEquipments != null && dryEquipments.size() > 0) {
-        dryEquipments.stream().forEach(item -> {
-            DryOrderVo order = (DryOrderVo) redisUtil.hget(CacheConstants.RedisKeyEnum.WORK_ORDER.getCode(), realTimeDataVo.getTenantid() + "_" + item.getCode());
-            if (order != null) {
-                double v = order.getOriginWeight() - order.getYield();
-                list.add(item.getName().substring(0, item.getName().indexOf('#')+1));
-                if (v > 0) {
-                    DecimalFormat df = new DecimalFormat("#.00");
-                    dList.add(Double.valueOf(df.format(v / order.getDryTime() * 60)));
-                } else {
-                    dList.add(50d);
+                if (dryEquipments != null && dryEquipments.size() > 0) {
+                    dryEquipments.stream().forEach(item -> {
+                        DryOrderVo order = (DryOrderVo) redisUtil.hget(CacheConstants.RedisKeyEnum.WORK_ORDER.getCode(), realTimeDataVo.getTenantid() + "_" + item.getCode());
+                        if (order != null) {
+                            double v = order.getOriginWeight() - order.getYield();
+                            list.add(item.getName().substring(0, item.getName().indexOf('#')+1));
+                            if (v > 0 && order.getDryTime()>0) {
+                                DecimalFormat df = new DecimalFormat("#.00");
+                                dList.add(Double.valueOf(df.format(v / order.getDryTime() * 60)));
+                            } else {
+                                dList.add(50d);
+                            }
+
+                        }
+                    });
                 }
+                if (orderVo != null) {
+                    orderVo.setCompEqpNum(list);
+                    orderVo.setCompEqpEffic(dList);
+                }
+            }catch (Exception e) {
+                e.printStackTrace();
+            }
+        return Result.ok(orderVo);
+    }
+
+
+    @Override
+    public Result<?> sendSocketMsg(CommandMessageVo msgVo) {
+
+
+        DryEquipment dryEquipment = equipmentService.selectByTenantIdEquipmentId(msgVo.getTenantId() + "", msgVo.getMachineId());
+        log.info("鑾峰彇璁惧锛�" + dryEquipment.toString());
+
+       // managedSessions.keySet().forEach(addr -> {
+           // ObjectOutputStream oos = null;
+            try {
+//                Socket socket = SocketServerConfig.clientMap.get(addr);
+                IoSession session = ServerHandler.clientSocket.get(dryEquipment.getIp());
+                if (session == null) {
+                    return Result.error("鏈幏鍙栧埌session,璇锋鏌ュ鎴风閰嶇疆鎴栬澶噄p閰嶇疆鏄惁姝e父");
+                }
+                SocketMsgVo smv = new SocketMsgVo(msgVo);
+                session.write(JSONObject.toJSONString(smv));
+//                oos = new ObjectOutputStream(socket.getOutputStream());
+//                String s = JSONObject.toJSONString(new SocketMsgVo(msgVo));
+//                oos.writeUTF(s);
+//                oos.flush();
+
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            } finally {
 
             }
-        });
-    }
-    if (orderVo != null) {
-        orderVo.setCompEqpNum(list);
-        orderVo.setCompEqpEffic(dList);
-    }
-}catch (Exception e) {
-    e.printStackTrace();
-}
-        return Result.ok(orderVo);
+     //   });
+        return Result.OK();
     }
 }

--
Gitblit v1.9.3