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; namespace Backend.Controllers { public class NiagaraDataSynchronizeController : MybaseController { private readonly IBackendRepository backendRepository; //private readonly IDeviceImportRepository deviceImportRepository; private readonly INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository; public NiagaraDataSynchronizeController(IBackendRepository backendRepository, INiagaraDataSynchronizeRepository niagaraDataSynchronizeRepository) { this.backendRepository = backendRepository; //this.deviceImportRepository = deviceImportRepository; this.niagaraDataSynchronizeRepository = niagaraDataSynchronizeRepository; } public IActionResult Index() { return View(); } /// /// N4匯入列表 /// /// [HttpPost] public Task>> RawDataList() { ApiResult> apiResult = new ApiResult>(); try { List ds; string url_slot = "slot:/TPE/B1|";//三菱 //string url_slot = "slot:/NTPC/B1|";//wsp 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("http://192.168.0.136:8080/obix/config/Program/ObixQuery/query/", bql);//三菱 //ds = svc.obixQuery("http://192.168.0.136:8081/obix/config/Program/ObixQuery/query/", bql);//wsp apiResult.Code = "0000"; apiResult.Data = ds; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } //return apiResult; return Task.FromResult(apiResult); } /// /// 比對N4與資料表,有差異在同步至資料表中 /// /// [HttpPost] public async Task> CompareData([FromBody] List ds) { bool result = false; ApiResult apiResult = new ApiResult(); try { var building = ds.FirstOrDefault().building; await niagaraDataSynchronizeRepository.InsertNiagaraTagList(ds, building); await niagaraDataSynchronizeRepository.DeviceComparison(); 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; } /// /// N4匯入列表(device_item) /// /// [HttpPost] public Task>> RawDataDevItemList() { ApiResult> apiResult = new ApiResult>(); try { List ds; string url_slot = "slot:/TPE/B1|";//三菱 //string url_slot = "slot:/NTPC/B1|";//wsp webRequestService svc = new webRequestService(); ds = svc.obixHisQuery("http://192.168.0.136:8080/obix/config/Program/ObixQuery/query/", "http://192.168.0.136:8080/obix/histories", url_slot);//三菱 //ds = svc.obixHisQuery("http://192.168.0.136:8081/obix/config/Program/ObixQuery/query/", "http://192.168.0.136:8081/obix/histories", url_slot);//wsp apiResult.Code = "0000"; apiResult.Data = ds; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } //return apiResult; return Task.FromResult(apiResult); } /// /// 比對N4與資料表,有差異在同步至資料表中(device_item) /// /// [HttpPost] public async Task> DevIteComData([FromBody] List ds) { bool result = false; ApiResult apiResult = new ApiResult(); try { var building = ds.FirstOrDefault().device_building_tag; await niagaraDataSynchronizeRepository.InsertItemFromNiagara(ds, building); await niagaraDataSynchronizeRepository.DeviceItemComparison(); result = true; apiResult.Code = "0000"; apiResult.Data = result; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } } }