| package com.dy.rtuMw3rd.tcp4Bjnl; | 
|   | 
| import com.alibaba.fastjson2.JSON; | 
| import com.dy.common.springUtil.SpringContextUtil; | 
| import com.dy.rtuMw3rd.tcp4Bjnl.deal.BjnlDealer; | 
| import com.dy.rtuMw3rd.tcp4Bjnl.protocol.BjnlProtocol; | 
| import com.dy.rtuMw3rd.tcp4Bjnl.protocol.Result; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.mina.core.buffer.IoBuffer; | 
| import org.apache.mina.core.session.IoSession; | 
| import org.apache.mina.filter.codec.CumulativeProtocolDecoder; | 
| import org.apache.mina.filter.codec.ProtocolDecoderOutput; | 
|   | 
| import java.io.IOException; | 
|   | 
| /** | 
|  * @Author: liurunyu | 
|  * @Date: 2025/03/18 14:20 | 
|  * @Description | 
|  */ | 
| @Slf4j | 
| public class NetDataDecoder extends CumulativeProtocolDecoder { | 
|   | 
|     /** | 
|      * 对网络传输来的数据进行解码 | 
|      */ | 
|     protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws IOException, Exception{ | 
|         int remain = in.remaining() ; | 
|         if(remain > 0){ | 
|             byte[] bs = new byte[remain]; | 
|             in.get(bs) ; | 
|             in.position(0) ; | 
|             String s = new String(bs, "UTF-8") ; | 
|             if(s != null && s.length() > 0){ | 
|                 s = s.trim() ; | 
|             } | 
|             this.receive(s); | 
|         } | 
|         return true; | 
|     } | 
|   | 
|   | 
|     private void receive(String json){ | 
|         log.info("北京农林--收到数据:" + json); | 
|         try{ | 
|             if(JSON.isValid(json)){ | 
|                 Result res = JSON.parseObject(json, Result.class) ; | 
|                 if(res.status != null && res.status.code != null && res.status.code.equals(BjnlProtocol.SuccessCode)){ | 
|                     if(res.status.type != null){ | 
|                         if(res.status.type.equals(BjnlProtocol.ResultTypeAuth)){ | 
|                             forLogin(res) ; | 
|                         }else if(res.status.type.equals(BjnlProtocol.ResultTypeData)){ | 
|                             forData(res) ; | 
|                         }else if(res.status.type.equals(BjnlProtocol.ResultTypeControl)){ | 
|                             forControl(res) ; | 
|                         }else{ | 
|                             log.error("北京农林--服务端返回数据其类型不可识别:" + res.status.type); | 
|                         } | 
|                     } | 
|                 }else{ | 
|                     //可能推送的数据中不包含status子数据 | 
|                     if(res.data != null && res.data.length() > 0){ | 
|                         forData(res) ; | 
|                     }else{ | 
|                         log.error("北京农林--服务端返回失败信息:" + json); | 
|                     } | 
|   | 
|                 } | 
|             }else{ | 
|                 log.error("北京农林--服务端返回数据非有效JSON数据,\n数据为" + json); | 
|             } | 
|         }catch (Exception e){ | 
|             log.error("北京农林--服务端返回数据转JSON异常,\n数据为" + json ,e); | 
|         } | 
|     } | 
|   | 
|     private void forLogin(Result res){ | 
|         Worker.getInstance().setLogined(); | 
|         BjnlDealer d = SpringContextUtil.getBean(BjnlDealer.class) ; | 
|         d.logined(res); | 
|     } | 
|   | 
|     private void forData(Result res){ | 
|         BjnlDealer d = SpringContextUtil.getBean(BjnlDealer.class) ; | 
|         d.receiveData(res); | 
|     } | 
|   | 
|     private void forControl(Result res){ | 
|         BjnlDealer d = SpringContextUtil.getBean(BjnlDealer.class) ; | 
|         d.controlRes(res); | 
|     } | 
| } |