using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; using System.Web.Mvc; using Models; using Models.ModelItems; using Services.Manager; using static Services.Manager.SyncAllFromOutterDB; using Newtonsoft.Json; using Models.ApiModei; using NLog; using Services.Tool; using Common; namespace UI.Controllers { public class UpgradeController : Controller { /// /// 基础URL /// private readonly string baseUrl = "https://www.boonlive-rcu.com/api/"; /// /// 新添加的方法,用于获取设备信息 /// /// /// /// /// [HttpPost()] public async Task GetDeviceInfo(int hotelId, int roomTypeId, string roomNumber) { // 初始化HttpClient using (var httpClient = new HttpClient()) { // 构建请求URL var url = baseUrl + "webChatLoadHostByRoomType"; // 构建表单数据 FormUrlEncodedContent formData = null; if (!string.IsNullOrEmpty(roomNumber)) { formData = new FormUrlEncodedContent(new[] { new KeyValuePair("hotelid", hotelId.ToString()), new KeyValuePair("roomTypeID", roomTypeId.ToString()), new KeyValuePair("roomNumber", roomNumber) }); } else { formData = new FormUrlEncodedContent(new[] { new KeyValuePair("hotelid", hotelId.ToString()), new KeyValuePair("roomTypeID", roomTypeId.ToString()) }); } // 发送POST请求 HttpResponseMessage response = await httpClient.PostAsync(url, formData); // 确保请求成功 if (response.IsSuccessStatusCode) { // 读取返回的字符串 string responseString = await response.Content.ReadAsStringAsync(); // 将字符串转换为ActionResult并返回 return Content(responseString, "application/json"); } else { // 如果请求失败,返回错误信息 return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "检索设备信息失败。"); } } } public static Logger logger= LogManager.GetCurrentClassLogger(); /// /// 调用微信升级接口 /// /// 房型ID /// 升级的设备ID列表 /// 升级文件名字 /// 返回调用结果 [HttpPost] public async Task WebChatUpgrade(int roomTypeID, string hostidLists, string upgradefileName) { using (var client = new HttpClient()) { var parameters = new FormUrlEncodedContent(new[] { new KeyValuePair("roomTypeID", roomTypeID.ToString()), new KeyValuePair("hostid_lists", hostidLists), new KeyValuePair("upgradefileName", upgradefileName) }); //logger.Error("hostlist:"+hostidLists); var response = await client.PostAsync(baseUrl + "WebChatUpgrade", parameters); if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { throw new Exception("调用升级接口失败,状态码:" + response.StatusCode); } } } /// /// 查询升级进度 /// /// 升级的设备ID号 /// 返回查询结果 [HttpPost] public async Task QueryUpdateHostStatus(int hotelID, int roomTypeID) { using (var client = new HttpClient()) { // 构建带参数的 URL var queryParams = new Dictionary { { "HotelID", hotelID.ToString() }, { "roomTypeID", roomTypeID.ToString() } }; string requestUrl = $"{baseUrl}QueryUpdateHostStatus"; var encodedContent = new FormUrlEncodedContent(queryParams); var response = await client.PostAsync(requestUrl, encodedContent); if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { throw new Exception($"调用接口失败,状态码:{response.StatusCode}"); } } } /// /// 获取文件名 /// /// /// /// [HttpPost()] public ActionResult GetFileName(int hotel_id, int room_type_id) { var db = SqlSugarBase.RcuDb; List roomList = SqlSugarBase.RcuDb.Queryable().Where(it => it.HOTEL_OLD_ID == hotel_id && it.ROOM_TYPE_OLD_ID == room_type_id && it.IsDel != 1).ToList(); List files = new List(); FileName_DTO dto = new FileName_DTO(); if (roomList != null && roomList.Count > 0) { foreach (var item in roomList) { dto.HOTEL_OLD_ID = item.HOTEL_OLD_ID; dto.ROOM_TYPE_OLD_ID = item.ROOM_TYPE_OLD_ID; dto.CONFIG_BIN = item.CONFIG_BIN; dto.App_Cfg_For_L2 = item.App_Cfg_For_L2; dto.App_Cfg_For_L4 = item.App_Cfg_For_L4; dto.Hex_Code_For_L2 = item.Hex_Code_For_L2; dto.Hex_Code_For_L4 = item.Hex_Code_For_L4; dto.APPTYPE = item.APPTYPE; dto.Cfg_Type = item.Cfg_Type; dto.LUNCHER_HEX = item.LUNCHER_HEX; files.Add(dto); } } return Json(files); } /// /// 查询更新主机进度条接口 /// /// 主机ID列表 /// 返回调用结果 [HttpPost] public async Task ForwardQueryUpdateHostProgressBar(string hostIDList) { using (var client = new HttpClient()) { // 将hostIDList字符串反序列化为List,因为你的原始方法接受的是List // 创建参数字典 var parameters = new FormUrlEncodedContent(new[] { new KeyValuePair("HostIDList", hostIDList) }); // 发送POST请求 var response = await client.PostAsync(baseUrl + "QueryUpdateHostProgressBar", parameters); if (response.IsSuccessStatusCode) { // 读取响应内容并返回 string responseBody = await response.Content.ReadAsStringAsync(); return Content(responseBody, "application/json"); } else { // 处理错误情况 throw new Exception("调用查询更新主机进度条接口失败,状态码:" + response.StatusCode); } } } /// /// 微信调用灯光控制接口 /// /// 灯光控制参数对象 /// 返回原始接口的JSON响应 [HttpPost] public async Task SetRCULight(RCULight jsonData) { using (var client = new HttpClient()) { // 将对象序列化为JSON字符串 string jsonString = JsonConvert.SerializeObject(jsonData); // 发送POST请求 var response = await client.GetAsync(baseUrl + "SetRCULight?jsonData=" + jsonString); // 处理响应 if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { var errorContent = await response.Content.ReadAsStringAsync(); throw new Exception($"接口调用失败,状态码:{response.StatusCode},错误信息:{errorContent}"); } } } /// /// 获取房间类型和模组列表 /// /// /// /// [HttpPost] public async Task GetRoomTypeAndModalsListLog(HostInfo jsonData) { using (var client = new HttpClient()) { // 将对象序列化为JSON字符串 string jsonString = JsonConvert.SerializeObject(jsonData); // 发送POST请求 var response = await client.GetAsync(baseUrl + "GetRoomTypeAndModalsListLog?jsonData=" + jsonString); // 处理响应 if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { var errorContent = await response.Content.ReadAsStringAsync(); throw new Exception($"接口调用失败,状态码:{response.StatusCode},错误信息:{errorContent}"); } } } /// /// 获取文件名 /// /// 参数对象 /// 返回原始接口的JSON响应 [HttpPost] public async Task GetFirmwareName(UpLoadRoom Data) { using (var client = new HttpClient()) { // 将对象序列化为JSON字符串 //string jsonString = JsonConvert.SerializeObject(jsonData); //var response0 = await client.PostAsync("http://rcu-data.blv-oa.com/OTApi/GainFileName" + PostAsync); var parameters1 = new FormUrlEncodedContent(new[] { new KeyValuePair("RoomTypeid", Data.RoomTypeID.ToString()), new KeyValuePair("DataType", "1") }); // 发送POST请求 var response1 = await client.PostAsync("http://rcu-data.blv-oa.com/OTApi/GainFileName", parameters1); var parameters2 = new FormUrlEncodedContent(new[] { new KeyValuePair("RoomTypeid", Data.RoomTypeID.ToString()), new KeyValuePair("DataType", "2") }); var response2 = await client.PostAsync("http://rcu-data.blv-oa.com/OTApi/GainFileName", parameters2); // 处理响应 if (response1.IsSuccessStatusCode && response2.IsSuccessStatusCode) { return await response1.Content.ReadAsStringAsync() + "@" + await response2.Content.ReadAsStringAsync(); } else { var errorContent = await response1.Content.ReadAsStringAsync() + await response2.Content.ReadAsStringAsync(); throw new Exception($"接口调用失败,状态码:{response1.StatusCode.ToString() + response2.StatusCode.ToString()},错误信息:{errorContent}"); } } } /// /// 调用RCU空调接口 /// /// /// /// [HttpPost] public async Task SetRCUAir(RCUAirRequest requestData) { using (var client = new HttpClient()) { // 1. 序列化请求对象为JSON string jsonString = JsonConvert.SerializeObject(requestData); // 2. 正确编码JSON字符串 string encodedJson = Uri.EscapeDataString(jsonString); // 3. 构建完整的GET请求URL string apiUrl = $"{baseUrl}SetRCUAir?jsonData={encodedJson}"; try { // 4. 发送GET请求 var response = await client.GetAsync(apiUrl); // 5. 处理响应 if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { var errorContent = await response.Content.ReadAsStringAsync(); throw new Exception($"接口调用失败: HTTP {response.StatusCode} - {errorContent}"); } } catch (Exception ex) { throw new Exception($"请求发送失败: {ex.Message}"); } } } /// /// 调用SetRCUService接口 /// /// 服务控制请求参数 /// 操作结果 [HttpPost] public async Task SetRCUService(RCUServiceRequest requestData) { using (var client = new HttpClient()) { // 1. 序列化请求对象为JSON string jsonString = JsonConvert.SerializeObject(requestData); // 2. 正确编码JSON字符串 string encodedJson = Uri.EscapeDataString(jsonString); // 3. 构建完整的GET请求URL string apiUrl = $"{baseUrl}SetRCUService?jsonData={encodedJson}"; try { // 4. 发送GET请求 var response = await client.GetAsync(apiUrl); // 5. 处理响应 if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { var errorContent = await response.Content.ReadAsStringAsync(); throw new Exception($"服务控制失败: HTTP {response.StatusCode} - {errorContent}"); } } catch (Exception ex) { throw new Exception($"请求发送失败: {ex.Message}"); } } } /// /// 调用SetRCUCurtain接口控制窗帘 /// /// 窗帘控制请求参数 /// 操作结果 [HttpPost] public async Task SetRCUCurtain(RCUCurtainRequest requestData) { using (var client = new HttpClient()) { // 1. 序列化请求对象为JSON string jsonString = JsonConvert.SerializeObject(requestData); // 2. 正确编码JSON字符串 string encodedJson = Uri.EscapeDataString(jsonString); // 3. 构建完整的GET请求URL string apiUrl = $"{baseUrl}SetRCUCurtain?jsonData={encodedJson}"; try { // 4. 发送GET请求 var response = await client.GetAsync(apiUrl); // 5. 处理响应 if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { var errorContent = await response.Content.ReadAsStringAsync(); throw new Exception($"窗帘控制失败: HTTP {response.StatusCode} - {errorContent}"); } } catch (Exception ex) { throw new Exception($"请求发送失败: {ex.Message}"); } } } /// /// 获取房间地址状态 /// /// /// [HttpPost] public ActionResult GetRoomAddressStatus(int Code, string RoomNum) { RoomAddressStatus roomAddressStatus = new RoomAddressStatus { Code = Code, RoomNum = RoomNum }; try { if (roomAddressStatus == null) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return Json(new { Status = 0, Message = "请求参数不能为空" }, JsonRequestBehavior.DenyGet); } string code = roomAddressStatus.Code.ToString(); string RoomNumVal = roomAddressStatus.RoomNum ?? string.Empty; string key = $"WXFault_{code}_{RoomNumVal}"; var result = CSRedisCacheHelper.HMGetAll(key); if (result != null && result.Count > 0) { return Json(new { Status = 1, Message = "获取成功", Data = result }, JsonRequestBehavior.DenyGet); } else { Response.StatusCode = (int)HttpStatusCode.NotFound; return Json(new { Status = 0, Message = "未找到数据", Data = result }, JsonRequestBehavior.DenyGet); } } catch (Exception ex) { logger.Error(ex, "GetRoomAddressStatus error"); Response.StatusCode = (int)HttpStatusCode.InternalServerError; return Json(new { Status = -1, Message = "服务器错误", Detail = ex.Message }, JsonRequestBehavior.DenyGet); } } } public class RoomAddressStatus { public string RoomNum { get; set; } public int Code { get; set; } } // 服务控制请求参数类 public class RCUServiceRequest { public string roomNumber { get; set; } // 房号 public string code { get; set; } // 编号 public string creatDate { get; set; } // 创建日期 public string modalAddress { get; set; } // 设备地址 public int status { get; set; } // 状态:1=开,2=关 } // 空调控制 public class RCUAirRequest { public string roomNumber { get; set; } public string code { get; set; } public string creatDate { get; set; } public string modalAddress { get; set; } public int? onOff { get; set; } // 使用可空类型表示可选参数 public int? temperature { get; set; } // 16~32 public int? fanSpeed { get; set; } // 0自动,1低速,2中速,3高速 public int? mode { get; set; } // 0自动,1制冷,2制热,3送风 public int? valve { get; set; } // 1开,2关 } // 窗帘控制请求参数类 public class RCUCurtainRequest { public string roomNumber { get; set; } // 房号 public string code { get; set; } // 编号 public string creatDate { get; set; } // 创建日期 public string modalAddress { get; set; } // 回路地址 public int status { get; set; } // 状态:1=开,2=关,6=停止 } public class UpLoadRoom { public int RoomTypeID { get; set; } } /// /// 灯光控制参数对象 /// public class RCULight { public string roomNumber { get; set; } public string code { get; set; } public string creatDate { get; set; } public string status { get; set; } public string modalAddress { get; set; } public string brightness { get; set; } } public class HostInfo { public string code { get; set; } } /// /// 文件名数据传输对象 /// public class FileName_DTO { public int HOTEL_OLD_ID { get; set; } public int ROOM_TYPE_OLD_ID { get; set; } public string CONFIG_BIN { get; set; } public string App_Cfg_For_L2 { get; set; } public string App_Cfg_For_L4 { get; set; } public string Hex_Code_For_L2 { get; set; } public string Hex_Code_For_L4 { get; set; } public string APPTYPE { get; set; } public string Cfg_Type { get; set; } public string LUNCHER_HEX { get; set; } } }