whyclj
2019-07-24 c9f8b9e09489e547734246140b4fd3635100fbb6
app/src/main/java/com/comm/MyWebSocketTestThread.java
@@ -1,7 +1,9 @@
package com.comm;
import com.fbo2206.FBO_ParamDischarge;
import com.fbo2206.FBO_ReadData;
import com.fbs9100.ComFn;
import com.fbs9100.Ecb_Aes;
import com.fbs9100.FBS_CapState;
import com.fbs9100.FBS_ChargeState;
@@ -16,7 +18,6 @@
import com.fbs9100.FBS_ResState;
import com.fbs9100.FBS_VCData;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -25,8 +26,6 @@
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
import io.dcloud.common.util.JSONUtil;
/**
 * Created by LiJun on 2019-04-28.
@@ -45,15 +44,15 @@
    private FBS_CapState m_CapTestState = new FBS_CapState();
    private FBS_ChargeState m_ChrTestState = new FBS_ChargeState();
    public FBO_ParamDischarge fbo_DiscParam = new FBO_ParamDischarge();
    public FBO_ReadData fbo_ReadData = new FBO_ReadData();
    private FBS_ResCapData m_FBS_BaseData = new FBS_ResCapData();               //单体基础数据
    private FBS_ResCapData m_FBS_ResCapData = new FBS_ResCapData();             //单体内阻数据
    private FBS_ResCapData m_FBS_TmpCapData  = new FBS_ResCapData();            //单体温度数据
    private static final String TAG = "SocketClient_Thread";
    public static String dev_path = "/dev/ttyS2";         //设备名称
    public static int baud_rate = 115200;                  //串口波特率
    private ServiceModel revicemodel;
    private ServiceModel returnmodel;
@@ -65,7 +64,6 @@
    private ByteBuffer bytebuffer_for_socket_RX = ByteBuffer.allocate(1500);
    private int rx_errcount_for_live_of_thread = 0 ;      //用于统计socket通信中的异常次数统计
    private boolean thread_run_flag = true;
    public static Socket socket;
@@ -210,22 +208,18 @@
   */
    /**
     * 向指定的socket通道发送数据,以及接收数据
     *    向指定的socket通道发送数据,以及接收数据
     * @return
     * @throws InterruptedException
     */
    public boolean SocketComm() throws InterruptedException
    public boolean SocketComm()
    {
        boolean res_t = false;
        InputStream in = null;
        OutputStream out = null;
        try
        {
            socket = new Socket(server_ip, server_port);
            if(this.socket != null)
@@ -273,10 +267,10 @@
              }
              //=====================================================================//
              */
                //Log.d(TAG, "SocketComm: ");
                int rx_read_time_out = 0;
                //int rx_len = 0;
                out.write(cipher_tx_t);
                System.out.println("数据长度"+plain_tx_t.length+"\t发送数据:"+ComFn.bytesToHexString(plain_tx_t, plain_tx_t.length));
                out.write(plain_tx_t);
                out.flush();
                while(true) {
                    if(in.available() > 0) {
@@ -322,10 +316,9 @@
                bytebuffer_for_socket_RX.get(cipher_buf);
                my_aes.ecb_decrypt(cipher_buf, plain_buf, 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(plain_buf)) {
                    rx_errcount_for_live_of_thread = 0;
                if(true == getDataFromCommBuf(cipher_buf)) {
                    res_t = true;
                 /*
                 System.err.println(ComFn.bytesToHexString(buffer, buffer.length));
@@ -335,7 +328,6 @@
                  */
                    returnmodel.code = 1;
                } else {
                    rx_errcount_for_live_of_thread++;
                    res_t = false;
                    returnmodel.code = 0;
                }
@@ -343,7 +335,7 @@
                //System.out.println(this.getName() + "-------socket  异常关闭-------" + Com.getNowTimeWithAt());
                thread_run_flag = false;
            }
        } catch (IOException e) {
        } catch (IOException | InterruptedException e) {
            thread_run_flag = false;
        } finally {
            if(in != null) {
@@ -428,32 +420,9 @@
//        }
//    }
    /*********************************************************************************************/
    public class ServiceModel
    {
        public int code;
        public int cmd;
        public String msg;
        public Object data;
        public ServiceModel()
        {
            String msg = "";
            int code = 0;
            int cmd = 0;
            Object data = null;
        }
    }
    public void setServiceModel(String json) {
        Gson gson = new Gson();
        revicemodel = gson.fromJson(json, MyWebSocketTestThread.ServiceModel.class);
    }
    public String getServiceModel() {
        Gson gson = new Gson();
        return gson.toJson(returnmodel);
    }
    /*********************************************************************************************/
    public byte[] makeCommBuf(final int cmd, ByteBuffer bf, boolean aes_en)
@@ -474,22 +443,34 @@
        bf.flip();
        String msg = "";
        //Log.d(TAG, "接收数据是: "+ComFn.bytesToHexString(bytes,bytes.length));
        if(true == m_FBS_Cmd.putByteBuffer(bf))
        {
            lastConTime = new Date();
            //------------------- 开始通讯 --------------------
            if(FBS_ComBase.CMD_STARTCNNECT == m_FBS_Cmd.CMD
                    && FBS_ComBase.DataReturn_Successs == m_FBS_Cmd.RES_Index
                    ) {
                returnmodel.msg = "Connect Device Success !!!";
                isSuccess = true;
            }
            //------------------- 读取FBS9100电压电流数据 --------------------
            if(FBS_ComBase.CMD_GetVIData == m_FBS_Cmd.CMD)
            else if(FBS_ComBase.CMD_GetVIData == m_FBS_Cmd.CMD)
            {
                if(true == m_FBS_VCData.m_SysState.putByteBuffer(bf))
                if(true == fbo_ReadData.putByteBuffer(bf)) {
                    System.out.println("读取实时数据成功:"+fbo_ReadData);
                    returnmodel.data = fbo_ReadData;
                    returnmodel.msg = "Get realdata from FBS9100 OK!!!";
                    isSuccess = true;
                }
                /*if(true == m_FBS_VCData.m_SysState.putByteBuffer(bf))
                {
                    if(true == m_FBS_VCData.putByteBuffer(bf)){
                        returnmodel.data = m_FBS_VCData;
                       returnmodel.data = m_FBS_VCData;
                        returnmodel.msg = "Get data from FBS9100 OK!!!";
                        isSuccess = true;
                    }
                }
                }*/
            }
            //------------------- 读取FBS9100内阻测试状态 -----------------------
            if(FBS_ComBase.CMD_GetResTestState == m_FBS_Cmd.CMD)
@@ -557,18 +538,22 @@
            //------------------- 读取FBS9100放电参数 -------------------------
            else if(FBS_ComBase.CMD_GetDischargeParm == m_FBS_Cmd.CMD)
            {
                if(true == m_FBS_DiscParam.putByteBuffer(bf)){
                //System.out.println("获取FBO参数");
                if(true == fbo_DiscParam.putByteBuffer(bf)){
                    System.out.println("读取放电参数成功" + fbo_DiscParam);
                    returnmodel.msg = "Get DischargeParm from FBS9100S OK!!!";
                    returnmodel.data = m_FBS_DiscParam;
                    returnmodel.data = fbo_DiscParam;
                    isSuccess = true;
                }
            }
            //------------------- 设置FBS9100放电参数 -------------------------
            else if(FBS_ComBase.CMD_SetDischargeParm == m_FBS_Cmd.CMD)
            {
                if(true == m_FBS_DiscParam.putByteBuffer(bf)){
                System.out.println("设置放电参数");
                if(FBS_ComBase.DataReturn_Successs == m_FBS_Cmd.RES_Index){
                    System.out.println("设置放电参数成功:" + fbo_DiscParam);
                    returnmodel.msg = "Set DischargeParam to FBS9100 OK!!!";
                    returnmodel.data = m_FBS_DiscParam;
                    returnmodel.data = fbo_DiscParam;
                    isSuccess = true;
                }
            }
@@ -607,15 +592,37 @@
            //-------------------  启动内阻测试启动核容测试成功 ---------------------------------------------
            else if((FBS_ComBase.CMD_StartDischarge == m_FBS_Cmd.CMD)
                    || (FBS_ComBase.CMD_StartResDischarge == m_FBS_Cmd.CMD)
                    &&(FBS_ComBase.DataReturn_Successs == m_FBS_Cmd.RES_Index)
                    ){
                System.out.println("启动放电成功");
                returnmodel.msg = "Start MonomerRESTest or MonomerCapTest from FBS9100 OK!!!";
                isSuccess = true;
            }
            //-------------------  启动充电测试     ---------------------------------------------
            else if(FBS_ComBase.CMD_StartCharge == m_FBS_Cmd.CMD
                    && (FBS_ComBase.DataReturn_Successs == m_FBS_Cmd.RES_Index)
                    ){
                System.out.println("启动充电成功");
                returnmodel.msg = "Start MonomerChargeTest from FBS9100 OK!!!";
                isSuccess = true;
            }
            //-------------------- 停止测试成功 -----------------------------------------
            else if(FBS_ComBase.CMD_Stop == m_FBS_Cmd.CMD){
            else if(FBS_ComBase.CMD_Stop == m_FBS_Cmd.CMD
                    && (FBS_ComBase.DataReturn_Successs == m_FBS_Cmd.RES_Index)
                    ){
                System.out.println("停止成功");
                returnmodel.msg = "Stop MonomerCapTest from FBS9100 OK!!!";
                isSuccess = true;
            }
            //-------------------- 暂停测试成功 -----------------------------------------
            else if(FBS_ComBase.CMD_Suspended == m_FBS_Cmd.CMD
                    && (FBS_ComBase.DataReturn_Successs == m_FBS_Cmd.RES_Index)
                    ){
                System.out.println("暂停成功");
                returnmodel.msg = "Suspended MonomerCapTest from FBS9100 OK!!!";
                isSuccess = true;
            }
        }
        return isSuccess;
    }
@@ -649,16 +656,96 @@
        return m_FBS_TmpCapData;
    }
    /**
     * js前台接收返回数据
     * @return
     */
    public String getServiceModel() {
        Gson gson = new Gson();
        return gson.toJson(returnmodel);
    }
    /**
     * js前台传入的数据
     * @param json
     */
    public void setServiceModel(String json) {
        Gson gson = new Gson();
        revicemodel = gson.fromJson(json, ServiceModel.class);
        sendByteBuffer = ByteBuffer.allocate(0);
        if(revicemodel.cmd == FBS_ComBase.CMD_SetDischargeParm) {
            //设置放电参数时
            FBO_ParamDischarge param = (FBO_ParamDischarge)revicemodel.data;
            sendByteBuffer = param.getByteBuffer();
        }
    }
    public void setFBO2206Param(MyWebSocketTestThread therad) {
        FBO_ParamDischarge param = therad.fbo_DiscParam;
        param.test_mode = 1;            //放电模式  0-离线放电          1在线放电
        param.acstop_op = 1;            //停电处理   0-停止放电         1继续放电
        param.nominal_cap = 300;         //标称容量
        param.hourly_rate = 10 ;         //小时率
        param.preset_cur = 20;            //预放电流      (0.1)
        param.preset_cap = 50;            //预放容量
        param.preset_time = 60;            //预放时间(分钟)
        param.mon_lower = 10.8;            //单体下限      (0.01)
        param.group_lower = 45;            //组端下限      (0.1)
        param.mon_number = 12;            //单体数量
        param.group_number = 4;            //电池组数
        param.lower_number = 5;            //单体下限个数
        param.mon_vol = 12;               //标称单体电压
        param.booster_ceiling = 56;         //升压上限
        param.charge_limit =  20;         //充电电流      (0.1)
        param.onlinevol_lowlimit = 60 ;       //在线阀值      (0.1)
        param.battName = "新建";         //电池组名称
        sendByteBuffer = fbo_DiscParam.getByteBuffer();
    }
    /**
     * 修改服务器的ip和通讯的端口号
     * @param server_ip         服务器IP
     * @param server_port       服务器端口号
     */
    public void setServerParam(String server_ip,int server_port){
        this.server_ip = server_ip;
        this.server_port = server_port;
    }
    public static void main(String[] args) {
        MyWebSocketTestThread thread = new MyWebSocketTestThread();
        ServiceModel model = thread.new ServiceModel();
        model.cmd = FBS_ComBase.CMD_GetVIData;
        MyWebSocketTestThread thread = new MyWebSocketTestThread();
        ServiceModel model = new ServiceModel();
        thread.server_ip = "192.168.1.126";
        thread.server_port = 9600;
        thread.revicemodel = model;
        model.cmd = FBS_ComBase.CMD_STARTCNNECT;            //开始通讯
        thread.SocketComm();
        //thread.setFBO2206Param(thread);
        //model.cmd = FBS_ComBase.CMD_SetDischargeParm;         //设置放电参数
        model.cmd = FBS_ComBase.CMD_GetDischargeParm;         //获取放电参数
        model.cmd = FBS_ComBase.CMD_GetVIData;               //获取电池组状态
        thread.SocketComm();
    }
    /*********************************************************************************************/
}