充放电一体机FGCD通信程序【二期初版】
whyclxw
2021-09-01 48b0d8bf2db9a5a0950946ad03544504633a86d0
一体机修改成多线程导入数据库并插入导入进度
4个文件已修改
1个文件已添加
282 ■■■■■ 已修改文件
BattMonitor_FGCD-A059NT/src/com/dev/fgcd/comm/FGCD_Export_Thread_SQL.java 64 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
BattMonitor_FGCD-A059NT/src/com/dev/fgcd/comm/FGCD_Task_Thread_SQL.java 121 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
BattMonitor_FGCD-A059NT/src/com/dev/fgcd/comm/FGCD_TestDataDownLoad.java 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
BattMonitor_FGCD-A059NT/src/com/dev/fgcd/data/FGCD_TestFBOFile.java 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
BattMonitor_FGCD-A059NT/src/com/dev/fgcd/fbo/FboData.java 76 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
BattMonitor_FGCD-A059NT/src/com/dev/fgcd/comm/FGCD_Export_Thread_SQL.java
New file
@@ -0,0 +1,64 @@
package com.dev.fgcd.comm;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.battmonitor.sql.MysqlConnPool;
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.sybase.jdbc3.a.b.g;
public class FGCD_Export_Thread_SQL implements Runnable{
    public MysqlConnPool m_ConnPool = null;
    public FBOFile file =null;
    private Logger logger = null;
    public int battgroupid=0;
    public FGCD_TestFBOFile fbofile=null;
    public FGCD_Export_Thread_SQL(MysqlConnPool m_ConnPool, FBOFile file,FGCD_TestFBOFile fbofile) {
        super();
        this.m_ConnPool = m_ConnPool;
        this.file = file;
        //this.battgroupid=battgroupid;
        this.fbofile=fbofile;
        this.logger = LogManager.getLogger(this);
    }
    @Override
    public void run() {
        //System.out.println("线程开启"+new Date());
        file.setDownload_state(FGCD_TestDataDownLoad.DownLoadState_ExportSucc);
        readFBOFileData(file,m_ConnPool);
    }
    /**
     *     解析FBO文件
     * @param file
     * @param pool
     */
     private boolean readFBOFileData(FBOFile file,MysqlConnPool pool) {
         boolean flag = false;
         File f = new File(file.filename);
         System.err.println(f.getName()+"  "+f.length());
         if(file.test_timelong > 0) {
             flag=FboData.checkFboFileByOneSql(m_ConnPool,f,fbofile);
         }
         if(f.exists()) {
            //删除下载的FBO文件
            f.delete();
         }
         return flag;
    }
}
BattMonitor_FGCD-A059NT/src/com/dev/fgcd/comm/FGCD_Task_Thread_SQL.java
@@ -16,7 +16,7 @@
import com.mchange.v2.resourcepool.ResourcePoolListener;
public class FGCD_Task_Thread_SQL {
    /**
     *     插入FBO历史数据
     * @param pool
@@ -120,7 +120,110 @@
        }    
        return success;
    }
    //插入FBO——inf信息
    public static boolean insertFBOInfIntoTable(MysqlConnPool pool,FboDataInf fbo,int BattGoupId,int max_test_record_count) {
        boolean flag=true;
        if(fbo.getTestTimeLong() > 0) {
            String sql_str="INSERT INTO " + Sql_Mysql.Tb_FboTestData_Inf_Table +
                    "(BattGroupId,test_record_count,test_starttime,test_timelong,device,test_type,hourrate,save_interval,monomervol,moncapstd,test_curr,test_cap,monvol_limit,gourpvol_limit,mon_count,monvol_limitcount,stop_type,download_time) " +
                    " VALUES(" + BattGoupId
                    + "," +max_test_record_count
                    + ",'" + Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)
                    + "'," + fbo.getTestTimeLong()
                    + "," + fbo.Device
                    + "," + fbo.DataType
                    + "," + fbo.HourRate
                     + "," + fbo.SaveInterval
                    + "," + fbo.MonomerVol
                    + "," + fbo.STDCap
                    + "," + fbo.TestCur
                    + "," + fbo.TestCap
                    + "," + fbo.MVLLimit
                     + "," + fbo.SumVLLimit
                    + "," + fbo.BattSum
                    + "," + fbo.MVLLimitCount
                    + "," + fbo.StopType
                    + ",'" + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)
                    + "')";
            Sql_Mysql sql = new Sql_Mysql(pool);
            ResultSet res = null;
            try {
                res = sql.sqlMysqlQuery(" SELECT * " +
                        " FROM " + Sql_Mysql.Tb_FboTestData_Inf_Table +
                        " WHERE BattGroupId = "+BattGoupId+" AND test_starttime = '"+Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)+"'");
                if(!res.next()) {
                    //如果不存在则添加inf表记录
                    sql.sqlMysqlExecute(sql_str);
                }else {
                    //System.out.println("已存在当前测试数据");
                    flag=false;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if(null != res) {
                    try {
                        res.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                sql.close_con();
            }
        }
        return flag;
    }
    //插入FBO历史数据
    public static boolean insertFBODataIntoTableByOneSql(MysqlConnPool pool,FboDataInf fbo,FboData data,int BattGoupId,int record_num,int max_test_record_count) {
        boolean success = false;
        if(fbo.getTestTimeLong() > 0) {
            createTb_FboTestData_Table(pool, BattGoupId);
            ArrayList<String> sql_strs = new ArrayList<>();
            String sql_str = "INSERT INTO "+Sql_Mysql.Tb_FboTestData_Table+BattGoupId+"(BattGroupId,test_record_count,test_starttime,test_timelong,record_time,record_num,online_vol,sum_vol,test_curr,test_cap,mon_num,mon_vol) " +
                             " VALUES";
            Date record_time = new Date(fbo.TestStartTime.getFBODateTime().getTime()+data.m_TestTime.getTestTimeLong()*1000);
            if(data.BattSum > 0) {
                //有单体时
                for(int k=0;k<data.BattSum && k<data.SingleVol.length;k++) {
                    sql_strs.add(sql_str + "("
                            + "" + BattGoupId
                            + "," + max_test_record_count
                            + ",'" + Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)
                            + "'," + data.m_TestTime.getTestTimeLong()
                            + ",'" + Com.getDateTimeFormat(record_time, Com.DTF_YMDhms)
                            + "'," + record_num
                            + "," + data.OnlineVol
                            + "," + data.SumVoltage
                            + "," + data.SumCurrent
                            + "," + data.AllCap
                            + "," + (k+1)
                            + "," + data.SingleVol[k]
                            + ")");
                }
            }else {
                //没有单体数据时
                sql_strs.add(sql_str + "("
                        + "" + BattGoupId
                        + "," + max_test_record_count
                        + ",'" + Com.getDateTimeFormat(fbo.TestStartTime.getFBODateTime(), Com.DTF_YMDhms)
                        + "'," + data.m_TestTime.getTestTimeLong()
                        + ",'" + Com.getDateTimeFormat(record_time, Com.DTF_YMDhms)
                        + "'," +record_num
                        + "," + data.OnlineVol
                        + "," + data.SumVoltage
                        + "," + data.SumCurrent
                        + "," + data.AllCap
                        + "," + 0
                        + "," + 0
                        + ")");
            }
            Sql_Mysql sql = new Sql_Mysql(pool);
            success = sql.makeManualCommit(sql_strs);
            sql.close_con();
        }
        return success;
    }
    
    /**
     *     创建FBO历史数据_ID表
@@ -187,7 +290,19 @@
        return (test_record_count+1);
    }
    //文件导入进度修改
    public static void updateFBOGressByOneSql(MysqlConnPool pool,int file_sum,int file_block,FGCD_TestFBOFile fbofile) {
        String sql_str = " update " + Sql_Mysql.Tb_Fgcd_Filedownload_Table +
                         " set now_data_block1="+file_block+",total_data_block1="+file_sum+" WHERE BattGroupId = " + fbofile.battgroupid+" and dev_id="+fbofile.dev_id;
        Sql_Mysql sql = new Sql_Mysql(pool);
        try {
            sql.sqlMysqlExecute(sql_str);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
    public static void updateFBOFIleExportFlag(MysqlConnPool pool, FGCD_TestFBOFile fbofile) {
        String sql_str = " Update  " + Sql_Mysql.Tb_Fgcd_Filedownload_Table +
BattMonitor_FGCD-A059NT/src/com/dev/fgcd/comm/FGCD_TestDataDownLoad.java
@@ -83,6 +83,7 @@
        for(int i=0;i<fbofile.files.length;i++) {
            //当前下载文件
            FBOFile file = fbofile.files[i];
            fbofile.file_downloadNum=i;//设置当前下载文件的索引
            if(now_download_state == DownLoadState_UserCancel) {
                file.setDownload_state(DownLoadState_UserCancel);
                file.setDownload_flag(DownloadFlag_Fail);
@@ -93,7 +94,7 @@
            }
            nowFbo = file;
            ReadTestData = true;
            fbofile.initDownLoadState();
            fbofile.initDownLoadState();//文件信息下载初始化
            file.setDownload_state(DownLoadState_ReadData);    
            while(ReadTestData) {
                try {
@@ -127,19 +128,24 @@
                    
                    if(nowFbo.getDownload_state() == DownLoadState_ExportData) {
                        //解析本地数据文件并导入数据库
                        boolean flag = readFBOFileData(file,pool);
                        /*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, fbofile);
                        new Thread(export_Thread).start();
                    }
                    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());
                    /*file.setDownload_state(nowFbo.getDownload_state());
                    updateDownLoadProgress(pool,fbofile);        //更新当前下载进度
                    */
                    Thread.sleep(50);
                } catch(Exception e) {
                    e.printStackTrace();
@@ -181,7 +187,7 @@
         }
         if(f.exists()) {
            //删除下载的FBO文件
            f.delete();
            //f.delete();
         }    
         return flag;        
    }
