package com.shzg.project.worn.controller;

import com.shzg.framework.web.domain.AjaxResult;
import com.shzg.project.worn.service.IMqttSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/worn/socket")
public class SocketController {

    @Autowired
    private IMqttSocketService mqttSocketService;

    /**
     * 控制智能排风插座开关
     */
    @PostMapping("/control")
    public AjaxResult control(@RequestParam String devEui,
                              @RequestParam Integer status) {

        if (devEui == null || devEui.isEmpty()) {
            return AjaxResult.error("devEui不能为空");
        }

        if (status == null || (status != 0 && status != 1)) {
            return AjaxResult.error("status只能为0或1");
        }

        boolean on = status == 1;

        mqttSocketService.controlSocket(devEui, on);

        return AjaxResult.success("指令已发送");
    }

    /**
     * 控制智慧照明开关（switch）
     */
    @PostMapping("/switch")
    public AjaxResult controlSwitch(@RequestParam String devEui,
                                    @RequestParam Integer channel,
                                    @RequestParam Integer status) {

        if (devEui == null || devEui.isEmpty()) {
            return AjaxResult.error("devEui不能为空");
        }

        if (channel == null || channel < 1 || channel > 3) {
            return AjaxResult.error("channel只能为1~3");
        }

        if (status == null || (status != 0 && status != 1)) {
            return AjaxResult.error("status只能为0或1");
        }

        mqttSocketService.controlSwitch(devEui, channel, status == 1);

        return AjaxResult.success("开关指令已发送");
    }
}