package com.yfd.monitor.utils;

import org.springframework.data.redis.core.RedisTemplate;

import java.util.Objects;

/**
 * JsonUtil
 *
 * @version 1.0.0
 * @since 2023/2/2 15:24
 */
public final class JsonUtil {

    private JsonUtil() {
    }

    /**
     * safe json type conversion
     *
     * @param key   redis key
     * @param clazz cast type
     * @param <T>
     * @return result type
     */
    public static <T> T redisJsonToObject(RedisTemplate<Object, Object> redisTemplate, String key, Class<T> clazz) {
        Object jsonObject = redisTemplate.opsForValue().get(key);
        if (Objects.isNull(jsonObject)) {
            return null;
        }
        return clazz.cast(jsonObject);
    }

    public static void main(String[] args) {
        int alarmType = 11111111; // 替换为你的实际32位告警类型值

        // 截取每位字节
        int videoLoss = (alarmType & 0x01) >> 0;
        int motionDetection = (alarmType & 0x02) >> 1;
        int videoMask = (alarmType & 0x04) >> 2;
        int highTemperature = (alarmType & 0x100) >> 8;
        int lowTemperature = (alarmType & 0x200) >> 9;
        int fanFailure = (alarmType & 0x400) >> 10;
        int diskFailure = (alarmType & 0x800) >> 11;
        int statusEvent = (alarmType & 0x10000) >> 16;

        // 打印告警类型
        System.out.println("Video Loss: " + videoLoss);
        System.out.println("Motion Detection: " + motionDetection);
        System.out.println("Video Mask: " + videoMask);
        System.out.println("High Temperature: " + highTemperature);
        System.out.println("Low Temperature: " + lowTemperature);
        System.out.println("Fan Failure: " + fanFailure);
        System.out.println("Disk Failure: " + diskFailure);
        System.out.println("Status Event: " + statusEvent);
    }
}
