package com.dev.fgcd.comm;
|
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.OutputStream;
|
import java.net.Socket;
|
import java.nio.ByteBuffer;
|
import java.nio.ByteOrder;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.util.ArrayList;
|
import java.util.Date;
|
|
import com.battmonitor.sql.MysqlConnPool;
|
import com.battmonitor.sql.Sql_Mysql;
|
import com.dev.btse.data.ComFn;
|
import com.dev.fgcd.data.FGCD_Cmd;
|
import com.dev.fgcd.data.FGCD_ComBase;
|
import com.dev.fgcd.data.FGCD_ComBuf;
|
import com.dev.fgcd.data.FGCD_Crc16;
|
import com.dev.fgcd.data.FGCD_TestFBOFile;
|
import com.dev.fgcd.data.FGCD_TestFBOFile.FBOFile;
|
import com.dev.fgcd.fbo.FboData;
|
import com.dev.fgcd.fbo.FboDataInf;
|
import com.dev.fgcd.fbo.FboTestTime;
|
|
|
public class FGCD_TestDataDownLoad {
|
|
public static final int DownloadFlag_Fail = -1; //µ¼Èëʧ°Ü
|
public static final int DownloadFlag_Null = 0; //²»ÏÂÔØ
|
public static final int DownloadFlag_Start = 1; //ÐèÒªµ¼Èë
|
public static final int DownloadFlag_Over = 2; //ÏÂÔØÍê³É
|
|
public static final int FialReason_DeFault = 0; //ÔÝÎÞ
|
public static final int FialReason_FIleNotFound = 1; //ÎļþδÕÒµ½
|
public static final int FialReason_ParamError = 2; //²ÎÊý´íÎó
|
public static final int FialReason_NetWorkError = 3; //ÍøÂçÒì³£´íÎó
|
public static final int FialReason_UserCancel = 4; //Óû§È¡Ïû
|
|
|
public static final int DownLoadState_ReadData = 1; //ÏÂÔØÎļþ
|
public static final int DownLoadState_CreateData = 2; //Éú³ÉÎļþ
|
public static final int DownLoadState_ExportData = 3; //µ¼³öÊý¾Ý
|
public static final int DownLoadState_ExportSucc = 4; //µ¼Èë³É¹¦
|
public static final int DownLoadState_ExportFail = 5; //µ¼Èëʧ°Ü
|
public static final int DownLoadState_UserCancel = 6; //Óû§È¡Ïû
|
public static final int DownLoadState_DownLoadCancel = 7; //ÏÂÔØÊ§°Ü
|
|
|
private ByteBuffer bytebuffer_for_socket_RX = ByteBuffer.allocate(1200);
|
|
private FGCD_TestFBOFile fbofile;
|
private FGCD_Cmd m_FBS_Cmd = new FGCD_Cmd();
|
private boolean ReadTestData = true;
|
public byte[] filebyte;
|
|
private MysqlConnPool pool;
|
private Socket socket;
|
private int ErrorCount = 0; //´íÎó¼ÆÊý;Á¬Ðø3´ÎͨÐÅʧ°Ü¼´ÎªÍøÂçÒì³£½áÊø
|
private String fileName;
|
private FBOFile nowFbo;
|
|
|
public FGCD_TestDataDownLoad(FGCD_TestFBOFile fbofile,Socket socket,MysqlConnPool pool) {
|
this.fbofile = fbofile;
|
this.socket = socket;
|
this.pool = pool;
|
|
filebyte = new byte[0];
|
downLoadParamCheck();
|
}
|
|
|
/**
|
* ÏÂÔØµ±Ç°µç³Ø×éÊý¾Ý
|
* @param socket
|
*/
|
public void downLoadTestData() {
|
int now_download_state = DownLoadState_ReadData;
|
fbofile.initDownLoadState();//ÎļþÐÅÏ¢ÏÂÔØ³õʼ»¯
|
for(int i=0;i<fbofile.files.length;i++) {
|
//µ±Ç°ÏÂÔØÎļþ
|
nowFbo = fbofile.files[i];
|
int file_downloadNum=i;//ÉèÖõ±Ç°ÏÂÔØÎļþµÄË÷Òý
|
if(now_download_state == DownLoadState_DownLoadCancel) {
|
fbofile.files[i].setDownload_state(DownLoadState_DownLoadCancel);
|
fbofile.files[i].setDownload_flag(DownloadFlag_Fail);
|
|
FGCD_Task_Thread_SQL.updateFgcd_Filedownload_TableCmdByDevId(pool,fbofile,file_downloadNum);
|
continue;
|
}
|
if(nowFbo.download_flag != DownloadFlag_Start) {
|
continue;
|
}
|
ReadTestData = true;
|
nowFbo.setDownload_state(DownLoadState_ReadData);
|
while(ReadTestData) {
|
try {
|
queryDownLoadState(pool, fbofile);
|
if(fbofile.op_cmd == FGCD_ComBase.CMD_DownLoadFBDFile_Over) {
|
now_download_state = FGCD_ComBase.CMD_DownLoadFBDFile_Over;
|
fbofile.setStop_reason(FialReason_UserCancel);
|
//ÊÖ¶¯ÖÕÖ¹ÏÂÔØÃüÁî
|
fbofile.files[file_downloadNum].setDownload_flag(DownloadFlag_Fail);
|
fbofile.files[file_downloadNum].setDownload_state(DownLoadState_DownLoadCancel);
|
fbofile.op_cmd = FGCD_ComBase.CMD_DownLoadFBDFile_Over_ACK;
|
FGCD_Task_Thread_SQL.updateFgcd_Filedownload_TableCmdByDevId(pool, fbofile,file_downloadNum);
|
continue;
|
}
|
if(nowFbo.getDownload_state() == DownLoadState_ReadData) {
|
//System.out.println("¶ÁÈ¡²âÊÔÎļþÖÐ"+file);
|
SocketComm(FGCD_ComBase.CMD_GETFILE,nowFbo.file_index,nowFbo.now_data_block);
|
if(nowFbo.download_state == DownLoadState_CreateData) {
|
//ÏÂÔØÊýÁ¿
|
nowFbo.setNow_data_block(0);
|
nowFbo.setTotal_data_block(0);
|
}
|
|
updateDownLoadProgressByOne(pool,fbofile,file_downloadNum); //¸üе±Ç°ÏÂÔØ½ø¶È
|
|
}
|
if(nowFbo.getDownload_state() == DownLoadState_CreateData) {
|
//Éú³Éµ±Ç°ÏÂÔØµÄÊý¾ÝÎļþ
|
if(createFBOFile(nowFbo)) {
|
//Éú³ÉFBOÎļþ³É¹¦¹ý
|
//System.err.println("Éú³ÉÎļþ³É¹¦");
|
nowFbo.setDownload_state(DownLoadState_ExportData);
|
}else{
|
//Éú³ÉFBOÎļþʧ°Ü
|
//System.err.println("Éú³ÉÎļþʧ°Ü");
|
nowFbo.setDownload_state(DownLoadState_ExportFail);
|
}
|
}
|
|
if(nowFbo.getDownload_state() == DownLoadState_ExportData) {
|
//½âÎö±¾µØÊý¾ÝÎļþ²¢µ¼ÈëÊý¾Ý¿â
|
/*boolean flag = readFBOFileData(file,pool);
|
if(flag) {
|
nowFbo.setDownload_state(DownLoadState_ExportSucc);
|
}else {
|
nowFbo.setDownload_state(DownLoadState_ExportFail);
|
}*/
|
//¿ªÆôÐÂÏ̵߳¼ÈëÊý¾Ý¿â
|
FGCD_Export_Thread_SQL export_Thread=new FGCD_Export_Thread_SQL(pool, nowFbo.clone(), fbofile.battgroupid,file_downloadNum);
|
new Thread(export_Thread).start();
|
ReadTestData = false;
|
}
|
//System.out.println("nowFbo.getDownload_state():"+nowFbo.getDownload_state());
|
// if(nowFbo.getDownload_state() == DownLoadState_ExportFail
|
// || nowFbo.getDownload_state() == DownLoadState_ExportSucc) {
|
// ReadTestData = false;
|
// }
|
//file.setDownload_state(nowFbo.getDownload_state());
|
//updateDownLoadProgress(pool,fbofile); //¸üе±Ç°ÏÂÔØ½ø¶È
|
Thread.sleep(50);
|
} catch(Exception e) {
|
e.printStackTrace();
|
}
|
}
|
filebyte = new byte[0];
|
/*{
|
if(nowFbo.getDownload_state() == DownLoadState_ExportSucc) {
|
//System.out.println("µ¼³öÀúÊ··ÅµçÊý¾Ý³É¹¦");
|
file.setDownload_flag(DownloadFlag_Over);
|
fbofile.op_cmd = FGCD_ComBase.CMD_DownLoadFBDFile_ACK;
|
}else {
|
//System.out.println("µ¼³öÀúÊ··ÅµçÊý¾Ýʧ°Ü");
|
file.setDownload_flag(DownloadFlag_Fail);
|
fbofile.op_cmd = FGCD_ComBase.CMD_DownLoadFBDFile_Over;
|
}
|
}*/
|
//FGCD_Task_Thread_SQL.updateFBOFIleExportFlag(pool,fbofile);
|
}
|
//FGCD_Task_Thread_SQL.updateFgcd_FiledownloadState_TableByDevId(pool, fbofile);
|
/******************************** ×îºóÉèÖõ±Ç°µ¼³ö״̬ ********************************************/
|
}
|
|
/**
|
* ½âÎöFBOÎļþ
|
* @param file
|
* @param pool
|
*/
|
private boolean readFBOFileData(FBOFile file,MysqlConnPool pool) {
|
boolean flag = false;
|
FboDataInf data_inf = new FboDataInf();
|
ArrayList<FboData> al_fbo_data = new ArrayList<>();
|
File f = new File(file.filename);
|
if(file.test_timelong > 0) {
|
FboData.checkFboFile(f, data_inf, al_fbo_data);
|
|
flag = FGCD_Task_Thread_SQL.insertFBODataIntoTable(pool, file, data_inf, al_fbo_data, fbofile.battgroupid);
|
flag = true;
|
}
|
if(f.exists()) {
|
//ɾ³ýÏÂÔØµÄFBOÎļþ
|
//f.delete();
|
}
|
return flag;
|
}
|
|
|
|
|
|
/**
|
* ÏòÖ¸¶¨µÄsocketͨµÀ·¢ËÍÊý¾Ý½çÃæÍ¬²½ÇëÇó
|
* @param cmd ¶ÁȡָÁî
|
* @param rec_index ¶ÁÈ¡Ë÷Òý
|
* @return
|
*/
|
public boolean SocketComm(int cmd,int file_index,int rec_index)
|
{
|
boolean res_t = false;
|
InputStream in = null;
|
OutputStream out = null;
|
try
|
{
|
if(null != this.socket && !this.socket.isClosed())
|
{
|
in = this.socket.getInputStream();
|
out = this.socket.getOutputStream();
|
//----------------- clear rx buff for tcp resend packet ----------------//
|
byte[] rx_buf_t = new byte[1024];
|
|
//----------------------------------------------------------------------//
|
bytebuffer_for_socket_RX.order(ByteOrder.LITTLE_ENDIAN);
|
bytebuffer_for_socket_RX.clear();
|
//--------------------- socket write -----------------------------------//
|
//--------------------- socket write -----------------------------------//
|
boolean aes_en = false;
|
byte[] plain_tx_t = makeCommBuf(cmd,file_index,rec_index);
|
Date d1 = new Date();
|
int rx_read_time_out = 0;
|
//int rx_len = 0;
|
//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) {
|
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);
|
}
|
//bytebuffer_for_socket_RX.put((byte)in.read());
|
} else {
|
rx_read_time_out++;
|
if((bytebuffer_for_socket_RX.position() >= 20) && (rx_read_time_out>10)){
|
// res = true;
|
break;
|
}
|
if(rx_read_time_out > 300) {
|
break;
|
}
|
}
|
Thread.sleep(10);
|
}
|
Date d2 = new Date();
|
long comm_tms = (d2.getTime() - d1.getTime());
|
if(comm_tms < 200) {
|
Thread.sleep(200 - comm_tms);
|
}
|
|
bytebuffer_for_socket_RX.flip();
|
|
byte[] cipher_buf = new byte[bytebuffer_for_socket_RX.limit()];
|
bytebuffer_for_socket_RX.get(cipher_buf);
|
|
//sysState.makeDevCommDataFlowSum(cipher_buf.length);
|
|
//System.out.println("Êý¾Ý³¤¶È"+cipher_buf.length+"\t·µ»ØÊý¾Ý£º"+ ComFn.bytesToHexString(cipher_buf, cipher_buf.length));
|
if(true == getDataFromCommBuf(cipher_buf)) {
|
res_t = true;
|
ErrorCount = 0;
|
} else {
|
ErrorCount ++;
|
res_t = false;
|
}
|
} else {
|
ReadTestData = false;
|
}
|
} catch (IOException | InterruptedException e) {
|
ErrorCount ++;
|
} finally {
|
if(ErrorCount > 5){
|
ReadTestData = false;
|
}
|
}
|
return res_t;
|
}
|
|
public Boolean getDataFromCommBuf(final byte[] bytes)
|
{
|
boolean isSuccess = false;
|
ByteBuffer bf = ByteBuffer.allocate(bytes.length);
|
bf.order(ByteOrder.LITTLE_ENDIAN);
|
bf.put(bytes);
|
bf.flip();
|
|
if(true == m_FBS_Cmd.putByteBuffer(bf))
|
{
|
//--------------------- ÐÄÌø°ü²âÊÔ ----------------------------------
|
if(FGCD_ComBase.CMD_GETFILE == m_FBS_Cmd.CMD) {
|
if(FGCD_ComBase.RETURN_SUCCESS == m_FBS_Cmd.RecState) {
|
this.nowFbo.total_data_block = m_FBS_Cmd.getTotalDownLoadBlock();
|
//System.out.println("Êý¾Ý°ü×ܳ¤¶È£º"+ this.fbofile.total_data_block+ m_FBS_Cmd);
|
if(m_FBS_Cmd.getNowDownLoadIndex() == nowFbo.now_data_block) {
|
if(putFileData(bf,m_FBS_Cmd.ByteLen-18)) {
|
|
nowFbo.now_data_block ++;
|
isSuccess = true;
|
System.out.println("¶ÁÈ¡µÚ"+m_FBS_Cmd.getNowDownLoadIndex()+"¸öÊý¾Ý°ü³É¹¦");
|
|
if(nowFbo.now_data_block == nowFbo.total_data_block) {
|
//System.out.println("ÏÂÔØÀúÊ·Êý¾ÝÎļþÍê³É");
|
nowFbo.setDownload_state(DownLoadState_CreateData);
|
}
|
}
|
}
|
}
|
}
|
}
|
return isSuccess;
|
}
|
|
public byte[] makeCommBuf(final int cmd,int file_index, int rec_index)
|
{
|
FGCD_Cmd m_cmd = new FGCD_Cmd();
|
m_cmd.CMD = cmd;
|
m_cmd.Type = file_index%256;
|
m_cmd.RecState = file_index/256;
|
m_cmd.Alarm = rec_index%256;
|
m_cmd.WorkState = rec_index/256;
|
|
ByteBuffer bbf = FGCD_ComBuf.makeFGCDTestDataCommBuf(m_cmd);
|
byte byte_rest[] = new byte[bbf.limit()];
|
bbf.get(byte_rest);
|
return byte_rest;
|
}
|
|
/**
|
*
|
* @param bf
|
* @param len µ±Ç°buffµÄÓÐЧ³¤¶È
|
* @return
|
*/
|
public boolean putFileData(ByteBuffer bf,int len) {
|
if(bf.limit() < 2) {
|
return false;
|
}
|
//System.out.println("ÓÐЧ³¤¶È:"+len+"\t"+ComFn.bytesToHexString(bf.array(), len));
|
ByteBuffer tmpbuf = bf;
|
tmpbuf.position(0);
|
int crc0 = tmpbuf.getShort(len-2) & 0xFFFF;
|
//Ö»¼ÆËãǰ1024¸ö×Ö½ÚµÄCRCÊý¾Ý
|
int crc1 = FGCD_Crc16.CalCRC16(tmpbuf, len-2);
|
if(crc0 != crc1) {
|
System.err.println("FGCD_TestDataDownLoad.CRCУÑé:"+crc0+"==="+crc1);
|
return false;
|
}
|
tmpbuf.position(0);
|
int last = filebyte.length;
|
byte[] tmp = new byte[last+len-2];
|
System.arraycopy(filebyte, 0, tmp, 0, last);
|
for(int i=0;i<len-2;i++) {
|
tmp[last+i] = tmpbuf.get();
|
}
|
this.filebyte = tmp;
|
return true;
|
}
|
|
/**
|
* ÀúÊ·ÎļþÏÂÔØ²ÎÊýУÑé
|
*/
|
private void downLoadParamCheck() {
|
if(null != this.fbofile) {
|
if(fbofile.battgroupid < 0 ||
|
fbofile.file_index >= fbofile.file_total) {
|
fbofile.setStop_reason(FialReason_ParamError);
|
fbofile.op_cmd = FGCD_ComBase.CMD_DownLoadFBDFile_Over; //ÏÂÔØ½áÊø
|
ReadTestData = false;
|
}else {
|
fbofile.op_cmd = FGCD_ComBase.CMD_DownLoadFBDFile_ACK;
|
}
|
}
|
}
|
|
/**
|
* ¸üе±Ç°ÏÂÔØÎļþ״̬
|
* @param pool
|
* @param fbofile
|
*/
|
public void updateDownLoadState(MysqlConnPool pool,FGCD_TestFBOFile fbofile) {
|
String sql_str = " UPDATE " + Sql_Mysql.Tb_Fgcd_Filedownload_Table +
|
" SET op_cmd = " + fbofile.op_cmd +
|
//Ìí¼Óʧ°ÜÔÒò×Ö¶ÎÐÞ¸Ä
|
" WHERE dev_id = " + fbofile.dev_id;
|
Sql_Mysql sql = null;
|
try {
|
sql = new Sql_Mysql(pool);
|
sql.sqlMysqlExecute(sql_str);
|
} catch (SQLException e) {
|
e.printStackTrace();
|
} finally {
|
sql.close_con();
|
}
|
}
|
|
/**
|
* ¸üе±Ç°É豸µÄÏÂÔØ½ø¶È
|
* @param pool
|
* @param fbofile
|
*/
|
public void updateDownLoadProgress(MysqlConnPool pool,FGCD_TestFBOFile fbofile) {
|
String sql_str = " UPDATE " + Sql_Mysql.Tb_Fgcd_Filedownload_Table +
|
" SET ";
|
Sql_Mysql sql = new Sql_Mysql(pool);
|
String sql_str_end = " WHERE dev_id = " + fbofile.dev_id;
|
for(int i=0;i<fbofile.files.length;i++) {
|
if(i>0) {
|
sql_str += ",";
|
}
|
sql_str += "download_state"+(i+1)+" = "+fbofile.files[i].getDownload_state()
|
+ ",now_data_block"+(i+1)+" = "+fbofile.files[i].getNow_data_block()
|
+ ",total_data_block"+(i+1)+" = "+fbofile.files[i].getTotal_data_block();
|
}
|
sql_str += sql_str_end;
|
try {
|
//System.out.println(sql_str);
|
sql.sqlMysqlExecute(sql_str);
|
} catch (SQLException e) {
|
e.printStackTrace();
|
} finally {
|
sql.close_con();
|
}
|
}
|
/**
|
* ¸üе±Ç°É豸µÄÏÂÔØ½ø¶È
|
* @param pool
|
* @param fbofile
|
*/
|
public void updateDownLoadProgressByOne(MysqlConnPool pool,FGCD_TestFBOFile fbofile,int file_downloadNum) {
|
String sql_str = " UPDATE " + Sql_Mysql.Tb_Fgcd_Filedownload_Table +
|
" SET ";
|
Sql_Mysql sql = new Sql_Mysql(pool);
|
String sql_str_end = " WHERE dev_id = " + fbofile.dev_id;
|
sql_str += "download_state"+(file_downloadNum+1)+" = "+fbofile.files[file_downloadNum].getDownload_state()
|
+ ",now_data_block"+(file_downloadNum+1)+" = "+fbofile.files[file_downloadNum].getNow_data_block()
|
+ ",total_data_block"+(file_downloadNum+1)+" = "+fbofile.files[file_downloadNum].getTotal_data_block();
|
sql_str += sql_str_end;
|
try {
|
//System.out.println(sql_str);
|
sql.sqlMysqlExecute(sql_str);
|
} catch (SQLException e) {
|
e.printStackTrace();
|
} finally {
|
sql.close_con();
|
}
|
}
|
|
/**
|
* ²éѯµ±Ç°É豸µÄÏÂÔØ½ø¶È,±ÜÃâ³öÏÖÊÖ¶¯Í£Ö¹,¿É¼°Ê±Í£Ö¹´íÎóµÄµ¼Èë
|
* @param pool
|
* @param fbofile
|
*/
|
public void queryDownLoadState(MysqlConnPool pool,FGCD_TestFBOFile fbofile) {
|
String sql_str = " SELECT * FROM " + Sql_Mysql.Tb_Fgcd_Filedownload_Table +
|
" WHERE dev_id = " + fbofile.dev_id;
|
Sql_Mysql sql = new Sql_Mysql(pool);
|
ResultSet res = null;
|
try {
|
res = sql.sqlMysqlQuery(sql_str);
|
if(res.next()) {
|
fbofile.op_cmd = res.getInt("op_cmd");
|
}
|
} catch (SQLException e) {
|
e.printStackTrace();
|
} finally {
|
if(null != res) {
|
try {
|
res.close();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
sql.close_con();
|
}
|
}
|
|
|
/**
|
* Éú³ÉFBOÀúÊ·Êý¾ÝÎļþ
|
*/
|
public boolean createFBOFile(FBOFile file){
|
boolean flag = true;
|
//System.err.println("ÎļþÊý¾Ý£º"+ComFn.bytesToHexString(filebyte, filebyte.length));
|
if(filebyte.length <0) {
|
file.download_flag = DownloadFlag_Fail;
|
return false;
|
}
|
File root = new File(System.getProperty("user.dir")+File.separator+"fbofiles"+File.separator+fbofile.dev_id);
|
if(!root.exists()) {
|
root.mkdirs();
|
}
|
fileName = root.getAbsolutePath()+File.separator+(new Date()).getTime()+".FBO";
|
//System.out.println(fileName);
|
file.filename = fileName;
|
File f = new File(fileName);
|
FileOutputStream fos = null;
|
try {
|
fos = new FileOutputStream(f);
|
fos.write(filebyte);
|
fos.flush();
|
} catch (IOException e) {
|
flag = false;
|
e.printStackTrace();
|
} finally {
|
if(null != fos) {
|
try {
|
fos.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return flag;
|
}
|
|
/**
|
* ½âÎöFBOÎļþÏß³Ì
|
* @author LiJun
|
*
|
*/
|
public class ParseFboThread extends Thread{
|
public MysqlConnPool pool;
|
public int port_index;
|
public FGCD_TestFBOFile fbofile;
|
|
public ParseFboThread(MysqlConnPool pool,int index,FGCD_TestFBOFile fbofile){
|
this.pool = pool;
|
this.port_index = index;
|
this.fbofile = fbofile;
|
}
|
|
@Override
|
public void run() {
|
|
}
|
|
}
|
}
|