zxl
2026-03-25 963a8c24874c2e10a329a6ea39774bc5eda0f762
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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);
            }
        }
    }
 
}