|
|
@@ -1,15 +1,25 @@
|
|
|
package org.jetlinks.community.device.service;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import javafx.concurrent.Task;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
|
|
|
import org.jetlinks.community.device.entity.DeviceFirmwareEntity;
|
|
|
import org.jetlinks.community.device.entity.DeviceFirmwareTaskEntity;
|
|
|
+import org.jetlinks.community.device.entity.DeviceUpgradeHistoryEntity;
|
|
|
import org.jetlinks.community.device.enums.TaskDeployMode;
|
|
|
+import org.jetlinks.community.device.enums.TaskMode;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collection;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author lifang
|
|
|
@@ -20,7 +30,12 @@ import java.util.List;
|
|
|
*/
|
|
|
@Service
|
|
|
public class LocalDeviceFirmwareTaskService extends GenericReactiveCrudService<DeviceFirmwareTaskEntity, String> {
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ @Lazy
|
|
|
+ private LocalFirmwareUpgradeHistoryService upgradeHistoryService;
|
|
|
+ @Autowired
|
|
|
+ @Lazy
|
|
|
+ private LocalDeviceFirmwareService firmwareService;
|
|
|
public Mono<Integer> deployAll(String id) {
|
|
|
return this
|
|
|
.createUpdate()
|
|
|
@@ -29,13 +44,48 @@ public class LocalDeviceFirmwareTaskService extends GenericReactiveCrudService<D
|
|
|
.execute();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @param id
|
|
|
+ * @param deviceIds
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public Mono<Integer> deploy(String id,List<String> deviceIds) {
|
|
|
- return this
|
|
|
- .createUpdate()
|
|
|
- .where(DeviceFirmwareEntity::getId,id)
|
|
|
- .set(DeviceFirmwareTaskEntity::getDeployMode, TaskDeployMode.special)
|
|
|
- .set(DeviceFirmwareTaskEntity::getDeployIds,deviceIds)
|
|
|
- .execute();
|
|
|
+ return this.findById(id)
|
|
|
+ .flatMap(task-> Mono.zip(Mono.just(task),firmwareService.findById(task.getFirmwareId())))
|
|
|
+ .flatMap(tp2->{
|
|
|
+ DeviceFirmwareTaskEntity task = tp2.getT1();
|
|
|
+ DeviceFirmwareEntity firmware = tp2.getT2();
|
|
|
+ //当为平台推送时,所有未推送的设备应为等待升级状态
|
|
|
+ if(TaskMode.push.eq(task.getMode())){
|
|
|
+ return upgradeHistoryService.createQuery()
|
|
|
+ .where(DeviceUpgradeHistoryEntity::getTaskId,task.getId())
|
|
|
+ .where(DeviceUpgradeHistoryEntity::getFirmwareId,task.getFirmwareId())
|
|
|
+ .fetch()
|
|
|
+ .collectList()
|
|
|
+ .map(existDevices->existDevices.stream().map(DeviceUpgradeHistoryEntity::getDeviceId).collect(Collectors.toList()))
|
|
|
+ .defaultIfEmpty(new ArrayList<String>())
|
|
|
+ //获取新增任务设备id
|
|
|
+ .map(existDeviceIds-> CollectionUtil.subtract(deviceIds, existDeviceIds))
|
|
|
+ //插入设备任务操作
|
|
|
+ .flatMap(ids->
|
|
|
+ upgradeHistoryService.save( ids.stream().map(deviceId-> DeviceUpgradeHistoryEntity.of(task,firmware,deviceId)).collect(Collectors.toList()))
|
|
|
+ ).then(this
|
|
|
+ .createUpdate()
|
|
|
+ .where(DeviceFirmwareEntity::getId,id)
|
|
|
+ .set(DeviceFirmwareTaskEntity::getDeployMode, TaskDeployMode.special)
|
|
|
+ .set(DeviceFirmwareTaskEntity::getDeployIds,deviceIds)
|
|
|
+ .execute());
|
|
|
+ }else {
|
|
|
+ //当为设备拉取时,直接更新即可
|
|
|
+ return this
|
|
|
+ .createUpdate()
|
|
|
+ .where(DeviceFirmwareEntity::getId,id)
|
|
|
+ .set(DeviceFirmwareTaskEntity::getDeployMode, TaskDeployMode.special)
|
|
|
+ .set(DeviceFirmwareTaskEntity::getDeployIds,deviceIds)
|
|
|
+ .execute();
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|