package com.zd.laboratory.mqtt.controller;

import com.alibaba.fastjson.JSONObject;
import com.zd.common.core.enums.HardwareOperate;
import com.zd.common.core.utils.SpringUtils;
import com.zd.common.core.web.domain.ResultData;
import com.zd.laboratory.domain.LabHardware;
import com.zd.laboratory.event.RelayHardwareStatusEvent;
import com.zd.laboratory.event.VideoHardwareStatusEvent;
import com.zd.laboratory.mapper.LabHardwareMapper;
import com.zd.laboratory.mqtt.MqttProducer;
import com.zd.laboratory.mqtt.entiy.EquipmentStatus;
import com.zd.laboratory.service.ILabHardwareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zpp
 * @date 2019/8/26 18:19
 */
@RestController
@ResponseBody
@RequestMapping("/service/mqtt")
public class MqttController {

    @Autowired
    private MqttProducer mqttProducer;

    @Autowired
    private LabHardwareMapper labHardwareMapper;

    /**
     * 设备状态通知
     * mttp 没法进行通道竞争，所以这个数据不用往mqtt推送了（数据生产给业务系统维护硬件状态使用）
     * 所以现在接到数据后，我就直接把这个数跟新到数据库，后推送给mqtt(给客户端（小程序，管理后台）使用)
     * @return
     */
    @GetMapping("/send/{num}/{hardwareOperate}")
    public ResultData sends(@PathVariable("num") String num, @PathVariable("hardwareOperate")  HardwareOperate hardwareOperate){

        EquipmentStatus equipmentStatus = new EquipmentStatus();
        equipmentStatus.setNum(num);
        equipmentStatus.setHardwareOperate(hardwareOperate);

        VideoHardwareStatusEvent videoHardwareStatusEvent = new VideoHardwareStatusEvent(equipmentStatus);
        SpringUtils.getApplicationContext().publishEvent(videoHardwareStatusEvent);
        return ResultData.success();
    }
}
