package com.jsowell.api.thirdparty;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.netty.service.camera.CameraBusinessService;
import com.jsowell.pile.dto.camera.CameraHeartBeatDTO;
import com.jsowell.pile.dto.camera.SendMsg2TopicDTO;
import com.jsowell.thirdparty.camera.common.CameraCommonResult;
import com.jsowell.service.CameraService;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * 充电相机 controller
 *
 * @author Lemon
 * @Date 2023/12/5 9:06:53
 */
@Anonymous
@RestController
@RequestMapping("/camera")
public class CameraController extends BaseController {
    @Autowired
    private CameraService cameraService;

    @Autowired
    private CameraBusinessService cameraBusinessService;

    /**
     * 心跳
     * @param dto
     */
    @PostMapping("/v1/receiveHeartBeat")
    public CameraCommonResult receiveHeartBeat(@RequestBody CameraHeartBeatDTO dto) {
        logger.info("接收相机系统心跳包 params:{}", JSON.toJSONString(dto));
        cameraService.saveHeartBeat2Redis(dto);
        CameraCommonResult result = new CameraCommonResult();
        logger.info("接收相机系统心跳包 result:{}", JSON.toJSONString(result.successResponse()));
        return result.successResponse();
    }

    /**
     * 识别结果
     * @param jsonObject
     */
    @PostMapping("/v1/receiveIdentifyResults")
    public CameraCommonResult receiveIdentifyResults(@RequestBody JSONObject jsonObject) {
        logger.info("相机系统接收识别结果 params:{}", jsonObject);
        CameraCommonResult result = new CameraCommonResult();
        try {
            // 调用service方法处理
            cameraService.receiveIdentifyResults(jsonObject);
        } catch (Exception e) {
            logger.error("相机系统接收识别结果 error,", e);
        }
        return result.successResponse();
    }

    /**
     * 给某个 Topic 发消息
     * @param dto
     */
    @PostMapping("/sendMsg2Topic")
    public void sendMsg2Topic(@RequestBody SendMsg2TopicDTO dto) {
        try {
            cameraService.sendMsg2Topic(dto.getSn(), dto.getMsgType(), dto.getMsgPrefix(), dto.getTopic(), dto.getMsgData());
        } catch (Exception e) {
            logger.error("发送消息 error, ", e);
        }
    }
}
