2026-06-24 f4bd1f3c89d906131495a0aca5aaf82966378510
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package cn.iocoder.yudao.module.iot.service.device;
 
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.iot.controller.admin.device.vo.device.*;
import cn.iocoder.yudao.module.iot.core.biz.dto.IotDeviceAuthReqDTO;
import cn.iocoder.yudao.module.iot.core.biz.dto.IotSubDeviceRegisterFullReqDTO;
import cn.iocoder.yudao.module.iot.core.enums.device.IotDeviceStateEnum;
import cn.iocoder.yudao.module.iot.core.mq.message.IotDeviceMessage;
import cn.iocoder.yudao.module.iot.core.topic.IotDeviceIdentity;
import cn.iocoder.yudao.module.iot.core.topic.auth.IotDeviceRegisterReqDTO;
import cn.iocoder.yudao.module.iot.core.topic.auth.IotDeviceRegisterRespDTO;
import cn.iocoder.yudao.module.iot.core.topic.auth.IotSubDeviceRegisterRespDTO;
import cn.iocoder.yudao.module.iot.core.topic.topo.IotDeviceTopoGetRespDTO;
import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO;
import jakarta.validation.Valid;
 
import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
 
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
 
/**
 * IoT 设备 Service 接口
 *
 * @author 芋道源码
 */
public interface IotDeviceService {
 
    /**
     * 【管理员】创建设备
     *
     * @param createReqVO 创建信息
     * @return 编号
     */
    Long createDevice(@Valid IotDeviceSaveReqVO createReqVO);
 
    /**
     * 更新设备
     *
     * @param updateReqVO 更新信息
     */
    void updateDevice(@Valid IotDeviceSaveReqVO updateReqVO);
 
    /**
     * 更新设备状态
     *
     * @param device 设备信息
     * @param state 状态
     */
    void updateDeviceState(IotDeviceDO device, Integer state);
 
    /**
     * 更新设备状态
     *
     * @param id    编号
     * @param state 状态
     */
    void updateDeviceState(Long id, Integer state);
 
    /**
     * 更新设备分组
     *
     * @param updateReqVO 更新信息
     */
    void updateDeviceGroup(@Valid IotDeviceUpdateGroupReqVO updateReqVO);
 
    /**
     * 删除单个设备
     *
     * @param id 编号
     */
    void deleteDevice(Long id);
 
    /**
     * 删除多个设备
     *
     * @param ids 编号数组
     */
    void deleteDeviceList(Collection<Long> ids);
 
    /**
     * 校验设备是否存在
     *
     * @param id 设备 ID
     * @return 设备对象
     */
    IotDeviceDO validateDeviceExists(Long id);
 
    /**
     * 【缓存】校验设备是否存在
     *
     * @param id 设备 ID
     * @return 设备对象
     */
    IotDeviceDO validateDeviceExistsFromCache(Long id);
 
    /**
     * 获得设备
     *
     * @param id 编号
     * @return IoT 设备
     */
    IotDeviceDO getDevice(Long id);
 
    /**
     * 【缓存】获得设备信息
     * <p>
     * 注意:该方法会忽略租户信息,所以调用时,需要确认会不会有跨租户访问的风险!!!
     *
     * @param id 编号
     * @return IoT 设备
     */
    IotDeviceDO getDeviceFromCache(Long id);
 
    /**
     * 【缓存】根据产品 key 和设备名称,获得设备信息
     * <p>
     * 注意:该方法会忽略租户信息,所以调用时,需要确认会不会有跨租户访问的风险!!!
     *
     * @param productKey 产品 key
     * @param deviceName 设备名称
     * @return 设备信息
     */
    IotDeviceDO getDeviceFromCache(String productKey, String deviceName);
 
    /**
     * 获得设备分页
     *
     * @param pageReqVO 分页查询
     * @return IoT 设备分页
     */
    PageResult<IotDeviceDO> getDevicePage(IotDevicePageReqVO pageReqVO);
 
    /**
     * 根据条件,获得设备列表
     *
     * @param deviceType 设备类型
     * @param productId 产品编号
     * @return 设备列表
     */
    List<IotDeviceDO> getDeviceListByCondition(@Nullable Integer deviceType,
                                               @Nullable Long productId);
 
    /**
     * 获得状态,获得设备列表
     *
     * @param state 状态
     * @return 设备列表
     */
    List<IotDeviceDO> getDeviceListByState(Integer state);
 
    /**
     * 根据产品编号,获取设备列表
     *
     * @param productId 产品编号
     * @return 设备列表
     */
    List<IotDeviceDO> getDeviceListByProductId(Long productId);
 
    /**
     * 基于产品编号,获得设备数量
     *
     * @param productId 产品编号
     * @return 设备数量
     */
    Long getDeviceCountByProductId(Long productId);
 
    /**
     * 获得设备数量
     *
     * @param groupId 分组编号
     * @return 设备数量
     */
    Long getDeviceCountByGroupId(Long groupId);
 
    /**
     * 导入设备
     *
     * @param importDevices 导入设备列表
     * @param updateSupport 是否支持更新
     * @return 导入结果
     */
    IotDeviceImportRespVO importDevice(List<IotDeviceImportExcelVO> importDevices, boolean updateSupport);
 
    /**
     * 获得设备数量
     *
     * @param createTime 创建时间,如果为空,则统计所有设备数量
     * @return 设备数量
     */
    Long getDeviceCount(@Nullable LocalDateTime createTime);
 
    /**
     * 获得设备认证信息
     *
     * @param id 设备编号
     * @return MQTT 连接参数
     */
    IotDeviceAuthInfoRespVO getDeviceAuthInfo(Long id);
 
