using CommonEntity;
using CSRedis;
using System.Text.Json;

namespace BLWLogProduce.Models
{
    public class MyPublishRedis
    {
        public static CSRedisClient? redis4;

        private const string ip = "127.0.0.1";
        private const string port = "6379";
        static MyPublishRedis()
        {
            var redisHostStr = string.Format("{0}:{1}", ip, port);
            if (!string.IsNullOrEmpty(redisHostStr))
            {
                redis4 = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=4");
                string channel = "__keyevent@4__:expired";
                var QQQ = new ValueTuple<string, Action<CSRedisClient.SubscribeMessageEventArgs>>(channel, (msg) =>
                {
                    try
                    {
                        var o = JsonSerializer.Deserialize<OnOffLineData>(msg.Body);
                        o.CurrentStatus = "off";
                        string str = Newtonsoft.Json.JsonConvert.SerializeObject(o);

                        redis4.Publish("redis-on_off_line", str);
                    }
                    catch (Exception ex)
                    {
                    }
                });
                redis4.Subscribe(QQQ);
            }
        }
    }

    public class MyPublishRedis2
    {

    }
}
