Commit de677d03 by huangjy

fix: 机构类型区分居家还是养老,告警配置开关,设备同步定时器逻辑修改

parent 4d7339b2
package com.makeit.task; package com.makeit.task;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.makeit.common.entity.BaseBusEntity;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.entity.saas.PlatTenant;
import com.makeit.enums.CommonEnum;
import com.makeit.enums.report.DeviceNameEnum;
import com.makeit.global.aspect.tenant.TenantIdIgnore; import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.module.iot.service.IotOrgService;
import com.makeit.module.iot.vo.DeviceInstanceEntity;
import com.makeit.module.iot.vo.DeviceState;
import com.makeit.module.system.service.SysDictionaryCategoryService;
import com.makeit.module.system.vo.DictionaryVo;
import com.makeit.service.platform.device.PlatDeviceService; import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.service.platform.elder.PlatElderDayReportDayService;
import com.makeit.service.platform.elder.PlatElderSleepService; import com.makeit.service.platform.elder.PlatElderSleepService;
import com.makeit.service.saas.PlatTenantService;
import com.makeit.utils.DeviceCacheUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; 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 @Component
@Slf4j @Slf4j
public class IotSyncTask { public class IotSyncTask {
@Autowired @Autowired
private IotOrgService iotOrgService;
@Autowired
private PlatTenantService platTenantService;
@Autowired
private PlatDeviceService platDeviceService; private PlatDeviceService platDeviceService;
@Autowired
private SysDictionaryCategoryService sysDictionaryCategoryService;
@Autowired
private DeviceCacheUtil deviceCacheUtil;
@Autowired @Autowired
private PlatElderSleepService platElderSleepService; private PlatElderSleepService platElderSleepService;
@Autowired
private PlatElderDayReportDayService platElderDayReportDayService;
/** /**
* 一小时同步一次 * 一小时同步一次
* 启用状态的租户才同步 * 启用状态的租户才同步
* 新增和更新平台端设备表 * 新增和更新平台端设备表
*/ */
@Scheduled(cron = "0 0/3 * * * ?") @Scheduled(cron = "0 0/1 * * * ?")
public void syncEquipmentInfo() { public void syncEquipmentInfo() {
savePlatDevice(); platDeviceService.savePlatDevice();
}
public void savePlatDevice(){
log.info("开始执行同步设备信息接口");
LambdaQueryWrapper<PlatTenant> tenantLambdaQueryWrapper = new LambdaQueryWrapper<PlatTenant>().eq(PlatTenant::getStatus, CommonEnum.YES.getValue());
List<PlatTenant> platTenants = platTenantService.list(tenantLambdaQueryWrapper);
List<DictionaryVo> dictionaryVos = sysDictionaryCategoryService.getByCategoryCode("device.category");
Map<String, String> dicNameIdMap = dictionaryVos.stream().collect(Collectors.toMap(DictionaryVo::getName, DictionaryVo::getValue, (v1, v2) -> v1));
for (PlatTenant platTenant : platTenants) {
String iotOrgId = platTenant.getIotOrgId();
if (StringUtils.isBlank(iotOrgId)) {
continue;
}
//查询iot设备
List<DeviceInstanceEntity> iotDeviceList = iotOrgService.getOrgDevice(iotOrgId);
//查询平台设备
Set<String> iotDeviceIdSet = iotDeviceList.stream().map(DeviceInstanceEntity::getId).collect(Collectors.toSet());
LambdaQueryWrapper<PlatDevice> deviceLambdaQueryWrapper = new LambdaQueryWrapper<PlatDevice>().eq(BaseBusEntity::getTenantId, platTenant.getId())
.in(PlatDevice::getOriDeviceId, iotDeviceIdSet);
iotDeviceIdSet.add("-1");
//删除设备
LambdaQueryWrapper<PlatDevice> removeQw = new LambdaQueryWrapper<PlatDevice>().notIn(PlatDevice::getOriDeviceId, iotDeviceIdSet)
.eq(BaseBusEntity::getTenantId, platTenant.getId());
platDeviceService.remove(removeQw);
List<PlatDevice> deviceList = platDeviceService.list(deviceLambdaQueryWrapper);
//更新平台设备
Collection<PlatDevice> platDevices = convertToPlatDevice(iotDeviceList, deviceList, platTenant.getId(), dicNameIdMap);
platDeviceService.saveOrUpdateBatch(platDevices);
deviceCacheUtil.putAll(platDevices);
}
log.info("结束执行同步设备信息接口");
} }
private Collection<PlatDevice> convertToPlatDevice(List<DeviceInstanceEntity> iotDeviceList, List<PlatDevice> deviceList, String tenantId, Map<String, String> dicNameIdMap) {
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);
deviceMap.put(iotDevice.getId(), platDevice);
}
platDevice.setOriDeviceId(iotDevice.getId());
platDevice.setName(iotDevice.getName());
platDevice.setOrgId(tenantId);
String productName = iotDevice.getProductName();
platDevice.setProductName(productName);
platDevice.setProductId(iotDevice.getProductId());
if(iotDevice.getRegistryTime()!=null) {
LocalDateTime registryTime = LocalDateTime.ofEpochSecond(iotDevice.getRegistryTime() / 1000, 0, ZoneOffset.ofHours(8));
platDevice.setRegistrationDate(registryTime);
}
platDevice.setDescription(iotDevice.getDescribe());
String state = iotDevice.getState();
DeviceState deviceState = JSON.parseObject(state, DeviceState.class);
platDevice.setStatus(deviceState.getValue());
//todo 根据类型名称来匹配
String categoryName = DeviceNameEnum.getNameByPrefix(productName);
platDevice.setCategory(dicNameIdMap.get(categoryName));
// 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();
});
return deviceMap.values();
}
/** /**
* 床位id 绑定的所有设备 * 床位id 绑定的所有设备
* 房间id 绑定的所有设备 * 房间id 绑定的所有设备
......
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