[緊急應變] 警戒值設定 表單前端介面及程序建置 | 後端程序建置 | obix config api 測試及介接

This commit is contained in:
dev01 2023-09-11 15:49:15 +08:00
parent d08754f8f6
commit d12dbc35b6
9 changed files with 420 additions and 8 deletions

View File

@ -0,0 +1,200 @@
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
{
/// <summary>
/// 告警紀錄
/// </summary>
public class WarningValueController : MyBaseApiController<WarningValueController>
{
private readonly IFrontendRepository frontendRepository;
public WarningValueController
(
IFrontendRepository frontendRepository
)
{
this.frontendRepository = frontendRepository;
}
/// <summary>
/// 取得警戒值
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
[HttpPost]
[Route("api/WarningValue/GetWarningValue")]
public async Task<ActionResult<ApiResult<List<WarningValueOutput>>>> GetWarningValue()
{
ApiResult<List<WarningValueOutput>> apiResult = new ApiResult<List<WarningValueOutput>>(jwt_str);
if (!jwtlife)
{
apiResult.Code = "5000";
return BadRequest(apiResult);
}
try
{
List<string> typeNames = Enum.GetNames(typeof(WarningValueType)).ToList();
apiResult.Data = new List<WarningValueOutput>();
var sqlString = $@"SELECT system_value FROM variable WHERE system_type = 'obixConfig' AND system_key = 'ApiBase' AND deleted = 0";
string baseApiUrl = await frontendRepository.GetOneAsync<string>(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<WarningValueType>(typeName);
sqlString = @$"SELECT system_value FROM variable WHERE system_type = 'warningValueApi' AND system_key = @type AND deleted = 0";
string apiUrl = await frontendRepository.GetOneAsync<string>(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);
}
/// <summary>
/// 取得警戒值
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
[HttpPost]
[Route("api/WarningValue/SetWarningValue")]
public async Task<ActionResult<ApiResult<string>>> SetWarningValue(List<WarningValueInput> wvs)
{
ApiResult<string> apiResult = new ApiResult<string>(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<string>(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<string>(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 = @$"<real val='{wv.targetValue}' />";
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);
}
}
}

View File

@ -0,0 +1,27 @@
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace FrontendWebApi.Models
{
public enum WarningValueType {
Rain, //降雨
Earthquake //地震
}
public class WarningValueInput {
public WarningValueType type { get; set; }
public string targetValue { get; set; }
}
public class WarningValueOutput {
public WarningValueType type { get; set; }
public string targetValue { get; set; }
}
}

View File

@ -170,11 +170,11 @@ namespace FrontendWebApi
app.UseAuthentication();
app.UseAuthorization();
app.Use(async (context, next) =>
{
context.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
await next();
});
//app.Use(async (context, next) =>
//{
// context.Response.Headers.Add("X-Frame-Options", "SAMEORIGIN");
// await next();
//});
//app.UseEndpoints(endpoints =>
//{

View File

@ -17,7 +17,8 @@
<!-- SweetAlert -->
<link rel="stylesheet" media="screen, print" href="~/css/notifications/sweetalert2/sweetalert2.bundle.css">
<link rel="stylesheet" media="screen, print" href="~/css/notifications/toastr/toastr.css">
<link href="~/js/font-awesome/fontawesome.min.css" rel="stylesheet" />
<link href="~/js/font-awesome/solid.min.css" rel="stylesheet" />
<!--Select2-->
<link rel="stylesheet" media="screen, print" href="~/css/formplugins/select2/select2.bundle.css" />
@ -852,10 +853,79 @@
</button>
</div>
<div class="modal-body">
<iframe id="warningValueIframe" src="@ViewBag.WarningValuePxPath" style="width:100%;height:100%;"></iframe>
<table class="table warning-value-table">
<tbody>
<tr class="disaster-title-bar">
<td rowspan="3" class="disaster-icon"><i class="fa-solid fa-cloud-showers-heavy"></i></td>
<td class="disaster-title">降雨警戒設定值</td>
<td class="disaster-value" id="rainCurName">大雨</td>
</tr>
<tr>
<td colspan="2">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="rainRadio" id="rain1" value="1">
<label class="form-check-label" for="rain1">大雨</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="rainRadio" id="rain2" value="2">
<label class="form-check-label" for="rain2">豪雨</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="rainRadio" id="rain3" value="3">
<label class="form-check-label" for="rain3">大豪雨</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="rainRadio" id="rain4" value="4">
<label class="form-check-label" for="rain4">超大豪雨</label>
</div>
</td>
</tr>
<tr>
<td colspan="2">24小時累積雨量達80毫米以上或時雨量達40毫米以上之降雨現象</td>
</tr>
<tr class="disaster-title-bar">
<td rowspan="3" class="disaster-icon"><i class="fa-solid fa-house-chimney-crack"></i></td>
<td class="disaster-title">地震警戒設定值</td>
<td class="disaster-value" id="eqCurName">7級</td>
</tr>
<tr>
<td colspan="2">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="eqRadio" id="eq1" value="3">
<label class="form-check-label" for="inlineRadio1">3級</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="eqRadio" id="eq2" value="4">
<label class="form-check-label" for="eq2">4級</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="eqRadio" id="eq3" value="5">
<label class="form-check-label" for="eq3">5級</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="eqRadio" id="eq4" value="6">
<label class="form-check-label" for="eq4">6級</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="eqRadio" id="eq5" value="7">
<label class="form-check-label" for="eq5">7級</label>
</div>
</td>
</tr>
<tr>
<td colspan="2">24小時累積雨量達80毫米以上或時雨量達40毫米以上之降雨現象</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary btn-save" onclick="setWarningValue()">儲存</button>
</div>
</div>
</div>
</div>
@ -874,6 +944,8 @@
<script src="~/js/notifications/toastr/toastr.js"></script>
<!--Toast-->
<script src="~/js/toast.js"></script>
<script src="~/js/font-awesome/fontawesome.min.js"></script>
<script src="~/js/font-awesome/solid.min.js"></script>
<!-- Select2 JS -->
<script src="~/js/formplugins/select2/select2.bundle.js"></script>
<!-- table2excel -->
@ -907,6 +979,66 @@
$("body").on("shown.bs.modal","#warning-value-modal",function() {
$("#warningValueIframe").attr("src","@ViewBag.WarningValuePxPath");
});
$("body").on("show.bs.modal","#warning-value-modal",getWarningValue);
function getWarningValue(){
let url = "api/WarningValue/GetWarningValue";
$.post(url, {}, function (rel) {
if (rel.code != "0000") {
if (rel.code == "9999") {
toast_error(rel.msg);
}
return;
}
else {
if(rel.data){
let eqValue = rel.data.filter(d => d.type == 1).map(d => d.targetValue)[0];
let rainValue = rel.data.filter(d => d.type == 0).map(d => d.targetValue)[0];
$(`input[name=rainRadio][value=${rainValue}]`).prop("checked",true);
$(`input[name=eqRadio][value=${eqValue}]`).prop("checked",true);
$("#rainCurName").text($(`input[name=rainRadio][value=${rainValue}]`).next("label").text());
$("#eqCurName").text($(`input[name=eqRadio][value=${eqValue}]`).next("label").text());
}
}
}, 'json');
}
function setWarningValue(){
let eqValue = $("input[name=eqRadio]:checked").val();
let rainValue = $("input[name=rainRadio]:checked").val();
if(!eqValue || !rainValue){
toast_error("請輸入警戒值");
return;
}
let sendData = {wvs:
[
{type:0,targetValue:rainValue},
{type:1,targetValue:eqValue}
]
};
let url = "api/WarningValue/SetWarningValue";
$.post(url, sendData, function (rel) {
if (rel.code != "0000") {
toast_error(rel.msg);
return;
}
else {
toast_ok(rel.msg);
$("#warning-value-modal").modal("hide");
}
}, 'json');
}
</script>
</body>
</html>

View File

@ -38,3 +38,29 @@ label.error {
.highcharts-credits {
display: none;
}
.warning-value-table td {
vertical-align:middle;
}
td.disaster-icon svg {
font-size:2rem;
color:#8d567f;
}
td.disaster-icon {
text-align: center;
vertical-align: middle;
}
tr.disaster-title-bar {
background: var(--theme-danger-50);
}
td.disaster-title {
font-weight: 700;
font-size: 1rem;
}
td.disaster-value {
color: #e23a3a;
font-weight: 700;
font-size: 1rem;
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,6 @@
/*!
* Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com
* License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License)
* Copyright 2023 Fonticons, Inc.
*/
:host,:root{--fa-style-family-classic:"Font Awesome 6 Free";--fa-font-solid:normal 900 1em/1 "Font Awesome 6 Free"}@font-face{font-family:"Font Awesome 6 Free";font-style:normal;font-weight:900;font-display:block;src:url(../webfonts/fa-solid-900.woff2) format("woff2"),url(../webfonts/fa-solid-900.ttf) format("truetype")}.fa-solid,.fas{font-weight:900}

File diff suppressed because one or more lines are too long