package com.dev.btse.data;
|
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.Socket;
|
import java.net.UnknownHostException;
|
import java.nio.ByteBuffer;
|
import java.nio.ByteOrder;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
import com.battmonitor.base.Com;
|
import com.battmonitor.base.ComBase;
|
import com.battmonitor.sql.MysqlConnPool;
|
|
public class TmpSensor_SocketThread extends Thread{
|
private static final int autoUpdateTime = 4;
|
private static final int maxTmpSensorCount = 32;
|
|
private List<TmpSensorState> tmpSensors = new ArrayList<TmpSensorState>(); //±äËÍÆ÷ÖеÄËùÓд«¸ÐÆ÷״̬
|
|
private Socket m_socket;
|
private MysqlConnPool pool;
|
private boolean hadreg_flag = false;
|
private TmpSensorState tmpState = new TmpSensorState();
|
private TmpSensor_inf tmpinf;
|
|
private TmpSensor_Cmd m_TMP_Cmd = new TmpSensor_Cmd();
|
private boolean S_thread_run_flag = true;
|
private int rx_errcount_for_live_of_thread = 0; //ÓÃÓÚͳ¼ÆsocketͨÐÅÖеÄÒì³£´ÎÊýͳ¼Æ
|
private ByteBuffer bytebuffer_for_socket_RX = ByteBuffer.allocate(1500);
|
|
public TmpSensor_SocketThread(Socket m_socket,MysqlConnPool pool) {
|
this.m_socket = m_socket;
|
this.pool = pool;
|
for(int i=0;i<maxTmpSensorCount;i++) {
|
tmpSensors.add(new TmpSensorState());
|
}
|
}
|
|
public void initDev_addr() {
|
try {
|
for(int i = 0;i<5;i++) {
|
if(!hadreg_flag) {
|
SocketComm(TmpSensor_ComBase.CMD_READDATAFROMMODBUS,TmpSensor_ComBase.CMD_DEVICEID_ADDR,createSingleBuffer(1) , this.m_socket);
|
|
Thread.sleep(200);
|
rx_errcount_for_live_of_thread = 0;
|
|
}else {
|
System.out.println("»ñÈ¡É豸id³É¹¦"+tmpState.sensor_dev_id+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
|
break;
|
}
|
System.out.println(i+""+tmpState.getSensor_dev_id());
|
}
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
|
@Override
|
public void run() {
|
System.out.println("TmpSensor_SocketThread start at "+Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
|
|
initDev_addr(); //ʶ±ðµ±Ç°É豸ÊÇ·ñΪζȴ«¸ÐÆ÷
|
|
TmpSensor_inf tmpinf = TmpSensor_SQL.queryTmpSensorById(pool, tmpState.getSensor_dev_id());
|
|
int cnt = 1;
|
while(S_thread_run_flag && tmpinf != null) {
|
try {
|
if(cnt%7 == 0) {
|
/************************ »ñÈ¡´«¸ÐÆ÷ʵʱµÄ״̬ÐÅÏ¢Êý¾Ý *******************************************************/
|
SocketComm(TmpSensor_ComBase.CMD_READDATAFROMMODBUS,TmpSensor_ComBase.CMD_GETTMPSENSORSTATE,tmpState.getReadBuffer() , this.m_socket);
|
}
|
cnt ++;
|
sleep(autoUpdateTime * 1000);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
|
/**
|
* ÏòÖ¸¶¨µÄsocketͨµÀ·¢ËÍÊý¾Ý£¬ÒÔ¼°½ÓÊÕÊý¾Ý
|
* @param cmd
|
* @param bf
|
* @return
|
* @throws InterruptedException
|
*/
|
|
public boolean SocketComm(int cmd,int reg_index, ByteBuffer bf, Socket socket) throws InterruptedException
|
{
|
boolean res_t = false;
|
try {
|
socket.setSoTimeout(3000);
|
if(S_thread_run_flag) {
|
InputStream in = socket.getInputStream();
|
OutputStream out = socket.getOutputStream();
|
//----------------- clear rx buff for tcp resend packet ----------------//
|
byte[] rx_buf_t = new byte[1024];
|
/*
|
int buf_clr_read_count = 0;
|
while(in.available() > 0) {
|
in.read(rx_buf_t);
|
if(buf_clr_read_count >= 10) {
|
S_thread_run_flag = false;
|
System.err.println(this.getName()
|
+ " dev_id: " + m_StatAndParam.dev_id
|
+ " socket rx_buff clear error......");
|
res_t = false;
|
return res_t;
|
}
|
}*/
|
//----------------------------------------------------------------------//
|
//--------------------- socket write -----------------------------------//
|
byte[] cipher_tx_t = null;
|
Ecb_Aes my_aes = new Ecb_Aes();
|
if(reg_index == FBS9100_ComBase.CMD_ReadGPRSCSQ) {
|
cipher_tx_t = new byte[bf.limit()];
|
bf.get(cipher_tx_t);
|
} else {
|
cipher_tx_t = makeCommBuf(cmd,reg_index, bf, false);
|
|
}
|
//System.err.println(ComFn.bytesToHexString(cipher_tx_t, cipher_tx_t.length));
|
//=====================================================================//
|
Date d1 = new Date();
|
int rx_read_time_out = 0;
|
out.write(cipher_tx_t);
|
out.flush();
|
|
bytebuffer_for_socket_RX.order(ByteOrder.BIG_ENDIAN);
|
bytebuffer_for_socket_RX.clear();
|
while(true) {
|
if(in.available() > 0) {
|
rx_read_time_out = 0;
|
int rx_len_t = in.read(rx_buf_t);
|
if((bytebuffer_for_socket_RX.position()+rx_len_t)
|
< (bytebuffer_for_socket_RX.capacity()-1)) {
|
bytebuffer_for_socket_RX.put(rx_buf_t, 0, rx_len_t);
|
}
|
} else {
|
rx_read_time_out += 1;
|
if((bytebuffer_for_socket_RX.position()>=7) && (rx_read_time_out>10)){
|
res_t = true;
|
break;
|
}
|
|
if(rx_read_time_out > 220) {
|
break;
|
}
|
}
|
Thread.sleep(10);
|
}
|
|
bytebuffer_for_socket_RX.flip();
|
|
Date d2 = new Date();
|
long comm_tms = (d2.getTime() - d1.getTime());
|
if(comm_tms < 200) {
|
Thread.sleep(200 - comm_tms);
|
}
|
/*
|
System.out.println(this.getName()
|
+ " dev_id:" + m_StatAndParam.dev_id
|
+ " cmd:" + cmd
|
+ " d2-d1:" + (comm_tms)+"ms"
|
+ " rx_len:" + bytebuffer_for_socket_RX.limit()
|
//+ " tx_count:" + m_StatAndParam.m_FBS_VCData.m_SysState.CommCount
|
//+ " rx_err_sum:" + m_StatAndParam.m_FBS_VCData.m_SysState.ErrCommCount
|
//+ " rx_err_count:" + rx_errcount_for_live_of_thread
|
+ Com.getNowTimeWithAt());
|
*/
|
byte[] cipher_buf = new byte[bytebuffer_for_socket_RX.limit()];
|
byte[] plain_buf = new byte[bytebuffer_for_socket_RX.limit()];
|
bytebuffer_for_socket_RX.get(plain_buf);
|
//System.out.println(ComFn.bytesToHexString(plain_buf, plain_buf.length));
|
if(cmd == FBS9100_ComBase.CMD_ReadGPRSCSQ) {
|
if(bytebuffer_for_socket_RX.limit() > 2) {
|
rx_errcount_for_live_of_thread = 0;
|
res_t = true;
|
|
//m_StatAndParam.m_GPRS_inf = new String(cipher_buf);
|
//System.out.println("this.getName() + " + " DevId: " + m_StatAndParam.dev_id
|
// + " GPRS RF Level:" + m_StatAndParam.m_GPRS_CSQ);
|
}
|
} else {
|
m_TMP_Cmd.RES_Index = reg_index;
|
if((cipher_buf.length >= 7) && (true == getDataFromCommBuf(plain_buf))) {
|
tmpState.setDev_commcount();
|
rx_errcount_for_live_of_thread = 0;
|
res_t = true;
|
} else {
|
res_t = false;
|
tmpState.setDev_errcommcount();
|
rx_errcount_for_live_of_thread++;
|
//m_StatAndParam.m_SysState.setErrCommCountInc(bytebuffer_for_socket_RX.limit());
|
}
|
}
|
} else {
|
S_thread_run_flag = false;
|
tmpState.setDev_errcommcount();
|
}
|
} catch (IOException e) {
|
//e.printStackTrace();
|
tmpState.setDev_errcommcount();
|
S_thread_run_flag = false;
|
} finally {
|
//ÈôÔÚͨÐŵĹý³ÌÖгöÏÖÁ¬Ðø5´ÎͨÐÅÒì³£¹Ø±ÕÁ¬½Ó
|
if(rx_errcount_for_live_of_thread >= 5){
|
S_thread_run_flag = false;
|
}
|
}
|
return res_t;
|
}
|
|
private boolean getDataFromCommBuf(byte[] bytes) {
|
boolean isSuccess = false;
|
ByteBuffer bf = ByteBuffer.allocate(bytes.length);
|
bf.order(ByteOrder.BIG_ENDIAN);
|
bf.put(bytes);
|
bf.flip();
|
if(true == m_TMP_Cmd.putByteBuffer(bf)) {
|
//System.out.println(m_TMP_Cmd);
|
if(TmpSensor_ComBase.CMD_GETTMPSENSORSTATE == m_TMP_Cmd.RES_Index) {
|
/*********************** »ñÈ¡´«¸ÐÆ÷µÄʵʱ״̬ÐÅÏ¢ *****************************************************/
|
if(true == tmpState.putByteBuffer(bf)) {
|
System.out.println(tmpState);
|
isSuccess = true;
|
TmpSensor_SQL.insertOrUpdateTmpSensorState(pool, tmpState); //¸üÐÂÊý¾Ý¿âÖеĴ«¸ÐÆ÷״̬
|
}
|
}else if(TmpSensor_ComBase.CMD_DEVICEID_ADDR == m_TMP_Cmd.RES_Index) {
|
/************************ »ñÈ¡´«¸ÐÆ÷É豸µÄid *******************************************************/
|
hadreg_flag = true;
|
int addr_id = ComBase.changeShortToInt(bf.getShort());
|
tmpState.createTmpDevid(addr_id);
|
}
|
}else {
|
System.err.println(1111);
|
}
|
return isSuccess;
|
}
|
|
/*********************************************************************************************/
|
/*
|
* ¹¹Ôì·¢ËÍÊý¾Ý
|
*/
|
public byte[] makeCommBuf(final int cmd,int reg_index, ByteBuffer bf, boolean aes_en)
|
{
|
ByteBuffer bbf = TmpSensor_ComBuf.makeTmpSensorCommBuf(0x01, cmd,reg_index, bf, aes_en);
|
byte byte_rest[] = new byte[bbf.limit()];
|
bbf.get(byte_rest);
|
return byte_rest;
|
}
|
/*********************************************************************************************/
|
|
|
public ByteBuffer createSingleBuffer(int regcount) {
|
ByteBuffer tmp = ByteBuffer.allocate(2);
|
tmp.order(ByteOrder.BIG_ENDIAN);
|
tmp.putShort(ComBase.changeIntToShort(regcount));
|
tmp.flip();
|
return tmp;
|
}
|
|
public static void main(String[] args) {
|
Socket socket;
|
try {
|
|
MysqlConnPool pool = new MysqlConnPool("127.0.0.1", 3360,100);
|
socket = new Socket("118.89.139.230",8919);
|
TmpSensor_SocketThread thread = new TmpSensor_SocketThread(socket,pool);
|
thread.start();
|
} catch (UnknownHostException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|