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
package com.northglass.service.TelecomsService;
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import com.northglass.constants.StateConstants.ConnectState;
import com.northglass.constants.StateConstants.GaoliweiMachineState;
import com.northglass.entity.LoadRack;
import com.northglass.entity.Machine;
import com.northglass.entity.OutTasks;
import com.northglass.entity.Telecoms;
import com.northglass.listener.MachineClientListener;
import com.northglass.repository.LoadRackDao;
import com.northglass.repository.MachineDao;
import com.northglass.repository.OutTasksDao;
import com.northglass.repository.TelecomsDao;
import com.northglass.service.message.MessageProcessor;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
@Component
@Transactional
public class TelecomsService {
    @Autowired
    private TelecomsDao telecomsDao;
    @Autowired
    private OutTasksDao outTasksDao;
    @Autowired
    private MachineDao machineDao;
    @Autowired
    private LoadRackDao loadRackDao;
 
    @Autowired
    private MessageProcessor processor;
    
    private static final Logger LOGGER = LoggerFactory.getLogger(TelecomsService.class);
    public static JSONObject jsonObj = new JSONObject();
    
    public void connect(Telecoms telecoms) {
        try {
            ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
            int total = Thread.activeCount();
            Thread[] threads = new Thread[total];
            threadGroup.enumerate(threads);
            boolean isexist=true;
            for (Thread therad:threads){
                if(telecoms.getNumber().equals(therad.getName())){
                    isexist=false;
                    break;
                }
            }
            if(isexist){
                String Log="创建:"+telecoms.getNumber()+","+telecoms.getipAddress();
                LOGGER.debug(Log);
                Thread thread = new Thread(new MachineClientListener(telecoms,this));
                thread.start();    
                thread.setName(telecoms.getNumber());
            }
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("无法开机!!");
        }
        
    }
    public void Stop(Telecoms telecoms) {
        ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
        int total = Thread.activeCount();
        Thread[] threads = new Thread[total];
        threadGroup.enumerate(threads);
        for (Thread therad:threads){
            if(telecoms.getNumber().equals(therad.getName())){
                String Log="开始关闭连接:"+telecoms.getNumber()+","+telecoms.getipAddress();
                LOGGER.debug(Log);      
                therad.interrupt();
                LOGGER.debug(telecoms.getNumber()+"是否已关闭:" + therad.isInterrupted());        
                therad.stop();
            }
        }
    }
 
    public String generateReturnMessage(String sendMessageHex, Telecoms telecoms) {
        return processor.generateReturnMessage(sendMessageHex, telecoms);
    }
    //保存TEXT文本
        public static void WriteFile(String URL,String FileName,String Content,boolean isappend){
            try {
                String FileUrl=URL+File.separator+FileName;
                   File file=new File(FileUrl);
                   if(file.exists()){
                       file.createNewFile();
                   }
                   OutputStream out =new FileOutputStream(file,isappend);
                   Content="\r"+Content;
                   byte[] b=Content.getBytes();
                   out.write(b);
                   out.close();
            } catch (Exception e) {
                // TODO: handle exception
            }    
        }
    public void TelecomsConnState(Telecoms telecoms,String state){
        telecoms.setConnState(state);
        telecoms.setModTime(new Date());
        telecomsDao.save(telecoms);
    }
    
}