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)
{
try
{
if (string.IsNullOrWhiteSpace(RoomNum))
{
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new { Status = 0, Message = "请求参数不能为空" }, JsonRequestBehavior.AllowGet);
}
string code = Code.ToString();
string RoomNumVal = RoomNum;
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.AllowGet);
}
else
{
// 业务层未找到数据:不返回 HTTP 404,避免 IIS 自定义 404 页覆盖 JSON
return Json(new { Status = 1, Message = "未找到数据", Data = result }, JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
logger.Error(ex, "GetRoomAddressStatus error");
Response.TrySkipIisCustomErrors = true;
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return Json(new { Status = -1, Message = "服务器错误", Detail = ex.Message }, JsonRequestBehavior.AllowGet);
}
}
///
/// 查询设备列表
///
[HttpPost()]
public List QueryDeviceList(int HotelID, int RoomTypeID)
{
try
{
string str = string.Format(@"SELECT `ID`
,`UpdateTime`
,`HotelID`
,`RoomTypeID`
,`DevName`
,`DevAddr`
,`Process`
FROM `blv_rcu_db`.`tbl_configured_devlist`
where `hoHotelID` ={0} and `RoomTypeID`={1};", HotelID, RoomTypeID);
var Data = SqlSugarBase.RcuDb.SqlQueryable(str).ToList();
return Data;
}
catch (Exception)
{
}
return new List();
}
///
/// 巡检
///
///
///
///
[HttpPost]
public async Task SetRCUService(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}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}");
}
}
}
///
/// 设备信息类
///
public class EquipmentInfo
{
public string EquipmentStatus { get; set; } // 设备状态
public string FaultDescription { get; set; } // 故障描述
public string DevName { get; set; } // 设备名称
}
///
/// 写入房间设备访问日志
///
/// 酒店ID
/// 房号
/// 设备信息列表
/// 返回操作结果
[HttpPost]
public ActionResult WriteRoomVisitLog(string HotelID, string RoomNumber, List EquipmentList)
{
try
{
if (string.IsNullOrWhiteSpace(HotelID) || string.IsNullOrWhiteSpace(RoomNumber))
{
return Json(new { Status = 0, Message = "酒店ID和房号不能为空" }, JsonRequestBehavior.AllowGet);
}
if (EquipmentList == null || EquipmentList.Count == 0)
{
return Json(new { Status = 0, Message = "设备信息列表不能为空" }, JsonRequestBehavior.AllowGet);
}
// 创建日志列表
var logList = new List