using Common;
using Newtonsoft.Json;
using RestSharp;
using System.Net;

namespace CRICS_SSL
{
    public class XuanZhuResponse
    {
        /// <summary>
        /// 酒店编码
        /// </summary>
        public string code { get; set; }
        /// <summary>
        /// 房号
        /// </summary>
        public string roomNumber { get; set; }
        /// <summary>
        /// 回路地址
        /// </summary>
        public string address { get; set; }
        /// <summary>
        /// 回路名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 状态
        /// </summary>
        public int status { get; set; }
        /// <summary>
        /// 异常类型
        /// </summary>
        public int faultType { get; set; }
        /// <summary>
        /// 异常值
        /// </summary>
        public int faultData { get; set; }
        /// <summary>
        /// 亮度
        /// </summary>
        public int brightness { get; set; }
        /// <summary>
        /// 当前温度
        /// </summary>
        public int currentTemp { get; set; }
        /// <summary>
        /// 设定温度
        /// </summary>
        public int settingTemp { get; set; }
        /// <summary>
        /// 风速
        /// </summary>
        public int fanSpeed { get; set; }
        /// <summary>
        /// 模式
        /// </summary>
        public int mode { get; set; }
        /// <summary>
        /// 阀门
        /// </summary>
        public int valve { get; set; }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    StreamConsume("UDPData", "Crics1", "task1");
                }
            }, TaskCreationOptions.LongRunning);
            SendHttpData<string>("", "");
            Console.WriteLine("Hello, World!");
            Console.ReadLine();
        }

        public static string SendHttpData<T>(string Url, T obj)
        {
            var client1 = new RestClient("https://f-xcx.blv-oa.com/rcu/report");
            var request1 = new RestRequest("", Method.Post);

            string jjj = """
                {"code":"1085","roomNumber":"8099","address":"009001004","name":"PM2.5浓度","status":1,"faultType":0,"faultData":0,"brightness":45,"currentTemp":0,"settingTemp":0,"fanSpeed":0,"mode":0,"valve":0}
                """;
            request1.AddJsonBody(jjj);
            var QQQ = client1.Execute(request1);
            HttpStatusCode HHH = QQQ.StatusCode;
            if (HHH == HttpStatusCode.OK)
            {

            }
            return "";
        }

        public static string UDPAllDataKey = "BaoYi_ReportData";
        public static void StreamConsume(string group = "BaoYiData", string consumer = "BaoYiCRICS1", string task_key = "task1")
        {
            try
            {
                var redis = CSRedisCacheHelper.redis1;
                var data = redis.XReadGroup(group, consumer, 500, 10, new ValueTuple<string, string>(UDPAllDataKey, ">"));

                if (data != null)
                {
                    //var pending = redis.XPending(UDPAllDataKey, group);
                    foreach (var item in data)
                    {
                        var idarray = item.Item2;
                        string nsa = string.Concat(task_key, "#", idarray.Count().ToString());
                        CSRedisCacheHelper.Publish("udp_package_consumer", nsa);
                        foreach (var SerializeNo in idarray)
                        {
                            var id1 = SerializeNo.Item1;
                            var str = SerializeNo.Item2[1];
                            var GGG = JsonConvert.DeserializeObject<XuanZhuResponse>(str);
                            redis.XAck(UDPAllDataKey, group, id1);
                            redis.XDel(UDPAllDataKey, id1);
                            //ProcessData(GGG);
                            SendHttpData("", "");
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}
