refactor:重构OTA升级管理

V0.5.x
荭琪枫 2023-06-10 15:59:42 +08:00
parent 8e41ad2711
commit eb2ba17ba2
17 changed files with 464 additions and 3 deletions

View File

@ -0,0 +1,14 @@
package cc.iotkit.model.ota;
import lombok.Data;
/**
* @Author:
* @Date: 2023/6/10 14:36
* @Description:
*/
@Data
public class DeviceOta {
private String currentVersion;
private String deviceId;
}

View File

@ -0,0 +1,32 @@
package cc.iotkit.model.ota;
import cc.iotkit.model.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author:
* @Date: 2023/6/10 14:36
* @Description:
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OtaDevice implements Id<String> {
private String id;
private String deviceName;
private String deviceId;
private String version;
private Integer status;
private Long createAt;
}

View File

@ -0,0 +1,35 @@
package cc.iotkit.model.ota;
import cc.iotkit.model.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author:
* @Date: 2023/6/10 14:35
* @Description:
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OtaPackage implements Id<String> {
private String id;
private Long size;
private String sign;
private String name;
private String desc;
private String version;
private String url;
private Long createAt;
}

View File

@ -0,0 +1,12 @@
package cc.iotkit.data.manager;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.ota.OtaDevice;
/**
* @Author:
* @Date: 2023/5/25 23:40
* @Description:
*/
public interface IOtaDeviceData extends ICommonData<OtaDevice, String> {
}

View File

@ -0,0 +1,17 @@
package cc.iotkit.data.manager;
import cc.iotkit.data.ICommonData;
import cc.iotkit.model.ota.OtaPackage;
import java.util.List;
/**
* @Author:
* @Date: 2023/5/19 21:46
* @Description:
*/
public interface IOtaPackageData extends ICommonData<OtaPackage, String> {
List<OtaPackage> findByVersionGreaterThan(String version);
}

View File

@ -0,0 +1,12 @@
package cc.iotkit.data.dao;
import cc.iotkit.data.model.TbOtaDevice;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* @Author:
* @Date: 2023/5/25 23:43
* @Description:
*/
public interface IOtaDeviceRepository extends JpaRepository<TbOtaDevice, String> {
}

View File

@ -0,0 +1,16 @@
package cc.iotkit.data.dao;
import cc.iotkit.data.model.TbOtaPackage;
import cc.iotkit.model.ota.OtaPackage;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* @Author:
* @Date: 2023/5/19 21:54
* @Description:
*/
public interface IOtaPackageRepository extends JpaRepository<TbOtaPackage, String> {
List<OtaPackage> findByVersionGreaterThan(String version);
}

View File

@ -0,0 +1,31 @@
package cc.iotkit.data.model;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @Author:
* @Date: 2023/5/25 23:26
* @Description:
*/
@Data
@Entity
@Table(name = "ota_device")
public class TbOtaDevice {
@Id
private String id;
private String deviceName;
private String deviceId;
private String version;
private Integer status;
private Long createAt;
}

View File

@ -0,0 +1,35 @@
package cc.iotkit.data.model;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @Author:
* @Date: 2023/5/19 21:25
* @Description:
*/
@Data
@Entity
@Table(name = "ota_package")
public class TbOtaPackage {
@Id
private String id;
private Long size;
private String sign;
private String name;
private String desc;
private String version;
private String url;
private Long createAt;
}

View File

