廖井涛
2024-03-04 eae17d27ec56a6b7887f5597335e38ca40273ef4
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
package toTcp;
 
 
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import ng.devices.HexUtil;
 
import java.io.*;
import java.net.Socket;
import java.util.LinkedHashMap;
import java.util.LinkedList;
 
import static druidConnect.getMeterParatermeter.meterToMysql;
 
public class TcpClientHandler extends Thread {
    private Socket clientSocket;
    private PrintWriter out;
    private InputStream in;
    private Socket socket;
    private LinkedList<LinkedHashMap> meterParameter;
    public TcpClientHandler(Socket socket, LinkedList<LinkedHashMap> meterParameter) {
        this.clientSocket = socket;
        this.meterParameter=meterParameter;
    }
 
    public void run() {
        try {
            while (true) {
                try {
                    System.out.println(clientSocket.getInetAddress().getHostAddress()+"¿Í»§¶ËÁ¬½ÓÉÏÁË");
                    InputStream in = clientSocket.getInputStream();
                    ByteArrayOutputStream output = new ByteArrayOutputStream();
 
                    byte[] b = new byte[20];
 
                    int len;
                    while ( (len = in.read(b)) != -1) {
                        output.write(b, 0, len);
 
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }
                        if(in.available() == 0) {
                            output.reset();
                        }
 
                        String result=new String(b, 0, len);
                        String HexDLT645=gethex.BinaryToHexString(b);
                        if(result.equals("123")){
                            for (int i = 0; i <meterParameter.size() ; i++) {
                                String hexString="";
                                if(meterParameter.get(i).get("ip").equals(clientSocket.getInetAddress().getHostAddress())){
                                    hexString= (String) meterParameter.get(i).get("hexdlt645");
                                    byte[] data = HexUtil.stringToInt(hexString);
                                    DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
                                    out.write(data);
                                    out.flush();
 
                                }
                            }
 
 
                        }else if(HexDLT645.indexOf("68")>-1){
                            String HEX=HexDLT645.replace(" ", "");
                            for (int i = 0; i < meterParameter.size(); i++) {
                                if(HEX.indexOf((String) meterParameter.get(i).get("addless"))>-1 && meterParameter.get(i).get("addless")!="" ){
                                    Double electricNum = gethex.toSqlElectric(HexDLT645);
                                    String ratio = (String) meterParameter.get(i).get("ratio");
                                    electricNum = Math.round(electricNum * Integer.parseInt(ratio) ) * 1d;
                                    String meterID = (String) meterParameter.get(i).get("id");
                                    meterToMysql(meterID,electricNum,HEX);
                                }
                            }
 
                            //System.out.println(HEX);
                        }
 
                        b = new byte[20];
                        len = 0;
 
                    }
                    output.close();
                    in.close();
 
                }catch (Exception e){
                    //
                    System.out.println("tcpµç±í¿Í»§¶ËÒѶϿªÁ¬½Ó---------"+e);
                    break;
                }
 
 
                //inputLine += new String(temp, 0, length);
                //System.out.println(inputLine);
//                if (".".equals(inputLine)) {
//                    System.out.println(inputLine);
//                    System.out.println("tcpµç±í¿Í»§¶ËÒѶϿªÁ¬½Ó1---------");
//                    //out.println("bye");
//                    break;
//                }
            }
 
            clientSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
 
        }
    }
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
}