充放电一体机FGCD通信程序【二期初版】
Administrator
2022-02-11 06cc6a688030171d1bff4453c76bc216dbde09e1
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
package com.batttest;
 
import java.sql.SQLException;
import java.util.Date;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.battmonitor.base.Com;
import com.battmonitor.data.BattData_RT;
import com.battmonitor.data.BattData_RT_Array;
import com.battmonitor.data.BattStatData;
import com.battmonitor.data_store.InsertDataToMysql_Task_SQL;
import com.battmonitor.sql.MysqlConnPool;
import com.battmonitor.sql.Sql_Mysql;
import com.config.AppConfig;
 
public class BattResStorePro_Thread extends Thread {
    
    private BattData_RT_Array m_Data;
    private MysqlConnPool m_Conn_Pool;
    
    private Logger logger = null;
    //private int battResStoreDelayForTestManual = 1200;
    //private int mSaveBattResDayInterval = 7;
    
    //private Main_Com_Data m_Main_Com_Data = null;
    
    public BattResStorePro_Thread(MysqlConnPool pool, AppConfig cfg, BattData_RT_Array data)
    {
        logger = LogManager.getLogger(this.getClass());
        
        m_Conn_Pool = pool;
        m_Data = data;
        
        //battResStoreDelayForTestManual = cfg.getBattResStoreDelayForTestManual();
        //mSaveBattResDayInterval = cfg.getBattResStoreDayInterval();
        
        //m_Main_Com_Data = Com_Data;
    }
    
