package bin;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.apache.log4j.Logger;
import util.DBhandle;
public class CJServer extends JFrame{
private JTextArea area;
private JButton button;
private JButton button2;
private int num=0;
public boolean key=true;
public HashMap<String, Socket> sockets;
public ServerSocket ss;
public static DBhandle hand=new DBhandle();
public Thread thread;
private String sjdata;//实时数据,为了验证本次与上次数据是否相同
private static Logger log = Logger.getLogger(CJServer.class);
public CJServer(){
try {
sockets=new HashMap<String, Socket>();
ss = new ServerSocket(8881);
System.out.println("22222222222222222222222222");
} catch (IOException e) {
e.printStackTrace();
}
area=new JTextArea(20,40);
area.setEditable(false);
button=new JButton("开启");
//button2=new JButton("关闭");
init();
addEventHandler();
}
public void init(){
JScrollPane jsp=new JScrollPane(area);
this.add(jsp,BorderLayout.CENTER);
JPanel panel=new JPanel();
panel.add(button);
//panel.add(button2);
this.add(panel,BorderLayout.SOUTH);
}
public void addEventHandler(){
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(key){
key=!key;
startService();
}
}
});
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
}
public void startService(){
thread=new ServerMain(ss);
thread.start();
}
private class ServerMain extends Thread{
private ServerSocket soc;
public ServerMain(ServerSocket s){
this.soc=s;
showMesg("服务4已开启");
}
public void run(){
while(true){
Socket s;
try {
s = soc.accept();
Date date=new Date();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
area.append("["+df.format(date)+"]有设备已连接,IP为:"+s.getInetAddress()+"\n");
num=num+1;
new ServerThread(s).start();
} catch(OutOfMemoryError o){
Date date=new Date();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
area=null;
sjdata=null;
area=new JTextArea();
area.append("数据清空---["+df.format(date)+"]\n");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
private class ServerThread extends Thread{
private Socket s;
private String sname;
private int type=1;
private int key=1;//控制 第一次读取
public ServerThread(Socket s){
this.s=s;
}
public void run(){
byte[] buff = new byte[1024];
String str = "";
try {
InputStream inputStream = s.getInputStream();
int rcvLength;
while ((rcvLength = inputStream.read(buff)) >0){
if(rcvLength>1024){
showMesg("数据过长-----");
sendsuccess(str);
}else{
byte[] bt2 = new byte[rcvLength];
for (int i = 0; i < rcvLength; i++) {
bt2[i] = buff[i];
}
str = printHexString(bt2);
//showMesg(str);
if(key==1){
//str="4A4E4C4F473A3030303030303031";
if(str.indexOf("4A4E4C4F473A")>-1){
int num=str.indexOf("4A4E4C4F473A");
String str3=str.substring(num+12, 28+num);//3030303030303031
key=2;
sname=toHexString2(str3);
sockets.put(sname, s);//00000001
hand.sfkq(sname, "1");
showMesg("注册码为: "+sname+" 已连上!");
sendsuccess(str);
data_save(str);
}else{
sendsuccess(str);
}
}else{
if(str.length()>4){
if(str.substring(0, 3).equals("550") && !str.substring(0, 4).equals("5507")){
sendsuccess(str);
data_save(str);
}
}else{
//Date date=new Date();
//DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
log.info("茶机编号:"+sname+":"+str);
}
}
}
}
sockets.remove(sname);
num=num-1;
//System.out.println("关闭。。。。");
Date date=new Date();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
hand.sfkq(sname, "0");
key=1;
area.append("["+df.format(date)+"]IP为:"+s.getInetAddress()+"已断开"+sname+"\n");
}catch(OutOfMemoryError o){
Date date=new Date();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
area=null;
sjdata=null;
area=new JTextArea();
area.append("数据清空---["+df.format(date)+"]\n");
}
catch(Exception e){
sockets.remove(sname);
num=num-1;
//System.out.println("关闭。。。。");
Date date=new Date();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
hand.sfkq(sname, "0");
key=1;
area.append("["+df.format(date)+"]IP为:"+s.getInetAddress()+"异常断开"+sname+"\n");
e.printStackTrace();
}
}
public void sendsuccess(String mesg)throws IOException{
Date date=new Date();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
PrintWriter pw=new PrintWriter(s.getOutputStream());
pw.println("001\t["+date+"]");
pw.flush();
}
public void data_save(String mesg){
if(mesg.substring(0, 4).equals("5502")){
if(mesg.length()>22)mesg=mesg.substring(0, 22);
hand.ztsave(sname,mesg,type);//状态数据
}
else if(mesg.substring(0, 4).equals("5503")){
hand.czsave(sname,mesg,type);//操作数据
}
else if(mesg.substring(0, 4).equals("5504")){
hand.xzsave(sname,mesg,type);//下载数据
}
else if(mesg.substring(0, 4).equals("5505")){
hand.scsave(sname,mesg,type);//上传数据
}
else if(mesg.substring(0, 4).equals("5506")){
if(mesg.length()>12)mesg=mesg.substring(0, 12);
hand.gzsave(sname,mesg,type);//故障数据
}
else if(mesg.substring(0, 4).equals("5500")){
if(mesg.length()>16)mesg=mesg.substring(0, 16);
hand.wzsave(sname,mesg,type);//位置数据
}
else if(mesg.substring(0, 4).equals("5501")){
if(mesg.length()>32)mesg=mesg.substring(0, 32);
//System.out.println("实时数据1--"+sjdata);
//System.out.println("实时数据2--"+mesg);
//如果实时数据不为空,且上次不等下本次数据
if(null==sjdata){
sjdata=mesg;
hand.save(sname,mesg,type);//实时数据 5501
}else if(!mesg.equals(sjdata)){
sjdata=mesg;
hand.save(sname,mesg,type);//实时数据 5501
}
}
}
}
public void showMe(){
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
public static String printHexString(byte[] b) {
String str="";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
str=str+hex.toUpperCase();
}
return str;
}
public static String toHexString(String s)
{
String str="";
for (int i=0;i<s.length();i++)
{
int ch = (int)s.charAt(i);
String s4 = Integer.toHexString(ch);
str = str + s4;
}
return str;
}
public static String toHexString2(String hex){
/*String str="";
for(int i=0;i<s.length()/2;i++){
str=str+Integer.valueOf(s.substring(i*2, (1+i)*2),16);
}
return str;*/
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
for( int i=0; i<hex.length()-1; i+=2 ){
String output = hex.substring(i, (i + 2));
int decimal = Integer.parseInt(output, 16);
sb.append((char)decimal);
temp.append(decimal);
}
return sb.toString();
}
public void showMesg(String msg){
Date date=new Date();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
area.append(msg+"\t["+df.format(date)+"]\n");
}catch(Exception e){
area=null;
sjdata=null;
area=new JTextArea();
area.append("数据清空---["+df.format(date)+"]\n");
area.append(msg+"\t["+df.format(date)+"]\n");
}
}
public static void main(String[] args) {
new CJServer().showMe();
}
}