package com.donghuan.c_interface;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.Socket;
|
import java.util.ArrayList;
|
|
import com.battdata_rt.BattData_RT;
|
import com.battdata_rt.BattData_RT_Array;
|
import com.battdata_rt.BattStatData;
|
import com.config.AppConfig;
|
|
public class CInterface_SocketClient extends Thread{
|
|
private final static int CommErrorCountMax = 50;
|
|
private Socket sk = null;
|
private InputStream mInputStream = null;
|
private OutputStream mOutputStream = null;
|
private AppConfig cfg = null;
|
|
private BattData_RT_Array al_RtData = null;
|
|
private ArrayList<BattData_RT> al_TestingBattGroups = null;
|
private CInterfaceState m_CInterfaceStat;
|
private int mOnTestingBattGroupIndex = 0;
|
|
private int mErrorCount = 0;
|
|
public CInterface_SocketClient(AppConfig cfg_t, BattData_RT_Array al_rt_data, CInterfaceState m_c_state)
|
{
|
cfg = cfg_t;
|
al_RtData = al_rt_data;
|
|
al_TestingBattGroups = new ArrayList<BattData_RT>();
|
m_CInterfaceStat = m_c_state;
|
}
|
|
private boolean initSocketClient(String svr_ip, int port)
|
{
|
boolean res = true;
|
try
|
{
|
sk = new Socket(svr_ip, port);
|
sk.setSoTimeout(4000);
|
mInputStream = sk.getInputStream();
|
mOutputStream = sk.getOutputStream();
|
}
|
catch (Exception e)
|
{
|
res = false;
|
e.printStackTrace();
|
try {
|
sleep(500);
|
} catch (InterruptedException e1) {
|
// TODO Auto-generated catch block
|
e1.printStackTrace();
|
}
|
}
|
|
return res;
|
}
|
|
private void clearInputStream() throws InterruptedException, IOException
|
{
|
for(int n=0; n<2; n++)
|
{
|
Thread.sleep(10);
|
while(mInputStream.available() > 0)
|
{
|
mInputStream.read();
|
}
|
}
|
}
|
|
private boolean loginToServer(String usrname, String pwd)
|
{
|
CInterface_PkComm data = new CInterface_PkComm();
|
|
byte[] data_out = null;
|
byte[] data_in = new byte[256];
|
|
int error_count = 0;
|
//int txdata_count = 0;
|
|
while(true)
|
{
|
try
|
{
|
data_out = data.makeLoginPkToByteArray(usrname, pwd);
|
mOutputStream.write(data_out);
|
mOutputStream.flush();
|
/*
|
{
|
System.out.println(this.getName() + ": LoginToServer");
|
|
String str = data_out.length + " data: ";
|
for(int n=0; n<data_out.length; n++)
|
{
|
str += Integer.toHexString(data_out[n]&0xFF);
|
}
|
System.out.println(str);
|
}
|
*/
|
|
int len = mInputStream.read(data_in);
|
clearInputStream();
|
if (len > 1)
|
{
|
/*
|
String str = "LoginToServer rx data: ";
|
for(int n=0; n<len; n++)
|
{
|
str += Integer.toHexString(data_in[n]&0xFF) + " ";
|
}
|
|
System.out.println(str);
|
*/
|
return true;
|
}
|
|
error_count = 0;
|
Thread.sleep(1000);
|
} catch (Exception e) {
|
//e.printStackTrace();
|
error_count++;
|
} finally {
|
if(error_count >= 5)
|
{
|
m_CInterfaceStat.setAlmErrCountInc();
|
return false;
|
}
|
}
|
}
|
}
|
|
private void readDataFromServer(BattData_RT batt_rtdata)
|
{
|
CInterface_PkComm data = new CInterface_PkComm();
|
CInterface_PkData pk_data = new CInterface_PkData();
|
|
ArrayList<ArrayList<Integer>> al_BattCId = batt_rtdata.al_MonVol_C_Id;
|
|
byte[] data_out = null;
|
byte[] data_in = new byte[8192];
|
|
m_CInterfaceStat.setTxCountInc(batt_rtdata.BattGroupId);
|
|
for(int q=0; q<al_BattCId.size(); q++)
|
{
|
boolean res = true;
|
for(int n=0; n<3; n++)
|
{
|
try {
|
ArrayList<Integer> al_mon_c_id = al_BattCId.get(q);
|
data_out = data.makeBattDataQueryPkToByteArray(batt_rtdata.BattGroupId, al_mon_c_id);
|
mOutputStream.write(data_out);
|
mOutputStream.flush();
|
|
int len = mInputStream.read(data_in);
|
clearInputStream();
|
if (len > 1)
|
{
|
pk_data.getBattDataFromAckPk(data_in, batt_rtdata);
|
mErrorCount = 0;
|
}
|
m_CInterfaceStat.clearErrCount(batt_rtdata.BattGroupId);
|
Thread.sleep(50);
|
} catch (Exception e) {
|
res = false;
|
}
|
|
if(true == res) {
|
break;
|
} else {
|
try {
|
Thread.sleep(200);
|
} catch (InterruptedException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
}
|
}
|
|
if(false == res) {
|
m_CInterfaceStat.setErrCountInc(batt_rtdata.BattGroupId);
|
batt_rtdata.setMonDataFromCInterfaceOk(false);
|
if(mErrorCount < (CommErrorCountMax+2))
|
{
|
mErrorCount++;
|
}
|
}
|
}
|
//System.out.println("m_CInterfaceStat.clearErrCount: " + batt_rtdata.BattGroupId);
|
//m_CInterfaceStat.clearErrCount(batt_rtdata.BattGroupId);
|
//m_CInterfaceStat.setErrCountInc(batt_rtdata.BattGroupId);
|
}
|
|
private void closeSocketClient()
|
{
|
try {
|
mInputStream.close();
|
mOutputStream.close();
|
sk.close();
|
} catch (IOException e1) {
|
// TODO Auto-generated catch block
|
//e1.printStackTrace();
|
}
|
}
|
|
private BattData_RT getOnTestingBattGroups()
|
{
|
al_TestingBattGroups.clear();
|
|
BattData_RT rt = null;
|
for(int n=0; n<al_RtData.getItemCount(); n++)
|
{
|
rt = al_RtData.getItem(n);
|
if((BattStatData.BATTSTATE_CHARGE == rt.getBattTestType())
|
|| (BattStatData.BATTSTATE_DISCHARGE == rt.getBattTestType()))
|
{
|
al_TestingBattGroups.add(rt);
|
}
|
}
|
|
rt = null;
|
if(mOnTestingBattGroupIndex >= al_TestingBattGroups.size())
|
{
|
mOnTestingBattGroupIndex = 0;
|
}
|
|
if(mOnTestingBattGroupIndex < al_TestingBattGroups.size())
|
{
|
rt = al_TestingBattGroups.get(mOnTestingBattGroupIndex);
|
mOnTestingBattGroupIndex += 1;
|
}
|
|
return rt;
|
}
|
|
public void run()
|
{
|
/**********************************************************************************/
|
System.out.println(this.getName() + " - CInterface_SocketClient Started ...");
|
/**********************************************************************************/
|
|
while (true)
|
{
|
if(false == initSocketClient(cfg.getCInterfaceServerIp(), cfg.getCInterfacePort()))
|
{
|
continue;
|
}
|
|
if(false == loginToServer(cfg.getCInterfaceUsrName(), cfg.getCInterfacePWD()))
|
{
|
continue;
|
}
|
|
while(true)
|
{
|
for(int n=0; n<al_RtData.getItemCount(); n++)
|
{
|
try {
|
Thread.sleep(100);
|
} catch (InterruptedException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
readDataFromServer(al_RtData.getItem(n));
|
|
BattData_RT testing_rt = getOnTestingBattGroups();
|
if(null != testing_rt)
|
{
|
readDataFromServer(testing_rt);
|
}
|
|
m_CInterfaceStat.setBaseProcessValue(n+1, al_RtData.getItemCount());
|
|
if(mErrorCount >= CommErrorCountMax)
|
{
|
break;
|
}
|
}
|
|
if(mErrorCount >= CommErrorCountMax)
|
{
|
mErrorCount = 0;
|
closeSocketClient();
|
break;
|
}
|
}
|
}
|
}
|
}
|