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 FuncType2BaseInfo implements NewMqttMessageRule {

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

    @Override
    public void execute(NewMqttRuleContext context) {
        log.info("开始处理强制声光报警开关，消息负载：{}", context.getPayloadDict());

        try {
            // 构建强制声光报警开关的Redis键
            String redisKey = GlobalConstants.GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX +
                             context.getDeviceImei() + ":force_audio_visual_alarm_switch";

            Map<String, Object> payloadDict = context.getPayloadDict();
            if (payloadDict != null) {
                // 存储强制声光报警开关状态到Redis
                RedisUtils.setCacheObject(redisKey, JSONObject.toJSONString(payloadDict));
            }
            log.info("强制声光报警开关处理完成，设备ID：{}", context.getDeviceImei());
        } catch (Exception e) {
            log.error("处理强制声光报警开关失败", e);
        }
    }
}
