package com.dev.idce8200;
|
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.ServerSocket;
|
import java.net.Socket;
|
import java.nio.ByteBuffer;
|
import java.nio.ByteOrder;
|
import java.util.Date;
|
|
import com.base.Com;
|
import com.base.Crc16;
|
import com.battdata_rt.BattData_RT;
|
import com.battdata_rt.BattData_RT_Array;
|
|
public class Socket_Server_IDCE8200 extends Thread {
|
ServerSocket server = null;
|
Socket sk = null;
|
|
BattData_RT_Array m_Data;
|
|
public Socket_Server_IDCE8200(BattData_RT_Array mdata, int port)
|
{
|
m_Data = mdata;
|
try
|
{
|
server = new ServerSocket(port);
|
}
|
catch (IOException e)
|
{
|
e.printStackTrace();
|
}
|
}
|
|
public void run()
|
{
|
System.out.println(this.getName()
|
+ " - Socket_Server_IDCE8200 Started, Listenning For IDCE8200 At PortNum "
|
+ server.getLocalPort());
|
|
IDCE8200_MonitPro_Thread MonitPro = new IDCE8200_MonitPro_Thread(m_Data);
|
MonitPro.start();
|
|
while (true) {
|
try {
|
sk = server.accept();
|
ServerThread th = new ServerThread(sk);
|
th.start();
|
byte[] ip = sk.getInetAddress().getAddress();
|
System.out.println( this.getName() + " - Socket_Server_IDCE8200: "
|
+ String.format("New IDCE8200 Client Connection, IP: %d.%d.%d.%d - ",
|
ip[0]&0xFF,ip[1]&0xFF,ip[2]&0xFF,ip[3]&0xFF)
|
+ Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms) );
|
sleep(50);
|
} catch (Exception e) {
|
//e.printStackTrace();
|
System.err.println(e.getMessage());
|
try {
|
sleep(5000);
|
} catch (InterruptedException e1) {
|
//e1.printStackTrace();
|
}
|
}
|
}
|
}
|
|
class ServerThread extends Thread
|
{
|
Socket sk = null;
|
private InputStream mInputStream = null;
|
private OutputStream mOutputStream = null;
|
|
BattData_RT targetData = null;
|
|
public ServerThread(Socket sk)
|
{
|
this.sk = sk;
|
|
try {
|
this.sk.setSoTimeout(10000);
|
mInputStream = sk.getInputStream();
|
mOutputStream = sk.getOutputStream();
|
//FBI_Ip = this.sk.getInetAddress().getAddress();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
private void close()
|
{
|
try {
|
sk.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
/*
|
private boolean read_data(InputStream is, byte[] buf) throws InterruptedException, IOException
|
{
|
final int 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 > 1000)
|
{
|
comm_ok = false;
|
break;
|
}
|
}
|
|
if(false == comm_ok)
|
break;
|
|
buf[rx_count] = (byte) is.read();
|
if(rx_count == 1)
|
{
|
if(((byte)0xAA != buf[0]) || ((byte)0xAA != buf[1]))
|
{
|
rx_count = 0;
|
continue;
|
}
|
}
|
}
|
|
if(rx_count > 4)
|
comm_ok = true;
|
|
return comm_ok;
|
}
|
*/
|
public void getData(byte[] bf) throws IOException
|
{
|
mInputStream.read(bf);
|
}
|
|
public void run()
|
{
|
try {
|
CommData data_in = new CommData();
|
CommData data_out = new CommData();
|
|
byte[] Station_Ip;
|
|
while(true)
|
{
|
/*****************************************************************************/
|
/*
|
if(false == read_data(mInputStream, data_in.data_byte))
|
break;
|
*/
|
//-----------------------------------------------------//
|
getData(data_in.data_byte);
|
//-----------------------------------------------------//
|
|
if(false == data_in.getDataFromByteArray())
|
{
|
continue;
|
}
|
if((0xAA != (data_in.syn_bytes[0]&0xFF)) || (0xAA != (data_in.syn_bytes[1]&0xFF)))
|
{
|
continue;
|
}
|
/*****************************************************************************/
|
Station_Ip = data_in.ipAddr;
|
if(null == targetData)
|
{
|
for(int index=0; index<m_Data.getItemCount(); index++)
|
{
|
BattData_RT tmp_targetData = m_Data.getItem(index);
|
if((Station_Ip[0] == tmp_targetData.StationIp[0])
|
&& (Station_Ip[1] == tmp_targetData.StationIp[1])
|
&& (Station_Ip[2] == tmp_targetData.StationIp[2])
|
&& (Station_Ip[3] == tmp_targetData.StationIp[3])
|
&& (data_in.battGroupNum == tmp_targetData.BattGroupNum)
|
&& (false == tmp_targetData.isMonDataUseForIdce8200()))
|
{
|
targetData = tmp_targetData;
|
targetData.setMonDataUseForIdce8200(true);
|
|
System.out.println(String.format("IDCE8200 get data ip is %d.%d.%d.%d",
|
Station_Ip[0]&0xFF, Station_Ip[1]&0xFF, Station_Ip[2]&0xFF, Station_Ip[3]&0xFF));
|
break;
|
}
|
}
|
}
|
/*****************************************************************************/
|
if(null != targetData)
|
{
|
for(int n=0; n<targetData.MonCount; n++)
|
{
|
targetData.updateMonVolFromIDCE8200(n, ((float)data_in.battData[n])/1000);
|
}
|
}
|
/*****************************************************************************/
|
/*************************** socket output ***********************************/
|
data_out.makeDataToByteArray();
|
mOutputStream.write(data_out.data_byte);
|
mOutputStream.flush();
|
}
|
} catch (IOException e) {
|
System.out.println(this.getName()
|
+ " - Socket_Server_IDCE8200: "
|
+ e.getMessage() + " - "
|
+ Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
|
} finally {
|
if(null != targetData) {
|
targetData.setMonDataUseForIdce8200(false);
|
}
|
close();
|
System.out.println(this.getName()
|
+ " - Socket_Server_IDCE8200: Comm Timeout And Break Down - "
|
+ Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
|
}
|
}
|
}
|
|
public class CommData {
|
final static public int MonCountMax = 300;
|
/*****************************************************/
|
byte[] syn_bytes = new byte[2];
|
short modbus_type;
|
short byte_count;
|
byte tag;
|
byte cmd;
|
byte modbus_readcount;
|
short battGroupNum;
|
byte[] ipAddr = new byte[4];
|
short[] battData = new short[480];
|
short crc = 0;
|
/*****************************************************/
|
short mon_count = 0;
|
/*****************************************************/
|
byte[] data_byte = new byte[1024];
|
ByteBuffer data_bf = ByteBuffer.allocate(2048);
|
/*****************************************************/
|
public CommData()
|
{
|
syn_bytes[0] = (byte) 0xAA;
|
syn_bytes[1] = (byte) 0xAA;
|
data_bf.order(ByteOrder.BIG_ENDIAN);
|
}
|
/*****************************************************/
|
|
void copyDataByte(byte[] databyte)
|
{
|
for(int n=0; n<data_byte.length; n++)
|
data_byte[n] = databyte[n];
|
|
getDataFromByteArray();
|
}
|
|
void makeDataToByteArray()
|
{
|
data_bf.clear();
|
|
for(int n=0; n<syn_bytes.length; n++)
|
data_bf.put(syn_bytes[n]);
|
|
data_bf.putShort(modbus_type);
|
data_bf.putShort(byte_count);
|
data_bf.put(tag);
|
data_bf.put(cmd);
|
data_bf.put(modbus_readcount);
|
|
for(int n=0; n<ipAddr.length; n++)
|
data_bf.put(ipAddr[n]);
|
|
data_bf.putShort(battGroupNum);
|
|
data_bf.putShort(crc);
|
|
data_bf.position(0);
|
data_bf.get(data_byte);
|
|
crc = (short) Crc16.CalCRC16(data_byte, data_byte.length-2);
|
data_byte[data_byte.length-2] = (byte)(crc);
|
data_byte[data_byte.length-1] = (byte)(crc>>8);
|
}
|
/*****************************************************/
|
boolean getDataFromByteArray()
|
{
|
boolean ck_crc_res = false;
|
|
data_bf.clear();
|
data_bf.put(data_byte);
|
data_bf.flip();
|
|
for(int n=0; n<syn_bytes.length; n++)
|
syn_bytes[n] = data_bf.get();
|
|
modbus_type = (short) (data_bf.getShort()&0xFFFF);
|
byte_count = (short) (data_bf.getShort()&0xFFFF);
|
tag = data_bf.get();
|
cmd = data_bf.get();
|
modbus_readcount = data_bf.get();
|
|
for(int n=0; n<ipAddr.length; n++)
|
ipAddr[n] = data_bf.get();
|
|
battGroupNum = (short) (data_bf.getShort()&0xFFFF);
|
|
for(int n=0; n<MonCountMax; n++)
|
{
|
battData[n] = (short) (data_bf.getShort()&0xFFFF);
|
}
|
/*
|
if((short)Crc16.CalCRC16(data_byte, data_byte.length-2) == crc)
|
{
|
ck_crc_res = true;
|
}
|
*/
|
ck_crc_res = true;
|
return ck_crc_res;
|
}
|
/*****************************************************/
|
}
|
}
|