using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LogCap.Entity;
using Microsoft.VisualBasic;
using MQTTnet;
using RestSharp;

namespace LogCap.Common
{
    public class HttpSend
    {

        public static readonly string debug_log_report_url = ReadConfig.Instance.get_debug_log_report_url;
        public static readonly string debug_log_report_mqtt_topic = ReadConfig.Instance.get_debug_log_report_mqtt_topic;

        public static readonly string debug_log_report_mqtt_topic_new = "blw/rcu/monitor/endpoint";
        //public static RestClient client1 = new RestClient(debug_log_report_url);
        public static async void SendLog(string xiaodu, string tianmao, string MyCommandType, long hotel_code, int host_id, string roomnumber, string hostnumber, string WWW_IP, int WWW_Port, string lan_ip, int lan_port, string mac, string send_or_receive, string dataHex)
        {
            try
            {
                //if (hostnumber.Equals("061004043027"))
                //{
                //    Console.WriteLine("研发发布mqtt");
                //}
                string ti = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                MonitorLog mm = new MonitorLog();
                mm.HotelCode = hotel_code;
                mm.HostID = host_id;
                mm.HostNumber = hostnumber;
                mm.RoomNo = roomnumber;

                mm.LanIP = lan_ip;
                mm.LanPort = lan_port;
                mm.WWW_IP = WWW_IP;
                mm.WWW_Port = WWW_Port;

                mm.MAC = mac;
                mm.SendOrReceive = send_or_receive;
                mm.CommandType = MyCommandType;
                mm.Data = dataHex;
                mm.CreateTime = ti;
                mm.XiaoDuCUID = xiaodu;
                mm.TiaoMaoCUID = tianmao;


                string str = Newtonsoft.Json.JsonConvert.SerializeObject(mm);
                //Console.WriteLine("发送MQTT的数据为：" + str);
                //var request1 = new RestRequest("/", Method.Post);

                ////注意方法是POST
                ////两个参数名字必须是 topic 和payload ，区分大小写的
                ////Console.WriteLine("Topic: " + debug_log_report_mqtt_topic);
                //request1.AddParameter("topic", debug_log_report_mqtt_topic);
                //request1.AddParameter("payload", str);

                //await HttpSend.client1.ExecuteAsync(request1);
                //Console.WriteLine("当前时间为：" + ti+" Data: "+dataHex);

                //var payload_data= Encoding.UTF8.GetBytes(str);

                if (Program.mqttClient != null && Program.mqttClient.IsConnected)
                {
                    await Program.mqttClient.PublishStringAsync(debug_log_report_mqtt_topic, str);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("发送MQTT数据出错：" + ex.Message);
            }
        }


        public static async void SendLog_2(string MyCommandType, long hotel_code, int host_id, string roomnumber, string hostnumber, string WWW_IP, int WWW_Port, string lan_ip, int lan_port, string mac, string send_or_receive, string dataHex)
        {
            try
            {
                string ti = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                MonitorLog mm = new MonitorLog();
                mm.HotelCode = hotel_code;
                mm.HostID = host_id;
                mm.HostNumber = hostnumber;
                mm.RoomNo = roomnumber;

                mm.LanIP = lan_ip;
                mm.LanPort = lan_port;
                mm.WWW_IP = WWW_IP;
                mm.WWW_Port = WWW_Port;

                mm.MAC = mac;
                mm.SendOrReceive = send_or_receive;
                mm.CommandType = MyCommandType;
                mm.Data = dataHex;
                mm.CreateTime = ti;
                mm.XiaoDuCUID = "";
                mm.TiaoMaoCUID = "";

                string str = Newtonsoft.Json.JsonConvert.SerializeObject(mm);
                if (Program.mqttClient != null && Program.mqttClient.IsConnected)
                {
                    await Program.mqttClient.PublishStringAsync(debug_log_report_mqtt_topic_new, str);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("发送MQTT数据出错：" + ex.Message);
            }
        }
    }
}
