9度通讯程序适用于9度多组设备
whyclj
2020-09-15 687881fd4e407d4a2f3cb520a9d4d2748b21a848
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
package com.dev.fbs9009;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import com.base.Com;
import com.battdata_rt.BattData_RT;
import com.battdata_rt.BattData_RT_Array;
import com.sql.MysqlConnPool;
 
public class MyModBusPowerOffRecordThread implements Runnable{
    public MysqlConnPool pool;
    public BattData_RT_Array battData;
    public List<RecordThread> threads;
    public final static int Max_Record_Interval = 3;        
    
    public MyModBusPowerOffRecordThread(MysqlConnPool pool,BattData_RT_Array battData) {
        this.pool = pool;
        this.battData = battData;
        threads = new ArrayList<RecordThread>();
    }
 
    @Override
    public void run() {
        System.out.println("MyModBusPowerOffRecordThread Start at " + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));        
        while(true) {
            try {
                for(int i =0;i<battData.getItemCount();i++) {
                    BattData_RT rt = battData.getItem(i);
                    boolean isExsist = false;
                    for(int k = 0;k<threads.size();i++) {
                        if(rt.BattGroupId == threads.get(i).battData.BattGroupId) {
                            //µ±Ç°µç³Ø×éÒÑ´æÔڼǼÏß³Ì
                            isExsist = true;
                            break;
                        }
                    }
                    if(!isExsist) {
                        RecordThread thread = new RecordThread(pool, rt);
                        threads.add(thread);
                        new Thread(thread).start();
                    }
                }                
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }            
        }
    }
    
    //É豸ͣµçÊý¾Ý¼Ç¼Ïß³Ì
    class RecordThread implements Runnable{
        public MysqlConnPool pool;
        public BattData_RT battData;            //µç³Ø×é¶ÔÏó
        
        public int test_record_count = 0;                                //²âÊÔ´ÎÊý
        public int record_num = 1;                                        //¼Ç¼±ÊÊý
        public int start_test_type = MyModBusCom.TestType_NULL;        
        public Date test_starttime = new Date();
        public Date record_time = new Date();
        public double test_cap = 0;                                        //²âÊÔÈÝÁ¿
        public long timelong = 0;
        
        public RecordThread(MysqlConnPool pool,BattData_RT battData) {
            this.pool = pool;
            this.battData = battData;
        }
 
        @Override
        public void run() {
            //´´½¨µ±Ç°µç³Ø×éµÄ²âÊԼǼ±í¸ñ     _inf±í
            MyModBusSocketThread_SQL.createTb_ld9testdata_inf(pool);        
            //´´½¨µ±Ç°µç³Ø×éµÄ²âÊԼǼ±í¸ñ     _id±í
            MyModBusSocketThread_SQL.createTb_ld9testdata(pool,battData.BattGroupId);
            //´´½¨µ±Ç°µç³Ø×éµÄ²âÊԼǼ±í¸ñ   stop_id±í
            MyModBusSocketThread_SQL.createTb_ld9testdatastop(pool,battData.BattGroupId);
            
            Date last = new Date(0);
            boolean isStartRecord = false;                            //ÊÇ·ñ¿ªÊ¼¼Ç¼Êý¾Ý
            boolean isNewStart = true;
            int now_test_type = MyModBusCom.TestType_NULL;            //µ±Ç°²âÊÔÀàÐÍ    
            Date now = null;
            boolean now_state = false;
            while(true) {
                try {
                    now = new Date();
                    now_state = battData.checkIFLD9State();
                    if(isStartRecord == false && now_state == true) {
                        //³õ´Î¼ì²âµ½¼à²â³ä·Åµç
                        isNewStart = true;
                        isStartRecord = true;
                        start_test_type = battData.getMyBattTestType();
                        now_test_type = start_test_type;
                        record_num = 1;
                        test_cap = 0;
                        test_starttime = new Date();
                        LD_sys_state state = MyModBusSocketThread_SQL.searchMaxTestRecordByBattGroupId(pool, battData.BattGroupId);
                        if(state == null) {
                            test_record_count = 1;                            
                        }else {
                            test_record_count = state.getTest_record_count()+1;
                        }
                    }
                    now_test_type = battData.getMyBattTestType();
                    if(isStartRecord && now_test_type != start_test_type) {
                        //µ±Ç°²âÊÔÀàÐͱ仯ʱ½áÊøµ±Ç°¼Ç¼
                        now_state = false;
                    }
                    if(isStartRecord && now_state == false) {
                        //³ä·Åµç½áÊøÖ±½Ó¼Ç¼µ±Ç°Êý¾Ý
                        isNewStart = true;
                    }
                    if(isStartRecord) {
                        //¼ä¸ôʱ³¤
                        long timelong = (now.getTime()-last.getTime())/1000;
                        //ÅжϼǼʱ³¤
                        if(timelong >= Max_Record_Interval || isNewStart) {
                            
                            record_time = new Date();
                            timelong = (record_time.getTime()-test_starttime.getTime())/1000;
                            //¼ÆËã²âÊÔÈÝÁ¿
                            test_cap += (battData.myModBusData.getConcenState().getGroupCurr()*timelong)/3600;
                            
                            //¼Ç¼Êý¾Ý
                            MyModBusSocketThread_SQL.insertOrUpdateMuilMonDataInf(pool, this);        //¸üвâÊÔµÄinf±íÖеÄÊý¾Ý
                            MyModBusSocketThread_SQL.insertOrUpdateMutilMonData(pool, this);        //¸üвâÊÔ_ID±íÖеÄÊý¾Ý
                            MyModBusSocketThread_SQL.insertOrUpdateMutilMonStopData(pool, this);    //¸üвâÊÔStop±íÖеÄÊý¾Ý
                            
                            if(!now_state) {
                                isStartRecord = false;                                
                            }
                            isNewStart = false;
                            record_num ++;
                            last = now;
                        }
                    }
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
}