using System;
using System.Collections.Generic;
using System.Text;
using TeamDev.Redis;
using System.Configuration;

namespace ChuPiao.Common.Utils
{
    public class RedisClient
    {
        public static string ChannelName = "machineError";
        public RedisDataAccessProvider redis;

        public RedisClient()
        {
            try
            {
                string host = "120.78.204.171";
                if (ConfigurationManager.AppSettings["redisHost"] != null)
                {
                    host = ConfigurationManager.AppSettings["redisHost"].ToString();
                }

                redis = new RedisDataAccessProvider();
                redis.Configuration.Host = host;
                redis.Configuration.Port = 6379;
               
                redis.Connect();

                LogUtil.LogInfo("redis Connected");
            }
            catch (Exception ex)
            {
                LogUtil.LogError("redis 连接异常：" + ex.ToString());
            }


            //redis.SendCommand(RedisCommand.KEYS, "*");
            //keys = redis.ReadMultiString();

            ////打印所有的key
            //foreach (var item in keys)
            //{
            //    Debug.Log(item);
            //}

            ////频道订阅
            //redis.ChannelSubscribed += new ChannelSubscribedHandler(redisDataAccessProvider_ChannelSubscribed);
            //redis.MessageReceived += new MessageReceivedHandler(redisDataAccessProvider_MessageReceived);
            //redis.Messaging.Subscribe("channeltest");

        }

        public void Publish(string message)
        { 
            try
            {
                redis.Messaging.Publish(ChannelName,message);

                //LogUtil.LogInfo("redis publish:"+message);
            }
            catch (Exception ex)
            {
                LogUtil.LogError("redis 发布消息异常：" + ex.ToString());
            }
        }


        //static void redisDataAccessProvider_MessageReceived(string channelname, string message)
        //{
        //    Debug.Log(message);
        //}

        //static void redisDataAccessProvider_ChannelSubscribed(string channelname)
        //{
        //    Debug.Log(channelname);
        //}

    }
}
