using FrontendWebApi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Repository.BackendRepository.Implement;
using Repository.FrontendRepository.Interface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Ubiety.Dns.Core;
namespace FrontendWebApi.ApiControllers
{
///
/// 告警紀錄
///
public class WarningValueController : MyBaseApiController
{
private readonly IFrontendRepository frontendRepository;
public WarningValueController
(
IFrontendRepository frontendRepository
)
{
this.frontendRepository = frontendRepository;
}
///
/// 取得警戒值
///
///
///
[HttpPost]
[Route("api/WarningValue/GetWarningValue")]
public async Task>>> GetWarningValue()
{
ApiResult> apiResult = new ApiResult>(jwt_str);
if (!jwtlife)
{
apiResult.Code = "5000";
return BadRequest(apiResult);
}
try
{
List typeNames = Enum.GetNames(typeof(WarningValueType)).ToList();
apiResult.Data = new List();
var sqlString = $@"SELECT system_value FROM variable WHERE system_type = 'obixConfig' AND system_key = 'ApiBase' AND deleted = 0";
string baseApiUrl = await frontendRepository.GetOneAsync(sqlString);
if (string.IsNullOrEmpty(baseApiUrl))
{
apiResult.Code = "9998";
apiResult.Msg = "未找到 obixConfig baseAPI,請聯絡管理者。";
return BadRequest(apiResult);
}
foreach (var typeName in typeNames) {
WarningValueOutput result = new WarningValueOutput();
result.type = Enum.Parse(typeName);
sqlString = @$"SELECT system_value FROM variable WHERE system_type = 'warningValueApi' AND system_key = @type AND deleted = 0";
string apiUrl = await frontendRepository.GetOneAsync(sqlString, new { type = $"get{typeName}" });
if (string.IsNullOrEmpty(apiUrl))
{
apiResult.Code = "9998";
apiResult.Msg = "未找到天氣警戒值 obix API,請聯絡管理者。";
return BadRequest(apiResult);
}
using (HttpClient client = new HttpClient())
{
apiUrl = Path.Combine(baseApiUrl, "obix/config/", apiUrl);
string username = "stanGG";
string password = "St12345678";
string encoded = Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
client.DefaultRequestHeaders.Add("Authorization", "Basic " + encoded);
var response = await client.GetAsync(apiUrl);
var resString = (await response.Content.ReadAsStringAsync()).ToString();
XDocument xmlDoc = XDocument.Parse(resString);
Logger.LogError("【" + controllerName + "/" + actionName + "】" + JsonConvert.SerializeXNode(xmlDoc));
if (xmlDoc?.Root?.Name?.LocalName == "str")
{
result.targetValue = xmlDoc.Root.Attribute("val").Value;
}
}
apiResult.Data.Add(result);
}
apiResult.Msg = "讀取成功";
apiResult.Code = "0000";
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
///
/// 取得警戒值
///
///
///
[HttpPost]
[Route("api/WarningValue/SetWarningValue")]
public async Task>> SetWarningValue(List wvs)
{
ApiResult apiResult = new ApiResult(jwt_str);
if (!jwtlife)
{
apiResult.Code = "5000";
return BadRequest(apiResult);
}
try
{
var sqlString = $@"SELECT system_value FROM variable WHERE system_type = 'obixConfig' AND system_key = 'ApiBase' AND deleted = 0";
string baseApiUrl = await frontendRepository.GetOneAsync(sqlString);
if (string.IsNullOrEmpty(baseApiUrl))
{
apiResult.Code = "9998";
apiResult.Msg = "未找到 obixConfig baseAPI,請聯絡管理者。";
return BadRequest(apiResult);
}
foreach (var wv in wvs) {
string typeName = wv.type.ToString();
sqlString = @$"SELECT system_value FROM variable WHERE system_type = 'warningValueApi' AND system_key = @type AND deleted = 0";
string apiUrl = await frontendRepository.GetOneAsync(sqlString, new { type = $"set{typeName}" });
if (string.IsNullOrEmpty(wv.targetValue)) {
apiResult.Code = "9998";
apiResult.Msg = "請輸入欲設定警戒值";
return BadRequest(apiResult);
}
if (string.IsNullOrEmpty(apiUrl))
{
apiResult.Code = "9998";
apiResult.Msg = "未找到天氣警戒值 obix API,請聯絡管理者。";
return BadRequest(apiResult);
}
using (HttpClient client = new HttpClient())
{
apiUrl = Path.Combine(baseApiUrl, "obix/config/", apiUrl);
string username = "stanGG";
string password = "St12345678";
string encoded = Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
client.DefaultRequestHeaders.Add("Authorization", "Basic " + encoded);
// 建構 XML 數據
string xmlData = @$"";
HttpContent content = new StringContent(xmlData, Encoding.UTF8, "application/xml");
var response = await client.PostAsync(apiUrl, content);
var resString = (await response.Content.ReadAsStringAsync()).ToString();
apiResult.Data = resString;
}
}
apiResult.Msg = "設定成功";
apiResult.Code = "0000";
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
}
}