using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Net;

namespace Common
{
    /// <summary>
    /// 与第三方选住云端对接：将相关服务信息推送过去
    /// </summary>
    public static class XuanZhuOperation
    {
        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(XuanZhuOperation));
        //private static readonly string _postURL = "http://111.231.106.196:19230/hogood/report";
        //###
        //POST http://111.231.106.196:19230/hogood/report
        //Content-Type: application/json; charset=UTF-8
        //{
        //  "code": "1094",//酒店编码
        //  "roomNumber": "8888",//房号
        //  "address": "001001001",//回路地址
        //  "name": "廊灯",//回路名称
        //  "status":"1" //状态：1开，2关
        //}
        //### 返回
        //{
        //  "retCode": 1, //0代表成功 其他代表失败
        //  "retMsg": "[003]非法访问",
        //  "retData": null
        //}
        //###
        /// <summary>
        /// 上报设备或服务状态信息
        /// </summary>
        /// <param name="url"></param>
        /// <param name="resp"></param>
        /// <returns></returns>
        public static bool ReportService(string url, XuanZhuResponse resp)
        {
            string param = Newtonsoft.Json.JsonConvert.SerializeObject(resp);
            try
            {
                var A = (SecurityProtocolType)48;
                var B = (SecurityProtocolType)192;
                var C = (SecurityProtocolType)768;
                var D = (SecurityProtocolType)3072;
                var E = (SecurityProtocolType)12288;
                ServicePointManager.SecurityProtocol = A | B | C | D | E;
                string result = HttpWebRequestHelper.PostWebRequest(url, param);
                if (resp.code.Equals("1003"))
                {
                    logger.Error(resp.roomNumber + " Params:" + param + " Result:" + result);
                }
                XuanZhuResult returnResult = JsonConvert.DeserializeObject<XuanZhuResult>(result);
                //if (returnResult.retCode == "0")//0代表成功 其他代表失败
                //{
                //    return true;
                //}
                //logger.Error(string.Format("酒店({0})客房({1})调用设备状态推送接口({2})结果：{3}", hotelCode, roomNumber, url, returnResult.retMsg));
                //return false;
                return true;
            }
            catch (Exception ex)
            {
                //string Key = "HttpRequest_" + resp.code + "_" + resp.roomNumber;
                //MemoryCacheHelper.Set(Key, 1, DateTimeOffset.Now.AddMinutes(10));
                logger.Error(string.Format("酒店({0})客房({1})调用设备状态或异常推送接口({2})失败：{3}，数据：{4}", resp.code, resp.roomNumber, url, ex.Message, param));
                return false;
            }
        }
    }

    public class XuanZhuHotelData
    {
        public static int[] dataArray = {
        1094, 1094, 1225, 1249, 1243, 1174, 1094, 1094, 1266, 1266,
        1094, 1237, 1317, 1317, 1325, 1329, 1001, 1333, 1283, 1371,
        1284, 1374, 1400, 1094, 3747, 1419, 1420, 1427, 1222, 1454,
        1489, 1989, 1515, 1527, 1524, 1557, 1337, 1332, 1490, 1672,
        1695, 1699, 1701, 1620, 1674};
    }
    public class XuanZhuRequest
    {
        /// <summary>
        /// 酒店编码
        /// </summary>
        public string code { get; set; }
        /// <summary>
        /// 房号
        /// </summary>
        public string roomNumber { get; set; }
        public string devicetype { 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 XuanZhuResponse_Fault
    {
        public XuanZhuResponse data { get; set; }
        public DateTime updatetime { get; set; }

        public string url { get; set; }
    }
    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; }
    }

    internal class XuanZhuResult
    {
        /// <summary>
        /// 返回码
        /// </summary>
        public string retCode { get; set; }
        /// <summary>
        /// 返回描述
        /// </summary>
        public string retMsg { get; set; }
        /// <summary>
        /// 返回描述
        /// </summary>
        public string retData { get; set; }
    }
}
