package com.tievd.jyz.service.impl;
|
|
import cn.hutool.core.util.ObjectUtil;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.tievd.cube.modules.system.model.SysDepartTreeModel;
|
import com.tievd.jyz.cache.ResourceCache;
|
import com.tievd.jyz.constants.SystemConstant;
|
import com.tievd.jyz.dto.DeviceDTO;
|
import com.tievd.jyz.entity.Device;
|
import com.tievd.jyz.entity.UpgradeRecord;
|
import com.tievd.jyz.mapper.CameraMapper;
|
import com.tievd.jyz.mapper.DeviceMapper;
|
import com.tievd.jyz.mapper.UpgradeRecordMapper;
|
import com.tievd.jyz.mqtt.PojoDataConverter;
|
import com.tievd.jyz.mqtt.command.InitConfigCommand;
|
import com.tievd.jyz.mqtt.command.MqttCommandReceiver;
|
import com.tievd.jyz.mqtt.dto.InitConfigDTO;
|
import com.tievd.jyz.mqtt.dto.MqttParamDTO;
|
import com.tievd.jyz.mqtt.dto.RegisterDTO;
|
import com.tievd.jyz.service.IDeviceService;
|
import com.tievd.jyz.util.S3Util;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* Device
|
*
|
* @author cube
|
* @version V2.0.0
|
* @since 2023-02-27
|
*/
|
@Slf4j
|
@Service
|
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {
|
@Autowired
|
private MqttCommandReceiver mqttCommandReceiver;
|
|
@Value("${init.sendCommandTopic:/ty/aibox/command/}")
|
private String sendCommandTopic;
|
|
@Value("${init.heartbeatTime:30}")
|
private Integer heartbeatTime;
|
|
@Autowired
|
private S3Util s3Util;
|
|
@Autowired
|
private CameraMapper cameraMapper;
|
|
@Autowired
|
private UpgradeRecordMapper upgradeRecordMapper;
|
|
/**
|
* 网关注册处理
|
*
|
* @param mqttParamDTO
|
*/
|
@Override
|
public void register(MqttParamDTO<JSON> mqttParamDTO) {
|
String deviceSN = mqttParamDTO.getSn();
|
RegisterDTO registerDTO = JSONObject.toJavaObject(mqttParamDTO.getData(), RegisterDTO.class);
|
|
Device device = baseMapper.selectOne(new QueryWrapper<Device>().eq("sn", deviceSN));
|
if (ObjectUtil.isNull(device)) {
|
log.error("平台未找到网关设备,注册失败,SN:{}", deviceSN);
|
return;
|
}
|
Device tmpDevice = PojoDataConverter.deviceConvert(deviceSN, registerDTO);
|
tmpDevice.setId(device.getId());
|
baseMapper.updateById(tmpDevice);
|
// 升级结果处理
|
List<UpgradeRecord> records = ResourceCache.getUpgradeRecord(deviceSN);
|
for (UpgradeRecord record : records) {
|
record.setStatus(SystemConstant.ISSUE_SUCCESS);
|
record.setUpdateTime(new Date());
|
upgradeRecordMapper.updateById(record);
|
ResourceCache.removeUpgradeRecord(record);
|
}
|
//返回配置信息
|
InitConfigDTO initConfigDTO = PojoDataConverter.InitConfigConvert(s3Util, heartbeatTime);
|
InitConfigCommand initConfigCommand = new InitConfigCommand(mqttCommandReceiver, initConfigDTO, sendCommandTopic, deviceSN);
|
if (initConfigCommand.init() == SystemConstant.DEAL_FAIL) {
|
log.error("初始化指令配置失败");
|
}
|
if (initConfigCommand.execute() == SystemConstant.DEAL_FAIL) {
|
log.error("初始化指令发送失败");
|
}
|
}
|
|
@Override
|
public IPage<DeviceDTO> tables(Page<Device> page, QueryWrapper<Device> queryWrapper) {
|
IPage<DeviceDTO> tables = baseMapper.tables(page, queryWrapper);
|
return tables;
|
}
|
|
@Override
|
public void deleteById(String id) {
|
baseMapper.deleteById(id);
|
cameraMapper.deleteByDeviceId(id);
|
}
|
|
@Override
|
public void assembleDevice(List<SysDepartTreeModel> list) {
|
for (SysDepartTreeModel sysDepartTreeModel : list) {
|
LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
|
queryWrapper.likeRight(Device::getOrgCode, sysDepartTreeModel.getOrgCode());
|
//卸油盒子不下发
|
queryWrapper.ne(Device::getDeviceType, SystemConstant.OILOUT_DEVICE_TYPE);
|
List<Device> devices = baseMapper.selectList(queryWrapper);
|
for (Device device : devices) {
|
SysDepartTreeModel treeModel = new SysDepartTreeModel();
|
treeModel.setKey(device.getId().toString());
|
treeModel.setValue(device.getId().toString());
|
treeModel.setId(device.getId().toString());
|
treeModel.setDepartName(device.getName());
|
treeModel.setTitle(device.getName());
|
treeModel.setOrgCode(device.getOrgCode());
|
treeModel.setStatus(device.getStatus().toString());
|
treeModel.setOrgType("99999");
|
treeModel.setOrgCategory("99999");
|
assemble(treeModel, sysDepartTreeModel);
|
}
|
}
|
}
|
|
private void assemble(SysDepartTreeModel device, SysDepartTreeModel sysDepartTreeModel) {
|
List<SysDepartTreeModel> children = sysDepartTreeModel.getChildren();
|
if (device.getOrgCode().equals(sysDepartTreeModel.getOrgCode())) {
|
if (children == null) {
|
children = new ArrayList<>();
|
}
|
device.setParentId(sysDepartTreeModel.getId());
|
children.add(device);
|
sysDepartTreeModel.setChildren(children);
|
} else {
|
if (children == null) {
|
return;
|
}
|
for (SysDepartTreeModel treeModel : children) {
|
assemble(device, treeModel);
|
}
|
}
|
}
|
|
}
|