//package com.ruoyi.web.v1.controller;
//
//import java.util.Date;
//import java.util.List;
//import java.util.stream.Collectors;
//import javax.servlet.http.HttpServletResponse;
//
//import com.ruoyi.web.v1.domain.request.ReqMonitorAction;
//import com.ruoyi.web.core.wvp.domain.DeviceChannelExtend;
//import com.ruoyi.common.utils.DateUtils;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.security.access.prepost.PreAuthorize;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.PutMapping;
//import org.springframework.web.bind.annotation.DeleteMapping;
//import org.springframework.web.bind.annotation.PathVariable;
//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.enums.BusinessType;
//import com.ruoyi.web.v1.domain.Monitor;
//import com.ruoyi.web.v1.service.IMonitorService;
//import com.ruoyi.common.utils.poi.ExcelUtil;
//import com.ruoyi.common.core.page.TableDataInfo;
//import com.ruoyi.web.core.wvp.DeviceHandler;
//
///**
// * 监控设备Controller
// *
// * @author coede
// * @date 2025-03-19
// */
//@Api("监控设备信息管理")
//@RestController
//@RequestMapping("/app/monitor")
//public class MonitorController extends BaseController
//{
//    @Autowired
//    private IMonitorService monitorService;
//
//    @Autowired
//    private DeviceHandler deviceHandler;
//
//    public static final Logger log = LoggerFactory.getLogger(ExcelUtil.class);
//    /**
//     * 查询监控设备列表
//     */
//    @ApiOperation("查询监控设备列表")
//    // @PreAuthorize("@ss.hasPermi('app:monitor:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(Monitor monitor)
//    {
//        startPage();
//        List<Monitor> list = monitorService.selectMonitorList(monitor);
//        return getDataTable(list);
//    }
//
//    /**
//     * 导出监控设备列表
//     */
//    @ApiOperation("导出监控设备列表")
//    // @PreAuthorize("@ss.hasPermi('app:monitor:export')")
//    @Log(title = "监控设备", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Monitor monitor)
//    {
//        List<Monitor> list = monitorService.selectMonitorList(monitor);
//        ExcelUtil<Monitor> util = new ExcelUtil<Monitor>(Monitor.class);
//        util.exportExcel(response, list, "监控设备数据");
//    }
//
//    /**
//     * 获取监控设备详细信息
//     */
//    @ApiOperation("获取监控设备详细信息")
//    // @PreAuthorize("@ss.hasPermi('app:monitor:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(monitorService.selectMonitorById(id));
//    }
//
//    /**
//     * 新增监控设备
//     */
//    @ApiOperation("新增监控设备")
//    // @PreAuthorize("@ss.hasPermi('app:monitor:add')")
//    @Log(title = "监控设备", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@Validated  @RequestBody Monitor monitor)
//    {
//        if (!monitorService.checkMonitorNameUnique(monitor))
//        {
//            return error("新增监控设备'" + monitor.getMonitorName() + "'失败，列表中存在重复的监控名称");
//        }
//        monitor.setCreateBy(getUsername());
//        return toAjax(monitorService.insertMonitor(monitor));
//    }
//
//    /**
//     * 同步监控设备
//     */
//    @ApiOperation("同步监控设备")
//    // @PreAuthorize("@ss.hasPermi('app:monitor:sync')")
//    @Log(title = "监控设备", businessType = BusinessType.INSERT)
//    @PostMapping("/sync")
//    public AjaxResult sync()
//    {
//        try {
//            //获取WVP所有的设备通道
//            List<DeviceChannelExtend> channelList = deviceHandler.getDeviceList();
//            Date now = DateUtils.getNowDate();
//            List<Monitor> monitorArr = channelList.stream()
//                    .map(item -> {
//                        Monitor monitor = new Monitor();
//                        monitor.setMonitorName(item.getName());
//                        monitor.setIpAddr(item.getIpAddress());
//                        monitor.setMonitorBrand(item.getManufacture());
//                        monitor.setDeviceId(item.getDeviceId());
//                        monitor.setChannelId(item.getChannelId());
//                        monitor.setCreateTime(now);
//                        monitor.setCreateBy(getUsername());
//                        return monitor;
//                    })
//                    .collect(Collectors.toList());
//            return toAjax(monitorService.syncMonitor(monitorArr));
//        }catch (Exception e){
//            log.error(e.getMessage());
//            return error("同步失败");
//        }
//    }
//
//    /**
//     * 修改监控设备
//     */
//    @ApiOperation("修改监控设备")
//    // @PreAuthorize("@ss.hasPermi('app:monitor:edit')")
//    @Log(title = "监控设备", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@Validated @RequestBody Monitor monitor)
//    {
//        if (!monitorService.checkMonitorNameUnique(monitor))
//        {
//            return error("修改监控设备'" + monitor.getMonitorName() + "'失败，列表中存在重复的监控名称");
//        }
//        monitor.setUpdateBy(getUsername());
//        return toAjax(monitorService.updateMonitor(monitor));
//    }
//
//    /**
//     * 删除监控设备
//     */
//    @ApiOperation("删除监控设备")
//    // @PreAuthorize("@ss.hasPermi('app:monitor:remove')")
//    @Log(title = "监控设备", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        return toAjax(monitorService.deleteMonitorByIds(ids));
//    }
//
//    /**
//     * 播放监控设备
//     */
//    @ApiOperation("播放监控设备")
//    // @PreAuthorize("@ss.hasPermi('app:monitor:playStart')")
//    @Log(title = "监控设备", businessType = BusinessType.OTHER)
//    @PostMapping("/playStart")
//    public AjaxResult playStart(@Validated @RequestBody ReqMonitorAction req)
//    {
//        try{
//        Object res = deviceHandler.playOrStop(DeviceHandler.VIDEO_PLAY_START,req.getDeviceId(),req.getChannelId());
//            if (res != null){
//                return AjaxResult.success(res);
//            }else{
//                throw new RuntimeException("无视频信息，请重试");
//            }
//        }catch (Exception e){
//            return error("播放失败:"+e.getMessage());
//        }
//    }
//
//    /**
//     * 停止监控设备
//     */
//    @ApiOperation("停止监控设备")
//    // @PreAuthorize("@ss.hasPermi('app:monitor:playStop')")
//    @Log(title = "监控设备", businessType = BusinessType.OTHER)
//    @PostMapping("/playStop")
//    public AjaxResult playStop(@Validated @RequestBody ReqMonitorAction req)
//    {
//        try{
//            Object res = deviceHandler.playOrStop(DeviceHandler.VIDEO_PLAY_STOP,req.getDeviceId(),req.getChannelId());
//            if (res != null){
//                return AjaxResult.success(res);
//            }else{
//                throw new RuntimeException("无视频信息，请重试");
//            }
//        }catch (Exception e){
//            return error("停止失败:"+e.getMessage());
//        }
//    }
//}
