package com.xydl.cac.controller;

import com.xydl.cac.model.Response;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.serialport.SerialPortServer;
import com.xydl.cac.service.IcdFileConfigService;
import com.xydl.cac.socket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = {"测试接口文档"})
@RequestMapping("test")
@Slf4j
public class TestController extends BasicController {

    @Resource
    IcdFileConfigService configService;
    @Resource
    WebSocketServer webSocketServer;

    @Resource
    SerialPortServer serialPortService;

    @GetMapping("compare61850")
    @ApiOperation("比对61850的不同点")
    public Response<List<String>> compare61850() throws Exception {
        List<String> result = configService.compare61850();
        return Response.success(result);
    }

    @GetMapping("sendMsg")
    @ApiOperation("发送消息")
    public void sendMsg(String msg, Integer warningId) {
        webSocketServer.sendMessage(msg, warningId);
    }

    @GetMapping("realTimeClientMap")
    @ApiOperation("realTimeClientMap")
    public Response<HashMap> realTimeClientMap() {
        return Response.success(StaticVariable.realTimeClientMap);
    }

    @GetMapping("paramRelationMap")
    @ApiOperation("paramRelationMap")
    public Response<HashMap> paramRelationMap() {
        return Response.success(StaticVariable.paramRelationMap);
    }

    @GetMapping("sensorLastDataMap")
    @ApiOperation("sensorLastDataMap")
    public Response<HashMap> sensorLastDataMap() {
        return Response.success(StaticVariable.sensorLastDataMap);
    }

    @GetMapping("iecServerMap")
    @ApiOperation("iecServerMap")
    public Response<HashMap> iecServerMap() {
        return Response.success(StaticVariable.iecServerMap);
    }

    @GetMapping("rptFromActiveMap")
    @ApiOperation("rptFromActiveMap")
    public Response<HashMap> rptFromActiveMap() {
        return Response.success(StaticVariable.rptFromActiveMap);
    }

    @GetMapping("rptToActiveMap")
    @ApiOperation("rptToActiveMap")
    public Response<HashMap> rptToActiveMap() {
        return Response.success(StaticVariable.rptToActiveMap);
    }

    @GetMapping("clearMap")
    @ApiOperation("clearMap")
    public void clearMap() {
        StaticVariable.sensorLastDataMap.clear();
        StaticVariable.paramRelationMap.clear();
    }

    @GetMapping("/discover")
    public Response<String> discoverSerialPort() {
        serialPortService.discoverSerialPort();
            return Response.fail("未找到");
    }

    @GetMapping("/open")
    public Response<Boolean> openSerialPort() {
        return Response.success(serialPortService.openSerialPort());
    }

    @GetMapping("/send")
    public String sendData() throws IOException {
        //发送数据注意，提前与接收设备沟通好协议，发送什么样类型的数据设备才可以进行响应，否则设备无响应
        serialPortService.sendData();
        return "Data sent>>>";
    }

    @GetMapping("/close")
    public String closeSerialPort() {
        serialPortService.closeSerialPort();
        return "Serial port closed";
    }
}
