package com.fastbee.data.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dtflys.forest.annotation.Post;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.iot.model.dto.DeviceOperationDTO;
import com.fastbee.mqttclient.PubMqttCallBack;
import com.fastbee.mqttclient.PubMqttClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
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;

import java.util.HashMap;
import java.util.Map;

/**
 * 下发指令操作设备
 */
@RestController
@RequestMapping("/device/operation")
public class DeviceOperationController {

    @Autowired
    private PubMqttClient pubMqttClient;

    @Autowired
    private PubMqttCallBack pubMqttCallBack;

    /**
     * 下发指令控制阀门设备
     */
    @PostMapping("/control")
    public AjaxResult control(@RequestBody DeviceOperationDTO deviceOperationDTO){

        //构建主题
        String topic ="hzlink/"+deviceOperationDTO.getProductId()+"/"+deviceOperationDTO.getDeviceNumber()+"/cmd/down";
        //构建消息
        Map<String,Object> param = new HashMap<>();
        //远程阀控
        if(deviceOperationDTO.getOperationType().equals("700")){
            param.put("cmd",700);
            Map<String,Object> data = new HashMap<>();
            data.put("status",deviceOperationDTO.getOperationCode());
            param.put("data",data);
            pubMqttClient.publish(1,true,topic, JSONUtil.toJsonStr(param));
            return AjaxResult.success();
            //设备重启
        } else if (deviceOperationDTO.getOperationType().equals("100")) {
            param.put("cmd",100);
            Map<String,Object> data = new HashMap<>();
            //status：0关1开2停止
            data.put("status",deviceOperationDTO.getOperationCode());
            param.put("data",data);
        }
        return AjaxResult.error("未知命令！");
    }


    /**
     * 下发指令CMD控制设备指令
     */
    @PostMapping("/control/cmd")
    public AjaxResult controlCmd(@RequestBody DeviceOperationDTO deviceOperationDTO){
        //构建主题
        String topic ="hzlink/"+deviceOperationDTO.getProductId()+"/"+deviceOperationDTO.getDeviceNumber()+"/cmd/down";
        //构建消息
        Map<String,Object> param = new HashMap<>();
        //远程阀控
            param.put("cmd",Integer.parseInt(deviceOperationDTO.getOperationType()));

            Map<String,Object> data = new HashMap<>();
            data.put("addr",deviceOperationDTO.getAddr());
        param.put("data",data);
            pubMqttClient.publish(0,true,topic, JSONUtil.toJsonStr(param));
            return AjaxResult.success();
    }

    /***
     * 下发指令控制设备灵活版
     */
    @PostMapping("/control/flexible")
    public AjaxResult controlFlexible(@Param("topic")String topic, @RequestBody JSONObject cmd){
        try {
            if(StringUtils.isBlank(topic)){
                return AjaxResult.error("topic不能为空！");
            }
            //构建主题
            // String topic ="hzlink/"+productId+"/"+deviceNumber+"/cmd/down";
            pubMqttClient.publish(0,true,topic, JSONUtil.toJsonStr(cmd));
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error("下发指令失败！");
        }

    }

}
