ibms-dome/Backend/Controllers/NiagaraDataSynchronizeController.cs

171 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Mvc;
using Repository.BackendRepository.Interface;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
//using Backend.Models;
using Microsoft.Extensions.Logging;
using Backend.Services;
using Backend.Services.Implement;
using Repository.Models;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using NPOI.SS.Formula.Functions;
using Repository.Helper;
namespace Backend.Controllers
{
public class NiagaraDataSynchronizeController : MybaseController<NiagaraDataSynchronizeController>
{
private readonly IBackendRepository backendRepository;
//private readonly IDeviceImportRepository deviceImportRepository;
private readonly INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository;
private readonly IBackEndConfigHelper backEndConfig;
public NiagaraDataSynchronizeController(IBackendRepository backendRepository, INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository,IBackEndConfigHelper backEndConfig)
{
this.backendRepository = backendRepository;
//this.deviceImportRepository = deviceImportRepository;
this.niagaraDataSynchronizeRepository = niagaraDataSynchronizeRepository;
this.backEndConfig = backEndConfig;
}
public IActionResult Index()
{
return View();
}
/// <summary>
/// N4匯入列表
/// </summary>
/// <returns></returns>
[HttpPost]
public Task<ApiResult<List<Device_value>>> RawDataList()
{
ApiResult<List<Device_value>> apiResult = new ApiResult<List<Device_value>>();
try
{
List<Device_value> ds;
string url_slot = backEndConfig.GetUrlSlot();
string tag_quantity = backEndConfig.Getobixtag_quantity();
string top100 = "";//" top 100 ";
webRequestService svc = new webRequestService();
string bql = url_slot + "bql:select " + top100 + " * from baja:Folder ";
//ds = svc.obixQuery("http://192.168.0.136:8080/obix/config/Arena/Program/ObixQuery/query/", bql);
//ds = svc.obixQuery("http://localhost:8080/obix/config/Program/ObixQuery/query/", bql);
ds = svc.obixQuery(backEndConfig.GetobixQuery(), bql, tag_quantity);//三菱
apiResult.Code = "0000";
apiResult.Data = ds;
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
//return apiResult;
return Task.FromResult(apiResult);
}
/// <summary>
/// 比對N4與資料表有差異在同步至資料表中
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<bool>> CompareData([FromBody] List<Device_value> ds)
{
bool result = false;
ApiResult<bool> apiResult = new ApiResult<bool>();
string tag_quantity = backEndConfig.Getobixtag_quantity();
try
{
var building = ds.FirstOrDefault().building;
string LightSwitchLevel = await niagaraDataSynchronizeRepository.getLightSwitchLevel(); //獲取照明開關 是否在 device or device_node
await niagaraDataSynchronizeRepository.InsertNiagaraTagList(ds, building, tag_quantity); // 匯入 MySQL table: import_niagara_tag
await niagaraDataSynchronizeRepository.DeviceComparison(LightSwitchLevel);
await niagaraDataSynchronizeRepository.CheckDiffFullNameAndCover();
await niagaraDataSynchronizeRepository.CheckFullNameEmptyReplaceByDeviceName();
await niagaraDataSynchronizeRepository.InsertBuildingMenu();
await niagaraDataSynchronizeRepository.InsertSubSystemFloor();
result = true;
apiResult.Code = "0000";
apiResult.Data = result;
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
return apiResult;
}
/// <summary>
/// N4匯入列表(device_item)
/// </summary>
/// <returns></returns>
[HttpPost]
public Task<ApiResult<List<ImpNiaItem>>> RawDataDevItemList()
{
ApiResult<List<ImpNiaItem>> apiResult = new ApiResult<List<ImpNiaItem>>();
try
{
List<ImpNiaItem> ds;
string url_slot = backEndConfig.GetUrlSlot();
webRequestService svc = new webRequestService();
ds = svc.obixHisQuery(backEndConfig.GetobixHisBqlQuery(), backEndConfig.GetobixHisUrlQuery(), url_slot);//三菱
apiResult.Code = "0000";
apiResult.Data = ds;
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
//return apiResult;
return Task.FromResult(apiResult);
}
/// <summary>
/// 比對N4與資料表有差異在同步至資料表中(device_item)
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<ApiResult<bool>> DevIteComData([FromBody] List<ImpNiaItem> ds)
{
bool result = false;
ApiResult<bool> apiResult = new ApiResult<bool>();
try
{
var building = ds.FirstOrDefault().device_building_tag;
await niagaraDataSynchronizeRepository.InsertItemFromNiagara(ds, building); // insert 暫存table import_niagara_item
await niagaraDataSynchronizeRepository.DeviceItemComparison(); //insert device_item
await niagaraDataSynchronizeRepository.CheckItemDiffFullNameAndCover(); // update device_item.fullname
await niagaraDataSynchronizeRepository.ItemCheckFullNameEmptyReplaceByDeviceName(); // 檢查device_item內FullName為空的值以points取代
await niagaraDataSynchronizeRepository.CheckItemIsShowHistory();
result = true;
apiResult.Code = "0000";
apiResult.Data = result;
}
catch (Exception exception)
{
apiResult.Code = "9999";
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
}
return apiResult;
}
}
}