/*
 * 文 件 名:  KekaBs86Handler
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  <描述>
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/4/15
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.ruoyi.ems.handle;

import com.huashe.common.exception.Assert;
import com.ruoyi.ems.domain.EmsDevice;
import com.ruoyi.ems.domain.EmsObjAttrValue;
import com.ruoyi.ems.model.AbilityPayload;
import com.ruoyi.ems.model.CallResponse;
import com.ruoyi.ems.model.QueryDevice;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 86开关对接处理
 * <功能详细描述>
 *
 * @author lvwenbin
 * @version [版本号, 2025/4/15]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service
public class Keka86BsHandler extends BaseDevHandler {
    // 主题前置
    private static final String TOPIC_PREFIX = "/sc/dtu/ctl/";

    // 设备模型代码
    private static final String MODE_CODE = "M_W2_QS_KEKA_86";


    @Override
    public CallResponse<Void> call(AbilityPayload abilityParam) {
        CallResponse<Void> callResponse = null;

        // 获取协议属性
        List<EmsObjAttrValue> attrList = objAttrValueService.selectByObjCode(MODE_CODE, abilityParam.getObjCode());
        Assert.notEmpty(attrList, -1, "设备协议属性未配置!");
        Map<String, String> attrMap = attrList.stream().collect(Collectors.toMap(EmsObjAttrValue::getAttrKey,EmsObjAttrValue::getAttrValue));

        String gatewayId = attrMap.get("gatewayId");

        // 发送消息到MQTT服务器
        String topic = TOPIC_PREFIX + gatewayId;
        sendMqttHex(topic, abilityParam.getAbilityParam(), 2, false);
        saveCallLog(abilityParam, System.currentTimeMillis(), 0);
        callResponse = new CallResponse<>(0, "执行成功!");
        return callResponse;
    }

    @Override
    public void msgHandle(String deviceCode, String payload) {

    }

    @Override
    public List<EmsDevice> getDeviceList() {
        QueryDevice queryDevice = new QueryDevice();
        queryDevice.setDeviceModel(MODE_CODE);
        return deviceService.selectList(queryDevice);
    }
}