528 lines
20 KiB
C#
528 lines
20 KiB
C#
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;
|
||
|
||
namespace UI.Controllers
|
||
{
|
||
public class UpgradeController : Controller
|
||
{
|
||
/// <summary>
|
||
/// 基础URL
|
||
/// </summary>
|
||
private readonly string baseUrl = "https://www.boonlive-rcu.com/api/";
|
||
|
||
|
||
/// <summary>
|
||
/// 新添加的方法,用于获取设备信息
|
||
/// </summary>
|
||
/// <param name="hotelId"></param>
|
||
/// <param name="roomTypeId"></param>
|
||
/// <param name="roomNumber"></param>
|
||
/// <returns></returns>
|
||
[HttpPost()]
|
||
public async Task<ActionResult> 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<string, string>("hotelid", hotelId.ToString()),
|
||
new KeyValuePair<string, string>("roomTypeID", roomTypeId.ToString()),
|
||
new KeyValuePair<string, string>("roomNumber", roomNumber)
|
||
});
|
||
}
|
||
else
|
||
{
|
||
formData = new FormUrlEncodedContent(new[]
|
||
{
|
||
new KeyValuePair<string, string>("hotelid", hotelId.ToString()),
|
||
new KeyValuePair<string, string>("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();
|
||
/// <summary>
|
||
/// 调用微信升级接口
|
||
/// </summary>
|
||
/// <param name="roomTypeID">房型ID</param>
|
||
/// <param name="hostidLists">升级的设备ID列表</param>
|
||
/// <param name="upgradefileName">升级文件名字</param>
|
||
/// <returns>返回调用结果</returns>
|
||
[HttpPost]
|
||
public async Task<string> WebChatUpgrade(int roomTypeID, string hostidLists, string upgradefileName)
|
||
{
|
||
using (var client = new HttpClient())
|
||
{
|
||
var parameters = new FormUrlEncodedContent(new[]
|
||
{
|
||
new KeyValuePair<string, string>("roomTypeID", roomTypeID.ToString()),
|
||
new KeyValuePair<string, string>("hostid_lists", hostidLists),
|
||
new KeyValuePair<string, string>("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);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询升级进度
|
||
/// </summary>
|
||
/// <param name="hostUpdateID">升级的设备ID号</param>
|
||
/// <returns>返回查询结果</returns>
|
||
[HttpPost]
|
||
public async Task<string> QueryUpdateHostStatus(int hotelID, int roomTypeID)
|
||
{
|
||
using (var client = new HttpClient())
|
||
{
|
||
// 构建带参数的 URL
|
||
var queryParams = new Dictionary<string, string>
|
||
{
|
||
{ "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}");
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取文件名
|
||
/// </summary>
|
||
/// <param name="hotel_id"></param>
|
||
/// <param name="room_type_id"></param>
|
||
/// <returns></returns>
|
||
[HttpPost()]
|
||
public ActionResult GetFileName(int hotel_id, int room_type_id)
|
||
{
|
||
|
||
var db = SqlSugarBase.RcuDb;
|
||
List<TBL_ROOM_TYPE_LIST> roomList = SqlSugarBase.RcuDb.Queryable<TBL_ROOM_TYPE_LIST>().Where(it => it.HOTEL_OLD_ID == hotel_id && it.ROOM_TYPE_OLD_ID == room_type_id && it.IsDel != 1).ToList();
|
||
|
||
List<FileName_DTO> files = new List<FileName_DTO>();
|
||
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);
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 查询更新主机进度条接口
|
||
/// </summary>
|
||
/// <param name="hostIDList">主机ID列表</param>
|
||
/// <returns>返回调用结果</returns>
|
||
[HttpPost]
|
||
public async Task<ActionResult> ForwardQueryUpdateHostProgressBar(string hostIDList)
|
||
{
|
||
using (var client = new HttpClient())
|
||
{
|
||
// 将hostIDList字符串反序列化为List<int>,因为你的原始方法接受的是List<int>
|
||
|
||
// 创建参数字典
|
||
var parameters = new FormUrlEncodedContent(new[]
|
||
{
|
||
new KeyValuePair<string, string>("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);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 微信调用灯光控制接口
|
||
/// </summary>
|
||
/// <param name="jsonData">灯光控制参数对象</param>
|
||
/// <returns>返回原始接口的JSON响应</returns>
|
||
[HttpPost]
|
||
public async Task<string> 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}");
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取房间类型和模组列表
|
||
/// </summary>
|
||
/// <param name="jsonData"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="Exception"></exception>
|
||
[HttpPost]
|
||
public async Task<string> 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}");
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取文件名
|
||
/// </summary>
|
||
/// <param name="Data">参数对象</param>
|
||
/// <returns>返回原始接口的JSON响应</returns>
|
||
[HttpPost]
|
||
public async Task<string> 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<string, string>("RoomTypeid", Data.RoomTypeID.ToString()),
|
||
new KeyValuePair<string, string>("DataType", "1")
|
||
});
|
||
// 发送POST请求
|
||
var response1 = await client.PostAsync("http://rcu-data.blv-oa.com/OTApi/GainFileName", parameters1);
|
||
var parameters2 = new FormUrlEncodedContent(new[]
|
||
{
|
||
new KeyValuePair<string, string>("RoomTypeid", Data.RoomTypeID.ToString()),
|
||
new KeyValuePair<string, string>("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}");
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 调用RCU空调接口
|
||
/// </summary>
|
||
/// <param name="requestData"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="Exception"></exception>
|
||
[HttpPost]
|
||
public async Task<string> 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}");
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 调用SetRCUService接口
|
||
/// </summary>
|
||
/// <param name="requestData">服务控制请求参数</param>
|
||
/// <returns>操作结果</returns>
|
||
[HttpPost]
|
||
public async Task<string> 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}");
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 调用SetRCUCurtain接口控制窗帘
|
||
/// </summary>
|
||
/// <param name="requestData">窗帘控制请求参数</param>
|
||
/// <returns>操作结果</returns>
|
||
[HttpPost]
|
||
public async Task<string> 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}");
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
// 服务控制请求参数类
|
||
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; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 灯光控制参数对象
|
||
/// </summary>
|
||
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; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 文件名数据传输对象
|
||
/// </summary>
|
||
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; }
|
||
}
|
||
}
|