jiapeng 6 gadi atpakaļ
vecāks
revīzija
c6e65d6e66

+ 89 - 84
src/main/java/com/ekexiu/project/bridge/collect/CollectService.java

@ -30,7 +30,6 @@ import org.jfw.util.jdbc.JdbcUtil;
30 30
import com.ekexiu.project.bridge.resource.dao.BridgeServerDao;
31 31
import com.ekexiu.project.bridge.resource.po.BridgeServer;
32 32

33

34 33
@Path("/collect")
35 34
public class CollectService {
36 35

@ -41,13 +40,14 @@ public class CollectService {
41 40
	@Autowrie
42 41
	private BridgeServerDao bridgeServerDao;
43 42

44
	public CollectService(){
45
		try{
43
	public CollectService() {
44
		try {
46 45
			this.backupPath = new File("/bridge/wave_backup");
47
		}catch(Exception e){}
48
		
46
		} catch (Exception e) {
47
		}
48

49 49
	}
50
	
50

51 51
	public BridgeServerDao getBridgeServerDao() {
52 52
		return bridgeServerDao;
53 53
	}
@ -89,25 +89,25 @@ public class CollectService {
89 89
		}
90 90
		return ret;
91 91
	}
92
	
92

93 93
	@Get
94 94
	@Path("/wave/server/day")
95
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con,String[] seq,String day)throws SQLException, IOException{
95
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con, String[] seq, String day) throws SQLException, IOException {
96 96
		List<WaveDataItem> ret = new LinkedList<WaveDataItem>();
97
		for(String s:seq){
98
			File path = new File(this.backupPath,s);
99
			if(path.exists() && path.isDirectory()){
100
				File file = new File(path,day+".wave");
101
				if(file.exists() && file.isFile()){
97
		for (String s : seq) {
98
			File path = new File(this.backupPath, s);
99
			if (path.exists() && path.isDirectory()) {
100
				File file = new File(path, day + ".wave");
101
				if (file.exists() && file.isFile()) {
102 102
					InputStream in = null;
103
					try{
103
					try {
104 104
						in = new FileInputStream(file);
105 105
						BufferedReader reader = new BufferedReader(new InputStreamReader(in, ConstData.UTF8));
106 106
						String line;
107
						while((line = reader.readLine())!=null){
108
							List<String> ls = ListUtil.splitTrimExcludeEmpty(line,',');
109
							if(ls.size()!=5){
110
								throw new IOException("invalid context:line===>"+line);
107
						while ((line = reader.readLine()) != null) {
108
							List<String> ls = ListUtil.splitTrimExcludeEmpty(line, ',');
109
							if (ls.size() != 5) {
110
								throw new IOException("invalid context:line===>" + line);
111 111
							}
112 112
							WaveDataItem wdi = new WaveDataItem();
113 113
							wdi.setSeq(s);
@ -119,8 +119,8 @@ public class CollectService {
119 119
							ret.add(wdi);
120 120
						}
121 121
						reader.close();
122
					}finally{
123
						if(in != null){
122
					} finally {
123
						if (in != null) {
124 124
							IoUtil.close(in);
125 125
						}
126 126
					}
@ -129,98 +129,103 @@ public class CollectService {
129 129
		}
130 130
		return ret;
131 131
	}
132
	
132

133 133
	@Get
134 134
	@Path("/wave/server/time")
135
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con,String[] seq,String begin,String end)throws SQLException{
136
		return this.collectDao.query(con, seq, begin,end);
135
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con, String[] seq, String begin, String end) throws SQLException {
136
		return this.collectDao.query(con, seq, begin, end);
137 137
	}
138

138 139
	@Get
139 140
	@Path("/wave/box/time")
140
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con,String seq,int box,String begin,String end)throws SQLException{
141
		return this.collectDao.query(con, seq,box, begin,end);
141
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con, String seq, int box, String begin, String end) throws SQLException {
142
		return this.collectDao.query(con, seq, box, begin, end);
142 143
	}
144

143 145
	@Get
144 146
	@Path("/wave/channel/time")
145
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con,String seq,int box,int channel,String begin,String end)throws SQLException{
146
		return this.collectDao.query(con, seq,box,channel,begin,end);
147
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con, String seq, int box, int channel, String begin, String end) throws SQLException {
148
		return this.collectDao.query(con, seq, box, channel, begin, end);
147 149
	}
148
	
149 150

150 151
	@Get
151 152
	@Path("/wave/backup")
152
	public void backup(@JdbcConn(true) Connection con)throws SQLException{
153
	public void backup(@JdbcConn(true) Connection con) throws SQLException {
153 154
		List<BridgeServer> srvs = this.bridgeServerDao.queryAll(con);
154 155
		long time = System.currentTimeMillis();
155
		String yd = DateUtil.formatDate(time-1000*60*60*24);
156
		String yyd = DateUtil.formatDate(time-1000*60*60*24*8);
157
		
156
		String yd = DateUtil.formatDate(time - 1000 * 60 * 60 * 24);
157
		String yyd = DateUtil.formatDate(time - 1000 * 60 * 60 * 24 * 8);
158

158 159
		List<String> error = new LinkedList<String>();
159
		
160
		for(BridgeServer srv:srvs){
161
			File path = new File(this.backupPath,srv.getSeq());
162
			if(!path.exists()){
163
				if(!path.mkdir()){
164
					error.add("mkdir:"+path.getAbsolutePath());
165
					continue;
160

161
		for (BridgeServer srv : srvs) {
162
			if (srv.isActive()) {
163

164
				File path = new File(this.backupPath, srv.getSeq());
165
				if (!path.exists()) {
166
					if (!path.mkdir()) {
167
						error.add("mkdir:" + path.getAbsolutePath());
168
						continue;
169
					}
166 170
				}
167
			}
168
			if(path.isFile()){
169
				if(!path.delete()){
170
					JdbcUtil.rollback(con);
171
					error.add("delete file:"+path.getAbsolutePath());
172
					continue;
171
				if (path.isFile()) {
172
					if (!path.delete()) {
173
						JdbcUtil.rollback(con);
174
						error.add("delete file:" + path.getAbsolutePath());
175
						continue;
176
					}
177
					if (!path.mkdir()) {
178
						error.add("mkdir:" + path.getAbsolutePath());
179
						continue;
180
					}
173 181
				}
174
				if(!path.mkdir()){
175
					error.add("mkdir:"+path.getAbsolutePath());
182
				File file = new File(path, yd + ".wave");
183
				if (file.exists()) {
184
					error.add("file exists:" + file.getAbsolutePath());
176 185
					continue;
177 186
				}
178
			}
179
			File file = new File(path,yd+".wave");
180
			if(file.exists()){
181
				error.add("file exists:"+file.getAbsolutePath());
182
				continue;
183
			}
184
			try{
185
				FileOutputStream fos = new FileOutputStream(file);
186
				try{
187
					Writer  write = new OutputStreamWriter(fos,ConstData.UTF8);
188
					PreparedStatement ps = con.prepareStatement("SELECT HVALUE,LVALUE,CID,DIC, CTIME FROM WAVE_DATA_ITME WHERE SEQ =? AND CTIME LIKE ? ORDER BY DIC,CID,CTIME");
187
				try {
188
					FileOutputStream fos = new FileOutputStream(file);
189 189
					try {
190
						ps.setString(1, srv.getSeq());
191
						ps.setString(2, yd+"%");
192
						ResultSet rs = ps.executeQuery();
190
						Writer write = new OutputStreamWriter(fos, ConstData.UTF8);
191
						PreparedStatement ps = con.prepareStatement(
192
								"SELECT HVALUE,LVALUE,CID,DIC, CTIME FROM WAVE_DATA_ITME WHERE SEQ =? AND CTIME LIKE ? ORDER BY DIC,CID,CTIME");
193 193
						try {
194
							while (rs.next()) {
195
							write.write(Integer.toString(rs.getInt(1)));
196
							write.write(",");
197
							write.write(Integer.toString(rs.getInt(2)));
198
							write.write(",");
199
							write.write(Integer.toString(rs.getInt(3)));
200
							write.write(",");
201
							write.write(Integer.toString(rs.getInt(4)));
202
							write.write(",");
203
							write.write(rs.getString(5));
204
							write.write("\n");
205
							} 
194
							ps.setString(1, srv.getSeq());
195
							ps.setString(2, yd + "%");
196
							ResultSet rs = ps.executeQuery();
197
							try {
198
								while (rs.next()) {
199
									write.write(Integer.toString(rs.getInt(1)));
200
									write.write(",");
201
									write.write(Integer.toString(rs.getInt(2)));
202
									write.write(",");
203
									write.write(Integer.toString(rs.getInt(3)));
204
									write.write(",");
205
									write.write(Integer.toString(rs.getInt(4)));
206
									write.write(",");
207
									write.write(rs.getString(5));
208
									write.write("\n");
209
								}
210
							} finally {
211
								JdbcUtil.close(rs);
212
							}
206 213
						} finally {
207
							JdbcUtil.close(rs);
214
							JdbcUtil.close(ps);
208 215
						}
216
						write.flush();
217
						write.close();
218
						fos.flush();
209 219
					} finally {
210
						JdbcUtil.close(ps);
220
						fos.close();
211 221
					}
212
					write.flush();
213
					write.close();
214
					fos.flush();
215
				}finally{
216
					fos.close();
222
				} catch (IOException e) {
223
					continue;
217 224
				}
218
			}catch(IOException e){
219
				continue;
220 225
			}
221 226
		}
222
		for(BridgeServer srv:srvs){
223
			JdbcUtil.execute(con,"DELETE FROM WAVE_ITEM_DATA WHERE SEQ='"+srv.getSeq()+"' AND CTIME LIKE '"+yyd+"%'");
227
		for (BridgeServer srv : srvs) {
228
			JdbcUtil.execute(con, "DELETE FROM WAVE_ITEM_DATA WHERE SEQ='" + srv.getSeq() + "' AND CTIME LIKE '" + yyd + "%'");
224 229
		}
225 230
	}
226 231
}