whyclj
2020-12-05 6b1b6b46fad3c7357706670994e1a23620d6c5c3
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
package com.data.Comm;
 
import com.data.Central_RT_Data;
import com.data.Central_ST_Data;
import com.data.Motor_inf;
import com.modbus.data.MyModbusMaster;
import com.modbus.data.MyModbusUtils;
import com.serotonin.modbus4j.BatchRead;
import com.serotonin.modbus4j.BatchResults;
import com.sql.MysqlConnPool;
 
public class Central_Data_SocketClient_Thread implements Runnable{
    public static int Dev_Type_LineScreen = 10004;            //½ø³öÏ߯Á
    public static int Dev_Type_DcDistribute = 10005;        //Ö±Á÷Ö÷Åä
    
    private MysqlConnPool pool;
    private Motor_inf motor;
    private MyModbusMaster master;
    
    public Central_Data_SocketClient_Thread(MysqlConnPool pool, Motor_inf motor) {
        this.motor = motor;
        this.pool = pool;
        
        master = new MyModbusMaster(motor.getDev_ip(), MyModbusMaster.SLAVEID_DEFAULT);
    }
 
    @Override
    public void run() {
        int run_count = 0;
        while(true) {
            try { 
                if(run_count % 2 == 0) {
                    if(Dev_Type_DcDistribute == motor.getDev_id()) {
                        //¶ÁȡֱÁ÷Ö÷ÅäÐÅÏ¢
                        readDcDistributinf(motor.stdata);
                        Central_DataParsing_Thread_SQL.insertOrUpdateTb_Central_Monitor_Sys_St(pool, motor.stdata);
                    }else {
                        //¶ÁÈ¡½ø³öÏ߯ÁÊý¾ÝÐÅÏ¢
                        readLineScreeninf(motor.rtdata);
                        Central_DataParsing_Thread_SQL.insertOrUpdateTb_Central_Monitor_Sys_Rt(pool, motor.rtdata);
                    }                    
                }    
                
                
                
                
                motor.conn_state = master.getConnectState();
                if(run_count > 999909) {
                    run_count = 0;
                }
                run_count ++;                
                Thread.sleep(200);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }        
    }
    
    /**
     *     ¶ÁȡֱÁ÷Ö÷ÅäÐÅÏ¢
     * @param stdata
     */
    public void readDcDistributinf(Central_ST_Data stdata) {
        BatchRead<Integer> batch = stdata.createBatchRead(master);    
        BatchResults<Integer> res = MyModbusUtils.readMutilRegisters(batch, master);
        stdata.putBatchResult(res);
    }
    
    /**
     *     ¶ÁÈ¡½ø³öÏ߯ÁÐÅÏ¢
     * @param stdata
     */
    public void readLineScreeninf(Central_RT_Data rtdata) {
        BatchRead<Integer> batch = rtdata.createBatchRead(master);    
        BatchResults<Integer> res = MyModbusUtils.readMutilRegisters(batch, master);
        rtdata.putBatchResult(res);
    }
    
}