    /**
     * ´´½¨¼°Ìí¼ÓÊý¾Ýµ½tb_battresdata_id±í£»Ìí¼ÓÊý¾Ý£¬ÒÔ¼°¸üÐÂtb_battresdata_infÊý¾Ý±í£¬²¢ÇÒÏò±íÖÐÌí¼ÓÊý¾Ý
     * @param conn_pool
     * @param data_rt
     * @param test_type
     */
    public static void insertDataToResTable(MysqlConnPool conn_pool, BattData_RT data_rt, int test_type)
    {
        int mon_count = data_rt.MonCount;
        if(mon_count < 1)
            return;
        
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        String sql_str = null;
        try {
            sql.sqlMysqlUseDB(Sql_Mysql.DB_BATT_TESTDATA);
            
            boolean result = true;
            Date rc_time = new Date();
            
            String tb_name = "tb_BattResData_" + data_rt.BattGroupId;
            if(false == sql.sqlMysqlCheckIfTableExist(tb_name))
            {
                sql_str = "CREATE TABLE IF NOT EXISTS " + tb_name + " "
                        + " ( `num` BIGINT NOT NULL AUTO_INCREMENT, "
                        + "`BattGroupId` INT NOT NULL DEFAULT 0, "
                        + "`test_record_count` INT NOT NULL DEFAULT 0, "
                        + "`test_type` INT NOT NULL DEFAULT 0, "
                        + "`data_new` BOOLEAN NOT NULL DEFAULT false, "
                        + "`data_available` BOOLEAN NOT NULL DEFAULT false, "
                        + "`test_starttime` DATETIME NOT NULL DEFAULT '1982-01-01 00:00:00', "
                        + "`group_vol` FLOAT NOT NULL DEFAULT 0, "
                        + "`test_curr` FLOAT NOT NULL DEFAULT 0, "
                        + "`mon_num` INT NOT NULL DEFAULT 0, "
                        + "`mon_vol` FLOAT NOT NULL DEFAULT 0, "
                        + "`mon_tmp` FLOAT NOT NULL DEFAULT 0, "
                        + "`mon_res` FLOAT NOT NULL DEFAULT 0, "
                        + "`mon_ser` FLOAT NOT NULL DEFAULT 0, "
                        + "`conn_res` FLOAT NOT NULL DEFAULT 0, "
                        + "`isStandard` int(11) NOT NULL DEFAULT '0' COMMENT 'ÊÇ·ñΪ±ê×¼ÄÚ×è¼Ç¼[0:²»±ê×¼;1:±ê×¼]',"
                        + "INDEX index_test_record_count (`test_record_count`), "
                        + "PRIMARY KEY (`num`));";
                //System.out.println(sql_str);
                sql.sqlMysqlExecute(sql_str);
            }
            
            int mtest_record_count = 0;//sql.getBattTestRecordCountNew(data_rt.BattGroupId, Sql_Mysql.BattResDataInf_Table);
            
            for(int c=0; c<3; c++)
            {
                try {
                    sql_str = "INSERT INTO " + tb_name + " "
                            + " (BattGroupId, "
                            + "test_record_count, "
                            + "test_type, "
                            + "data_new, "
                            + "data_available, "
                            + "test_starttime, "
                            + "group_vol, "
                            + "test_curr, "
                            + "mon_num, "
                            + "mon_vol,"
                            + "mon_tmp,"
                            + "mon_res,"
                            + "mon_ser,"
                            + "conn_res)"
                            + " VALUES ";
                    try {
                        for(int n=0; n<mon_count; n++) {
                            String tmp_sql_str = "("
                                        + data_rt.BattGroupId + ", "
                                        + mtest_record_count + ", "
                                        + test_type + ", "
                                        + true + ", "
                                        + true + ", "
                                        + "'" + Com.getDateTimeFormat(rc_time, Com.DTF_YMDhms) + "', "
                                        + data_rt.mTestData.groupVol + ", "
                                        + data_rt.mTestData.testCurr + ", "
                                        + ((n+1)*10 + 1) + ", "
                                        + data_rt.al_MonVol.get(n).monVol + ","
                                        + data_rt.al_MonVol.get(n).monTmp + ","
                                        + data_rt.al_MonVol.get(n).monRes + ","
                                        + data_rt.al_MonVol.get(n).monSer + ","
                                        + data_rt.al_MonVol.get(n).connRes
                                        + ")";
                            if(n > 0) {
                                sql_str += ", ";
                            }
                            sql_str += tmp_sql_str;
                        }
                    } catch (Exception e) {
                        sql.logger.error("BattResStorePro_Thread.insertDataToResTable():" + e.toString(), e);
                    }
                
                    String sql_str2 = "INSERT INTO " + Sql_Mysql.BattResDataInf_Table
                                + " (BattGroupId, "
                                + "test_record_count, "
                                + "test_type, "
                                + "data_new, "
                                + "data_available, "
                                + "test_starttime, "
                                + "test_devtype, "
                                + "record_time, "
                                + "group_vol, "
                                + "test_curr, "
                                + "upload_usr_id) "
                                + " VALUES "
                                + "("+ data_rt.BattGroupId + ", "
                                + mtest_record_count + ", "
                                + test_type + ", "
                                + true + ", "
                                + true + ", "
                                + "'" + Com.getDateTimeFormat(rc_time, Com.DTF_YMDhms) + "', "
                                + 0 + ", "
                                + "'" + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms) + "', "
                                + 0 + ", "
                                + 0 + ", "
                                + 0 + ") ";
                    if(1 == mtest_record_count) {
                        sql_str2 = "UPDATE " + Sql_Mysql.BattResDataInf_Table + " SET "
                                + "test_type=" + test_type + ", "
                                + "data_new=" + true + ", "
                                + "data_available=" + true + ", "
                                + "test_starttime='" + Com.getDateTimeFormat(rc_time, Com.DTF_YMDhms) + "', "
                                + "test_devtype=" + 0 + ", "
                                + "record_time='" + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms) + "', "
                                + "group_vol=" + 0 + ", "
                                + "test_curr=" + 0 + ", "
                                + "upload_usr_id=" + 0 + " "
                                + " WHERE BattGroupId=" + data_rt.BattGroupId
                                + " AND test_record_count=" + mtest_record_count;
                    }
                    //System.out.println(sql_str);
                    //System.out.println(sql_str2);
                    sql.mysql_con.setAutoCommit(false);
                    sql.sqlMysqlExecute(sql_str);
                    sql.sqlMysqlExecute(sql_str2);
                    sql.mysql_con.commit();
                } catch (SQLException e) {
                    sql.mysql_con.rollback();
                    //System.out.println(sql_str);
                    sql.logger.error("BattResStorePro_Thread.insertDataToResTable():" + e.toString(), e);
                    result = false;
                } finally {
                    sql.mysql_con.setAutoCommit(true);
                    if(true == result)
                    {
                        break;
                    }
                    else
                    {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            sql.logger.error("BattResStorePro_Thread.insertDataToResTable():" + e.toString(), e);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            sql.logger.error("BattResStorePro_Thread.insertDataToResTable():" + e.toString(), e);
        } finally {
            sql.close_con();
        }
    }
    
    /**
     * 
     *     @lijun  2021-08-18 ÖØÐ´¼Ç¼µ¥ÌåÄÚ×èÊý¾Ý·½·¨£¬Îª¼Ç¼·ÅµçÊý¾Ýʱ¼Ç¼µ±Ç°µ¥ÌåµÄÄÚ×èÊý¾Ý
     * 
     *     ´´½¨¼°Ìí¼ÓÊý¾Ýµ½tb_battresdata_id±í£»Ìí¼ÓÊý¾Ý£¬ÒÔ¼°¸üÐÂtb_battresdata_infÊý¾Ý±í£¬²¢ÇÒÏò±íÖÐÌí¼ÓÊý¾Ý
     * @param conn_pool
     * @param data_rt
     * @param test_type
     */
    public static void insertMonDataToResTable(MysqlConnPool conn_pool, BattData_RT data_rt,Date test_starttime, int test_type)
    {
        int mon_count = data_rt.MonCount;
        if(mon_count < 1)
            return;
        
        Sql_Mysql sql = new Sql_Mysql(conn_pool.getConn());
        String sql_str = null;
        try {
            sql.sqlMysqlUseDB(Sql_Mysql.DB_BATT_TESTDATA);
            
            boolean result = true;
            Date rc_time = test_starttime;
            
            String tb_name = "tb_BattResData_" + data_rt.BattGroupId;
            if(false == sql.sqlMysqlCheckIfTableExist(tb_name))
            {
                sql_str = "CREATE TABLE IF NOT EXISTS " + tb_name + " "
                        + " ( `num` BIGINT NOT NULL AUTO_INCREMENT, "
                        + "`BattGroupId` INT NOT NULL DEFAULT 0, "
                        + "`test_record_count` INT NOT NULL DEFAULT 0, "
                        + "`test_type` INT NOT NULL DEFAULT 0, "
                        + "`data_new` BOOLEAN NOT NULL DEFAULT false, "
                        + "`data_available` BOOLEAN NOT NULL DEFAULT false, "
                        + "`test_starttime` DATETIME NOT NULL DEFAULT '1982-01-01 00:00:00', "
                        + "`group_vol` FLOAT NOT NULL DEFAULT 0, "
                        + "`test_curr` FLOAT NOT NULL DEFAULT 0, "
                        + "`mon_num` INT NOT NULL DEFAULT 0, "
                        + "`mon_vol` FLOAT NOT NULL DEFAULT 0, "
                        + "`mon_tmp` FLOAT NOT NULL DEFAULT 0, "
                        + "`mon_res` FLOAT NOT NULL DEFAULT 0, "
                        + "`mon_ser` FLOAT NOT NULL DEFAULT 0, "
                        + "`conn_res` FLOAT NOT NULL DEFAULT 0, "
                        + "`isStandard` int(11) NOT NULL DEFAULT '0' COMMENT 'ÊÇ·ñΪ±ê×¼ÄÚ×è¼Ç¼[0:²»±ê×¼;1:±ê×¼]',"
                        + "INDEX index_test_record_count (`test_record_count`), "
                        + "PRIMARY KEY (`num`));";
                //System.out.println(sql_str);
                sql.sqlMysqlExecute(sql_str);
            }
            
            int mtest_record_count = sql.getBattTestRecordCountNew(data_rt.BattGroupId, Sql_Mysql.BattResDataInf_Table);
            
            for(int c=0; c<3; c++)
            {
                try {
                    sql_str = "INSERT INTO " + tb_name + " "
                            + " (BattGroupId, "
                            + "test_record_count, "
                            + "test_type, "
                            + "data_new, "
                            + "data_available, "
                            + "test_starttime, "
                            + "group_vol, "
                            + "test_curr, "
                            + "mon_num, "
                            + "mon_vol,"
                            + "mon_tmp,"
                            + "mon_res,"
                            + "mon_ser,"
                            + "conn_res)"
                            + " VALUES ";
                    try {
                        for(int n=0; n<mon_count; n++) {
                            String tmp_sql_str = "("
                                        + data_rt.BattGroupId + ", "
                                        + mtest_record_count + ", "
                                        + test_type + ", "
                                        + true + ", "
                                        + true + ", "
                                        + "'" + Com.getDateTimeFormat(rc_time, Com.DTF_YMDhms) + "', "
                                        + data_rt.mTestData.groupVol + ", "
                                        + data_rt.mTestData.testCurr + ", "
                                        + ((n+1)*10 + 1) + ", "
                                        + data_rt.al_MonVol.get(n).monVol + ","
                                        + data_rt.al_MonVol.get(n).monTmp + ","
                                        + data_rt.al_MonVol.get(n).monRes + ","
                                        + data_rt.al_MonVol.get(n).monSer + ","
                                        + data_rt.al_MonVol.get(n).connRes
                                        + ")";
                            if(n > 0) {
                                sql_str += ", ";
                            }
                            sql_str += tmp_sql_str;
                        }
                    } catch (Exception e) {
                        sql.logger.error("BattResStorePro_Thread.insertDataToResTable():" + e.toString(), e);
                    }
                
                    String sql_str2 = "INSERT INTO " + Sql_Mysql.BattResDataInf_Table
                                + " (BattGroupId, "
                                + "test_record_count, "
                                + "test_type, "
                                + "data_new, "
                                + "data_available, "
                                + "test_starttime, "
                                + "test_devtype, "
                                + "record_time, "
                                + "group_vol, "
                                + "test_curr, "
                                + "upload_usr_id) "
                                + " VALUES "
                                + "("+ data_rt.BattGroupId + ", "
                                + mtest_record_count + ", "
                                + test_type + ", "
                                + true + ", "
                                + true + ", "
                                + "'" + Com.getDateTimeFormat(rc_time, Com.DTF_YMDhms) + "', "
                                + 0 + ", "
                                + "'" + Com.getDateTimeFormat(rc_time, Com.DTF_YMDhms) + "', "
                                + 0 + ", "
                                + 0 + ", "
                                + 0 + ") ";
                    if(1 == mtest_record_count) {
                        sql_str2 = "UPDATE " + Sql_Mysql.BattResDataInf_Table + " SET "
                                + "test_type=" + test_type + ", "
                                + "data_new=" + true + ", "
                                + "data_available=" + true + ", "
                                + "test_starttime='" + Com.getDateTimeFormat(rc_time, Com.DTF_YMDhms) + "', "
                                + "test_devtype=" + 0 + ", "
                                + "record_time='" + Com.getDateTimeFormat(rc_time, Com.DTF_YMDhms) + "', "
                                + "group_vol=" + 0 + ", "
                                + "test_curr=" + 0 + ", "
                                + "upload_usr_id=" + 0 + " "
                                + " WHERE BattGroupId=" + data_rt.BattGroupId
                                + " AND test_record_count=" + mtest_record_count;
                    }
                    //System.out.println(sql_str);
                    //System.out.println(sql_str2);
                    sql.mysql_con.setAutoCommit(false);
                    sql.sqlMysqlExecute(sql_str);
                    sql.sqlMysqlExecute(sql_str2);
                    sql.mysql_con.commit();
                } catch (SQLException e) {
                    sql.mysql_con.rollback();
                    //System.out.println(sql_str);
                    sql.logger.error("BattResStorePro_Thread.insertDataToResTable():" + e.toString(), e);
                    result = false;
                } finally {
                    sql.mysql_con.setAutoCommit(true);
                    if(true == result)
                    {
                        break;
                    }
                    else
                    {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            sql.logger.error("BattResStorePro_Thread.insertDataToResTable():" + e.toString(), e);
                        }
                    }
                }
            }
        } catch (SQLException e) {
            sql.logger.error("BattResStorePro_Thread.insertDataToResTable():" + e.toString(), e);
        } finally {
            sql.close_con();
        }
    }
    
