From 26a2fb7c65f18d7d832b6eb7bf609b2118c1396c Mon Sep 17 00:00:00 2001 From: whyclj <1525436766@qq.com> Date: 星期二, 16 七月 2019 08:58:28 +0800 Subject: [PATCH] 提交后台通讯 --- app/src/main/java/com/comm/MyWebSocketTestThread.java | 190 ++++++++++++++++++++++++++++++++++------------- 1 files changed, 136 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/com/comm/MyWebSocketTestThread.java b/app/src/main/java/com/comm/MyWebSocketTestThread.java index 1c16c20..74b2396 100644 --- a/app/src/main/java/com/comm/MyWebSocketTestThread.java +++ b/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; @@ -213,19 +211,20 @@ /** - * 鍚戞寚瀹氱殑socket閫氶亾鍙戦�佹暟鎹紝浠ュ強鎺ユ敹鏁版嵁 + * 鍚戞寚瀹氱殑socket閫氶亾鍙戦�佹暟鎹紝浠ュ強鎺ユ敹鏁版嵁 + * @param cmd + * @param bf * @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 +272,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 +321,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 +333,6 @@ */ returnmodel.code = 1; } else { - rx_errcount_for_live_of_thread++; res_t = false; returnmodel.code = 0; } @@ -343,7 +340,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 +425,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 +448,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 +543,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 +597,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 +661,86 @@ 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(); + + } 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(); + + } /*********************************************************************************************/ } -- Gitblit v1.9.1