ibms-dome/FrontendWebApi/ApiControllers/DeviceManageController.cs

558 lines
24 KiB
C#

using FrontendWebApi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Repository.BackendRepository.Interface;
using Repository.FrontendRepository.Interface;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace FrontendWebApi.ApiControllers
{
public class DeviceManageController : MyBaseApiController<DeviceManageController>
{
private readonly IBackendRepository backendRepository;
private readonly IFrontendRepository frontendRepository;
public DeviceManageController
(
IBackendRepository backendRepository,
IFrontendRepository frontendRepository
)
{
this.backendRepository = backendRepository;
this.frontendRepository = frontendRepository;
}
/// <summary>
/// 系統監控列表
/// </summary>
/// <param name="account"></param>
/// <returns></returns>
[HttpPost]
[Route("api/Device/GetMainSub")]
public async Task<ActionResult<ApiResult<History_MainSubBuildFloor>>> GetMainSub()
{
ApiResult<History_MainSubBuildFloor> apiResult = new ApiResult<History_MainSubBuildFloor>(jwt_str);
if (!jwtlife)
{
apiResult.Code = "5000";
return BadRequest(apiResult);
}
try
{
var dbsub = await frontendRepository.GetAllAsync<HistoryDBMainSub>(
@$"select distinct v1.system_key main_name, v1.system_value main_system_tag, v2.system_key sub_name, v2.system_value sub_system_tag, v1.system_priority, v2.system_priority
from role_auth a
join auth_page b on a.AuthCode = b.AuthCode
join userinfo c on c.role_guid = a.role_guid
join variable v2 on b.ShowView = v2.id and v2.system_type = @sub_system_type
join variable v1 on v1.id = v2.system_parent_id and v1.system_type = @main_system_type
where c.account = @account
order by v1.system_priority, v2.system_priority", new { @account = myUser.account, @sub_system_type = sub_system_type, @main_system_type = main_system_type });
var mains = dbsub.GroupBy(a => a.main_system_tag).ToList();
apiResult.Data = new History_MainSubBuildFloor();
apiResult.Data.history_Main_Systems = new List<History_Main_system>();
foreach (var main in mains)
{
History_Main_system history_Main_System = new History_Main_system();
history_Main_System.main_system_tag = main.Select(a => a.main_system_tag).FirstOrDefault();
history_Main_System.full_name = main.Select(a => a.main_name).FirstOrDefault();
apiResult.Data.history_Main_Systems.Add(history_Main_System);
}
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="account"></param>
/// <returns></returns>
[HttpPost]
[Route("api/Device/GetBuild")]
public async Task<ActionResult<ApiResult<List<BuildList>>>> GetBuild()
{
ApiResult<List<BuildList>> apiResult = new ApiResult<List<BuildList>>();
try
{
var sqlString = $@"select building_tag, full_name from building where deleted = 0";
var bl = await backendRepository.GetAllAsync<BuildList>(sqlString);
apiResult.Code = "0000";
apiResult.Data = bl;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
/// <summary>
/// 樓層列表
/// </summary>
/// <param name="account"></param>
/// <returns></returns>
[HttpPost]
[Route("api/Device/GetFloor")]
public async Task<ActionResult<ApiResult<List<FloorList>>>> GetFloor([FromBody] FindDevice fd)
{
ApiResult<List<FloorList>> apiResult = new ApiResult<List<FloorList>>();
if (string.IsNullOrEmpty(fd.building_tag))
{
apiResult.Code = "0002";
apiResult.Msg = "必須選擇東別";
return apiResult;
}
try
{
var sqlString = $@"select full_name as floor_tag from floor where deleted = 0 and building_tag = @building_tag";
var param = new { @building_tag = fd.building_tag };
var fl = await backendRepository.GetAllAsync<FloorList>(sqlString, param);
apiResult.Code = "0000";
apiResult.Data = fl;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = "系統內部錯誤,請聯絡管理者。";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
/// <summary>
/// 設備列表
/// </summary>
/// <param name="fd">floor_tag: null->總覽</param>
/// <returns></returns>
[HttpPost]
[Route("api/Device/GetDeviceList")]
public async Task<ActionResult<ApiResult<List<FloorList>>>> GetDeviceList([FromBody] FindDevice fd)
{
ApiResult<List<FloorList>> apiResult = new ApiResult<List<FloorList>>();
if (string.IsNullOrEmpty(fd.main_system_tag))
{
apiResult.Code = "0001";
apiResult.Msg = "必須系統類別";
return BadRequest(apiResult);
}
else if (string.IsNullOrEmpty(fd.building_tag))
{
apiResult.Code = "0002";
apiResult.Msg = "必須選擇東別";
return BadRequest(apiResult);
}
try
{
var sqlString = $@"select full_name, InitMapName as map_name, floor_map_name + '.svg' as floor_map_name from floor where deleted = 0 and building_tag = @building_tag and full_name = ifnull(@floor_tag, full_name)";
var param = new { @building_tag = fd.building_tag, @floor_tag = fd.floor_tag };
var fl = await backendRepository.GetAllAsync<FloorList>(sqlString, param);
foreach (var f in fl)
{
List<DeviceLists> dl = new List<DeviceLists>();
sqlString = $@"select d.device_guid, d.full_name, d.status, d.device_coordinate, dm.device_master_icon
from device d
left join device_master dm on d.device_building_tag = dm.device_building_tag and d.device_name_tag = dm.device_name_tag
where d.deleted = 0 and d.device_system_tag = @main_system_tag and d.device_building_tag = @building_tag and d.device_floor_tag = ifnull(@floor_tag, d.device_floor_tag)";
var dlParam = new { @main_system_tag = fd.main_system_tag, @building_tag = fd.building_tag, @floor_tag = fd.floor_tag };
dl = await backendRepository.GetAllAsync<DeviceLists>(sqlString, dlParam);
f.device_list = dl;
}
apiResult.Data = fl;
apiResult.Code = "0000";
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
/// <summary>
/// 獲取設備資料
/// </summary>
/// <param name="fd"></param>
/// <returns></returns>
[HttpPost]
[Route("api/Device/GetBaseDevice")]
public async Task<ActionResult<ApiResult<DeviceBaseInfo>>> GetBaseDevice([FromBody] FindDevice fd)
{
ApiResult<DeviceBaseInfo> apiResult = new ApiResult<DeviceBaseInfo>();
try
{
string sql = $@"select device_number, full_name, device_coordinate, device_coordinate_3d from device where deleted = 0 and device_guid = @device_guid";
object param = new { @device_guid = fd.device_guid };
var device = await backendRepository.GetOneAsync<DeviceBaseInfo>(sql, param);
if(device == null)
{
apiResult.Msg = "查無次設備";
apiResult.Code = "0001";
return apiResult;
}
apiResult.Data = device;
apiResult.Code = "0000";
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
/// <summary>
/// 獲取設備運維資料
/// </summary>
/// <param name="fd"></param>
/// <returns></returns>
[HttpPost]
[Route("api/Device/GetOpeDevice")]
public async Task<ActionResult<ApiResult<List<DeviceOpeRecord>>>> GetOpeDevice([FromBody] FindDevice fd)
{
ApiResult<List<DeviceOpeRecord>> apiResult = new ApiResult<List<DeviceOpeRecord>>();
try
{
string sql = $@"select orr.work_type, orr.fix_do, ui.full_name as work_person_name, finish_time, created_at
from device d
join operation_record orr on d.device_number = orr.fix_do_code and orr.deleted = 0
left join userinfo ui on orr.work_person_id = ui.userinfo_guid
where d.deleted = 0 and d.device_guid = @device_guid";
object param = new { @device_guid = fd.device_guid };
var device = await backendRepository.GetAllAsync<DeviceOpeRecord>(sql, param);
if (device.Count == 0)
{
apiResult.Msg = "查無次設備";
apiResult.Code = "0001";
return apiResult;
}
apiResult.Data = device;
apiResult.Code = "0000";
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
/// <summary>
/// 取得L型選單的alarm的報警設備
/// 由原本的obix打API方式改為此action
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("api/Device/MenuAlarm")]
public async Task<ActionResult<ApiResult<List<OntimeAlarmRawData>>>> MenuAlarm(string account)
{
ApiResult<List<OntimeAlarmRawData>> apiResult = new ApiResult<List<OntimeAlarmRawData>>(jwt_str);
if (!jwtlife)
{
apiResult.Code = "5000";
return BadRequest(apiResult);
}
try
{
//取得發生異常的設備
string sql = $@"
SELECT
unicode_decode(value) device_point
FROM alarmorion_orionalarmrecord a
JOIN alarmorion_orionalarmfacetvalue b on a.id = b.alarm
WHERE a.sourceState = 1 and b.facetName = 9";
var alarmDevicePoints = await frontendRepository.GetAllAsync<string>(sql);
List<string> alarmDevices = new List<string>();
foreach (var alarmDevicePoint in alarmDevicePoints)
{
var alarmDevicePointSplit = alarmDevicePoint.Split("_");
var alarmDevice = string.Join("_", alarmDevicePointSplit.SkipLast(1));
if (!alarmDevices.Contains(alarmDevice))
{
alarmDevices.Add(alarmDevice);
}
}
var sqlDevice = $@"
SELECT
d.building_guid,
d.main_system_guid,
d.sub_system_guid,
d.floor_guid,
d.device_number
FROM Device d
INNER JOIN (
SELECT
ap.building_guid,
ap.ShowView AS sub_system_guid
FROM
(
SELECT *
FROM role_auth ra
WHERE ra.role_guid = (SELECT ui.role_guid FROM userinfo ui WHERE account = @Account)
) ra
LEFT JOIN auth_page ap ON ra.AuthCode = ap.AuthCode
WHERE ap.AuthType = 1
) shower ON d.building_guid = shower.building_guid AND d.sub_system_guid = shower.sub_system_guid
WHERE d.deleted = 0 AND d.device_number IN @DeviceNumbers
";
var menuAlarmDevices = await frontendRepository.GetAllAsync<OntimeAlarmRawData>(sqlDevice, new { Account = account, DeviceNumbers = alarmDevices });
apiResult.Data = menuAlarmDevices;
apiResult.Code = "0000";
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
/// <summary>
/// 取得報警設備
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("api/Device/Getalarm")]
public async Task<ActionResult<ApiResult<AlarmObj>>> Alarm()
{
ApiResult<AlarmObj> apiResult = new ApiResult<AlarmObj>(jwt_str);
if (!jwtlife)
{
apiResult.Code = "5000";
return BadRequest(apiResult);
}
try
{
AlarmObj alarmObj = new AlarmObj()
{
alarmorion = new List<AlarmorionString>(),
buildingAlarmDeviceAmount = new List<BuildingAlarmDeviceAmount>()
};
// old寫法
//string sql = $@"SELECT sl.source device_number,from_unixtime(amc.timestamp/1000,'%Y-%m-%d %H:%i:%s') alarm_timestamp FROM alarmorion_orionalarmrecord amc
// JOIN (
// SELECT MAX(amc.alarm) ad,m.source FROM alarmorion_orionalarmsourceorder amc
// JOIN (SELECT * FROM alarmorion_orionalarmsource a WHERE substring(a.source,23,5) ='Arena') m ON amc.alarmSource = m.id
// GROUP BY m.source
// ) sl ON amc.id = sl.ad
// WHERE amc.sourceState = 1
// ";
string sql = $@"
select * from
(
SELECT a.* ,from_unixtime(a.timestamp/1000,'%Y-%m-%d %H:%i:%s') alarm_timestamp, errmsg device_point,
substring( errmsg, 1,
LENGTH(errmsg) -
LENGTH(SUBSTRING_INDEX(errmsg, '_', -1) -1) -- 最後一段的長度
-1 -- 減掉最後的 _ ex:D3_B_B1F_CO_
) device_tag
FROM alarmorion_orionalarmrecord a
JOIN alarmorion_orionalarmfacetvalue b on a.id = b.alarm
WHERE a.sourceState = 1 and b.facetName = 9
) a
left join device b on a.device_tag = b.device_number AND b.deleted = 0
left join device_disaster c on b.device_guid = c.device_guid
WHERE c.device_system_value IS NOT NULL";
var alarms = await frontendRepository.GetAllAsync<AlarmorionString>(sql);
// old寫法
//List<AlarmorionString> Alarmorions = new List<AlarmorionString>();
//foreach (var alarm in alarms)
//{
// var source = alarm.device_number.Split('/');
// //if(source[2] != "H")
// //{
// // continue;
// //}
// alarm.device_number = source[6];
// Alarmorions.Add(alarm);
//}
//alarmObj.alarmorion = Alarmorions.GroupBy(a => new { a.device_number, a.alarm_timestamp }).Select(a => a.First()).ToList();
foreach(var alarm in alarms)
{
var temp_alarm = alarmObj.alarmorion.Find(x => x.device_number == alarm.device_number);
if (temp_alarm == null)
{
alarmObj.alarmorion.Add(alarm);
}
}
var device_amount_sql = $@"SELECT
d.building_guid,
COUNT(*) AS device_amount
FROM device d
WHERE d.deleted = 0
AND d.device_number IN @devices
GROUP BY d.building_guid";
alarmObj.buildingAlarmDeviceAmount = await frontendRepository.GetAllAsync<BuildingAlarmDeviceAmount>(device_amount_sql, new { devices = alarmObj.alarmorion.Select(x => x.device_number).ToList() });
apiResult.Data = alarmObj;
apiResult.Code = "0000";
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
/// <summary>
/// 修改設備名稱(打後台API)
/// </summary>
/// <param name="change"></param>
/// <returns></returns>
[HttpPost]
[Route("api/Device/SaveChangeName")]
public async Task<ActionResult<ApiResult<string>>> SaveChangeName(ChangeName change)
{
ApiResult<string> apiResult = new ApiResult<string>(jwt_str);
if (!jwtlife)
{
apiResult.Code = "5000";
return BadRequest(apiResult);
}
try
{
string authHeader = HttpContext.Request.Headers["Authorization"];
var websiteurl = await frontendRepository.GetOneAsync<string>("select system_value from variable where system_type = 'website_config' and system_key = 'website_url' ");
//傳送到後台API
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"{websiteurl}api/Device/SaveChangeName");
request.Method = "POST";
request.Headers.Add("Authorization", authHeader);
request.ContentType = "application/x-www-form-urlencoded";
request.PreAuthenticate = true;
NameValueCollection postParams = System.Web.HttpUtility.ParseQueryString(string.Empty);
postParams.Add("TagName", change.TagName);
postParams.Add("ChangeN", change.ChangeN);
postParams.Add("ChooseTable", change.ChooseTable.ToString());
byte[] postData = Encoding.UTF8.GetBytes(postParams.ToString());
using (Stream st = request.GetRequestStream())
{
st.Write(postData, 0, postData.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
var statusNumber = (int)response.StatusCode;
if (statusNumber != 200)
{
throw new NotImplementedException(responseString);
}
else
{
//解析回傳內容
var final = JObject.Parse(responseString);
apiResult.Code = final["code"].ToString();
apiResult.Data = final["data"].ToString();
}
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
[HttpPost]
[Route("api/Device/GetHaveChangeNameRole")]
public async Task<ActionResult<ApiResult<bool>>> GetHaveChangeNameRole(ChangeNameRole role)
{
ApiResult<bool> apiResult = new ApiResult<bool>(jwt_str);
if (!jwtlife)
{
apiResult.Code = "5000";
return BadRequest(apiResult);
}
try
{
var sql = $@"
select * from userinfo u
join role_auth a on a.role_guid = u.role_guid
join auth_page p on p.AuthCode = a.AuthCode
where u.account = N'{role.Account}'
and p.SubName = N'編輯設備名稱'
and p.building_guid = N'{role.Building_guid}'";
var have = await backendRepository.GetOneAsync<string>(sql);
if (have == null)
{
apiResult.Data = false;
}
else
{
apiResult.Data = true;
}
apiResult.Code = "0000";
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
return Ok(apiResult);
}
return Ok(apiResult);
}
}
}