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);
|
}
|
|
}
|