    /**
     * 获得各个产品下的设备数量 Map
     *
     * @return key: 产品编号, value: 设备数量
     */
    Map<Long, Integer> getDeviceCountMapByProductId();
 
    /**
     * 获得各个状态下的设备数量 Map
     *
     * @return key: 设备状态枚举 {@link IotDeviceStateEnum}
     *         value: 设备数量
     */
    Map<Integer, Long> getDeviceCountMapByState();
 
    /**
     * 通过产品标识和设备名称列表获取设备列表
     *
     * @param productKey  产品标识
     * @param deviceNames 设备名称列表
     * @return 设备列表
     */
    List<IotDeviceDO> getDeviceListByProductKeyAndNames(String productKey, List<String> deviceNames);
 
    /**
     * 认证设备
     *
     * @param authReqDTO 认证信息
     * @return 是否认证成功
     */
    boolean authDevice(@Valid IotDeviceAuthReqDTO authReqDTO);
 
    /**
     * 校验设备是否存在
     *
     * @param ids 设备编号数组
     */
    List<IotDeviceDO> validateDeviceListExists(Collection<Long> ids);
 
    /**
     * 获得设备列表
     *
     * @param ids 设备编号数组
     * @return 设备列表
     */
    List<IotDeviceDO> getDeviceList(Collection<Long> ids);
 
    /**
     * 获得设备 Map
     *
     * @param ids 设备编号数组
     * @return 设备 Map
     */
    default Map<Long, IotDeviceDO> getDeviceMap(Collection<Long> ids) {
        return convertMap(getDeviceList(ids), IotDeviceDO::getId);
    }
 
    /**
     * 更新设备固件版本
     *
     * @param deviceId 设备编号
     * @param firmwareId 固件编号
     */
    void updateDeviceFirmware(Long deviceId, Long firmwareId);
 
    /**
     * 更新设备定位信息(GeoLocation 上报时调用)
     *
     * @param device    设备信息(用于清除缓存)
     * @param longitude 经度
     * @param latitude  纬度
     */
    void updateDeviceLocation(IotDeviceDO device, BigDecimal longitude, BigDecimal latitude);
 
    /**
     * 获得有位置信息的设备列表
     *
     * @return 设备列表
     */
    List<IotDeviceDO> getDeviceListByHasLocation();
 
    // ========== 网关-拓扑管理(后台操作) ==========
 
    /**
     * 绑定子设备到网关
     *
     * @param subIds    子设备编号列表
     * @param gatewayId 网关设备编号
     */
    void bindDeviceGateway(Collection<Long> subIds, Long gatewayId);
 
    /**
     * 解绑子设备与网关
     *
     * @param subIds    子设备编号列表
     * @param gatewayId 网关设备编号
     */
    void unbindDeviceGateway(Collection<Long> subIds, Long gatewayId);
 
    /**
     * 获取未绑定网关的子设备分页
     *
     * @param pageReqVO 分页查询参数(仅使用 productId、deviceName、nickname)
     * @return 子设备分页
     */
    PageResult<IotDeviceDO> getUnboundSubDevicePage(IotDevicePageReqVO pageReqVO);
 
    /**
     * 根据网关编号获取子设备列表
     *
     * @param gatewayId 网关设备编号
     * @return 子设备列表
     */
    List<IotDeviceDO> getDeviceListByGatewayId(Long gatewayId);
 
    // ========== 网关-拓扑管理(设备上报) ==========
 
    /**
     * 处理添加拓扑关系消息(网关设备上报)
     *
     * @param message       消息
     * @param gatewayDevice 网关设备
     * @return 成功添加的子设备列表
     */
    List<IotDeviceIdentity> handleTopoAddMessage(IotDeviceMessage message, IotDeviceDO gatewayDevice);
 
    /**
     * 处理删除拓扑关系消息(网关设备上报)
     *
     * @param message       消息
     * @param gatewayDevice 网关设备
     * @return 成功删除的子设备列表
     */
    List<IotDeviceIdentity> handleTopoDeleteMessage(IotDeviceMessage message, IotDeviceDO gatewayDevice);
 
    /**
     * 处理获取拓扑关系消息(网关设备上报)
     *
     * @param gatewayDevice 网关设备
     * @return 拓扑关系响应
     */
    IotDeviceTopoGetRespDTO handleTopoGetMessage(IotDeviceDO gatewayDevice);
 
    // ========== 设备动态注册 ==========
 
    /**
     * 直连/网关设备动态注册
     *
     * @param reqDTO 动态注册请求
     * @return 注册结果(包含 DeviceSecret)
     */
    IotDeviceRegisterRespDTO registerDevice(@Valid IotDeviceRegisterReqDTO reqDTO);
 
    /**
     * 网关子设备动态注册
     * <p>
     * 与 {@link #handleSubDeviceRegisterMessage} 方法的区别:
     * 该方法网关设备信息通过 reqDTO 参数传入,而 {@link #handleSubDeviceRegisterMessage} 方法通过 gatewayDevice 参数传入
     *
     * @param reqDTO 子设备注册请求(包含网关设备信息)
     * @return 注册结果列表
     */
    List<IotSubDeviceRegisterRespDTO> registerSubDevices(@Valid IotSubDeviceRegisterFullReqDTO reqDTO);
 
    /**
     * 处理子设备动态注册消息(网关设备上报)
     *
     * @param message       消息
     * @param gatewayDevice 网关设备
     * @return 注册结果列表
     */
    List<IotSubDeviceRegisterRespDTO> handleSubDeviceRegisterMessage(IotDeviceMessage message, IotDeviceDO gatewayDevice);
 
}