|
@@ -97,7 +97,7 @@ public class DeviceCollectionTaskJob {
|
|
|
List<Map> list = JSONUtil.toList(jsonArray, Map.class);
|
|
|
CompletableFuture[] task = list.stream().map(map -> CompletableFuture.runAsync(() -> {
|
|
|
map.put("type","Collect");
|
|
|
- // if("192.168.10.101".equals(map.get("serverUrl").toString())){
|
|
|
+ //if("192.168.10.109".equals(map.get("serverUrl").toString())){
|
|
|
String data = JSONUtil.toJsonStr(map);
|
|
|
String returnInfo = this.httpPost(data);
|
|
|
if(returnInfo !=null){
|
|
@@ -105,10 +105,14 @@ public class DeviceCollectionTaskJob {
|
|
|
if((boolean)returnJson.get("result")){
|
|
|
this.saveOrUpdateDeviceCollection(returnJson,map.get("serverUrl"));
|
|
|
this.saveOrUpdateDeviceCollectionDetail(returnJson,map.get("serverUrl"));
|
|
|
- this.deviceCollectionInfoWriteFile(returnJson,map.get("serverUrl"));
|
|
|
+ try {
|
|
|
+ this.deviceCollectionInfoWriteFile(returnJson,map.get("serverUrl"));
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- // }
|
|
|
+ //}
|
|
|
|
|
|
})).toArray(CompletableFuture[]::new);
|
|
|
CompletableFuture.allOf(task).join();
|
|
@@ -167,7 +171,7 @@ public class DeviceCollectionTaskJob {
|
|
|
Date date = new Date();
|
|
|
String nowDate = DateUtil.format(date, "yyyy-MM-dd");
|
|
|
|
|
|
- Map<String, Object> deviceCollectionDetailInfoMap = jdbcService.findOne("select id,device_rate,today_earliest_power_on_time,detail_info from device_collection_detail where device_ip = ? and create_date = ?", ip,nowDate);
|
|
|
+ Map<String, Object> deviceCollectionDetailInfoMap = jdbcService.findOne("select id,device_rate,today_earliest_power_on_time,detail_info,today_power_on_time from device_collection_detail where device_ip = ? and create_date = ?", ip,nowDate);
|
|
|
|
|
|
String detailInfo = "";
|
|
|
if(CollectionUtil.isNotEmpty(deviceCollectionDetailInfoMap)){
|
|
@@ -221,10 +225,12 @@ public class DeviceCollectionTaskJob {
|
|
|
|
|
|
if(!Objects.isNull(returnJson.get("deviceState")) && "在线".equals(returnJson.get("deviceState").toString())){
|
|
|
BigDecimal currentPowerOnTime = new BigDecimal(powerOnTime.toString());
|
|
|
- // 间隔时间 = 当前开机总时长 - 最近的上一次采集开机总时长
|
|
|
- BigDecimal intervalTime = currentPowerOnTime.subtract(hisPowerOnTime);
|
|
|
- //当日设备开机时长 = 当日设备开机时长 + 间隔时间
|
|
|
- todayPowerOnTime = todayPowerOnTime.add(intervalTime);
|
|
|
+ if(hisPowerOnTime.compareTo(BigDecimal.ZERO) > 0){
|
|
|
+ // 间隔时间 = 当前开机总时长 - 最近的上一次采集开机总时长
|
|
|
+ BigDecimal intervalTime = currentPowerOnTime.subtract(hisPowerOnTime);
|
|
|
+ //当日设备开机时长 = 当日设备开机时长 + 间隔时间
|
|
|
+ todayPowerOnTime = todayPowerOnTime.add(intervalTime);
|
|
|
+ }
|
|
|
|
|
|
String startDate = nowDate + " " + "08:00:00"; //默认早上八点
|
|
|
|
|
@@ -244,31 +250,41 @@ public class DeviceCollectionTaskJob {
|
|
|
List<Map> list = new ArrayList<>();
|
|
|
list.add(map);
|
|
|
detailInfo = JSONUtil.toJsonStr(list);
|
|
|
-
|
|
|
+ long todayEarliestPowerOnTime = 0;
|
|
|
+ if(!Objects.isNull(powerOnTime.toString())){
|
|
|
+ todayEarliestPowerOnTime = Long.parseLong(powerOnTime.toString());
|
|
|
+ }
|
|
|
String sql = "insert into device_collection_detail (device_rate,today_earliest_power_on_time,detail_info,create_date,device_ip) values(?,?,?,?,?) ";
|
|
|
- jdbcService.insert(sql,0,Long.parseLong(powerOnTime.toString()),detailInfo,nowDate,ip);
|
|
|
+ jdbcService.insert(sql,0,todayEarliestPowerOnTime,detailInfo,nowDate,ip);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void deviceCollectionInfoWriteFile(JSONObject returnJson,Object ip){
|
|
|
+ public void deviceCollectionInfoWriteFile(JSONObject returnJson,Object ip) throws IOException {
|
|
|
Date date = new Date();
|
|
|
String nowDate = DateUtil.format(date, "yyyy-MM-dd");
|
|
|
|
|
|
String folderPath = "D:\\collection" + File.separator + DateUtil.format(date, "yyyy-MM");
|
|
|
|
|
|
- // 创建文件夹(如果不存在)
|
|
|
+ // 创建月文件夹(如果不存在)
|
|
|
File folder = new File(folderPath);
|
|
|
if (!folder.exists()) {
|
|
|
folder.mkdirs();
|
|
|
}
|
|
|
|
|
|
+ String dayFolderPath = "D:\\collection" + File.separator + DateUtil.format(date, "yyyy-MM") + File.separator + nowDate;
|
|
|
+ // 创建天文件夹(如果不存在)
|
|
|
+ File dayFolder = new File(dayFolderPath);
|
|
|
+ if (!dayFolder.exists()) {
|
|
|
+ dayFolder.mkdirs();
|
|
|
+ }
|
|
|
+
|
|
|
// 构建文件名,以每天 + ip 一个文件
|
|
|
String fileName = ip + "_" + nowDate + ".txt";
|
|
|
|
|
|
- String filePath = folderPath + File.separator + fileName;
|
|
|
-
|
|
|
+ String filePath = dayFolderPath + File.separator + fileName;
|
|
|
+ BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true));
|
|
|
// 写入数据到文件
|
|
|
- try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
|
|
|
+ try {
|
|
|
|
|
|
String deviceCollectionInfo = JSONUtil.toJsonStr(returnJson);
|
|
|
|
|
@@ -276,8 +292,20 @@ public class DeviceCollectionTaskJob {
|
|
|
|
|
|
// 追加写入数据,并在每条数据后面添加换行符
|
|
|
writer.write(data + System.lineSeparator());
|
|
|
+
|
|
|
+ //刷新缓冲区
|
|
|
+ writer.flush();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ if(writer !=null){
|
|
|
+ try{
|
|
|
+ //关闭
|
|
|
+ writer.close();
|
|
|
+ }catch (IOException e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|