@ -0,0 +1,91 @@
package cc.iotkit.data.service;
import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.dao.IJPACommData;
import cc.iotkit.data.dao.IOtaDeviceRepository;
import cc.iotkit.data.manager.IOtaDeviceData;
import cc.iotkit.data.model.TbOtaDevice;
import cc.iotkit.model.ota.OtaDevice;
import cc.iotkit.model.space.Home;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* @Author:
* @Date: 2023/5/25 23:41
* @Description:
*/
@Primary
@Service
public class IOtaDeviceDataImpl implements IOtaDeviceData, IJPACommData<OtaDevice, String> {
@Resource
private IOtaDeviceRepository iOtaDeviceRepository;
@Override
public JpaRepository getBaseRepository() {
return iOtaDeviceRepository;
}
@Override
public Class getJpaRepositoryClass() {
return TbOtaDevice.class;
}
@Override
public Class getTClass() {
return OtaDevice.class;
}
@Override
public OtaDevice save(OtaDevice data) {
if (StringUtils.isBlank(data.getId())) {
data.setId(UUID.randomUUID().toString());
}
iOtaDeviceRepository.save(MapstructUtils.convert(data, TbOtaDevice.class));
return data;
}
@Override
public OtaDevice findById(String id) {
return MapstructUtils.convert(iOtaDeviceRepository.findById(id).orElse(null), OtaDevice.class);
}
@Override
public void deleteById(String id) {
iOtaDeviceRepository.deleteById(id);
}
@Override
public long count() {
return iOtaDeviceRepository.count();
}
@Override
public List<OtaDevice> findAll() {
return iOtaDeviceRepository.findAll().stream().map(e -> MapstructUtils.convert(e, OtaDevice.class))
.collect(Collectors.toList());
}
@Override
public Paging<OtaDevice> findAll(PageRequest<OtaDevice> pageRequest) {
Page<TbOtaDevice> tbOtaPackages = iOtaDeviceRepository.findAll(Pageable.ofSize(pageRequest.getPageSize()).withPage(pageRequest.getPageNum() - 1));
return new Paging<>(
tbOtaPackages.getTotalElements(),
tbOtaPackages.getContent()
.stream().map(e -> MapstructUtils.convert(e, OtaDevice.class))
.collect(Collectors.toList())
);
}
}

View File

@ -0,0 +1,91 @@
package cc.iotkit.data.service;
import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.dao.IJPACommData;
import cc.iotkit.data.dao.IOtaPackageRepository;
import cc.iotkit.data.manager.IOtaPackageData;
import cc.iotkit.data.model.TbOtaDevice;
import cc.iotkit.data.model.TbOtaPackage;
import cc.iotkit.model.ota.OtaDevice;
import cc.iotkit.model.ota.OtaPackage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* @Author:
* @Date: 2023/5/19 21:53
* @Description:
*/
@Primary
@Service
public class IOtaPackageDataImpl implements IOtaPackageData, IJPACommData<OtaPackage, String> {
@Resource
private IOtaPackageRepository iOtaPackageRepository;
@Override
public List<OtaPackage> findByVersionGreaterThan(String version) {
return null;
}
@Override
public JpaRepository getBaseRepository() {
return iOtaPackageRepository;
}
@Override
public Class getJpaRepositoryClass() {
return TbOtaPackage.class;
}
@Override
public Class getTClass() {
return OtaPackage.class;
}
@Override
public OtaPackage save(OtaPackage data) {
if (StringUtils.isBlank(data.getId())) {
data.setId(UUID.randomUUID().toString());
}
iOtaPackageRepository.save(MapstructUtils.convert(data, TbOtaPackage.class));
return data;
}
@Override
public OtaPackage findById(String id) {
return MapstructUtils.convert(iOtaPackageRepository.findById(id).orElse(null), OtaPackage.class);
}
@Override
public void deleteById(String id) {
iOtaPackageRepository.deleteById(id);
}
@Override
public long count() {
return iOtaPackageRepository.count();
}
@Override
public List<OtaPackage> findAll() {
return iOtaPackageRepository.findAll().stream().map(e -> MapstructUtils.convert(e, OtaPackage.class)).collect(Collectors.toList());
}
@Override
public Paging<OtaPackage> findAll(PageRequest<OtaPackage> pageRequest) {
Page<TbOtaPackage> tbOtaPackages = iOtaPackageRepository.findAll(Pageable.ofSize(pageRequest.getPageSize()).withPage(pageRequest.getPageNum() - 1));
return new Paging<>(tbOtaPackages.getTotalElements(), tbOtaPackages.getContent().stream().map(e -> MapstructUtils.convert(e, OtaPackage.class)).collect(Collectors.toList()));
}
}

