package com.config;
|
|
import java.io.File;
|
import java.io.FileWriter;
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
|
import org.dom4j.Document;
|
import org.dom4j.DocumentException;
|
import org.dom4j.DocumentHelper;
|
import org.dom4j.Element;
|
import org.dom4j.io.OutputFormat;
|
import org.dom4j.io.SAXReader;
|
import org.dom4j.io.XMLWriter;
|
|
/**
|
*
|
* @author ¾ü
|
*
|
*/
|
public class AppConfig {
|
|
private final String ConfigFileName = "config.xml";
|
|
private int sqlServerType = 1; //Á¬½ÓÊý¾Ý¿âÀàÐÍ 1-MySQL 2-嫸ß
|
private String sqlServerIp = "127.0.0.1"; //Êý¾Ý¿âIPµØÖ·
|
private int sqlServerPort = 5866; //Êý¾Ý¿â¶Ë¿ÚºÅ
|
private int sqlConnCountMax = 200; //Êý¾Ý¿âÁ¬½Ó³ØÊýÁ¿
|
private int workThreadCountMax = 300; //
|
private boolean battTestStateReloadEn = true; //µç³Ø²âÊÔÊý¾ÝÖØÔØ-Èô³ÌÐòÖØÆôǰÕýÔÚ²âÊÔ£¬ºóÐøÊÇ·ñ½Ó×żÇ¼ÀúÊ·Êý¾Ý£¬Ä¬ÈÏÁ¬Ðø
|
|
private int powerAcceptPort = 9000; //µçԴͨÐŶ˿Ú
|
|
//----------------------------------------------------------------//
|
private int discharge_SaveDataTimeInterval = 10; //·Åµç²âÊÔÊý¾Ý´æ´¢¼ä¸ô
|
private float discharge_MonVolChangeLevel = 0.05F; //·Åµçµ¥Ìåµçѹ±ä»¯ãÐÖµ(µ¥Ìåµçѹ±ä»¯²îÖµ>0.05F¼´¼Ç¼һ±Ê²âÊÔÊý¾Ý)
|
private int discharge_TestTimeLongMinimum = 30; //¼Ç¼·Åµç²âÊÔµÄ×î¶Ìʱ¼ä
|
private int discharge_TestDataRecordCountMax = 15000; //·Åµç²âÊÔ×î´ó±ÊÊý
|
private int discharge_BattTestGroupCountMax = 100; //ͬʱ¼Ç¼²âÊÔµÄµç³Ø×éÊýÁ¿
|
//----------------------------------------------------------------//
|
//----------------------------------------------------------------//
|
public int charge_SaveDataTimeInterval = 10; //³äµç²âÊÔÊý¾Ý´æ´¢¼ä¸ô
|
public float charge_MonVolChangeLevel = 0.05F; //³äµçµ¥Ìåµçѹ±ä»¯ãÐÖµ(µ¥Ìåµçѹ±ä»¯²îÖµ>0.05F¼´¼Ç¼һ±Ê²âÊÔÊý¾Ý)
|
public int charge_TestTimeLongMinimum = 30; //¼Ç¼³äµç²âÊÔµÄ×î¶Ìʱ¼ä
|
public int charge_TestDataRecordCountMax = 15000; //³äµç²âÊÔ×î´ó±ÊÊý
|
public int charge_BattTestGroupCountMax = 100; //ͬʱ¼Ç¼²âÊÔµÄµç³Ø×éÊýÁ¿
|
//----------------------------------------------------------------//
|
|
private boolean RealDataRecord_fn_En = false; //ÀúʷʵʱÊý¾Ý¼Ç¼ʹÄÜ
|
private int RealDataRecord_MinTimeLong = 60; //ʵʱÊý¾Ý×î¶Ì¼Ç¼¼ä¸ô(Ãë)
|
private int RealDataRecord_MaxSaveTime = 90; //ʵʱÊý¾Ý×î¶Ì±£ÁôÌìÊý
|
|
public AppConfig()
|
{
|
/*
|
String path = System.getProperty("user.dir");
|
if(path.contains("\\"))
|
path += "\\";
|
else path += "/";
|
*/
|
String path = ConfigFileName;
|
//System.out.println(path);
|
File f = new File(path);
|
if(false == f.exists())
|
{
|
writeConfigToXml();
|
}
|
|
readConfigFromXml();
|
}
|
|
public void copyAppParam(AppConfig config) {
|
this.sqlServerIp = config.sqlServerIp; //Êý¾Ý¿âIPµØÖ·
|
this.sqlServerPort = config.sqlServerPort; //Êý¾Ý¿â¶Ë¿ÚºÅ
|
this.sqlConnCountMax = config.sqlConnCountMax; //Êý¾Ý¿âÁ¬½Ó³ØÊýÁ¿
|
this.workThreadCountMax = config.workThreadCountMax; //
|
|
//----------------------------------------------------------------//
|
this.discharge_SaveDataTimeInterval = config.discharge_SaveDataTimeInterval; //·Åµç²âÊÔÊý¾Ý´æ´¢¼ä¸ô
|
this.discharge_MonVolChangeLevel = config.discharge_MonVolChangeLevel; //·Åµçµ¥Ìåµçѹ±ä»¯ãÐÖµ(µ¥Ìåµçѹ±ä»¯²îÖµ>0.05F¼´¼Ç¼һ±Ê²âÊÔÊý¾Ý)
|
this.discharge_TestTimeLongMinimum = config.discharge_TestTimeLongMinimum; //¼Ç¼·Åµç²âÊÔµÄ×î¶Ìʱ¼ä
|
this.discharge_TestDataRecordCountMax = config.discharge_TestDataRecordCountMax; //·Åµç²âÊÔ×î´ó±ÊÊý
|
this.discharge_BattTestGroupCountMax = config.discharge_BattTestGroupCountMax; //ͬʱ¼Ç¼²âÊÔµÄµç³Ø×éÊýÁ¿
|
//----------------------------------------------------------------//
|
//----------------------------------------------------------------//
|
this.charge_SaveDataTimeInterval = config.charge_SaveDataTimeInterval; //³äµç²âÊÔÊý¾Ý´æ´¢¼ä¸ô
|
this.charge_MonVolChangeLevel = config.charge_MonVolChangeLevel; //³äµçµ¥Ìåµçѹ±ä»¯ãÐÖµ(µ¥Ìåµçѹ±ä»¯²îÖµ>0.05F¼´¼Ç¼һ±Ê²âÊÔÊý¾Ý)
|
this.charge_TestTimeLongMinimum = config.charge_TestTimeLongMinimum; //¼Ç¼³äµç²âÊÔµÄ×î¶Ìʱ¼ä
|
this.charge_TestDataRecordCountMax = config.charge_TestDataRecordCountMax; //³äµç²âÊÔ×î´ó±ÊÊý
|
this.charge_BattTestGroupCountMax = config.charge_BattTestGroupCountMax; //ͬʱ¼Ç¼²âÊÔµÄµç³Ø×éÊýÁ¿
|
//----------------------------------------------------------------//
|
|
this.RealDataRecord_fn_En = config.RealDataRecord_fn_En; //ÀúʷʵʱÊý¾Ý¼Ç¼ʹÄÜ
|
this.RealDataRecord_MinTimeLong = config.RealDataRecord_MinTimeLong; //ʵʱÊý¾Ý×î¶Ì¼Ç¼¼ä¸ô(Ãë)
|
this.RealDataRecord_MaxSaveTime = config.RealDataRecord_MaxSaveTime; //ʵʱÊý¾Ý×î¶Ì±£ÁôÌìÊý
|
}
|
|
|
public boolean isRealDataRecord_fn_En() {
|
return RealDataRecord_fn_En;
|
}
|
|
public int getRealDataRecord_MinTimeLong() {
|
return RealDataRecord_MinTimeLong;
|
}
|
|
public int getRealDataRecord_MaxSaveTime() {
|
return RealDataRecord_MaxSaveTime;
|
}
|
|
public void setRealDataRecord_fn_En(boolean realDataRecord_fn_En) {
|
RealDataRecord_fn_En = realDataRecord_fn_En;
|
}
|
|
public void setRealDataRecord_MinTimeLong(int realDataRecord_MinTimeLong) {
|
RealDataRecord_MinTimeLong = realDataRecord_MinTimeLong;
|
}
|
|
public void setRealDataRecord_MaxSaveTime(int realDataRecord_MaxSaveTime) {
|
RealDataRecord_MaxSaveTime = realDataRecord_MaxSaveTime;
|
}
|
|
public int getWorkThreadCountMax() {
|
return workThreadCountMax;
|
}
|
|
public int getSaveDataTimeInterval(int type) {
|
int val = 0;
|
switch (type) {
|
case 0:
|
val = this.discharge_SaveDataTimeInterval;
|
break;
|
case 1:
|
val = this.charge_SaveDataTimeInterval;
|
break;
|
}
|
|
return val;
|
}
|
|
public float getMonVolChangeLevel(int type) {
|
float val = 0.0F;
|
switch (type) {
|
case 0:
|
val = this.discharge_MonVolChangeLevel;
|
break;
|
case 1:
|
val = this.charge_MonVolChangeLevel;
|
break;
|
}
|
|
return val;
|
}
|
|
public int getTestTimeLongMinimum(int type) {
|
int val = 0;
|
switch (type) {
|
case 0:
|
val = this.discharge_TestTimeLongMinimum;
|
break;
|
case 1:
|
val = this.charge_TestTimeLongMinimum;
|
break;
|
}
|
|
return val;
|
}
|
|
public int getTestDataRecordCountMax(int type) {
|
int val = 0;
|
switch (type) {
|
case 0:
|
val = this.discharge_TestDataRecordCountMax;
|
break;
|
case 1:
|
val = this.charge_TestDataRecordCountMax;
|
break;
|
}
|
|
return val;
|
}
|
|
public int getBattTestGroupCountMax(int type) {
|
int val = 0;
|
switch (type) {
|
case 0:
|
val = this.discharge_BattTestGroupCountMax;
|
break;
|
case 1:
|
val = this.charge_BattTestGroupCountMax;
|
break;
|
}
|
|
return val;
|
}
|
|
|
public void writeConfigToXml()
|
{
|
try {
|
Document document = DocumentHelper.createDocument();
|
Element root = document.addElement("root");
|
//root.addComment("Õâ¸öÒ»¸ö×¢ÊÍ");
|
|
Element param;
|
Element sub_param;
|
|
//-------------------------------------------------------//
|
param = root.addElement("sql_server_type");
|
param.addText(String.valueOf(sqlServerType));
|
param = root.addElement("sql_server_ip");
|
param.addText(sqlServerIp);
|
param = root.addElement("sql_server_port");
|
param.addText(String.valueOf(sqlServerPort));
|
param = root.addElement("sql_conncount_max");
|
param.addText(String.valueOf(sqlConnCountMax));
|
param = root.addElement("workThreadCountMax");
|
param.addText(String.valueOf(workThreadCountMax));
|
|
param = root.addElement("battTestStateReloadEn");
|
param.addText(String.valueOf(battTestStateReloadEn));
|
|
|
param = root.addElement("powerAcceptPort");
|
param.addText(String.valueOf(powerAcceptPort));
|
//-------------------------------------------------------//
|
|
//-------------------------------------------------------//
|
param = root.addElement("discharge_test_config"); //·Åµç²âÊÔ
|
|
// param.addComment("SOURCE_SQLSERVER_TYPE_SYBASE_DEMO = 0"); ×¢ÊÍ
|
// param.addComment("SOURCE_SQLSERVER_TYPE_SYBASE = 1"); ×¢ÊÍ
|
// param.addComment("SOURCE_SQLSERVER_TYPE_MSSQLSERVER = 2"); ×¢ÊÍ
|
|
sub_param = param.addElement("discharge_SaveDataTimeInterval");
|
sub_param.addText(String.valueOf(discharge_SaveDataTimeInterval));
|
|
sub_param = param.addElement("discharge_MonVolChangeLevel");
|
sub_param.addText(String.valueOf(discharge_MonVolChangeLevel));
|
|
sub_param = param.addElement("discharge_TestTimeLongMinimum");
|
sub_param.addText(String.valueOf(discharge_TestTimeLongMinimum));
|
|
sub_param = param.addElement("discharge_TestDataRecordCountMax");
|
sub_param.addText(String.valueOf(discharge_TestDataRecordCountMax));
|
|
sub_param = param.addElement("discharge_BattTestGroupCountMax");
|
sub_param.addText(String.valueOf(discharge_BattTestGroupCountMax));
|
//-------------------------------------------------------//
|
|
//--------------------------------------------------------//
|
param = root.addElement("chrcharge_test_config");
|
|
sub_param = param.addElement("charge_SaveDataTimeInterval");
|
sub_param.addText(String.valueOf(charge_SaveDataTimeInterval));
|
|
sub_param = param.addElement("charge_MonVolChangeLevel");
|
sub_param.addText(String.valueOf(charge_MonVolChangeLevel));
|
|
sub_param = param.addElement("charge_TestTimeLongMinimum");
|
sub_param.addText(String.valueOf(charge_TestTimeLongMinimum));
|
|
sub_param = param.addElement("charge_TestDataRecordCountMax");
|
sub_param.addText(String.valueOf(charge_TestDataRecordCountMax));
|
|
sub_param = param.addElement("charge_BattTestGroupCountMax");
|
sub_param.addText(String.valueOf(charge_BattTestGroupCountMax));
|
//--------------------------------------------------------//
|
|
//-------------------------------------------------------//
|
//--------------------------------------------------------//
|
param = root.addElement("realdatarecord_fn_cofig");
|
sub_param = param.addElement("realdatarecord_fn_en");
|
sub_param.addText(String.valueOf(RealDataRecord_fn_En));
|
|
sub_param = param.addElement("realdatarecord_mintimelong");
|
sub_param.addText(String.valueOf(RealDataRecord_MinTimeLong));
|
|
sub_param = param.addElement("realdatarecord_maxsavetime");
|
sub_param.addText(String.valueOf(RealDataRecord_MaxSaveTime));
|
|
//--------------------------------------------------------//
|
OutputFormat format = OutputFormat.createPrettyPrint();
|
format.setEncoding("gbk");
|
XMLWriter writer2 = new XMLWriter(new FileWriter(ConfigFileName), format);
|
writer2.write(document);
|
writer2.close();
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
public void readConfigFromXml()
|
{
|
boolean res = true;
|
try
|
{
|
SAXReader reader = new SAXReader();
|
Document document = reader.read(new File(ConfigFileName));
|
Element rootnode = document.getRootElement();
|
|
Element node;
|
Element sub_node;
|
|
//------------------------------------------------------------//
|
node = rootnode.element("sql_server_type");
|
sqlServerType = Integer.parseInt(node.getTextTrim());
|
//------------------------------------------------------------//
|
node = rootnode.element("sql_server_ip");
|
sqlServerIp = node.getTextTrim();
|
//------------------------------------------------------------//
|
node = rootnode.element("sql_server_port");
|
sqlServerPort = Integer.parseInt(node.getTextTrim());
|
//------------------------------------------------------------//
|
node = rootnode.element("sql_conncount_max");
|
sqlConnCountMax = Integer.parseInt(node.getTextTrim());
|
|
if(sqlConnCountMax < 10)
|
sqlConnCountMax = 10;
|
if(sqlConnCountMax > 1000)
|
sqlConnCountMax = 800;
|
//------------------------------------------------------------//
|
node = rootnode.element("workThreadCountMax");
|
workThreadCountMax = Integer.parseInt(node.getTextTrim());
|
|
if(workThreadCountMax < 10)
|
workThreadCountMax = 10;
|
if(workThreadCountMax > 20000)
|
workThreadCountMax = 20000;
|
//------------------------------------------------------------//
|
node = rootnode.element("battTestStateReloadEn");
|
battTestStateReloadEn = Boolean.parseBoolean(node.getTextTrim());
|
//------------------------------------------------------------//
|
node = rootnode.element("powerAcceptPort");
|
powerAcceptPort = Integer.parseInt(node.getTextTrim());
|
|
if(powerAcceptPort < 1000) {
|
powerAcceptPort = 9000;
|
}
|
//------------------------------------------------------------//
|
|
//------------------------------------------------------------//
|
node = rootnode.element("discharge_test_config");
|
|
sub_node = node.element("discharge_SaveDataTimeInterval");
|
discharge_SaveDataTimeInterval = Integer.parseInt(sub_node.getTextTrim());
|
|
sub_node = node.element("discharge_MonVolChangeLevel");
|
discharge_MonVolChangeLevel = Float.parseFloat(sub_node.getTextTrim());
|
|
sub_node = node.element("discharge_TestTimeLongMinimum");
|
discharge_TestTimeLongMinimum = Integer.parseInt(sub_node.getTextTrim());
|
|
sub_node = node.element("discharge_TestDataRecordCountMax");
|
discharge_TestDataRecordCountMax = Integer.parseInt(sub_node.getTextTrim());
|
|
sub_node = node.element("discharge_BattTestGroupCountMax");
|
discharge_BattTestGroupCountMax = Integer.parseInt(sub_node.getTextTrim());
|
//------------------------------------------------------------//
|
|
//------------------------------------------------------------//
|
node = rootnode.element("chrcharge_test_config");
|
|
sub_node = node.element("charge_SaveDataTimeInterval");
|
charge_SaveDataTimeInterval = Integer.parseInt(sub_node.getTextTrim());
|
|
sub_node = node.element("charge_MonVolChangeLevel");
|
charge_MonVolChangeLevel = Float.parseFloat(sub_node.getTextTrim());
|
|
sub_node = node.element("charge_TestTimeLongMinimum");
|
charge_TestTimeLongMinimum = Integer.parseInt(sub_node.getTextTrim());
|
|
sub_node = node.element("charge_TestDataRecordCountMax");
|
charge_TestDataRecordCountMax = Integer.parseInt(sub_node.getTextTrim());
|
|
sub_node = node.element("charge_BattTestGroupCountMax");
|
charge_BattTestGroupCountMax = Integer.parseInt(sub_node.getTextTrim());
|
//------------------------------------------------------------//
|
node = rootnode.element("realdatarecord_fn_cofig");
|
|
sub_node = node.element("realdatarecord_fn_en");
|
RealDataRecord_fn_En = Boolean.parseBoolean(sub_node.getTextTrim());
|
|
sub_node = node.element("realdatarecord_mintimelong");
|
RealDataRecord_MinTimeLong = Integer.parseInt(sub_node.getTextTrim());
|
|
sub_node = node.element("realdatarecord_maxsavetime");
|
RealDataRecord_MaxSaveTime = Integer.parseInt(sub_node.getTextTrim());
|
//-------------------------------------------------------//
|
|
} catch (NullPointerException | NumberFormatException | DocumentException e) {
|
res = false;
|
e.printStackTrace();
|
} finally {
|
if(false == res)
|
writeConfigToXml();
|
}
|
}
|
|
public int getSqlServerType() {
|
return sqlServerType;
|
}
|
|
public void setSqlServerType(int sqlServerType) {
|
this.sqlServerType = sqlServerType;
|
}
|
|
public String getSqlServerIp() {
|
return sqlServerIp;
|
}
|
|
public int getSqlServerPort() {
|
return sqlServerPort;
|
}
|
|
public int getSqlConnCountMax() {
|
return sqlConnCountMax;
|
}
|
|
public int getDischarge_SaveDataTimeInterval() {
|
return discharge_SaveDataTimeInterval;
|
}
|
|
public float getDischarge_MonVolChangeLevel() {
|
return discharge_MonVolChangeLevel;
|
}
|
|
public int getDischarge_TestTimeLongMinimum() {
|
return discharge_TestTimeLongMinimum;
|
}
|
|
public int getDischarge_TestDataRecordCountMax() {
|
return discharge_TestDataRecordCountMax;
|
}
|
|
public int getDischarge_BattTestGroupCountMax() {
|
return discharge_BattTestGroupCountMax;
|
}
|
|
public int getCharge_SaveDataTimeInterval() {
|
return charge_SaveDataTimeInterval;
|
}
|
|
public float getCharge_MonVolChangeLevel() {
|
return charge_MonVolChangeLevel;
|
}
|
|
public int getCharge_TestTimeLongMinimum() {
|
return charge_TestTimeLongMinimum;
|
}
|
|
public int getCharge_TestDataRecordCountMax() {
|
return charge_TestDataRecordCountMax;
|
}
|
|
public int getCharge_BattTestGroupCountMax() {
|
return charge_BattTestGroupCountMax;
|
}
|
|
public int getPowerAcceptPort() {
|
return powerAcceptPort;
|
}
|
|
public boolean getBattTestStateReloadEn() {
|
return battTestStateReloadEn;
|
}
|
|
public void setBattTestStateReloadEn(boolean battTestStateReloadEn) {
|
this.battTestStateReloadEn = battTestStateReloadEn;
|
}
|
|
public void setPowerAcceptPort(int powerAcceptPort) {
|
this.powerAcceptPort = powerAcceptPort;
|
}
|
|
|
}
|