    //-----------------------------------------------------------------------------//
    @Override
    public void run() {
        logger.info("BattResStorePro_Thread Started ...");
        
        try {
            for(int s=0; s<6; s++) {
                sleep(1000);
            }
        } catch (InterruptedException e) {
            logger.error(e.toString(), e);
        }
        
        while(true) {
            try {
                sleep(2000);
                
                for(int n=0; n<m_Data.getItemCount(); n++) {
                    BattData_RT rt_data = m_Data.getItem(n);
                    if(((rt_data.FBSDeviceId/1000000) != 910) 
                            && ((rt_data.FBSDeviceId/1000000) != 911)
                            && ((rt_data.FBSDeviceId/1000000) != 91)
                            && ((rt_data.FBSDeviceId/10000) != 9600)
                            && ((rt_data.FBSDeviceId/10000) != 9610)
                            && ((rt_data.FBSDeviceId/100000) != 9616)
                            && ((rt_data.FBSDeviceId/100000) != 3601)) //¼¼Êõ²¿7´çÏÔʾÆÁ
                    {
                        InsertDataToMysql_Task_SQL.get_MonData_From_BattData_RT_RamDB_Table(m_Conn_Pool, rt_data);
                        InsertDataToMysql_Task_SQL.get_ResTestState_From_FBS9100State_RT_RamDB_Table(m_Conn_Pool, rt_data);
                    }
                    
                    if(0x00 == rt_data.mTestData.batt_res_test_state) {
                        try {
                            if(rt_data.al_MonVol.size() > 0) {
                                if(rt_data.al_MonVol.get(0).monRes > 0) {
                                    if(rt_data.mTestData.batt_res_test_cnt_last != rt_data.mTestData.batt_res_test_cnt) {
                                        rt_data.mTestData.batt_res_test_cnt_last = rt_data.mTestData.batt_res_test_cnt;
                                        if(rt_data.mTestData.batt_res_test_cnt > 0) {
                                            rt_data.setStoreBattRes_DataEnStat(true);
                                        }
                                    }
                                }
                            }
                        } catch (Exception e) {
                            logger.error(e.toString(), e);
                        }
                    }
                    
                    if(true == rt_data.getStoreBattRes_DataEnStat()) {
                        logger.info("dev_id:" + rt_data.FBSDeviceId + " bg_id:" + rt_data.BattGroupId
                                    + " Insert Batt Res Data Into DB");
                        
                        insertDataToResTable(m_Conn_Pool, rt_data, BattStatData.BATTDATA_RES);
                        rt_data.setStoreBattRes_DataEnStat(false);
                    }
                }
                //------------------------------------------------------------------------//
            } catch (InterruptedException e) {
                logger.error(e.toString(), e);
            }
        }
    }
}