Pārlūkot izejas kodu

Merge branch 'master' of http://121.42.53.174:3000/jiapeng/bridge

XMTT 6 gadi atpakaļ
vecāks
revīzija
dc5eafd533

+ 90 - 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

@ -38,15 +37,17 @@ public class CollectService {
38 37
	private File backupPath;
39 38
	@Autowrie
40 39
	private CollectDao collectDao;
40
	@Autowrie
41 41
	private BridgeServerDao bridgeServerDao;
42 42

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

48 49
	}
49
	
50

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

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

132 133
	@Get
133 134
	@Path("/wave/server/time")
134
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con,String[] seq,String begin,String end)throws SQLException{
135
		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);
136 137
	}
138

137 139
	@Get
138 140
	@Path("/wave/box/time")
139
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con,String seq,int box,String begin,String end)throws SQLException{
140
		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);
141 143
	}
144

142 145
	@Get
143 146
	@Path("/wave/channel/time")
144
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con,String seq,int box,int channel,String begin,String end)throws SQLException{
145
		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);
146 149
	}
147
	
148 150

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

157 159
		List<String> error = new LinkedList<String>();
158
		
159
		for(BridgeServer srv:srvs){
160
			File path = new File(this.backupPath,srv.getSeq());
161
			if(!path.exists()){
162
				if(!path.mkdir()){
163
					error.add("mkdir:"+path.getAbsolutePath());
164
					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
					}
165 170
				}
166
			}
167
			if(path.isFile()){
168
				if(!path.delete()){
169
					JdbcUtil.rollback(con);
170
					error.add("delete file:"+path.getAbsolutePath());
171
					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
					}
172 181
				}
173
				if(!path.mkdir()){
174
					error.add("mkdir:"+path.getAbsolutePath());
182
				File file = new File(path, yd + ".wave");
183
				if (file.exists()) {
184
					error.add("file exists:" + file.getAbsolutePath());
175 185
					continue;
176 186
				}
177
			}
178
			File file = new File(path,yd+".wave");
179
			if(file.exists()){
180
				error.add("file exists:"+file.getAbsolutePath());
181
				continue;
182
			}
183
			try{
184
				FileOutputStream fos = new FileOutputStream(file);
185
				try{
186
					Writer  write = new OutputStreamWriter(fos,ConstData.UTF8);
187
					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);
188 189
					try {
189
						ps.setString(1, srv.getSeq());
190
						ps.setString(2, yd+"%");
191
						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");
192 193
						try {
193
							while (rs.next()) {
194
							write.write(Integer.toString(rs.getInt(1)));
195
							write.write(",");
196
							write.write(Integer.toString(rs.getInt(2)));
197
							write.write(",");
198
							write.write(Integer.toString(rs.getInt(3)));
199
							write.write(",");
200
							write.write(Integer.toString(rs.getInt(4)));
201
							write.write(",");
202
							write.write(rs.getString(5));
203
							write.write("\n");
204
							} 
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
							}
205 213
						} finally {
206
							JdbcUtil.close(rs);
214
							JdbcUtil.close(ps);
207 215
						}
216
						write.flush();
217
						write.close();
218
						fos.flush();
208 219
					} finally {
209
						JdbcUtil.close(ps);
220
						fos.close();
210 221
					}
211
					write.flush();
212
					write.close();
213
					fos.flush();
214
				}finally{
215
					fos.close();
222
				} catch (IOException e) {
223
					continue;
216 224
				}
217
			}catch(IOException e){
218
				continue;
219 225
			}
220 226
		}
221
		for(BridgeServer srv:srvs){
222
			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 + "%'");
223 229
		}
224 230
	}
225 231
}