package com.ruoyi.web.controller.device; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.system.domain.TDeviceAdapter; import com.ruoyi.system.service.IDeviceAdapterService; /** 设备适配器实例管理与发现 REST。 */ @RestController @RequestMapping("/system/deviceAdapter") public class DeviceAdapterController extends BaseController { private final IDeviceAdapterService service; public DeviceAdapterController(IDeviceAdapterService service) { this.service = service; } @PreAuthorize("@ss.hasPermi('system:deviceAdapter:list')") @GetMapping("/list") /** 分页查询适配器配置及其最近运行状态。 */ public TableDataInfo list() { startPage(); return getDataTable(service.selectList(new TDeviceAdapter())); } @PreAuthorize("@ss.hasPermi('system:deviceAdapter:query')") @GetMapping("/{adapterId}") /** 查询指定适配器详情;适配器必须存在。 */ public AjaxResult getInfo(@PathVariable Long adapterId) { return success(service.selectById(adapterId)); } @PreAuthorize("@ss.hasPermi('system:deviceAdapter:add')") @Log(title = "设备适配器实例", businessType = BusinessType.INSERT) @PostMapping /** 新增默认禁用的只读适配器配置。 */ public AjaxResult add(@Validated @RequestBody TDeviceAdapter adapter) { adapter.setCreateBy(getUsername()); return toAjax(service.insert(adapter)); } @PreAuthorize("@ss.hasPermi('system:deviceAdapter:edit')") @Log(title = "设备适配器实例", businessType = BusinessType.UPDATE) @PutMapping /** 修改适配器配置;启用实例会同步替换运行时连接。 */ public AjaxResult edit(@Validated @RequestBody TDeviceAdapter adapter) { adapter.setUpdateBy(getUsername()); return toAjax(service.update(adapter)); } @PreAuthorize("@ss.hasPermi('system:deviceAdapter:changeStatus')") @Log(title = "设备适配器实例启停", businessType = BusinessType.UPDATE) @PutMapping("/{adapterId}/status") /** 启用或停用适配器;启用前配置必须通过只读校验。 */ public AjaxResult changeStatus(@PathVariable Long adapterId, @RequestBody StatusCommand command) { return toAjax(service.changeStatus(adapterId, command.enabled(), getUsername())); } @PreAuthorize("@ss.hasPermi('system:deviceAdapter:discover')") @Log(title = "设备适配器发现", businessType = BusinessType.OTHER) @PostMapping("/{adapterId}/discover") /** 执行一次 getdevinfo 与实时 inquire,并返回本轮发现摘要。 */ public AjaxResult discover(@PathVariable Long adapterId) { return success(service.discover(adapterId)); } @PreAuthorize("@ss.hasPermi('system:deviceAdapter:list')") @GetMapping("/{adapterId}/channels") /** 查询适配器当前未过期的 Redis 发现通道。 */ public AjaxResult channels(@PathVariable Long adapterId) { return success(service.selectChannels(adapterId)); } public record StatusCommand(boolean enabled) { } }