Ei kuvausta

CollectService.java 8.8KB

    package com.ekexiu.project.bridge.collect; import com.ekexiu.project.bridge.resource.dao.BridgeServerDao; import com.ekexiu.project.bridge.resource.po.BridgeServer; import org.jfw.apt.annotation.Autowrie; import org.jfw.apt.web.annotation.Path; import org.jfw.apt.web.annotation.operate.Get; import org.jfw.apt.web.annotation.param.JdbcConn; import org.jfw.util.ConstData; import org.jfw.util.DateUtil; import org.jfw.util.ListUtil; import org.jfw.util.io.IoUtil; import org.jfw.util.jdbc.JdbcUtil; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; @Path("/collect") public class CollectService { private Map<String, List<WaveDataItem>> waveCache; private File backupPath; @Autowrie private CollectDao collectDao; @Autowrie private BridgeServerDao bridgeServerDao; public CollectService() { try { this.backupPath = new File("/bridge/wave_backup"); } catch (Exception e) { } } public BridgeServerDao getBridgeServerDao() { return bridgeServerDao; } public void setBridgeServerDao(BridgeServerDao bridgeServerDao) { this.bridgeServerDao = bridgeServerDao; } public File getBackupPath() { return backupPath; } public void setBackupPath(File backupPath) { this.backupPath = backupPath; } public CollectDao getCollectDao() { return collectDao; } public void setCollectDao(CollectDao collectDao) { this.collectDao = collectDao; } public Map<String, List<WaveDataItem>> getWaveCache() { return waveCache; } public void setWaveCache(Map<String, List<WaveDataItem>> waveCache) { this.waveCache = waveCache; } @Get @Path("/wave/curr") public List<WaveDataItem> wave(String[] seq) { List<WaveDataItem> ret = null; List<WaveDataItem> retAll = new ArrayList<>(); for (String aseq : seq) { synchronized (this.waveCache) { ret = waveCache.get(aseq); } if(ret!=null) { retAll.addAll(ret); } } return retAll; } @Get @Path("/wave/server/day") public List<WaveDataItem> waveByDay(@JdbcConn Connection con, String[] seq, String day) throws SQLException, IOException { List<WaveDataItem> ret = new LinkedList<WaveDataItem>(); for (String s : seq) { File path = new File(this.backupPath, s); if (path.exists() && path.isDirectory()) { File file = new File(path, day + ".wave"); if (file.exists() && file.isFile()) { InputStream in = null; try { in = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(in, ConstData.UTF8)); String line; while ((line = reader.readLine()) != null) { List<String> ls = ListUtil.splitTrimExcludeEmpty(line, ','); if (ls.size() != 5) { throw new IOException("invalid context:line===>" + line); } WaveDataItem wdi = new WaveDataItem(); wdi.setSeq(s); wdi.setHvalue(Integer.parseInt(ls.get(0))); wdi.setLvalue(Integer.parseInt(ls.get(1))); wdi.setCid(Integer.parseInt(ls.get(2))); wdi.setDid(Integer.parseInt(ls.get(3))); wdi.setCtime(ls.get(4)); ret.add(wdi); } reader.close(); } finally { if (in != null) { IoUtil.close(in); } } } } } return ret; } @Get @Path("/wave/server/dayTime") public List<WaveDataItem> waveByDayTime(@JdbcConn Connection con, String[] seq, String begin,String end) throws SQLException, IOException { List<WaveDataItem> ret = new LinkedList<WaveDataItem>(); for (String s : seq) { File path = new File(this.backupPath, s); if (path.exists() && path.isDirectory()) { File file = new File(path, begin.substring(0,8) + ".wave"); if (file.exists() && file.isFile()) { InputStream in = null; try { in = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(in, ConstData.UTF8)); String line; while ((line = reader.readLine()) != null) { List<String> ls = ListUtil.splitTrimExcludeEmpty(line, ','); if (ls.size() != 5) { throw new IOException("invalid context:line===>" + line); } WaveDataItem wdi = new WaveDataItem(); wdi.setSeq(s); wdi.setHvalue(Integer.parseInt(ls.get(0))); wdi.setLvalue(Integer.parseInt(ls.get(1))); wdi.setCid(Integer.parseInt(ls.get(2))); wdi.setDid(Integer.parseInt(ls.get(3))); wdi.setCtime(ls.get(4)); String time = wdi.getCtime(); if((time.compareTo(begin)>=0)&&(time.compareTo(end)<=0)) { ret.add(wdi); } } reader.close(); } finally { if (in != null) { IoUtil.close(in); } } } } } return ret; } @Get @Path("/wave/server/time") public List<WaveDataItem> waveByDay(@JdbcConn Connection con, String[] seq, String begin, String end) throws SQLException { return this.collectDao.query(con, seq, begin, end); } @Get @Path("/wave/box/time") public List<WaveDataItem> waveByDay(@JdbcConn Connection con, String seq, int box, String begin, String end) throws SQLException { return this.collectDao.query(con, seq, box, begin, end); } @Get @Path("/wave/channel/time") public List<WaveDataItem> waveByDay(@JdbcConn Connection con, String seq, int box, int channel, String begin, String end) throws SQLException { return this.collectDao.query(con, seq, box, channel, begin, end); } @Get @Path("/wave/backup") public void backup(@JdbcConn(true) Connection con) throws SQLException { List<BridgeServer> srvs = this.bridgeServerDao.queryAll(con); long time = System.currentTimeMillis(); String yd = DateUtil.formatDate(time - 1000 * 60 * 60 * 24); String yyd = DateUtil.formatDate(time - 1000 * 60 * 60 * 24 * 8); List<String> error = new LinkedList<String>(); for (BridgeServer srv : srvs) { if (srv.isActive()) { File path = new File(this.backupPath, srv.getSeq()); if (!path.exists()) { if (!path.mkdir()) { error.add("mkdir:" + path.getAbsolutePath()); continue; } } if (path.isFile()) { if (!path.delete()) { JdbcUtil.rollback(con); error.add("delete file:" + path.getAbsolutePath()); continue; } if (!path.mkdir()) { error.add("mkdir:" + path.getAbsolutePath()); continue; } } File file = new File(path, yd + ".wave"); if (file.exists()) { error.add("file exists:" + file.getAbsolutePath()); continue; } try { FileOutputStream fos = new FileOutputStream(file); try { Writer write = new OutputStreamWriter(fos, ConstData.UTF8); PreparedStatement ps = con.prepareStatement( "SELECT HVALUE,LVALUE,CID,DID, CTIME FROM WAVE_DATA_ITEM WHERE SEQ =? AND CTIME LIKE ? ORDER BY DID,CID,CTIME"); try { ps.setString(1, srv.getSeq()); ps.setString(2, yd + "%"); ResultSet rs = ps.executeQuery(); try { while (rs.next()) { write.write(Integer.toString(rs.getInt(1))); write.write(","); write.write(Integer.toString(rs.getInt(2))); write.write(","); write.write(Integer.toString(rs.getInt(3))); write.write(","); write.write(Integer.toString(rs.getInt(4))); write.write(","); write.write(rs.getString(5)); write.write("\n"); } } finally { JdbcUtil.close(rs); } } finally { JdbcUtil.close(ps); } write.flush(); write.close(); fos.flush(); } finally { fos.close(); } } catch (IOException e) { continue; } } } JdbcUtil.execute(con, "DELETE FROM WAVE_DATA_ITEM WHERE CTIME LIKE '" + yyd + "%'"); } }