BattMonitor_FGCD-A059NT/src/com/dev/fgcd/data/FGCD_TestFBOFile.java
@@ -16,6 +16,8 @@
    public int file_count;                        //当前文件数
    public int file_total;                        //数据文件总数
    
    public int file_downloadNum;               //当前下载文件编号索引
    private int stop_reason;                    //结束原因
            
    public FBOFile[] files = new FBOFile[8];    //读取的历史文件
@@ -152,6 +154,8 @@
        public int download_state;        //下载状态
        public int now_data_block;        //当前数据块数目
        public int total_data_block;     //当前下载状态
        
        public void clear() {
            file_count = 0;            //数据文件总数
@@ -328,6 +332,7 @@
            this.monvol_type = monvol_type;
        }
        
        public void setFileName() {
            filename = "F"+(2000+year);
            if(month>=10) {
BattMonitor_FGCD-A059NT/src/com/dev/fgcd/fbo/FboData.java
@@ -10,6 +10,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import com.battmonitor.sql.MysqlConnPool;
import com.dev.fgcd.comm.FGCD_Task_Thread_SQL;
import com.dev.fgcd.data.FGCD_TestFBOFile;
public class FboData {
    public FboDataType m_DataType = new FboDataType();
@@ -90,6 +94,17 @@
        }
    }
    
    @Override
    public String toString() {
        return "FboData [m_DataType=" + m_DataType + ", CRC16=" + CRC16 + ", m_TestTime=" + m_TestTime + ", BattGroup="
                + BattGroup + ", BattSum=" + BattSum + ", OnlineVol=" + OnlineVol + ", SumVoltage=" + SumVoltage
                + ", SumCurrent=" + SumCurrent + ", SubCurrent=" + Arrays.toString(SubCurrent) + ", AllCap=" + AllCap
                + ", SubCap=" + Arrays.toString(SubCap) + ", SingleVol=" + Arrays.toString(SingleVol) + ", maxMonVol="
                + maxMonVol + ", minMonVol=" + minMonVol + "]";
    }
    public static void checkFboFile(File file, FboDataInf data_inf, ArrayList<FboData> al_fbo_data)
    {
        File f = file;
@@ -134,6 +149,67 @@
        }
    }
    
    //按照每读取一笔数据插入数据库的方式
    public static boolean checkFboFileByOneSql(MysqlConnPool m_ConnPool,File file,FGCD_TestFBOFile fbofile)
    {
        File f = file;
        int file_sum=(int) f.length(); //需要导入文件的文件大小字节(导入数据库进度)
        int file_block=0;  //当前文件导入数据的字节
        FileInputStream fis = null;
        int record_num=1;//记录每次放电的笔数
        FboDataInf data_inf = new FboDataInf();
        boolean sucess=true;
        try {
            fis = new FileInputStream(f);
            byte[] buf = new byte[256];
            if(fis.read(buf, 0, buf.length) == 256)
            {
                data_inf.setDataInf(buf);
                int max_test_record_count =FGCD_Task_Thread_SQL.quereyBattMaxTestRecordCount(m_ConnPool, fbofile.battgroupid);
                //添加inf表记录
                boolean flag=FGCD_Task_Thread_SQL.insertFBOInfIntoTable(m_ConnPool, data_inf, fbofile.battgroupid,max_test_record_count);
                while(true&&flag)
                {
                    FboDataType mType = new FboDataType();
                    int tag = mType.checkDataHead(fis);
                    if((0xFD == tag) || (0xFC == tag))
                    {
                        byte[] databuf = new byte[data_inf.BattSum*2 + 32];
                        if(fis.read(databuf) == databuf.length)
                        {
                            FboData m_FboData = new FboData();
                            m_FboData.m_DataType = mType;
                            m_FboData.setData(databuf);
                            //al_fbo_data.add(m_FboData);
                            //System.err.println(m_FboData.toString());
                            boolean bl=FGCD_Task_Thread_SQL.insertFBODataIntoTableByOneSql(m_ConnPool,data_inf,m_FboData,fbofile.battgroupid,record_num,max_test_record_count);
                            sucess=sucess&bl;
                            file_block=record_num*(data_inf.BattSum*2 + 32)+256;
                            FGCD_Task_Thread_SQL.updateFBOGressByOneSql(m_ConnPool,file_sum,file_block,fbofile);//插入文件的进度
                            System.err.println("总数:"+file_sum+"  导入数据:"+file_block);
                            record_num++;
                        }
                    }
                    if(tag == 1)
                        break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != fis)
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sucess;
    }
    
    public void setData(IdcData i_data)
    {