View File

@ -59,6 +59,12 @@
<artifactId>iot-rule-engine</artifactId>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-component-oss</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>cc.iotkit</groupId>
<artifactId>iot-component-server</artifactId>

View File

@ -1,12 +1,24 @@
package cc.iotkit.manager.controller;
import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.api.Request;
import cc.iotkit.manager.service.OtaService;
import cc.iotkit.model.ota.DeviceOta;
import cc.iotkit.model.ota.OtaPackage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.InputStream;
/**
* @Author:
@ -22,7 +34,7 @@ public class OtaController {
@Resource
private OtaService otaService;
/*@ApiOperation("升级包上传")
@ApiOperation("升级包上传")
@PostMapping("/package/upload")
public String packageUpload(MultipartFile file) throws Exception {
if (!file.isEmpty()) {
@ -49,7 +61,7 @@ public class OtaController {
@ApiOperation("升级包列表")
@PostMapping("/package/getList")
public Paging<OtaPackage> packageList(@RequestBody @Valid PageRequest<Void> request) {
return otaService.getOtaPackagePageList(request.getPageNo(), request.getPageSize());
return null;
}
@ApiOperation("设备获取升级包")
@ -57,5 +69,5 @@ public class OtaController {
public void deviceUpgrade(@RequestBody Request<DeviceOta> deviceOtaRequest) {
DeviceOta deviceOta = deviceOtaRequest.getData();
otaService.findByVersionGreaterThan(deviceOta.getCurrentVersion(), deviceOta.getDeviceId());
}*/
}
}

View File

@ -1,9 +1,16 @@
package cc.iotkit.manager.service;
import cc.iotkit.data.manager.IOtaDeviceData;
import cc.iotkit.data.manager.IOtaPackageData;
import cc.iotkit.model.ota.OtaPackage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.util.UUID;
/**
* @Author:
* @Date: 2023/5/19 20:49
@ -14,5 +21,34 @@ import org.springframework.stereotype.Service;
@RequiredArgsConstructor
public class OtaService {
private final IOtaPackageData iOtaPackageData;
private final IOtaDeviceData iOtaDeviceData;
@Value("${oss.region}")
private String region;
@Value("${oss.buckName}")
private String buckName;
public String uploadFile(InputStream inputStream, String suffix) throws Exception {
String objectName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
return "https://" + region + "/" + objectName;
}
public OtaPackage addOtaPackage(OtaPackage otaPackage) {
return iOtaPackageData.save(otaPackage);
}
public Boolean delOtaPackageById(String id) {
iOtaPackageData.deleteById(id);
return Boolean.TRUE;
}
public void findByVersionGreaterThan(String version, String deviceId) {
iOtaPackageData.findByVersionGreaterThan(version);
}
public void batchOta() {
}
}

View File

@ -117,3 +117,10 @@ sa-token:
#认证中心地址
oauth2:
auth-server-url: http://127.0.0.1:8086
oss:
region: xxxxx
endpoint: xxxxx
accessKey: xxxxx
secretKey: xxxxx
buckName: xxxx

View File

@ -91,3 +91,10 @@ sa-token:
#认证中心地址
oauth2:
auth-server-url: http://127.0.0.1:8086
oss:
region: xxxxx
endpoint: xxxxx
accessKey: xxxxx
secretKey: xxxxx
buckName: xxxx

View File

@ -153,3 +153,10 @@ user:
maxRetryCount: 5
# 密码锁定时间默认10分钟
lockTime: 10
oss:
region: xxxxx
endpoint: xxxxx
accessKey: xxxxx
secretKey: xxxxx
buckName: xxxx