package com.usky.cdi.controller;

import com.alibaba.fastjson.JSONObject;
import com.usky.cdi.service.impl.IotDataTransferService;
import com.usky.cdi.service.vo.IotDataTransferVO;
import com.usky.cdi.service.vo.base.*;
import com.usky.common.core.bean.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author fyc
 * @email yuchuan.fu@chinausky.com
 * @date 2025/11/20
 */
@Slf4j
@RestController
@RequestMapping("/api/iotInfo")
@ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
public class IotDataController {

    @Autowired
    private IotDataTransferService iotDataTransferService;

    /**
     * 上报水浸状态
     */
    @PostMapping("/flooded")
    public ApiResult<Void> sendWaterLeak(@RequestBody IotDataTransferVO jsonObject) {
        // iotDataTransferService.createMqttConnection("3101130019", "ptrEQZK2");
        iotDataTransferService.sendWaterLeak(jsonObject);
        return ApiResult.success();
    }

    /**
     * 上报温度、湿度、氧气、一氧化碳、二氧化碳
     */
    @PostMapping("/envData")
    public ApiResult<Void> sendEnvData(@RequestBody IotDataTransferVO jsonObject) {
        iotDataTransferService.sendEnvData(jsonObject);
        return ApiResult.success();
    }

    /**
     * 上报人员闯入
     */
    @PostMapping("/personPresence")
    public ApiResult<Void> sendPerson(@RequestBody IotDataTransferVO jsonObject) {
        iotDataTransferService.sendPersonPresence(jsonObject);
        return ApiResult.success();
    }

    /**
     * 上报用电负荷
     */
    @PostMapping("/electricityLoad")
    public ApiResult<Void> sendElectricityLoad(@RequestBody IotDataTransferVO jsonObject) {
        iotDataTransferService.sendElectricityLoad(jsonObject);
        return ApiResult.success();
    }

}
