package com.dev.data;
|
|
import java.io.BufferedWriter;
|
import java.io.File;
|
import java.io.FileOutputStream;
|
import java.io.FileWriter;
|
import java.io.IOException;
|
import java.io.OutputStreamWriter;
|
import java.io.PrintWriter;
|
import java.util.Date;
|
|
import org.apache.log4j.chainsaw.Main;
|
|
import com.base.Com;
|
import com.base.ComFn;
|
|
/**
|
* ¼Ç¼UDPÊý¾ÝÖ¡
|
* @author LiJun
|
*
|
*/
|
public class RecordUDPPackage {
|
public static final int PackageType_ChildStood = 1; //×ÓÕ¾Êý¾Ý°ü
|
public static final int PackageType_Battery = 2; //Ðîµç³Ø×éÊý¾Ý
|
public static final int PackageType_InverPower = 3; //Äæ±äµçÔ´Êý¾Ý
|
public static final int PackageType_SteamTurbine = 4; //ÆûÂÖ·¢µç»úÊý¾Ý
|
public static final int PackageType_SwitchBoard = 5; //¿ª¹ØÁ¿°åÊý¾Ý
|
|
//udp±¨Îı¸·ÝĿ¼
|
//public static final String BACKUP_FILEPATH = "D:/tomcat7/webapps/ftpbackup/updbackup/";
|
public static final String BACKUP_FILEPATH = "D:/tomcat7/webapps/ftpbackup/updbackup/";
|
|
static {
|
File file = new File(BACKUP_FILEPATH);
|
if(!file.exists()) {
|
file.mkdir();
|
}
|
}
|
|
|
/**
|
* ¼Ç¼Êý¾ÝÖ¡
|
* @param packagetype
|
* @param ip
|
* @param arr
|
*/
|
public static void RecordUDPPackageData(int packagetype,String ip,byte[] source) {
|
File file = null;
|
switch(packagetype) {
|
case PackageType_Battery:{
|
file = new File(BACKUP_FILEPATH+"battery_"+Com.getDateTimeFormat(new Date(), Com.DTF_Y_M_D)+".log");
|
}break;
|
case PackageType_ChildStood:{
|
file = new File(BACKUP_FILEPATH+"childstood_"+Com.getDateTimeFormat(new Date(), Com.DTF_Y_M_D)+".log");
|
}break;
|
case PackageType_InverPower:{
|
file = new File(BACKUP_FILEPATH+"interpower_"+Com.getDateTimeFormat(new Date(), Com.DTF_Y_M_D)+".log");
|
}break;
|
case PackageType_SteamTurbine:{
|
file = new File(BACKUP_FILEPATH+"steamturbine_"+Com.getDateTimeFormat(new Date(), Com.DTF_Y_M_D)+".log");
|
}break;
|
case PackageType_SwitchBoard:{
|
file = new File(BACKUP_FILEPATH+"switchboard_"+Com.getDateTimeFormat(new Date(), Com.DTF_Y_M_D)+".log");
|
}break;
|
}
|
if(null != file) {
|
BufferedWriter out = null;
|
if(!file.exists()) {
|
try {
|
file.createNewFile();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
//System.out.println("¿ªÊ¼Ð´Èë");
|
try {
|
|
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
|
out.write(Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms)+":"+ip+"#"+ComFn.bytesToHexString(source, source.length));
|
out.write("\n");
|
out.flush();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
if(out != null) {
|
try {
|
out.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
|
}
|
|
/**
|
* ¼Ç¼Êý¾ÝÖ¡
|
* @param packagetype
|
* @param ip
|
* @param arr
|
*/
|
public static void RecordUDPPackageData(int packagetype,String ip,byte[] source,Date time) {
|
File file = null;
|
switch(packagetype) {
|
case PackageType_Battery:{
|
file = new File(BACKUP_FILEPATH+"battery_"+Com.getDateTimeFormat(time, Com.DTF_Y_M_D)+".log");
|
}break;
|
case PackageType_ChildStood:{
|
file = new File(BACKUP_FILEPATH+"childstood_"+Com.getDateTimeFormat(time, Com.DTF_Y_M_D)+".log");
|
}break;
|
case PackageType_InverPower:{
|
file = new File(BACKUP_FILEPATH+"interpower_"+Com.getDateTimeFormat(time, Com.DTF_Y_M_D)+".log");
|
}break;
|
case PackageType_SteamTurbine:{
|
file = new File(BACKUP_FILEPATH+"steamturbine_"+Com.getDateTimeFormat(time, Com.DTF_Y_M_D)+".log");
|
}break;
|
case PackageType_SwitchBoard:{
|
file = new File(BACKUP_FILEPATH+"switchboard_"+Com.getDateTimeFormat(time, Com.DTF_Y_M_D)+".log");
|
}break;
|
}
|
if(null != file) {
|
BufferedWriter out = null;
|
if(!file.exists()) {
|
try {
|
file.createNewFile();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
//System.out.println("¿ªÊ¼Ð´Èë");
|
try {
|
|
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
|
out.write(Com.getDateTimeFormat(time, Com.DTF_YMDhms)+":"+ip+"#"+ComFn.bytesToHexString(source, source.length));
|
out.write("\n");
|
out.flush();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
if(out != null) {
|
try {
|
out.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
|
}
|
|
public static void main(String[] args) {
|
for(int i=0;i<10;i++) {
|
RecordUDPPackageData(1, "127.0.0.1", "hhehhehe".getBytes());
|
}
|
}
|
}
|