Commit 466c8931 by 李小龙

创建租户同步iot

定时同步设备信息
parent dcaad85e
......@@ -133,7 +133,7 @@ CREATE TABLE `saas_operation_log` (
CREATE TABLE `plat_tenant` (
`id` varchar(64) NOT NULL COLLATE utf8mb4_general_ci COMMENT 'id',
`id` varchar(64) NOT NULL COMMENT 'id',
`name` varchar(128) DEFAULT NULL COMMENT '名称',
`status` varchar(10) NOT NULL COMMENT '状态 0停用 1启用',
`start_time` datetime NOT NULL COMMENT '租户有效期-开始时间',
......@@ -143,9 +143,10 @@ CREATE TABLE `plat_tenant` (
`alert_channel` varchar(32) DEFAULT NULL COMMENT '告警渠道 1-短信 2-邮件 3-语音短信 4-云龄工单 5-晶奇工单',
`create_date` datetime NOT NULL COMMENT '创建时间',
`update_date` datetime NOT NULL COMMENT '更新时间',
`del_flag` CHAR(1) DEFAULT NULL COMMENT '删除标识',
`del_flag` char(1) DEFAULT NULL COMMENT '删除标识',
`create_by` varchar(64) NOT NULL COMMENT '创建人',
`update_by` varchar(64) NOT NULL COMMENT '更新人',
`iot_org_id` varchar(64) DEFAULT NULL COMMENT 'iot组织id',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 STATS_AUTO_RECALC=0 ROW_FORMAT=COMPACT COMMENT='租户管理';
......
package com.makeit.task;
import com.makeit.module.iot.service.IotOrgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class IotSyncTask {
@Autowired
private IotOrgService iotOrgService;
@Scheduled(cron = "0 */1 * * * ?")
public void syncEquipmentInfo() {
log.info("开始执行同步设备信息接口");
log.info("结束执行同步设备信息接口");
}
@Scheduled(cron = "0 0/1 * * * ?")
public void syncDeviceLog() {
log.info("开始同步设备日志");
log.info("同步设备日志结束");
}
}
......@@ -49,5 +49,10 @@ public class PlatTenant extends BaseEntity {
*/
private String alertChannel;
/**
* 新增租户时,会在iot创建一个组织
*/
private String iotOrgId;
}
\ No newline at end of file
......@@ -22,6 +22,8 @@ import com.makeit.module.admin.dto.plat.PlatTenantDTOVO;
import com.makeit.module.admin.dto.plat.PlatTenantMenuDTO;
import com.makeit.module.admin.dto.plat.PlatTenantStatusDTO;
import com.makeit.module.admin.vo.plat.PlatTenantVO;
import com.makeit.module.iot.service.IotOrgService;
import com.makeit.module.iot.vo.OrganizationEntity;
import com.makeit.module.system.service.SysConfigService;
import com.makeit.service.platform.auth.PlatOrgService;
import com.makeit.service.platform.auth.PlatUserService;
......@@ -75,6 +77,9 @@ implements PlatTenantService {
@Autowired
private PlatOrgService platOrgService;
@Autowired
private IotOrgService iotOrgService;
private LambdaQueryWrapper<PlatTenant> listLambdaQueryWrapper(PlatTenantVO dto, boolean userAccountLike) {
List<String> tenantUserIdList = new ArrayList<>(10);
......@@ -192,6 +197,13 @@ implements PlatTenantService {
checkName(dto);
checkDate(dto);
PlatTenant tntTenant = BeanDtoVoUtils.convert(dto, PlatTenant.class);
//新租户同步到iot
PlatTenantVO platTenantVO = new PlatTenantVO();
platTenantVO.setName(dto.getName());
OrganizationEntity organizationEntity = iotOrgService.syncTenantInfoToIot(platTenantVO);
tntTenant.setIotOrgId(organizationEntity.getId());
save(tntTenant);
dto.setId(tntTenant.getId());
......
package com.makeit.task;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.makeit.common.entity.BaseBusEntity;
import com.makeit.common.entity.BaseEntity;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.entity.saas.PlatTenant;
import com.makeit.enums.CommonEnum;
import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.module.iot.service.IotOrgService;
import com.makeit.module.iot.vo.DeviceInstanceEntity;
import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.service.saas.PlatTenantService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Component
@Slf4j
public class IotSyncTask {
@Autowired
private IotOrgService iotOrgService;
@Autowired
private PlatTenantService platTenantService;
@Autowired
private PlatDeviceService platDeviceService;
/**
* 一小时同步一次
* 启用状态的租户才同步
* 新增和更新平台端设备表
*/
@Scheduled(cron = "0 0 */1 * * ?")
// @Scheduled(cron = "0 */1 * * * ?")
@TenantIdIgnore
public void syncEquipmentInfo() {
log.info("开始执行同步设备信息接口");
LambdaQueryWrapper<PlatTenant> tenantLambdaQueryWrapper = new LambdaQueryWrapper<PlatTenant>().eq(PlatTenant::getStatus, CommonEnum.YES.getValue());
List<PlatTenant> platTenants = platTenantService.list(tenantLambdaQueryWrapper);
for (PlatTenant platTenant : platTenants) {
String iotOrgId = platTenant.getIotOrgId();
if(StringUtils.isBlank(iotOrgId)){
continue;
}
//查询iot设备
List<DeviceInstanceEntity> iotDeviceList = iotOrgService.getOrgDevice(iotOrgId);
if(CollectionUtils.isEmpty(iotDeviceList)){
continue;
}
//查询平台设备
Set<String> iotDeviceIdSet = iotDeviceList.stream().map(DeviceInstanceEntity::getId).collect(Collectors.toSet());
LambdaQueryWrapper<PlatDevice> deviceLambdaQueryWrapper = new LambdaQueryWrapper<PlatDevice>().eq(BaseBusEntity::getTenantId, platTenant.getId())
.in(BaseEntity::getId, iotDeviceIdSet);
List<PlatDevice> deviceList = platDeviceService.list(deviceLambdaQueryWrapper);
//更新平台设备
Collection<PlatDevice> platDevices = convertToPlatDevice(iotDeviceList, deviceList,platTenant.getId());
platDeviceService.saveOrUpdateBatch(platDevices);
}
log.info("结束执行同步设备信息接口");
}
@Scheduled(cron = "0 0/1 * * * ?")
public void syncDeviceLog() {
log.info("开始同步设备日志");
log.info("同步设备日志结束");
}
private Collection<PlatDevice> convertToPlatDevice(List<DeviceInstanceEntity> iotDeviceList, List<PlatDevice> deviceList,String tenantId){
Map<String, PlatDevice> deviceMap = deviceList.stream().collect(Collectors.toMap(PlatDevice::getOriDeviceId, v -> v, (a, b) -> a));
iotDeviceList.forEach(iotDevice->{
PlatDevice platDevice = deviceMap.get(iotDevice.getId());
if(platDevice==null){
platDevice=new PlatDevice();
platDevice.setTenantId(tenantId);
}
platDevice.setOriDeviceId(iotDevice.getId());
platDevice.setName(iotDevice.getName());
platDevice.setProductName(iotDevice.getProductName());
platDevice.setProductId(iotDevice.getProductId());
LocalDateTime registryTime = LocalDateTime.ofEpochSecond(iotDevice.getRegistryTime()/1000, 0, ZoneOffset.ofHours(8));
platDevice.setRegistrationDate(registryTime);
platDevice.setDescription(iotDevice.getDescribe());
String state = iotDevice.getState();
platDevice.setStatus(StringUtils.equals("online",state)?CommonEnum.YES.getValue() : CommonEnum.NO.getValue());
// platDevice.setFirmwareVersion();
// platDevice.setLastOnlineData();
// platDevice.setOrgId();
// platDevice.setCityOrgId();
// platDevice.setDistrictOrgId();
// platDevice.setStreetOrgId();
// platDevice.setOrgPath();
// platDevice.setId();
// platDevice.setCreateDate();
// platDevice.setUpdateDate();
// platDevice.setDelFlag();
// platDevice.setCreateBy();
// platDevice.setUpdateBy();
deviceMap.put(iotDevice.getId(),platDevice);
});
return deviceMap.values();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment