充放电一体机FGCD通信程序【二期初版】
Administrator
2021-08-13 938850765bf02b67336140b329ad8fc97382d8a4
BattMonitor_FGCD-A059NT/src/com/dev/fgcd/comm/FGCD_SocketClient_Thread.java
@@ -34,6 +34,7 @@
import com.dev.fgcd.data.FGCD_ComBuf;
import com.dev.fgcd.data.FGCD_StateAndParam;
import com.dev.fgcd.data.FGCD_SysState;
import com.dev.fgcd.data.FGCD_TestFBOFile;
import com.dev.fgcd.data.FGCD_VCData;
public class FGCD_SocketClient_Thread extends Thread{
@@ -47,6 +48,8 @@
    private FGCD_BattTestParam testParam;           //电池测试参数
    private FGCD_SysState sysState;                //ϵͳ״̬
    private FGCD_Cmd m_FBS_Cmd = new FGCD_Cmd();
    public FGCD_TestFBOFile fboFile;            //历史数据文件
    private ByteBuffer bytebuffer_for_socket_RX = ByteBuffer.allocate(1500);
    public boolean isRuning = false;
@@ -167,12 +170,16 @@
             }
          }
          
          fboFile = new FGCD_TestFBOFile(m_StatAndParam.dev_id);
          sysState.dev_data_flowsum += FGCD_SocketClient_Thread_SQL.getFbs9100DataCommFlowSum_ById(m_ConnPool, m_StatAndParam);
          
          
          FGCD_SocketClient_Thread_SQL.insertOrUpdateFbs9100SetParamBydev_id(m_ConnPool,m_StatAndParam.dev_id);
          FGCD_SocketClient_Thread_SQL.insertFBS9100StateTableOnRam(m_ConnPool, m_StatAndParam.dev_id);
          FGCD_SocketClient_Thread_SQL.insertFBS9100_SysParamDefaultData(m_ConnPool, m_StatAndParam);
          FGCD_SocketClient_Thread_SQL.insertTb_Fgcd_Filedownload_Table(m_ConnPool, m_StatAndParam.dev_id);
          
          BattData_RT_SQL.checkTbFbs9100StateIntegrity(m_ConnPool, m_StatAndParam.dev_id);
          
