|
|
@ -9,9 +9,11 @@ import com.iflytop.profilometer.core.migration.measure.drawer.type.XYPoint; |
|
|
|
import com.iflytop.profilometer.core.websocket.WebSocketManager; |
|
|
|
import com.iflytop.profilometer.dao.ProfileRecordDao; |
|
|
|
import com.iflytop.profilometer.dao.ProfileRecordPointSetDao; |
|
|
|
import com.iflytop.profilometer.dao.SyncTaskDao; |
|
|
|
import com.iflytop.profilometer.dao.SystemConfigDao; |
|
|
|
import com.iflytop.profilometer.model.entity.ProfileRecordDescription; |
|
|
|
import com.iflytop.profilometer.model.entity.ProfileRecordPointSet; |
|
|
|
import com.iflytop.profilometer.model.entity.SyncTask; |
|
|
|
import com.iflytop.profilometer.model.entity.SystemConfig; |
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
@ -53,7 +55,19 @@ public class UploadManager { |
|
|
|
try { |
|
|
|
// 阻塞获取任务,队列为空时会一直等待 |
|
|
|
Long uploadId = uploadQueue.take(); |
|
|
|
processUpload(uploadId); |
|
|
|
boolean result = false; |
|
|
|
try { |
|
|
|
result = processUpload(uploadId); |
|
|
|
} catch (Exception e) { |
|
|
|
fail++; |
|
|
|
} |
|
|
|
Map<String, Object> itemMap = new HashMap<>(); |
|
|
|
itemMap.put("type", "sync-item-finish"); |
|
|
|
Map<String, Object> itemDataMap = new HashMap<>(); |
|
|
|
itemDataMap.put("id", uploadId); |
|
|
|
itemDataMap.put("success", result); |
|
|
|
itemMap.put("data", itemDataMap); |
|
|
|
WebSocketManager.send(JSONUtil.toJsonStr(itemMap)); |
|
|
|
|
|
|
|
Map<String, Object> map = new HashMap<>(); |
|
|
|
map.put("type", "sync-progress"); |
|
|
@ -62,11 +76,13 @@ public class UploadManager { |
|
|
|
dataMap.put("remaining", remainingTasks); |
|
|
|
dataMap.put("fail", fail); |
|
|
|
if (uploadQueue.isEmpty()) { |
|
|
|
fail = 0; |
|
|
|
dataMap.put("status", SyncStatusType.FINISHED); |
|
|
|
} else { |
|
|
|
dataMap.put("status", SyncStatusType.UPLOADING); |
|
|
|
} |
|
|
|
WebSocketManager.send(JSONUtil.toJsonStr(dataMap)); |
|
|
|
map.put("data", dataMap); |
|
|
|
WebSocketManager.send(JSONUtil.toJsonStr(map)); |
|
|
|
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
// 如果希望线程一直运行,不退出则不break,可加上延时后继续循环 |
|
|
@ -92,17 +108,19 @@ public class UploadManager { |
|
|
|
uploadThread.start(); |
|
|
|
} |
|
|
|
|
|
|
|
private void processUpload(Long uploadId) { |
|
|
|
private boolean processUpload(Long uploadId) { |
|
|
|
ProfileRecordDao profileRecordDao = new ProfileRecordDao(ProfilometerApplication.getContext()); |
|
|
|
ProfileRecordPointSetDao profileRecordPointSetDao = new ProfileRecordPointSetDao(ProfilometerApplication.getContext()); |
|
|
|
ProfileRecordDescription profileRecordDescription = profileRecordDao.getProfileRecordById(uploadId); |
|
|
|
if (profileRecordDescription.getSyncStatus() != SyncStatus.wait) { |
|
|
|
return; |
|
|
|
if (profileRecordDescription.getSyncStatus() == SyncStatus.finish) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
SystemConfigDao systemConfigDao = new SystemConfigDao(ProfilometerApplication.getContext()); |
|
|
|
SystemConfig serverConfig = systemConfigDao.getSystemConfigByKey(SystemConfigType.SERVER); |
|
|
|
String serverPath = serverConfig.getConfigValue(); |
|
|
|
|
|
|
|
SyncTaskDao syncTaskDao = new SyncTaskDao(ProfilometerApplication.getContext()); |
|
|
|
SyncTask syncTask = syncTaskDao.getSyncTaskById(uploadId); |
|
|
|
ProfileRecordPointSet profileRecordPointSet = profileRecordPointSetDao.getProfileRecordPointSetByUuid(profileRecordDescription.getUuid()); |
|
|
|
String leftPointJsonStr = profileRecordPointSet.getLeftPoints(); |
|
|
|
String rightPointJsonStr = profileRecordPointSet.getLeftPoints(); |
|
|
@ -116,15 +134,15 @@ public class UploadManager { |
|
|
|
String zipName = ""; |
|
|
|
if (profileRecordDescription.getDataSource().equals("XLDC")) { |
|
|
|
// 文件名生成 线名-行别里程股别半径-日期 |
|
|
|
fileName = profileRecordDescription.getXmCode() + "-" + profileRecordDescription.getXbCode()+ profileRecordDescription.getMileage()+ |
|
|
|
fileName = profileRecordDescription.getXmCode() + "-" + profileRecordDescription.getXbCode() + profileRecordDescription.getMileage() + |
|
|
|
profileRecordDescription.getUnitType() + (profileRecordDescription.getRadius() == null || profileRecordDescription.getRadius().isEmpty() ? "" : profileRecordDescription.getRadius()) + |
|
|
|
"-" + profileRecordDescription.getCreateTime().format(DateTimeFormatter.ofPattern("yyyyMMdd"))+ ".ban"; |
|
|
|
"-" + profileRecordDescription.getCreateTime().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + ".ban"; |
|
|
|
zipName = "线路廓形数据" + "-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + ".zip"; |
|
|
|
} else if (profileRecordDescription.getDataSource().equals("DCDC")) { |
|
|
|
// 文件名生成 线名-站名-行别-道岔号-枕木号-股别-日期 |
|
|
|
fileName = profileRecordDescription.getXmCode() + "-" + profileRecordDescription.getStationCode() +"-"+ profileRecordDescription.getXbCode() + |
|
|
|
"-"+ profileRecordDescription.getTurnoutNum() +"-"+ profileRecordDescription.getSleeperNum() +"-"+ profileRecordDescription.getUnitType() + |
|
|
|
"-"+ profileRecordDescription.getCreateTime().format(DateTimeFormatter.ofPattern("yyyyMMdd"))+ ".ban"; |
|
|
|
fileName = profileRecordDescription.getXmCode() + "-" + profileRecordDescription.getStationCode() + "-" + profileRecordDescription.getXbCode() + |
|
|
|
"-" + profileRecordDescription.getTurnoutNum() + "-" + profileRecordDescription.getSleeperNum() + "-" + profileRecordDescription.getUnitType() + |
|
|
|
"-" + profileRecordDescription.getCreateTime().format(DateTimeFormatter.ofPattern("yyyyMMdd")) + ".ban"; |
|
|
|
zipName = "道岔廓形数据" + "-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) + ".zip"; |
|
|
|
} |
|
|
|
byte[] banByte = pointToBanFile(mergedPoints, fileName); |
|
|
@ -154,17 +172,32 @@ public class UploadManager { |
|
|
|
JSONObject resultJsonObject = JSONUtil.parseObj(resultStr); |
|
|
|
if (resultJsonObject.getBool("success")) {//成功 |
|
|
|
profileRecordDescription.setSyncStatus(SyncStatus.finish); |
|
|
|
syncTask.setSyncStatus(SyncStatus.finish); |
|
|
|
profileRecordDao.updateProfileRecord(profileRecordDescription); |
|
|
|
syncTaskDao.updateSyncTask(syncTask); |
|
|
|
return true; |
|
|
|
} else { |
|
|
|
if (resultJsonObject.getInt("code") == 501) {//成功 |
|
|
|
profileRecordDescription.setSyncStatus(SyncStatus.finish); |
|
|
|
syncTask.setSyncStatus(SyncStatus.finish); |
|
|
|
profileRecordDao.updateProfileRecord(profileRecordDescription); |
|
|
|
syncTaskDao.updateSyncTask(syncTask); |
|
|
|
return true; |
|
|
|
} else {//失败 |
|
|
|
fail++; |
|
|
|
profileRecordDescription.setSyncStatus(SyncStatus.fail); |
|
|
|
syncTask.setSyncStatus(SyncStatus.fail); |
|
|
|
syncTask.setErrorMsg(resultJsonObject.getStr("message")); |
|
|
|
profileRecordDao.updateProfileRecord(profileRecordDescription); |
|
|
|
syncTaskDao.updateSyncTask(syncTask); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
profileRecordDao.updateProfileRecord(profileRecordDescription); |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} catch (IOException ignored) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} else if (profileRecordDescription.getDataSource().equals("DCDC")) { |
|
|
|
formMap.put("tljCode", profileRecordDescription.getTljCode()); |
|
|
@ -186,19 +219,31 @@ public class UploadManager { |
|
|
|
JSONObject resultJsonObject = JSONUtil.parseObj(resultStr); |
|
|
|
if (resultJsonObject.getBool("success")) {//成功 |
|
|
|
profileRecordDescription.setSyncStatus(SyncStatus.finish); |
|
|
|
profileRecordDao.updateProfileRecord(profileRecordDescription); |
|
|
|
syncTaskDao.updateSyncTask(syncTask); |
|
|
|
return true; |
|
|
|
} else { |
|
|
|
if (resultJsonObject.getInt("code") == 501) {//成功 |
|
|
|
profileRecordDescription.setSyncStatus(SyncStatus.finish); |
|
|
|
profileRecordDao.updateProfileRecord(profileRecordDescription); |
|
|
|
syncTaskDao.updateSyncTask(syncTask); |
|
|
|
return true; |
|
|
|
} else {//失败 |
|
|
|
fail++; |
|
|
|
profileRecordDescription.setSyncStatus(SyncStatus.fail); |
|
|
|
profileRecordDao.updateProfileRecord(profileRecordDescription); |
|
|
|
syncTaskDao.updateSyncTask(syncTask); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
profileRecordDao.updateProfileRecord(profileRecordDescription); |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} catch (IOException ignored) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -235,7 +280,7 @@ public class UploadManager { |
|
|
|
* @param banFiles 存放在内存中的 ban 文件列表,每个 ban 文件包含文件名和对应的内容 |
|
|
|
* @return ZIP 压缩包数据对应的字节数组,如果压缩出现异常则返回 null。 |
|
|
|
*/ |
|
|
|
public static byte[] compressionToZipBytes(List<BanFile> banFiles) { |
|
|
|
private static byte[] compressionToZipBytes(List<BanFile> banFiles) { |
|
|
|
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
|
|
ZipOutputStream zos = new ZipOutputStream(baos)) { |
|
|
|
for (BanFile banFile : banFiles) { |
|
|
@ -270,7 +315,7 @@ public class UploadManager { |
|
|
|
* @param data 内存中完整的文件数据 |
|
|
|
* @throws IOException 如果在数据处理过程中发生异常 |
|
|
|
*/ |
|
|
|
public static void precalc(ZipEntry entry, byte[] data) throws IOException { |
|
|
|
private static void precalc(ZipEntry entry, byte[] data) throws IOException { |
|
|
|
// 未压缩大小 |
|
|
|
long uncompressed = data.length; |
|
|
|
int method = entry.getMethod(); |
|
|
@ -306,4 +351,8 @@ public class UploadManager { |
|
|
|
} |
|
|
|
entry.setCrc(crc.getValue()); |
|
|
|
} |
|
|
|
|
|
|
|
public BlockingQueue<Long> getUploadQueue() { |
|
|
|
return uploadQueue; |
|
|
|
} |
|
|
|
} |