蓄电池监控管理平台数据库初始化程序
DELL
2024-12-07 1d04bea0deadc20a07599d15ee96559665ffe60a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
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;
    }
    
    
}