@@ -245,13 +252,29 @@
               }
               
            }
            int op_cmd = FGCD_SocketClient_Thread_SQL.queryClearAlarmState(m_ConnPool, m_StatAndParam);
            if(FGCD_ComBase.CMD_ClearSysAlarm == op_cmd) {
               //清除设备告警
               SocketComm(FGCD_ComBase.CMD_CLEARALARM, ByteBuffer.allocate(0));
            /************************    清除告警      *****************************************************/
            {
               int op_cmd = FGCD_SocketClient_Thread_SQL.queryClearAlarmState(m_ConnPool, m_StatAndParam);
               if(FGCD_ComBase.CMD_ClearSysAlarm == op_cmd) {
                  //清除设备告警
                  SocketComm(FGCD_ComBase.CMD_CLEARALARM, ByteBuffer.allocate(0));
               }
            }
            Thread.sleep(200);
            /************************    文件下载      *****************************************************/
            {
               FGCD_SocketClient_Thread_SQL.queryTb_Fgcd_Filedownload_Table(m_ConnPool, fboFile);
               if(FGCD_ComBase.CMD_ReadFBDFile == fboFile.op_cmd) {
                  System.out.println("读取FBO文件");
                  //读取文件列表
                  SocketComm(fboFile.createCmd(FGCD_ComBase.CMD_GETFILELIST), ByteBuffer.allocate(0));
               }else if(FGCD_ComBase.CMD_DownLoadFBDFile == fboFile.op_cmd) {
                  //开始下载文件
               }
            }
            if(DevReadCount %10 == 0) {
               //logger.info("获取单体数据");
               SocketComm(FGCD_ComBase.CMD_GETBATTDATA, ByteBuffer.allocate(0));
@@ -299,6 +322,108 @@
                //--------------------- socket write -----------------------------------//
                boolean aes_en = false;
                byte[] plain_tx_t = makeCommBuf(cmd, byteBuffer, aes_en);
                Date d1 = new Date();
                int rx_read_time_out = 0;
                //int rx_len = 0;
                //System.out.println("数据长度" + plain_tx_t.length + "\t发送数据:" + ComFn.bytesToHexString(plain_tx_t, plain_tx_t.length));
                out.write(plain_tx_t);
                out.flush();
                sysState.makeDevCommDataFlowSum(plain_tx_t.length);
                while(true) {
                    if(in.available() > 0) {
                        rx_read_time_out = 0;
                        int rx_len_t = in.read(rx_buf_t);
                        if((bytebuffer_for_socket_RX.position()+rx_len_t)
                                < (bytebuffer_for_socket_RX.capacity()-1)) {
                            bytebuffer_for_socket_RX.put(rx_buf_t, 0, rx_len_t);
                        }
                        //bytebuffer_for_socket_RX.put((byte)in.read());
                    } else {
                        rx_read_time_out++;
                        if((bytebuffer_for_socket_RX.position() >= 20) && (rx_read_time_out>10)){
                            //   res = true;
                            break;
                        }
                        if(rx_read_time_out > 300) {
                            break;
                        }
                    }
                    Thread.sleep(10);
                }
                bytebuffer_for_socket_RX.flip();
                Date d2 = new Date();
                long comm_tms = (d2.getTime() - d1.getTime());
                if(comm_tms < 200) {
                    Thread.sleep(200 - comm_tms);
                }
              /*
              System.out.println(this.getName()
                             + "  dev_id: " + m_StatAndParam.dev_id
                             + "  d2-d1:" + (comm_tms)+"ms"
                             + "  rx_len:" + bytebuffer_for_socket_RX.limit()
                             + "  tx_count:" + m_FBS_VCData.m_SysState.CommCount
                             + "  rx_err_sum:" + m_FBS_VCData.m_SysState.ErrCommCount
                             + "  rx_err_count:" + rx_errcount_for_live_of_thread
                             + Com.getNowTimeWithAt());
              */
                byte[] cipher_buf = new byte[bytebuffer_for_socket_RX.limit()];
                bytebuffer_for_socket_RX.get(cipher_buf);
                sysState.makeDevCommDataFlowSum(cipher_buf.length);
                //System.err.println("数据长度"+cipher_buf.length+"\t返回数据:"+ ComFn.bytesToHexString(cipher_buf, cipher_buf.length));
                //Log.e(TAG, "SocketComm: "+ComFn.bytesToHexString(plain_buf, plain_buf.length));
                if(true == getDataFromCommBuf(cipher_buf)) {
                   res_t = true;
                   errcount = 0;
                   sysState.addCommCount();
                } else {
                   errcount ++;
                   res_t = false;
                   sysState.addErrorCount();
                }
            } else {
                //System.out.println(this.getName() + "-------socket  异常关闭-------" + Com.getNowTimeWithAt());
               FGCD_Thread_Mark = false;
            }
        } catch (IOException | InterruptedException e) {
           errcount ++;
        } finally {
            if(errcount > 5){
               FGCD_Thread_Mark = false;
            }
        }
        return res_t;
    }
    /**
     *    向指定的socket通道发送数据,以及接收数据
     * @return
     */
    public boolean SocketComm(FGCD_Cmd cmd,ByteBuffer byteBuffer)
    {
        boolean res_t = false;
        InputStream in = null;
        OutputStream out = null;
        try
        {
            if(null != this.socket && !this.socket.isClosed())
            {
                in = this.socket.getInputStream();
                out = this.socket.getOutputStream();
                //----------------- clear rx buff for tcp resend packet ----------------//
                byte[] rx_buf_t = new byte[1024];
                //----------------------------------------------------------------------//
                bytebuffer_for_socket_RX.order(ByteOrder.LITTLE_ENDIAN);
                bytebuffer_for_socket_RX.clear();
                //--------------------- socket write -----------------------------------//
                //--------------------- socket write -----------------------------------//
                boolean aes_en = false;
                byte[] plain_tx_t = makeCommBuf(cmd, byteBuffer);
                Date d1 = new Date();
                int rx_read_time_out = 0;
                //int rx_len = 0;
@@ -504,6 +629,14 @@
    public byte[] makeCommBuf(final int cmd, ByteBuffer bf, boolean aes_en)
    {
        ByteBuffer bbf = FGCD_ComBuf.makeFbs9100CommBuf(0xFF, cmd, bf);
        byte byte_rest[] = new byte[bbf.limit()];
        bbf.get(byte_rest);
        return byte_rest;
    }
    public byte[] makeCommBuf(FGCD_Cmd cmd, ByteBuffer bf)
    {
        ByteBuffer bbf = FGCD_ComBuf.makeFbs9100CommBuf(cmd, bf);
        byte byte_rest[] = new byte[bbf.limit()];
        bbf.get(byte_rest);
        return byte_rest;
@@ -748,6 +881,15 @@
                    isSuccess = true;
                    //System.out.println("停止活化测试成功");
                }
            }else if(FGCD_ComBase.CMD_GETFILELIST == m_FBS_Cmd.CMD) {
               //获取文件列表
               if(fboFile.putByteBuffer(bf,m_FBS_Cmd.Db2)) {
                  //读取文件列表成功
                  fboFile.op_cmd = FGCD_ComBase.CMD_ReadFBDFile_ACK;
                  System.out.println("读取文件列表成功");
                  FGCD_SocketClient_Thread_SQL.updateTb_Fgcd_Filedownload_Table(m_ConnPool, fboFile);
                  isSuccess = true;
               }
            }
        }
        return isSuccess;