whychdw
2019-12-03 3a95c5d85ca0405a35abb416c2304fe78d37abb4
platforms/android/app/src/main/java/com/socket/CommSockClientThread.java
@@ -21,7 +21,9 @@
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * 底层和汇集器通讯线程
@@ -42,6 +44,7 @@
    private boolean thread_run_flag = true;
    private Gson gson = new Gson();
    private ServiceModel remodel = new ServiceModel();
    byte[] rx_buf_t = new byte[1024];
    private CommSockClientThread(){
        this.concentratorState = new Concentrator_State();
@@ -77,7 +80,13 @@
            public void handleMessage(Message msg)
            {
                ServiceModel model = (ServiceModel) (msg.obj);
                //Log.e(TAG, "handleMessage: " + model);
//                int what = msg.what;
//                List<BattDataThread> battData = FBS9600S_DeviceService.allBattDatt;
//                if(model.battindex>0 && model.battindex<FBS9600S_DeviceService.MAXCONCENTRATORCOUNT){
//                    if(!battData.get(model.battindex-1).isInstall){
//                        return;
//                    }
//                }
                try
                {
                    if(null != model.mhandler)
@@ -97,7 +106,7 @@
                                //
                                remodel.data = concentratorParam;
                            }
                            else if(remodel.cmd >= 0x201B && remodel.cmd <= 0x239E)
                            else if(remodel.cmd >= Concentrator_ComBase.CMD_MODEBUS_BATTMONVOL_INDEX && remodel.cmd <= 0x239E)
                            {
                                remodel.data = concentratorState;
                            }
@@ -108,7 +117,7 @@
                        Message toMain = handler.obtainMessage();
                        toMain.obj = remodel;
                        handler.sendMessage(toMain);
                        sleep(200);
                        sleep(5);
                    }
                }
                catch (InterruptedException e)
@@ -142,17 +151,14 @@
                InputStream in = this.socket.getInputStream();
                OutputStream out = this.socket.getOutputStream();
                //----------------- clear rx buff for tcp resend packet ----------------//
                byte[] rx_buf_t = new byte[1024];
                //----------------------------------------------------------------------//
                bytebuffer_for_socket_RX.order(ByteOrder.BIG_ENDIAN);
                bytebuffer_for_socket_RX.clear();
                //--------------------- socket write -----------------------------------//
                //--------------------- socket write -----------------------------------//
                boolean aes_en = true;
                byte[] plain_tx_t = datasource;
                byte[] cipher_tx_t = new byte[plain_tx_t.length];
                Date d1 = new Date();
                //=====================================================================//
              /*
              for(int n=0; n<3; n++) {
@@ -180,7 +186,7 @@
              }
              //=====================================================================//
              */
                Log.e(TAG, "发送数据:"+ComFn.bytesToHexString(plain_tx_t,plain_tx_t.length));
                //Log.e(TAG, "发送数据:"+ComFn.bytesToHexString(plain_tx_t,plain_tx_t.length));
                int rx_read_time_out = 0;
                //int rx_len = 0;
                out.write(plain_tx_t);
@@ -200,7 +206,7 @@
                            //   res = true;
                            break;
                        }
                        if(rx_read_time_out > 40) {
                        if(rx_read_time_out > 30) {
                            break;
                        }
                    }
@@ -209,11 +215,6 @@
                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
@@ -228,7 +229,7 @@
                bytebuffer_for_socket_RX.get(cipher_buf);
                Log.e(TAG, "接收数据: "+ ComFn.bytesToHexString(cipher_buf, cipher_buf.length));
                //Log.e(TAG, "接收数据: "+ ComFn.bytesToHexString(cipher_buf, cipher_buf.length));
                if(true == getDataFromCommBuf(cipher_buf,remodel)) {
                    rx_errcount_for_live_of_thread = 0;
                    res_t = true;
@@ -353,7 +354,7 @@
        ){
            Concentrator_Cmd Ccmd = new Concentrator_Cmd();
            Ccmd.makeCmd(model.battindex,model.cmd_type,model.cmd);
            ByteBuffer buffer = null;
            ByteBuffer buffer = ByteBuffer.allocate(0);
            if(Concentrator_ComBase.CMD_MODEBUS_SYSTEMPARAM_INDEX == model.cmd){
                //读写汇集器参数
                if(Concentrator_ComBase.CMD_MODEBUS_READCMD == model.cmd_type){
@@ -369,9 +370,7 @@
                //读取系统状态
                buffer = Ccmd.getReadByteBuffer(Concentrator_State.SYS_REG_COUNT);
            }
            if(Concentrator_ComBase.CMD_MODEBUS_BATTMONVOL_INDEX == model.cmd ||
                Concentrator_ComBase.CMD_MODEBUS_BATTMONTMP_INDEX == model.cmd ||
                Concentrator_ComBase.CMD_MODEBUS_BATTMONRES_INDEX == model.cmd ){
            if(Concentrator_ComBase.CMD_MODEBUS_BATTMONVOL_INDEX <= model.cmd &&model.cmd < 0x0239E ){
                //读取单体数据(电压/温度/内阻)
                buffer = Ccmd.getReadByteBuffer(Concentrator_State.MON_REG_COUNT);
            }