package com.dev.bmp7100;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.InetSocketAddress;
|
import java.net.Socket;
|
import java.util.ArrayList;
|
|
import com.battdata_rt.BattData_RT;
|
import com.battdata_rt.MonVolData;
|
|
public class bpm7100_SocketClient extends Thread{
|
|
private static final short MB_READ_REG_COUNT = 29;
|
private static final int BPM7100_Socket_Port = 23;
|
|
private String m_FBSDev_IP = "127.0.0.1";
|
//private BPM7100_State m_BPM7100_State;
|
private Socket socket = null;
|
private InputStream mInputStream = null;
|
private OutputStream mOutputStream = null;
|
|
private bpm7100_CommData m_CommDataOut = new bpm7100_CommData();
|
private bpm7100_CommData m_CommDataIn = new bpm7100_CommData();
|
|
private BattData_RT[] m_RTData = new BattData_RT[bpm7100_CommData.BATT_GROUP_COUNT_MAX];
|
private BPM7100_State[] m_devState = new BPM7100_State[bpm7100_CommData.BATT_GROUP_COUNT_MAX];
|
private int m_BattMonCount = 0;
|
private boolean mCommErrDataClearEn = false;
|
|
public bpm7100_SocketClient(boolean comm_err_data_clear_en, String dev_ip,
|
ArrayList<BPM7100_State> al_stat, ArrayList<BattData_RT> al_rt_data)
|
{
|
mCommErrDataClearEn = comm_err_data_clear_en;
|
m_FBSDev_IP = dev_ip;
|
|
int group_max = bpm7100_CommData.BATT_GROUP_COUNT_MAX;
|
for(int n=0; n<group_max; n++) {
|
m_RTData[n] = null;
|
m_devState[n] = null;
|
}
|
|
for(int n=0; n<al_rt_data.size(); n++)
|
{
|
BattData_RT brt = al_rt_data.get(n);
|
if(true == m_FBSDev_IP.equals(brt.FBSDeviceIp))
|
{
|
System.out.println("bpm7100_dev ip: " + m_FBSDev_IP + ", id: " + brt.FBSDeviceId);
|
int dev_index = (brt.FBSDeviceId%100) % group_max;
|
m_RTData[dev_index] = brt;
|
m_BattMonCount += brt.MonCount;
|
|
m_devState[dev_index] = al_stat.get(n);
|
}
|
}
|
|
if(m_BattMonCount > (bpm7100_CommData.BATT_MON_COUNT_MAX-120))
|
{
|
m_BattMonCount = (bpm7100_CommData.BATT_MON_COUNT_MAX-120);
|
}
|
}
|
|
private void clearInputStream() throws InterruptedException, IOException
|
{
|
for(int n=0; n<10; n++)
|
{
|
Thread.sleep(10);
|
while(mInputStream.available() > 0)
|
{
|
mInputStream.read();
|
}
|
}
|
}
|
|
private boolean read_data(InputStream is, byte[] buf, final int rx_cnt)
|
throws InterruptedException, IOException
|
{
|
int rx_sum = rx_cnt;
|
if(rx_sum > buf.length)
|
rx_sum = buf.length;
|
|
int rx_count = 0;
|
|
boolean comm_ok = true;
|
for(rx_count=0; rx_count<rx_sum; rx_count++)
|
{
|
int time_out_count = 0;
|
while(is.available() < 1)
|
{
|
Thread.sleep(10);
|
time_out_count++;
|
if(time_out_count > 300)
|
{
|
comm_ok = false;
|
break;
|
}
|
}
|
|
if(false == comm_ok)
|
break;
|
|
buf[rx_count] = (byte) is.read();
|
}
|
|
return comm_ok;
|
}
|
|
private boolean readDataFromRs485Dev() {
|
boolean res = false;
|
try
|
{
|
res = true;
|
for(int cnt=0; cnt<bpm7100_CommData.BATT_GROUP_COUNT_MAX; cnt++)
|
{
|
if((null == m_RTData[cnt]) || (null == m_devState[cnt])) {
|
continue;
|
}
|
|
BattData_RT brt = m_RTData[cnt];
|
BPM7100_State bstat = m_devState[cnt];
|
//--------------------------------------------------------//
|
byte rs485_addr = (byte) (brt.FBSDeviceId % 100);
|
byte cmd_t = bpm7100_CommData.MB_CMD_READ;
|
short reg_count = MB_READ_REG_COUNT;
|
short rx_datacount = (short) (reg_count*2 + 5);
|
//--------------------------------------------------------//
|
/**********************************************************/
|
/**********************************************************/
|
m_CommDataOut.makeDataToByteArray(rs485_addr, cmd_t, bpm7100_CommData.REGADDR_START, reg_count);
|
boolean comm_res = false;
|
m_CommDataIn.rs485_addr = rs485_addr;
|
for(int com_cnt=0; com_cnt<3; com_cnt++)
|
{
|
mOutputStream.write(m_CommDataOut.data_byte_tx);
|
mOutputStream.flush();
|
|
if(true == read_data(mInputStream, m_CommDataIn.data_byte_rx, rx_datacount))
|
{
|
if(true == m_CommDataIn.getDataFromByteArray(rx_datacount)) {
|
comm_res = true;
|
} else {
|
comm_res = false;
|
}
|
} else {
|
comm_res = false;
|
}
|
//--------------------------------------------------------//
|
clearInputStream();
|
//--------------------------------------------------------//
|
if(true == comm_res) {
|
break;
|
}
|
|
Thread.sleep(100);
|
}
|
/**********************************************************/
|
if(true == comm_res) {
|
bstat.setComCountInc();
|
} else {
|
bstat.setComErrCountInc();
|
if(true == mCommErrDataClearEn) {
|
m_CommDataIn.clearData();
|
} else {
|
m_CommDataIn.rs485_addr = 0;
|
}
|
}
|
/**********************************************************/
|
getBPM7100MonData(m_CommDataIn);
|
/**********************************************************/
|
if(bstat.com_err_count > 10) {
|
res = false;
|
break;
|
}
|
//--------------------------------------------------------//
|
Thread.sleep(100);
|
//--------------------------------------------------------//
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
res = false;
|
}
|
|
return res;
|
}
|
|
public void run()
|
{
|
while (true)
|
{
|
try
|
{
|
socket = new Socket();
|
socket.connect(new InetSocketAddress(m_FBSDev_IP, BPM7100_Socket_Port), 3000);
|
|
mInputStream = socket.getInputStream();
|
mOutputStream = socket.getOutputStream();
|
|
while(true)
|
{
|
if(false == readDataFromRs485Dev()) {
|
break;
|
}
|
}
|
} catch (Exception e) {
|
for(int cnt=0; cnt<bpm7100_CommData.BATT_GROUP_COUNT_MAX; cnt++)
|
{
|
if(null != m_devState[cnt]) {
|
m_devState[cnt].setComErrCountInc();
|
}
|
}
|
} finally {
|
try {
|
if(null != mOutputStream) {
|
mOutputStream.close();
|
}
|
if(null != mInputStream) {
|
mInputStream.close();
|
}
|
if(null != socket) {
|
socket.close();
|
}
|
|
Thread.sleep(1000);
|
} catch (InterruptedException | IOException e1) {
|
//e1.printStackTrace();
|
}
|
}
|
}
|
}
|
|
private void getBPM7100MonData(bpm7100_CommData c_data)
|
{
|
int dev_index = c_data.rs485_addr;
|
|
if((dev_index > 0) && (dev_index < bpm7100_CommData.BATT_GROUP_COUNT_MAX)) {
|
if(null != m_RTData[dev_index]) {
|
m_RTData[dev_index].mTestData.updateCurrFrom_FBSDev(c_data.battcurr);
|
m_RTData[dev_index].mTestData.updateGroupVolFrom_FBSDev(c_data.groupvol);
|
m_RTData[dev_index].mTestData.groupTmp = c_data.grouptmp;
|
|
for(int n=0; n<m_RTData[dev_index].MonCount; n++)
|
{
|
if(n >= bpm7100_CommData.BATT_COUNT_PER_DEV)
|
break;
|
|
MonVolData tmp_m_data = m_RTData[dev_index].al_MonVol.get(n);
|
tmp_m_data.monVol = c_data.battvol[n];
|
tmp_m_data.monTmp = c_data.grouptmp;
|
tmp_m_data.monRes = 0;
|
tmp_m_data.connRes = 0;
|
tmp_m_data.monSer = 0;
|
}
|
}
|
}
|
}
|
}
|