package org.springblade.third.iotdevice.listen;

import org.springblade.third.iotdevice.feign.IPerceptionIotDeviceClient;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    //感知平台token缓存名字
    public static final String IOT_AUTH_ACCESS_TOKEN = "sptg:iot:auth:accessToken";
    @Resource(name = "perceptionIotDeviceClient")
    private  IPerceptionIotDeviceClient perceptionIotDeviceClient;


    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {

        String keyStr = message.toString();
        if(keyStr.startsWith(IOT_AUTH_ACCESS_TOKEN)) {
            String appId = keyStr.split("@#")[1];
            this.perceptionIotDeviceClient.auth(appId);
        }
    }
}
