package com.fuyuanshen.global.mqtt.rule.hby100j;

import com.alibaba.fastjson2.JSONObject;
import com.fuyuanshen.common.core.constant.GlobalConstants;
import com.fuyuanshen.common.redis.utils.RedisUtils;
import com.fuyuanshen.global.mqtt.base.NewMqttMessageRule;
import com.fuyuanshen.global.mqtt.base.NewMqttRuleContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants.DEVICE_KEY_PREFIX;

/**
 * 语音播报开启/关闭
 */
@Slf4j
@Component
public class FuncType6VoicePlay implements NewMqttMessageRule {

    @Override
    public String getCommandType() {
        return "HBY100_6";
    }

    @Override
    public void execute(NewMqttRuleContext context) {
        log.info("HBY100J语音播报开启/关闭，消息负载：{}", context.getPayloadDict());

        try {
            String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
                             context.getDeviceImei() + ":voicePlay";

            Map<String, Object> payloadDict = context.getPayloadDict();
            if (payloadDict != null) {
                RedisUtils.setCacheObject(redisKey, JSONObject.toJSONString(payloadDict));
            }
        } catch (Exception e) {
            log.error("HBY100J语音播报开启/关闭失败", e);
        }
    }
}
